Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmeenkaur committed Sep 21, 2023
1 parent 4974d56 commit 99f87e5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 63 deletions.

This file was deleted.

28 changes: 28 additions & 0 deletions tools/integration_tests/local_file/local_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/dynamic_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/only_dir_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/static_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/setup"
)

Expand All @@ -42,6 +43,33 @@ var (
ctx context.Context
)

////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////

func WritingToLocalFileShouldNotWriteToGCS(ctx context.Context, storageClient *storage.Client,
fh *os.File, testDirName, fileName string, t *testing.T) {
operations.WriteWithoutClose(fh, client.FileContents, t)
client.ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, fileName, t)
}

func NewFileShouldGetSyncedToGCSAtClose(ctx context.Context, storageClient *storage.Client,
testDirPath, fileName string, t *testing.T) {
// Create a local file.
_, fh := client.CreateLocalFileInTestDir(ctx, storageClient, testDirPath, fileName, t)

// Writing contents to local file shouldn't create file on GCS.
testDirName := client.GetDirName(testDirPath)
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, fileName, t)

// Close the file and validate if the file is created on GCS.
client.CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, fileName, client.FileContents, t)
}

////////////////////////////////////////////////////////////////////////
// TestMain
////////////////////////////////////////////////////////////////////////

func TestMain(m *testing.M) {
setup.ParseSetUpFlags()

Expand Down
5 changes: 2 additions & 3 deletions tools/integration_tests/local_file/remove_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"path"
"testing"

. "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/local_file/helpers"
. "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/setup"
Expand All @@ -39,7 +38,7 @@ func TestRmDirOfDirectoryContainingGCSAndLocalFiles(t *testing.T) {
operations.RemoveDir(path.Join(testDirPath, ExplicitDirName))

// Verify that directory is removed.
ValidateNoFileOrDirError(path.Join(testDirPath, ExplicitDirName), t)
operations.ValidateNoFileOrDirError(path.Join(testDirPath, ExplicitDirName), t)
// Validate writing content to unlinked local file does not throw error.
operations.WriteWithoutClose(fh2, FileContents, t)
// Validate flush file does not throw error and does not create object on GCS.
Expand All @@ -63,7 +62,7 @@ func TestRmDirOfDirectoryContainingOnlyLocalFiles(t *testing.T) {
operations.RemoveDir(path.Join(testDirPath, ExplicitDirName))

// Verify rmDir operation succeeds.
ValidateNoFileOrDirError(path.Join(testDirPath, ExplicitDirName), t)
operations.ValidateNoFileOrDirError(path.Join(testDirPath, ExplicitDirName), t)
// Close the local files and validate they are not present on GCS.
operations.CloseFileShouldNotThrowError(fh1, t)
ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, localFile1, t)
Expand Down
21 changes: 18 additions & 3 deletions tools/integration_tests/local_file/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ package local_file_test
import (
"os"
"path"
"strings"
"testing"

. "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/local_file/helpers"
. "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/setup"
)

////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////

func verifyRenameOperationNotSupported(err error, t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "operation not supported") {
t.Fatalf("os.Rename(), expected err: %s, got err: %v",
"operation not supported", err)
}
}

////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////

func TestRenameOfLocalFileFails(t *testing.T) {
testDirPath = setup.SetupTestDirectory(testDirName)
// Create local file with some content.
Expand All @@ -38,7 +53,7 @@ func TestRenameOfLocalFileFails(t *testing.T) {
path.Join(testDirPath, NewFileName))

// Verify rename operation fails.
VerifyRenameOperationNotSupported(err, t)
verifyRenameOperationNotSupported(err, t)
// write more content to local file.
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, FileName1, t)
// Close the local file.
Expand All @@ -65,7 +80,7 @@ func TestRenameOfDirectoryWithLocalFileFails(t *testing.T) {
path.Join(testDirPath, NewDirName))

// Verify rename operation fails.
VerifyRenameOperationNotSupported(err, t)
verifyRenameOperationNotSupported(err, t)
// Write more content to local file.
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, FileName2, t)
// Close the local file.
Expand Down
23 changes: 2 additions & 21 deletions tools/integration_tests/util/client/gcs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,16 @@ func CreateLocalFileInTestDir(ctx context.Context, storageClient *storage.Client
testDirPath, fileName string, t *testing.T) (string, *os.File) {
filePath := path.Join(testDirPath, fileName)
fh := operations.CreateFile(filePath, FilePerms, t)
testDirName := getDirName(testDirPath)
testDirName := GetDirName(testDirPath)
ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, fileName, t)
return filePath, fh
}

func getDirName(testDirPath string) string {
func GetDirName(testDirPath string) string {
dirName := testDirPath[strings.LastIndex(testDirPath, "/")+1:]
return dirName
}

func WritingToLocalFileShouldNotWriteToGCS(ctx context.Context, storageClient *storage.Client,
fh *os.File, testDirName, fileName string, t *testing.T) {
operations.WriteWithoutClose(fh, FileContents, t)
ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, fileName, t)
}

func NewFileShouldGetSyncedToGCSAtClose(ctx context.Context, storageClient *storage.Client,
testDirPath, fileName string, t *testing.T) {
// Create a local file.
_, fh := CreateLocalFileInTestDir(ctx, storageClient, testDirPath, fileName, t)

// Writing contents to local file shouldn't create file on GCS.
testDirName := getDirName(testDirPath)
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, fileName, t)

// Close the file and validate if the file is created on GCS.
CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, fileName, FileContents, t)
}

func CreateObjectInGCSTestDir(ctx context.Context, storageClient *storage.Client,
testDirName, fileName, content string, t *testing.T) {
objectName := path.Join(testDirName, fileName)
Expand Down
15 changes: 15 additions & 0 deletions tools/integration_tests/util/operations/validate_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package operations

import (
"os"
"strings"
"testing"
)

func ValidateNoFileOrDirError(path string, t *testing.T) {
_, err := os.Stat(path)
if err == nil || !strings.Contains(err.Error(), "no such file or directory") {
t.Fatalf("os.Stat(%s). Expected: %s, Got: %v", path,
"no such file or directory", err)
}
}

0 comments on commit 99f87e5

Please sign in to comment.