Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky modification time check in integration tests #1875

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -68,25 +70,27 @@ 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)
}

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)
}
6 changes: 6 additions & 0 deletions tools/integration_tests/util/operations/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ import (
"strings"
"syscall"
"testing"
"time"
)

const (
OneKiB = 1024
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) {
Expand Down
Loading