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..cc56ce3673 100644 --- a/tools/integration_tests/operations/file_and_dir_attributes_test.go +++ b/tools/integration_tests/operations/file_and_dir_attributes_test.go @@ -40,7 +40,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) + + statModTime := oStat.ModTime() if (preCreateTime.After(statModTime)) || (postCreateTime.Before(statModTime)) { t.Errorf("File modification time not in the expected time-range") } @@ -53,12 +54,13 @@ 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) + // 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().Round(time.Second) + 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 @@ -68,12 +70,13 @@ 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) + // 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().Round(time.Second) + postCreateTime := time.Now().Add(operations.TimeSlop) checkIfObjectAttrIsCorrect(path.Join(testDir, DirAttrTest), preCreateTime, postCreateTime, 0, t) } @@ -81,12 +84,13 @@ 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) + // 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().Round(time.Second) + postCreateTime := time.Now().Add(operations.TimeSlop) 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) {