From e30e0865b1148a9df0c4695a211c79677923efd3 Mon Sep 17 00:00:00 2001 From: Ashmeen Kaur Date: Mon, 29 Apr 2024 08:47:58 +0000 Subject: [PATCH 1/2] fix flaky file attributes tests --- .../file_and_dir_attributes_test.go | 26 +++++++------------ .../util/operations/file_operations.go | 6 +++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tools/integration_tests/operations/file_and_dir_attributes_test.go b/tools/integration_tests/operations/file_and_dir_attributes_test.go index 3b402aac73..4140f39956 100644 --- a/tools/integration_tests/operations/file_and_dir_attributes_test.go +++ b/tools/integration_tests/operations/file_and_dir_attributes_test.go @@ -23,6 +23,8 @@ import ( "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations" "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup" + "github.com/jacobsa/fuse/fusetesting" + "github.com/jacobsa/ogletest" ) const DirAttrTest = "dirAttrTest" @@ -40,10 +42,8 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre if objName != statObjName { t.Errorf("File name not matched in os.Stat, found: %s, expected: %s", statObjName, objName) } - statModTime := oStat.ModTime().Round(time.Second) - if (preCreateTime.After(statModTime)) || (postCreateTime.Before(statModTime)) { - t.Errorf("File modification time not in the expected time-range") - } + ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(preCreateTime, operations.TimeSlop)) + ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(postCreateTime, operations.TimeSlop)) if oStat.Size() != byteSize { t.Errorf("File size is not %v bytes, found size: %d bytes", BytesWrittenInFile, oStat.Size()) @@ -53,12 +53,10 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre func TestFileAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - // kernel time can be slightly out of sync of time.Now(), so rounding off - // times to seconds. Ref: https://github.com/golang/go/issues/33510 - preCreateTime := time.Now().Round(time.Second) + preCreateTime := time.Now() fileName := path.Join(testDir, tempFileName) operations.CreateFileWithContent(fileName, setup.FilePermission_0600, Content, t) - postCreateTime := time.Now().Round(time.Second) + postCreateTime := time.Now() // The file size in createTempFile() is BytesWrittenInFile bytes // https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/tools/integration_tests/util/setup/setup.go#L124 @@ -68,12 +66,10 @@ func TestFileAttributes(t *testing.T) { func TestEmptyDirAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - // kernel time can be slightly out of sync of time.Now(), so rounding off - // times to seconds. Ref: https://github.com/golang/go/issues/33510 - preCreateTime := time.Now().Round(time.Second) + preCreateTime := time.Now() dirName := path.Join(testDir, DirAttrTest) operations.CreateDirectoryWithNFiles(0, dirName, "", t) - postCreateTime := time.Now().Round(time.Second) + postCreateTime := time.Now() checkIfObjectAttrIsCorrect(path.Join(testDir, DirAttrTest), preCreateTime, postCreateTime, 0, t) } @@ -81,12 +77,10 @@ func TestEmptyDirAttributes(t *testing.T) { func TestNonEmptyDirAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - // kernel time can be slightly out of sync of time.Now(), so rounding off - // times to seconds. Ref: https://github.com/golang/go/issues/33510 - preCreateTime := time.Now().Round(time.Second) + preCreateTime := time.Now() dirName := path.Join(testDir, DirAttrTest) operations.CreateDirectoryWithNFiles(NumberOfFilesInDirAttrTest, dirName, PrefixFileInDirAttrTest, t) - postCreateTime := time.Now().Round(time.Second) + postCreateTime := time.Now() checkIfObjectAttrIsCorrect(dirName, preCreateTime, postCreateTime, 0, t) } diff --git a/tools/integration_tests/util/operations/file_operations.go b/tools/integration_tests/util/operations/file_operations.go index b5b54c0d68..7bc432b525 100644 --- a/tools/integration_tests/util/operations/file_operations.go +++ b/tools/integration_tests/util/operations/file_operations.go @@ -29,6 +29,7 @@ import ( "strings" "syscall" "testing" + "time" ) const ( @@ -36,6 +37,11 @@ const ( OneMiB = OneKiB * OneKiB // ChunkSizeForContentComparison is currently set to 1 MiB. ChunkSizeForContentComparison int = OneMiB + + // TimeSlop The radius we use for "expect mtime is within"-style assertions as kernel + // time can be slightly out of sync of time.Now(). + // Ref: https://github.com/golang/go/issues/33510 + TimeSlop = 25 * time.Millisecond ) func copyFile(srcFileName, dstFileName string, allowOverwrite bool) (err error) { From e99b351911e3a3a5a3a6b3c970259fc03814183b Mon Sep 17 00:00:00 2001 From: Ashmeen Kaur Date: Mon, 29 Apr 2024 10:04:17 +0000 Subject: [PATCH 2/2] changes to use time slop --- .../file_and_dir_attributes_test.go | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tools/integration_tests/operations/file_and_dir_attributes_test.go b/tools/integration_tests/operations/file_and_dir_attributes_test.go index 4140f39956..cc56ce3673 100644 --- a/tools/integration_tests/operations/file_and_dir_attributes_test.go +++ b/tools/integration_tests/operations/file_and_dir_attributes_test.go @@ -23,8 +23,6 @@ import ( "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations" "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup" - "github.com/jacobsa/fuse/fusetesting" - "github.com/jacobsa/ogletest" ) const DirAttrTest = "dirAttrTest" @@ -42,8 +40,11 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre if objName != statObjName { t.Errorf("File name not matched in os.Stat, found: %s, expected: %s", statObjName, objName) } - ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(preCreateTime, operations.TimeSlop)) - ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(postCreateTime, operations.TimeSlop)) + + statModTime := oStat.ModTime() + if (preCreateTime.After(statModTime)) || (postCreateTime.Before(statModTime)) { + t.Errorf("File modification time not in the expected time-range") + } if oStat.Size() != byteSize { t.Errorf("File size is not %v bytes, found size: %d bytes", BytesWrittenInFile, oStat.Size()) @@ -53,10 +54,13 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre func TestFileAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - preCreateTime := time.Now() + // kernel time can be slightly out of sync of time.Now(), so using + // operations.TimeSlop to adjust pre and post create time. + // Ref: https://github.com/golang/go/issues/33510 + preCreateTime := time.Now().Add(-operations.TimeSlop) fileName := path.Join(testDir, tempFileName) operations.CreateFileWithContent(fileName, setup.FilePermission_0600, Content, t) - postCreateTime := time.Now() + postCreateTime := time.Now().Add(+operations.TimeSlop) // The file size in createTempFile() is BytesWrittenInFile bytes // https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/tools/integration_tests/util/setup/setup.go#L124 @@ -66,10 +70,13 @@ func TestFileAttributes(t *testing.T) { func TestEmptyDirAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - preCreateTime := time.Now() + // kernel time can be slightly out of sync of time.Now(), so using + // operations.TimeSlop to adjust pre and post create time. + // Ref: https://github.com/golang/go/issues/33510 + preCreateTime := time.Now().Add(-operations.TimeSlop) dirName := path.Join(testDir, DirAttrTest) operations.CreateDirectoryWithNFiles(0, dirName, "", t) - postCreateTime := time.Now() + postCreateTime := time.Now().Add(operations.TimeSlop) checkIfObjectAttrIsCorrect(path.Join(testDir, DirAttrTest), preCreateTime, postCreateTime, 0, t) } @@ -77,10 +84,13 @@ func TestEmptyDirAttributes(t *testing.T) { func TestNonEmptyDirAttributes(t *testing.T) { testDir := setup.SetupTestDirectory(DirForOperationTests) - preCreateTime := time.Now() + // kernel time can be slightly out of sync of time.Now(), so using + // operations.TimeSlop to adjust pre and post create time. + // Ref: https://github.com/golang/go/issues/33510 + preCreateTime := time.Now().Add(-operations.TimeSlop) dirName := path.Join(testDir, DirAttrTest) operations.CreateDirectoryWithNFiles(NumberOfFilesInDirAttrTest, dirName, PrefixFileInDirAttrTest, t) - postCreateTime := time.Now() + postCreateTime := time.Now().Add(operations.TimeSlop) checkIfObjectAttrIsCorrect(dirName, preCreateTime, postCreateTime, 0, t) }