1515package stale_handle
1616
1717import (
18+ "log"
1819 "path"
19- "slices"
2020 "testing"
2121
2222 "cloud.google.com/go/storage"
@@ -30,6 +30,9 @@ import (
3030// //////////////////////////////////////////////////////////////////////
3131// Boilerplate
3232// //////////////////////////////////////////////////////////////////////
33+ type staleFileHandleLocalFile struct {
34+ staleFileHandleCommon
35+ }
3336
3437type staleFileHandleEmptyGcsFile struct {
3538 staleFileHandleCommon
@@ -39,12 +42,24 @@ type staleFileHandleEmptyGcsFile struct {
3942// Helpers
4043// //////////////////////////////////////////////////////////////////////
4144
45+ func (s * staleFileHandleLocalFile ) SetupTest () {
46+ // Create a local file.
47+ s .fileName = path .Base (s .T ().Name ()) + setup .GenerateRandomString (5 )
48+ s .f1 = operations .OpenFileWithODirect (s .T (), path .Join (testEnv .testDirPath , s .fileName ))
49+ s .isLocal = true
50+ }
51+ func (s * staleFileHandleLocalFile ) TearDownTest () {
52+ setup .SaveGCSFuseLogFileInCaseOfFailure (s .T ())
53+ }
54+
4255func (s * staleFileHandleEmptyGcsFile ) SetupTest () {
43- // Create an empty object on GCS.
4456 s .fileName = path .Base (s .T ().Name ()) + setup .GenerateRandomString (5 )
45- err := CreateObjectOnGCS (ctx , storageClient , path .Join (testDirName , s .fileName ), "" )
57+ err := CreateObjectOnGCS (testEnv . ctx , testEnv . storageClient , path .Join (testDirName , s .fileName ), "" )
4658 assert .NoError (s .T (), err )
47- s .f1 = operations .OpenFileWithODirect (s .T (), path .Join (s .testDirPath , s .fileName ))
59+ s .f1 = operations .OpenFileWithODirect (s .T (), path .Join (testEnv .testDirPath , s .fileName ))
60+ }
61+ func (s * staleFileHandleEmptyGcsFile ) TearDownTest () {
62+ setup .SaveGCSFuseLogFileInCaseOfFailure (s .T ())
4863}
4964
5065////////////////////////////////////////////////////////////////////////
@@ -62,7 +77,7 @@ func (s *staleFileHandleEmptyGcsFile) TestClobberedFileReadThrowsStaleFileHandle
6277 operations .SyncFile (s .f1 , s .T ())
6378
6479 // Replace the underlying object with a new generation.
65- err = WriteToObject (ctx , storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
80+ err = WriteToObject (testEnv . ctx , testEnv . storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
6681
6782 assert .NoError (s .T (), err )
6883 buffer := make ([]byte , len (s .data ))
@@ -76,7 +91,7 @@ func (s *staleFileHandleEmptyGcsFile) TestClobberedFileFirstWriteThrowsStaleFile
7691 s .T ().Skip ("Skip test due to takeover support not available." )
7792 }
7893 // Clobber file by replacing the underlying object with a new generation.
79- err := WriteToObject (ctx , storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
94+ err := WriteToObject (testEnv . ctx , testEnv . storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
8095 assert .NoError (s .T (), err )
8196
8297 // Attempt first write to the file should give stale NFS file handle error.
@@ -86,7 +101,7 @@ func (s *staleFileHandleEmptyGcsFile) TestClobberedFileFirstWriteThrowsStaleFile
86101 operations .ValidateSyncGivenThatFileIsClobbered (s .T (), s .f1 , s .isStreamingWritesEnabled )
87102 err = s .f1 .Close ()
88103 operations .ValidateESTALEError (s .T (), err )
89- ValidateObjectContentsFromGCS (ctx , storageClient , testDirName , s .fileName , FileContents , s .T ())
104+ ValidateObjectContentsFromGCS (testEnv . ctx , testEnv . storageClient , testDirName , s .fileName , FileContents , s .T ())
90105}
91106
92107func (s * staleFileHandleEmptyGcsFile ) TestFileDeletedRemotelySyncAndCloseThrowsStaleFileHandleError () {
@@ -97,34 +112,80 @@ func (s *staleFileHandleEmptyGcsFile) TestFileDeletedRemotelySyncAndCloseThrowsS
97112 // Dirty the file by giving it some contents.
98113 operations .WriteWithoutClose (s .f1 , s .data , s .T ())
99114 // Delete the file remotely.
100- err := DeleteObjectOnGCS (ctx , storageClient , path .Join (testDirName , s .fileName ))
115+ err := DeleteObjectOnGCS (testEnv . ctx , testEnv . storageClient , path .Join (testDirName , s .fileName ))
101116 assert .NoError (s .T (), err )
102117 // Verify unlink operation succeeds.
103- ValidateObjectNotFoundErrOnGCS (ctx , storageClient , testDirName , s .fileName , s .T ())
118+ ValidateObjectNotFoundErrOnGCS (testEnv . ctx , testEnv . storageClient , testDirName , s .fileName , s .T ())
104119 // Attempt to write to file should not give any error.
105120 operations .WriteWithoutClose (s .f1 , s .data , s .T ())
106121
107122 operations .ValidateSyncGivenThatFileIsClobbered (s .T (), s .f1 , s .isStreamingWritesEnabled )
108123
109124 err = s .f1 .Close ()
110125 operations .ValidateESTALEError (s .T (), err )
111- ValidateObjectNotFoundErrOnGCS (ctx , storageClient , testDirName , s .fileName , s .T ())
126+ ValidateObjectNotFoundErrOnGCS (testEnv . ctx , testEnv . storageClient , testDirName , s .fileName , s .T ())
112127}
113128
114129////////////////////////////////////////////////////////////////////////
115130// Test Function (Runs once before all tests)
116131////////////////////////////////////////////////////////////////////////
117132
118- func TestStaleFileHandleEmptyGcsFileTest (t * testing.T ) {
133+ func TestStaleHandleStreamingWritesEnabled (t * testing.T ) {
119134 // Run tests for mounted directory if the flag is set and return.
120135 if setup .AreBothMountedDirectoryAndTestBucketFlagsSet () {
121- suite .Run (t , new (staleFileHandleEmptyGcsFile ))
136+ // Run tests for local file.
137+ suite .Run (t , & staleFileHandleLocalFile {staleFileHandleCommon {isStreamingWritesEnabled : true }})
138+
139+ // Run tests for empty gcs file.
140+ suite .Run (t , & staleFileHandleEmptyGcsFile {staleFileHandleCommon {isStreamingWritesEnabled : true }})
141+
122142 return
123143 }
144+
145+ flagsSet := setup .BuildFlagSets (* testEnv .cfg , testEnv .bucketType , t .Name ())
146+ for _ , flags := range flagsSet {
147+ // Run local file tests
148+ sLocal := new (staleFileHandleLocalFile )
149+ sLocal .flags = flags
150+ log .Printf ("Running local file tests with flags: %s" , sLocal .flags )
151+ sLocal .isStreamingWritesEnabled = true
152+ suite .Run (t , sLocal )
153+
154+ // Run empty GCS file tests
155+ sEmptyGCS := new (staleFileHandleEmptyGcsFile )
156+ sEmptyGCS .flags = flags
157+ log .Printf ("Running empty GCS file tests with flags: %s" , sEmptyGCS .flags )
158+ sEmptyGCS .isStreamingWritesEnabled = true
159+ suite .Run (t , sEmptyGCS )
160+ }
161+ }
162+
163+ func TestStaleHandleStreamingWritesDisabled (t * testing.T ) {
164+ // Run tests for mounted directory if the flag is set and return.
165+ if setup .AreBothMountedDirectoryAndTestBucketFlagsSet () {
166+ // Run tests for local file.
167+ suite .Run (t , & staleFileHandleLocalFile {staleFileHandleCommon {isStreamingWritesEnabled : false }})
168+
169+ // Run tests for empty gcs file.
170+ suite .Run (t , & staleFileHandleEmptyGcsFile {staleFileHandleCommon {isStreamingWritesEnabled : false }})
171+
172+ return
173+ }
174+
175+ flagsSet := setup .BuildFlagSets (* testEnv .cfg , testEnv .bucketType , t .Name ())
124176 for _ , flags := range flagsSet {
125- s := new (staleFileHandleEmptyGcsFile )
126- s .flags = flags
127- s .isStreamingWritesEnabled = ! slices .Contains (s .flags , "--enable-streaming-writes=false" )
128- suite .Run (t , s )
177+ // Run local file tests
178+ sLocal := new (staleFileHandleLocalFile )
179+ sLocal .flags = flags
180+ log .Printf ("Running local file tests with flags: %s" , sLocal .flags )
181+ sLocal .isStreamingWritesEnabled = false
182+ suite .Run (t , sLocal )
183+
184+ // Run empty GCS file tests
185+ sEmptyGCS := new (staleFileHandleEmptyGcsFile )
186+ sEmptyGCS .flags = flags
187+ log .Printf ("Running empty GCS file tests with flags: %s" , sEmptyGCS .flags )
188+ sEmptyGCS .isStreamingWritesEnabled = false
189+ suite .Run (t , sEmptyGCS )
129190 }
130191}
0 commit comments