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,
@@ -19,104 +19,48 @@ import (
1919 "slices"
2020 "testing"
2121
22- "cloud.google.com/go/storage"
23- . "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
22+ "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/client"
2423 "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/operations"
2524 "github.com/googlecloudplatform/gcsfuse/v3/tools/integration_tests/util/setup"
26- "github.com/stretchr/testify/assert"
2725 "github.com/stretchr/testify/suite"
2826)
2927
3028// //////////////////////////////////////////////////////////////////////
3129// Boilerplate
3230// //////////////////////////////////////////////////////////////////////
3331
34- type staleFileHandleEmptyGcsFileTest struct {
32+ type staleFileHandleSyncedFileTest struct {
3533 staleFileHandleCommon
3634}
3735
3836// //////////////////////////////////////////////////////////////////////
3937// Helpers
4038// //////////////////////////////////////////////////////////////////////
4139
42- func (s * staleFileHandleEmptyGcsFileTest ) SetupTest () {
43- s .staleFileHandleCommon .SetupTest ()
44- // Create an empty object on GCS.
40+ func (s * staleFileHandleSyncedFileTest ) SetupTest () {
4541 s .fileName = path .Base (s .T ().Name ()) + setup .GenerateRandomString (5 )
46- err := CreateObjectOnGCS ( testEnv . ctx , testEnv . storageClient , path . Join ( testDirName , s . fileName ), "" )
47- assert . NoError ( s . T (), err )
42+ // Create a file and sync it to GCS.
43+ client . SetupFileInTestDirectory ( testEnv . ctx , testEnv . storageClient , testDirName , s . fileName , 1 , s . T ())
4844 s .f1 = operations .OpenFileWithODirect (s .T (), path .Join (testEnv .testDirPath , s .fileName ))
49- s .isStreamingWritesEnabled = ! slices . Contains ( s . flags [ 0 ], "--enable-streaming-writes= false" )
45+ s .isLocal = false
5046}
5147
5248////////////////////////////////////////////////////////////////////////
53- // Tests
49+ // Test Function (Runs once before all tests)
5450////////////////////////////////////////////////////////////////////////
5551
56- func (s * staleFileHandleEmptyGcsFileTest ) TestClobberedFileReadThrowsStaleFileHandleError () {
57- // TODO(b/410698332): Remove skip condition once takeover support is available.
58- if s .isStreamingWritesEnabled && setup .IsZonalBucketRun () {
59- s .T ().Skip ("Skip test due to takeover support not available." )
60- }
61- // Dirty the file by giving it some contents.
62- _ , err := s .f1 .WriteAt ([]byte (s .data ), 0 )
63- assert .NoError (s .T (), err )
64- operations .SyncFile (s .f1 , s .T ())
65-
66- // Replace the underlying object with a new generation.
67- err = WriteToObject (testEnv .ctx , testEnv .storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
68-
69- assert .NoError (s .T (), err )
70- buffer := make ([]byte , len (s .data ))
71- _ , err = s .f1 .Read (buffer )
72- operations .ValidateESTALEError (s .T (), err )
73- }
74-
75- func (s * staleFileHandleEmptyGcsFileTest ) TestClobberedFileFirstWriteThrowsStaleFileHandleError () {
76- // TODO(b/410698332): Remove skip condition once takeover support is available.
77- if s .isStreamingWritesEnabled && setup .IsZonalBucketRun () {
78- s .T ().Skip ("Skip test due to takeover support not available." )
52+ func TestStaleFileHandleSyncedFileTest (t * testing.T ) {
53+ // Run tests for mounted directory if the flag is set and return.
54+ if setup .AreBothMountedDirectoryAndTestBucketFlagsSet () {
55+ suite .Run (t , new (staleFileHandleSyncedFileTest ))
56+ return
7957 }
80- // Clobber file by replacing the underlying object with a new generation.
81- err := WriteToObject (testEnv .ctx , testEnv .storageClient , path .Join (testDirName , s .fileName ), FileContents , storage.Conditions {})
82- assert .NoError (s .T (), err )
83-
84- // Attempt first write to the file should give stale NFS file handle error.
85- _ , err = s .f1 .Write ([]byte (s .data ))
86-
87- assert .NoError (s .T (), err )
88- operations .ValidateSyncGivenThatFileIsClobbered (s .T (), s .f1 , s .isStreamingWritesEnabled )
89- err = s .f1 .Close ()
90- operations .ValidateESTALEError (s .T (), err )
91- ValidateObjectContentsFromGCS (testEnv .ctx , testEnv .storageClient , testDirName , s .fileName , FileContents , s .T ())
92- }
9358
94- func (s * staleFileHandleEmptyGcsFileTest ) TestFileDeletedRemotelySyncAndCloseThrowsStaleFileHandleError () {
95- // TODO(mohitkyadav): Enable test once fix in b/415713332 is released
96- if s .isStreamingWritesEnabled && setup .IsZonalBucketRun () {
97- s .T ().Skip ("Skip test due to bug (b/415713332) in client." )
59+ flagsSet := setup .BuildFlagSets (* testEnv .cfg , testEnv .bucketType , t .Name ())
60+ for _ , flags := range flagsSet {
61+ s := new (staleFileHandleSyncedFileTest )
62+ s .flags = flags
63+ s .isStreamingWritesEnabled = ! slices .Contains (s .flags , "--enable-streaming-writes=false" )
64+ suite .Run (t , s )
9865 }
99- // Dirty the file by giving it some contents.
100- operations .WriteWithoutClose (s .f1 , s .data , s .T ())
101- // Delete the file remotely.
102- err := DeleteObjectOnGCS (testEnv .ctx , testEnv .storageClient , path .Join (testDirName , s .fileName ))
103- assert .NoError (s .T (), err )
104- // Verify unlink operation succeeds.
105- ValidateObjectNotFoundErrOnGCS (testEnv .ctx , testEnv .storageClient , testDirName , s .fileName , s .T ())
106- // Attempt to write to file should not give any error.
107- operations .WriteWithoutClose (s .f1 , s .data , s .T ())
108-
109- operations .ValidateSyncGivenThatFileIsClobbered (s .T (), s .f1 , s .isStreamingWritesEnabled )
110-
111- err = s .f1 .Close ()
112- operations .ValidateESTALEError (s .T (), err )
113- ValidateObjectNotFoundErrOnGCS (testEnv .ctx , testEnv .storageClient , testDirName , s .fileName , s .T ())
11466}
115-
116- ////////////////////////////////////////////////////////////////////////
117- // Test Function (Runs once before all tests)
118- ////////////////////////////////////////////////////////////////////////
119-
120- func TestStaleFileHandleEmptyGcsFileTest (t * testing.T ) {
121- suite .Run (t , new (staleFileHandleEmptyGcsFileTest ))
122- }
0 commit comments