Skip to content

Commit

Permalink
Integration tests to verify read and write operations on local file (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
ashmeenkaur authored Sep 20, 2023
1 parent ee62245 commit 84c348e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
45 changes: 45 additions & 0 deletions tools/integration_tests/local_file/read_file_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
56 changes: 56 additions & 0 deletions tools/integration_tests/local_file/write_file_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 5 additions & 4 deletions tools/integration_tests/util/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 84c348e

Please sign in to comment.