Skip to content

Commit

Permalink
integration tests to verify rename and removeDir operations on local …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
ashmeenkaur committed Sep 19, 2023
1 parent cbda23f commit 3e2b216
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

package helpers

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)
}
}

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)
}
}
3 changes: 3 additions & 0 deletions tools/integration_tests/local_file/helpers/gcs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import (

const (
FileName1 = "foo1"
FileName2 = "foo2"
ExplicitDirName = "explicit"
ExplicitFileName1 = "explicitFile1"
FilePerms = 0644
FileContents = "teststring"
GCSFileContent = "gcsContent"
NewFileName = "newName"
NewDirName = "newDirName"
LocalFileTestDirInBucket = "LocalFileTest"
ReadSize = 1024
)
Expand Down
73 changes: 73 additions & 0 deletions tools/integration_tests/local_file/remove_dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 removeDir operation on directories containing local files.
package local_file_test

import (
"path"
"testing"

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

func TestRmDirOfDirectoryContainingGCSAndLocalFiles(t *testing.T) {
testDirPath = setup.SetupTestDirectory(LocalFileTestDirInBucket)
// Create explicit directory with one synced and one local file.
operations.CreateDirectory(path.Join(testDirPath, ExplicitDirName), t)
syncedFile := path.Join(ExplicitDirName, FileName1)
localFile := path.Join(ExplicitDirName, FileName2)
_, fh1 := CreateLocalFileInTestDir(testDirPath, syncedFile, t)
CloseFileAndValidateObjectContentsFromGCS(fh1, syncedFile, "", t)
_, fh2 := CreateLocalFileInTestDir(testDirPath, localFile, t)

// Attempt to remove explicit directory.
operations.RemoveDir(path.Join(testDirPath, ExplicitDirName))

// Verify that directory is removed.
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.
operations.CloseFileShouldNotThrowError(fh2, t)
ValidateObjectNotFoundErrOnGCS(localFile, t)
// Validate synced files are also deleted.
ValidateObjectNotFoundErrOnGCS(syncedFile, t)
ValidateObjectNotFoundErrOnGCS(ExplicitDirName, t)
}

func TestRmDirOfDirectoryContainingOnlyLocalFiles(t *testing.T) {
testDirPath = setup.SetupTestDirectory(LocalFileTestDirInBucket)
// Create a directory with two local files.
operations.CreateDirectory(path.Join(testDirPath, ExplicitDirName), t)
localFile1 := path.Join(ExplicitDirName, FileName1)
localFile2 := path.Join(ExplicitDirName, FileName2)
_, fh1 := CreateLocalFileInTestDir(testDirPath, localFile1, t)
_, fh2 := CreateLocalFileInTestDir(testDirPath, localFile2, t)

// Attempt to remove explicit directory.
operations.RemoveDir(path.Join(testDirPath, ExplicitDirName))

// Verify rmDir operation succeeds.
ValidateNoFileOrDirError(path.Join(testDirPath, ExplicitDirName), t)
// Close the local files and validate they are not present on GCS.
operations.CloseFileShouldNotThrowError(fh1, t)
ValidateObjectNotFoundErrOnGCS(localFile1, t)
operations.CloseFileShouldNotThrowError(fh2, t)
ValidateObjectNotFoundErrOnGCS(localFile2, t)
// Validate directory is also deleted.
ValidateObjectNotFoundErrOnGCS(ExplicitDirName, t)
}
103 changes: 103 additions & 0 deletions tools/integration_tests/local_file/rename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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 rename operation on local files.
package local_file_test

import (
"os"
"path"
"testing"

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

func TestRenameOfLocalFileFails(t *testing.T) {
testDirPath = setup.SetupTestDirectory(LocalFileTestDirInBucket)
// Create local file with some content.
_, fh := CreateLocalFileInTestDir(testDirPath, FileName1, t)
WritingToLocalFileShouldNotWriteToGCS(fh, FileName1, t)

// Attempt to rename local file.
err := os.Rename(
path.Join(testDirPath, FileName1),
path.Join(testDirPath, NewFileName))

// Verify rename operation fails.
VerifyRenameOperationNotSupported(err, t)
// write more content to local file.
WritingToLocalFileShouldNotWriteToGCS(fh, FileName1, t)
// Close the local file.
CloseFileAndValidateObjectContentsFromGCS(fh, FileName1, FileContents+FileContents, t)
}

func TestRenameOfDirectoryWithLocalFileFails(t *testing.T) {
testDirPath = setup.SetupTestDirectory(LocalFileTestDirInBucket)
//Create directory with 1 synced and 1 local file.
operations.CreateDirectory(path.Join(testDirPath, ExplicitDirName), t)
// Create synced file.
CreateObjectInGCSTestDir(path.Join(ExplicitDirName, FileName1), GCSFileContent, t)
// Create local file with some content.
_, fh := CreateLocalFileInTestDir(testDirPath, path.Join(ExplicitDirName, FileName2), t)
WritingToLocalFileShouldNotWriteToGCS(fh, path.Join(ExplicitDirName, FileName2), t)

// Attempt to rename directory containing local file.
err := os.Rename(
path.Join(testDirPath, ExplicitDirName),
path.Join(testDirPath, NewDirName))

// Verify rename operation fails.
VerifyRenameOperationNotSupported(err, t)
// Write more content to local file.
WritingToLocalFileShouldNotWriteToGCS(fh, FileName2, t)
// Close the local file.
CloseFileAndValidateObjectContentsFromGCS(fh, path.Join(ExplicitDirName, FileName2),
FileContents+FileContents, t)
}

func TestRenameOfLocalFileSucceedsAfterSync(t *testing.T) {
TestRenameOfLocalFileFails(t)

// Attempt to Rename synced file.
err := os.Rename(
path.Join(testDirPath, FileName1),
path.Join(testDirPath, NewFileName))

// Validate.
if err != nil {
t.Fatalf("os.Rename() failed on synced file: %v", err)
}
ValidateObjectContentsFromGCS(NewFileName, FileContents+FileContents, t)
ValidateObjectNotFoundErrOnGCS(FileName1, t)
}

func TestRenameOfDirectoryWithLocalFileSucceedsAfterSync(t *testing.T) {
TestRenameOfDirectoryWithLocalFileFails(t)

// Attempt to rename directory again after sync.
err := os.Rename(
path.Join(testDirPath, ExplicitDirName),
path.Join(testDirPath, NewDirName))

// Validate.
if err != nil {
t.Fatalf("os.Rename() failed on directory containing synced files: %v", err)
}
ValidateObjectContentsFromGCS(path.Join(NewDirName, FileName1), GCSFileContent, t)
ValidateObjectNotFoundErrOnGCS(path.Join(ExplicitDirName, FileName1), t)
ValidateObjectContentsFromGCS(path.Join(NewDirName, FileName2), FileContents+FileContents, t)
ValidateObjectNotFoundErrOnGCS(path.Join(ExplicitDirName, FileName2), t)
}

0 comments on commit 3e2b216

Please sign in to comment.