这是indexloc提供的服务,不要输入任何密码
Skip to content

feat(dentry cache): Add integration tests for dentry cache #3562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions internal/fs/wrappers/error_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func errno(err error, preconditionErrCfg bool) error {
return nil
}

// The object is modified or deleted by a concurrent process.
var clobberedErr *gcsfuse_errors.FileClobberedError
if errors.As(err, &clobberedErr) {
if preconditionErrCfg {
return syscall.ESTALE
}
return nil
}

// Use existing em errno
var errno syscall.Errno
if errors.As(err, &errno) {
Expand All @@ -51,15 +60,6 @@ func errno(err error, preconditionErrCfg bool) error {
return syscall.EINTR
}

// The object is modified or deleted by a concurrent process.
var clobberedErr *gcsfuse_errors.FileClobberedError
if errors.As(err, &clobberedErr) {
if preconditionErrCfg {
return syscall.ESTALE
}
return nil
}

if errors.Is(err, storage.ErrObjectNotExist) {
return syscall.ENOENT
}
Expand Down
2 changes: 2 additions & 0 deletions tools/cd_scripts/e2e_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ TEST_DIR_PARALLEL=(
"negative_stat_cache"
"streaming_writes"
"release_version"
"readdirplus"
"dentry_cache"
)

# These tests never become parallel as they are changing bucket permissions.
Expand Down
111 changes: 111 additions & 0 deletions tools/integration_tests/dentry_cache/notifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2025 Google LLC
//
// 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 dentry_cache

import (
"log"
"os"
"path"
"testing"

"cloud.google.com/go/storage"

"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/setup"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/test_setup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type notifierTest struct {
flags []string
}

func (s *notifierTest) Setup(t *testing.T) {
mountGCSFuseAndSetupTestDir(s.flags, testDirName)
}

func (s *notifierTest) Teardown(t *testing.T) {
if setup.MountedDirectory() == "" { // Only unmount if not using a pre-mounted directory
setup.CleanupDirectoryOnGCS(ctx, storageClient, path.Join(setup.TestBucket(), testDirName))
setup.UnmountGCSFuseAndDeleteLogFile(rootDir)
}
}

func (s *notifierTest) TestWriteFileWithDentryCacheEnabled(t *testing.T) {
// Create a file with initial content directly in GCS.
filePath := path.Join(setup.MntDir(), testDirName, testFileName)
client.SetupFileInTestDirectory(ctx, storageClient, testDirName, testFileName, initialContentSize, t)
// Stat file to cache the entry
_, err := os.Stat(filePath)
require.Nil(t, err)
// Modify the object on GCS.
objectName := path.Join(testDirName, testFileName)
smallContent, err := operations.GenerateRandomData(updatedContentSize)
require.Nil(t, err)
require.Nil(t, client.WriteToObject(ctx, storageClient, objectName, string(smallContent), storage.Conditions{}))

// First Write File attempt.
err = operations.WriteFile(filePath, "ShouldNotWrite")

// First Write File attempt should fail because file has been clobbered.
operations.ValidateESTALEError(t, err)
// Second Write File attempt.
err = operations.WriteFile(filePath, "ShouldWrite")
// The notifier is triggered after the first write failure, invalidating the kernel cache entry.
// Therefore, the second write succeeds even before the metadata cache TTL expires.
assert.Nil(t, err)
}

func (s *notifierTest) TestReadFileWithDentryCacheEnabled(t *testing.T) {
// Create a file with initial content directly in GCS.
filePath := path.Join(setup.MntDir(), testDirName, testFileName)
client.SetupFileInTestDirectory(ctx, storageClient, testDirName, testFileName, initialContentSize, t)
// Stat file to cache the entry
_, err := os.Stat(filePath)
require.Nil(t, err)
// Modify the object on GCS.
objectName := path.Join(testDirName, testFileName)
smallContent, err := operations.GenerateRandomData(updatedContentSize)
require.Nil(t, err)
require.Nil(t, client.WriteToObject(ctx, storageClient, objectName, string(smallContent), storage.Conditions{}))

// First Read File attempt.
_, err = operations.ReadFile(filePath)

// First Read File attempt should fail because file has been clobbered.
operations.ValidateESTALEError(t, err)
// Second Read File attempt.
_, err = operations.ReadFile(filePath)
// The notifier is triggered after the first read failure, invalidating the kernel cache entry.
// Therefore, the second read succeeds even before the metadata cache TTL expires.
assert.Nil(t, err)
}

func TestNotifierTest(t *testing.T) {
ts := &notifierTest{}

// Run tests for mounted directory if the flag is set.
if setup.AreBothMountedDirectoryAndTestBucketFlagsSet() {
test_setup.RunTests(t, ts)
return
}

// Setup flags and run tests.
ts.flags = []string{"--implicit-dirs", "--experimental-enable-dentry-cache", "--metadata-cache-ttl-secs=1000"}
log.Printf("Running tests with flags: %s", ts.flags)
test_setup.RunTests(t, ts)
}
89 changes: 89 additions & 0 deletions tools/integration_tests/dentry_cache/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2025 Google LLC
//
// 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 enabling dentry cache.
package dentry_cache

import (
"context"
"log"
"os"
"testing"

"cloud.google.com/go/storage"

"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/mounting/static_mounting"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/setup"
)

const (
testDirName = "testDirForDentryCache"
testFileName = "testFile"
initialContentSize = 5
updatedContentSize = 10
)

var (
storageClient *storage.Client
ctx context.Context
testDirPath string
mountFunc func([]string) error
// mount directory is where our tests run.
mountDir string
// root directory is the directory to be unmounted.
rootDir string
)

func mountGCSFuseAndSetupTestDir(flags []string, testDirName string) {
setup.MountGCSFuseWithGivenMountFunc(flags, mountFunc)
setup.SetMntDir(mountDir)
testDirPath = client.SetupTestDirectory(ctx, storageClient, testDirName)
}

func TestMain(m *testing.M) {
setup.ParseSetUpFlags()
if setup.TestBucket() == "" {
log.Print("--testbucket must be specified")
os.Exit(1)
}

// Create common storage client to be used in test.
ctx = context.Background()
closeStorageClient := client.CreateStorageClientWithCancel(&ctx, &storageClient)
defer func() {
err := closeStorageClient()
if err != nil {
log.Fatalf("closeStorageClient failed: %v", err)
}
}()

if setup.MountedDirectory() != "" {
mountDir = setup.MountedDirectory()
// Run tests for mounted directory if the flag is set.
os.Exit(m.Run())
}
// Else run tests for testBucket.
// Set up test directory.
setup.SetUpTestDirForTestBucketFlag()

// Save mount and root directory variables.
mountDir, rootDir = setup.MntDir(), setup.MntDir()

log.Println("Running static mounting tests...")
mountFunc = static_mounting.MountGcsfuseWithStaticMounting
successCode := m.Run()

os.Exit(successCode)
}
88 changes: 88 additions & 0 deletions tools/integration_tests/dentry_cache/stat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2025 Google LLC
//
// 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 dentry_cache

import (
"log"
"os"
"path"
"testing"
"time"

"cloud.google.com/go/storage"

"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/setup"
"github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/test_setup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type statWithDentryCacheEnabledTest struct {
flags []string
}

func (s *statWithDentryCacheEnabledTest) Setup(t *testing.T) {
mountGCSFuseAndSetupTestDir(s.flags, testDirName)
}

func (s *statWithDentryCacheEnabledTest) Teardown(t *testing.T) {
if setup.MountedDirectory() == "" { // Only unmount if not using a pre-mounted directory
setup.CleanupDirectoryOnGCS(ctx, storageClient, path.Join(setup.TestBucket(), testDirName))
setup.UnmountGCSFuseAndDeleteLogFile(rootDir)
}
}

func (s *statWithDentryCacheEnabledTest) TestStatWithDentryCacheEnabled(t *testing.T) {
// Create a file with initial content directly in GCS.
filePath := path.Join(setup.MntDir(), testDirName, testFileName)
client.SetupFileInTestDirectory(ctx, storageClient, testDirName, testFileName, initialContentSize, t)
// Stat file to cache the entry
_, err := os.Stat(filePath)
require.Nil(t, err)
// Modify the object on GCS.
objectName := path.Join(testDirName, testFileName)
smallContent, err := operations.GenerateRandomData(updatedContentSize)
require.Nil(t, err)
require.Nil(t, client.WriteToObject(ctx, storageClient, objectName, string(smallContent), storage.Conditions{}))

// Stat again, it should give old cached attributes.
fileInfo, err := os.Stat(filePath)

assert.Nil(t, err)
assert.Equal(t, int64(initialContentSize), fileInfo.Size())
// Wait until entry expires in cache.
time.Sleep(1100 * time.Millisecond)
// Stat again, it should give updated attributes.
fileInfo, err = os.Stat(filePath)
assert.Nil(t, err)
assert.Equal(t, int64(updatedContentSize), fileInfo.Size())
}

func TestStatWithDentryCacheEnabledTest(t *testing.T) {
ts := &statWithDentryCacheEnabledTest{}

// Run tests for mounted directory if the flag is set.
if setup.AreBothMountedDirectoryAndTestBucketFlagsSet() {
test_setup.RunTests(t, ts)
return
}

// Setup flags and run tests.
ts.flags = []string{"--implicit-dirs", "--experimental-enable-dentry-cache", "--metadata-cache-ttl-secs=1"}
log.Printf("Running tests with flags: %s", ts.flags)
test_setup.RunTests(t, ts)
}
1 change: 1 addition & 0 deletions tools/integration_tests/improved_run_e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ TEST_PACKAGES_COMMON=(
"stale_handle"
"release_version"
"readdirplus"
"dentry_cache"
)

# Test packages for regional buckets.
Expand Down
3 changes: 3 additions & 0 deletions tools/integration_tests/run_e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ TEST_DIR_PARALLEL=(
"cloud_profiler"
"release_version"
"readdirplus"
"dentry_cache"
)

# These tests never become parallel as it is changing bucket permissions.
Expand Down Expand Up @@ -154,6 +155,8 @@ TEST_DIR_PARALLEL_FOR_ZB=(
"write_large_files"
"unfinalized_object"
"release_version"
"readdirplus"
"dentry_cache"
)

# Subset of TEST_DIR_NON_PARALLEL,
Expand Down
18 changes: 18 additions & 0 deletions tools/integration_tests/run_tests_mounted_directory.sh
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,21 @@ gcsfuse --implicit-dirs --experimental-enable-readdirplus --log-file $log_file -
GODEBUG=asyncpreemptoff=1 go test ./tools/integration_tests/readdirplus/... -p 1 --integrationTest -v --mountedDirectory="$MOUNT_DIR" --testbucket="$TEST_BUCKET_NAME" -run $test_case
sudo umount "$MOUNT_DIR"
rm -rf $log_dir

# Test package: dentry_cache
# Run stat with dentry cache enabled
test_case="TestStatWithDentryCacheEnabledTest/TestStatWithDentryCacheEnabled"
gcsfuse --implicit-dirs --experimental-enable-dentry-cache --metadata-cache-ttl-secs=1 "$TEST_BUCKET_NAME" "$MOUNT_DIR"
GODEBUG=asyncpreemptoff=1 go test ./tools/integration_tests/dentry_cache/... -p 1 --integrationTest -v --mountedDirectory="$MOUNT_DIR" --testbucket="$TEST_BUCKET_NAME" -run $test_case
sudo umount "$MOUNT_DIR"

# Run notifier tests
test_cases=(
"TestNotifierTest/TestReadFileWithDentryCacheEnabled"
"TestNotifierTest/TestWriteFileWithDentryCacheEnabled"
)
for test_case in "${test_cases[@]}"; do
gcsfuse --implicit-dirs --experimental-enable-dentry-cache --metadata-cache-ttl-secs=1000 "$TEST_BUCKET_NAME" "$MOUNT_DIR"
GODEBUG=asyncpreemptoff=1 go test ./tools/integration_tests/dentry_cache/... -p 1 --integrationTest -v --mountedDirectory="$MOUNT_DIR" --testbucket="$TEST_BUCKET_NAME" -run $test_case
sudo umount "$MOUNT_DIR"
done
Loading