From 84c348e536dafd54a65e7a730121d79b37df40a2 Mon Sep 17 00:00:00 2001 From: Ashmeen Kaur <57195160+ashmeenkaur@users.noreply.github.com> Date: Wed, 20 Sep 2023 18:31:15 +0530 Subject: [PATCH] Integration tests to verify read and write operations on local file (#1383) * integration tests to verify read and write operations on local file rebase changes format * rebase changes * do not run testBucket tests if flag is not set. --- .../local_file/read_file_test.go | 45 +++++++++++++++ .../local_file/write_file_test.go | 56 +++++++++++++++++++ tools/integration_tests/util/setup/setup.go | 9 +-- 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 tools/integration_tests/local_file/read_file_test.go create mode 100644 tools/integration_tests/local_file/write_file_test.go diff --git a/tools/integration_tests/local_file/read_file_test.go b/tools/integration_tests/local_file/read_file_test.go new file mode 100644 index 0000000000..3f77080375 --- /dev/null +++ b/tools/integration_tests/local_file/read_file_test.go @@ -0,0 +1,45 @@ +// Copyright 2023 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Provides integration tests for read operation on local files. +package local_file_test + +import ( + "testing" + + . "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/client" + "github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/setup" +) + +func TestReadLocalFile(t *testing.T) { + testDirPath = setup.SetupTestDirectory(testDirName) + // Create a local file. + _, fh := CreateLocalFileInTestDir(ctx, storageClient, testDirPath, FileName1, t) + + // Write FileContents twice to local file. + content := FileContents + FileContents + WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, FileName1, t) + WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, FileName1, t) + + // Read the local file contents. + buf := make([]byte, len(content)) + n, err := fh.ReadAt(buf, 0) + if err != nil || len(content) != n || content != string(buf) { + t.Fatalf("Read file operation failed on local file: %v "+ + "Expected content: %s, Got Content: %s", err, content, string(buf)) + } + + // Close the file and validate that the file is created on GCS. + CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, FileName1, content, t) +} diff --git a/tools/integration_tests/local_file/write_file_test.go b/tools/integration_tests/local_file/write_file_test.go new file mode 100644 index 0000000000..c13c77d206 --- /dev/null +++ b/tools/integration_tests/local_file/write_file_test.go @@ -0,0 +1,56 @@ +// Copyright 2023 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Provides integration tests for write on local files. +package local_file_test + +import ( + "testing" + + . "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" +) + +func TestMultipleWritesToLocalFile(t *testing.T) { + testDirPath = setup.SetupTestDirectory(testDirName) + // Create a local file. + _, fh := CreateLocalFileInTestDir(ctx, storageClient, testDirPath, FileName1, t) + + // Write some contents to file sequentially. + for i := 0; i < 3; i++ { + operations.WriteWithoutClose(fh, FileContents, t) + } + ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, FileName1, t) + + // Close the file and validate that the file is created on GCS. + CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, + FileName1, FileContents+FileContents+FileContents, t) +} + +func TestRandomWritesToLocalFile(t *testing.T) { + testDirPath = setup.SetupTestDirectory(testDirName) + // Create a local file. + _, fh := CreateLocalFileInTestDir(ctx, storageClient, testDirPath, FileName1, t) + + // Write some contents to file randomly. + operations.WriteAt("string1", 0, fh, t) + operations.WriteAt("string2", 2, fh, t) + operations.WriteAt("string3", 3, fh, t) + ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, FileName1, t) + + // Close the file and validate that the file is created on GCS. + CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, + FileName1, "stsstring3", t) +} diff --git a/tools/integration_tests/util/setup/setup.go b/tools/integration_tests/util/setup/setup.go index 44751f76e6..dfee7345ac 100644 --- a/tools/integration_tests/util/setup/setup.go +++ b/tools/integration_tests/util/setup/setup.go @@ -276,6 +276,9 @@ func RunTestsForMountedDirectoryFlag(m *testing.M) { } func SetUpTestDirForTestBucketFlag() { + if TestBucket() == "" { + log.Fatal("Not running TestBucket tests as --testBucket flag is not set.") + } if err := SetUpTestDir(); err != nil { log.Printf("setUpTestDir: %v\n", err) os.Exit(1) @@ -329,11 +332,9 @@ func CleanupDirectoryOnGCS(directoryPathOnGCS string) { } func AreBothMountedDirectoryAndTestBucketFlagsSet() bool { - if MountedDirectory() != "" { - if TestBucket() == "" { - log.Fatal("Set both --mountedDirectory and --testBucket to run mounted directory tests.") - } + if MountedDirectory() != "" && TestBucket() != "" { return true } + log.Print("Not running mounted directory tests as both --mountedDirectory and --testBucket flags are not set.") return false }