44// you may not use this file except in compliance with the License.
55// You may obtain a copy of the License at
66//
7- // http://www.apache.org/licenses/LICENSE-2.0
7+ // http://www.apache.org/licenses/LICENSE-2.0
88//
99// Unless required by applicable law or agreed to in writing, software
1010// distributed under the License is distributed on an "AS IS" BASIS,
1515package readdirplus
1616
1717import (
18+ "context"
1819 "log"
1920 "os"
2021 "path"
2122 "testing"
2223 "time"
2324
25+ "cloud.google.com/go/storage"
26+ "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
2427 "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/operations"
2528 "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/setup"
26- "github.com/jacobsa/fuse/fusetesting"
2729 "github.com/stretchr/testify/assert"
2830 "github.com/stretchr/testify/require"
2931 "github.com/stretchr/testify/suite"
3032)
3133
32- type readdirplusWithDentryCacheTest struct {
33- flags []string
34+ type ReaddirplusWithDentryCacheTest struct {
35+ flags []string
36+ storageClient * storage.Client
37+ ctx context.Context
38+ baseTestName string
3439 suite.Suite
3540}
3641
37- func (s * readdirplusWithDentryCacheTest ) SetupTest () {
38- mountGCSFuseAndSetupTestDir (s .flags , testDirName )
42+ func (s * ReaddirplusWithDentryCacheTest ) SetupTest () {
43+ //Truncate log file created.
44+ err := os .Truncate (testEnv .cfg .LogFile , 0 )
45+ require .NoError (s .T (), err )
46+ // Clean up the cache directory path as gcsfuse don't clean up on mounting.
47+ // operations.RemoveDir(testEnv.cacheDirPath)
48+ testEnv .testDirPath = client .SetupTestDirectory (s .ctx , s .storageClient , testDirName )
3949}
4050
41- func (s * readdirplusWithDentryCacheTest ) TearDownTest () {
42- if setup .MountedDirectory () == "" { // Only unmount if not using a pre-mounted directory
43- setup .CleanupDirectoryOnGCS (ctx , storageClient , path .Join (setup .TestBucket (), testDirName ))
44- setup .UnmountGCSFuseAndDeleteLogFile (rootDir )
45- }
51+ func (s * ReaddirplusWithDentryCacheTest ) TearDownTest () {
52+ setup .SaveGCSFuseLogFileInCaseOfFailure (s .T ())
53+ }
54+
55+ func (s * ReaddirplusWithDentryCacheTest ) TearDownSuite () {
56+ setup .UnmountGCSFuseWithConfig (testEnv .cfg )
57+ }
58+
59+ func (s * ReaddirplusWithDentryCacheTest ) SetupSuite () {
60+ // setupLogFileAndCacheDir(s.baseTestName)
61+ mountGCSFuseAndSetupTestDir (s .flags , s .ctx , s .storageClient )
4662}
4763
48- func (s * readdirplusWithDentryCacheTest ) TestReaddirplusWithDentryCache () {
49- // Create directory structure
50- // testBucket/target_dir/ -- Dir
51- // testBucket/target_dir/file -- File
52- // testBucket/target_dir/emptySubDirectory -- Dir
53- // testBucket/target_dir/subDirectory -- Dir
54- // testBucket/target_dir/subDirectory/file1 -- File
55- targetDir := path .Join (testDirPath , targetDirName )
64+ func (s * ReaddirplusWithDentryCacheTest ) TestReaddirplusWithDentryCache () {
65+ // Create directory structure:
66+ // testBucket/dirForReaddirplusTest/ target_dir/
67+ // testBucket/dirForReaddirplusTest/ target_dir/file
68+ // testBucket/dirForReaddirplusTest/ target_dir/emptySubDirectory/
69+ // testBucket/dirForReaddirplusTest/ target_dir/subDirectory/
70+ // testBucket/dirForReaddirplusTest/ target_dir/subDirectory/file1
71+ targetDir := path .Join (testEnv . testDirPath , targetDirName )
5672 operations .CreateDirectory (targetDir , s .T ())
57- // Create a file in the target directory.
58- f1 := operations .CreateFile (path .Join (targetDir , "file" ), setup .FilePermission_0600 , s .T ())
59- operations .CloseFileShouldNotThrowError (s .T (), f1 )
60- // Create an empty subdirectory
73+ operations .CreateFile (path .Join (targetDir , "file" ), setup .FilePermission_0600 , s .T ())
6174 operations .CreateDirectory (path .Join (targetDir , "emptySubDirectory" ), s .T ())
62- // Create a subdirectory with file
63- operations .CreateDirectoryWithNFiles ( 1 , path .Join (targetDir , "subDirectory" ) , "file" , s .T ())
75+ operations . CreateDirectory ( path . Join ( targetDir , "subDirectory" ), s . T ())
76+ operations .CreateFile ( path .Join (targetDir , "subDirectory" , "file1" ), setup . FilePermission_0600 , s .T ())
6477
65- // Call Readdirplus to list the directory.
6678 startTime := time .Now ()
67- entries , err := fusetesting .ReadDirPlusPicky (targetDir )
79+ // ls the directory. This should call ReadDirPlus.
80+ entries , err := os .ReadDir (targetDir )
81+ s .Require ().NoError (err )
6882 endTime := time .Now ()
6983
70- require . NoError ( s . T (), err , "ReadDirPlusPicky failed" )
84+ // Verify the entries.
7185 expectedEntries := []struct {
7286 name string
7387 isDir bool
@@ -77,31 +91,35 @@ func (s *readdirplusWithDentryCacheTest) TestReaddirplusWithDentryCache() {
7791 {name : "file" , isDir : false , mode : 0644 },
7892 {name : "subDirectory" , isDir : true , mode : os .ModeDir | 0755 },
7993 }
80- // Verify the entries.
8194 assert .Equal (s .T (), len (expectedEntries ), len (entries ), "Number of entries mismatch" )
8295 for i , expected := range expectedEntries {
8396 entry := entries [i ]
84- assert .Equal (s .T (), expected .name , entry .Name (), "Name mismatch for entry %d" , i )
97+ assert .Equal (s .T (), expected .name , entry .Name (), "Name mismatch for entry %d" , i ) //
98+ fileInfo , err := entry .Info ()
99+ s .Require ().NoError (err )
85100 assert .Equal (s .T (), expected .isDir , entry .IsDir (), "IsDir mismatch for entry %s" , entry .Name ())
86- assert .Equal (s .T (), expected .mode , entry .Mode (), "Mode mismatch for entry %s" , entry .Name ())
101+ assert .Equal (s .T (), expected .mode , fileInfo .Mode (), "Mode mismatch for entry %s" , entry .Name ())
87102 }
103+
88104 // Dentry cache is enabled, so LookUpInode should also not be called.
89105 // This applies even to the parent directory, as its inode is cached during
90106 // the test setup phase when the directory structure is created.
91- validateLogsForReaddirplus (s .T (), setup . LogFile () , true , startTime , endTime )
107+ validateLogsForReaddirplus (s .T (), testEnv . cfg . LogFile , true , startTime , endTime )
92108}
93109
94110func TestReaddirplusWithDentryCacheTest (t * testing.T ) {
95- ts := & readdirplusWithDentryCacheTest { }
111+ ts := & ReaddirplusWithDentryCacheTest { ctx : context . Background (), storageClient : testEnv . storageClient , baseTestName : t . Name () }
96112
97- // Run tests for mounted directory if the flag is set.
98- if setup . AreBothMountedDirectoryAndTestBucketFlagsSet () {
113+ // Run tests for mounted directory if the flag is set. This assumes that run flag is properly passed by GKE team as per the config.
114+ if testEnv . cfg . GKEMountedDirectory != "" && testEnv . cfg . TestBucket != "" {
99115 suite .Run (t , ts )
100116 return
101117 }
102118
103- // Setup flags and run tests.
104- ts .flags = []string {"--implicit-dirs" , "--experimental-enable-readdirplus" , "--experimental-enable-dentry-cache" }
105- log .Printf ("Running tests with flags: %s" , ts .flags )
106- suite .Run (t , ts )
119+ // Run tests for GCE environment otherwise.
120+ flagsSet := setup .BuildFlagSets (* testEnv .cfg , testEnv .bucketType , t .Name ())
121+ for _ , ts .flags = range flagsSet {
122+ log .Printf ("Running tests with flags: %s" , ts .flags )
123+ suite .Run (t , ts )
124+ }
107125}
0 commit comments