Skip to content

Commit

Permalink
Merge pull request #99926 from gjkim42/deflake-TestWatchFileChanged
Browse files Browse the repository at this point in the history
kubelet_test: Deflake TestWatchFileChanged
  • Loading branch information
k8s-ci-robot committed Mar 23, 2021
2 parents e471fa4 + d12072a commit ea07644
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions pkg/kubelet/config/file_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ var (
testCases = []struct {
watchDir bool
symlink bool
period time.Duration
}{
{true, true},
{true, false},
{false, true},
{false, false},
// set the period to be long enough for the file to be changed
// and short enough to trigger the event
{true, true, 3 * time.Second},

// set the period to avoid periodic PodUpdate event
{true, false, 60 * time.Second},
{false, true, 60 * time.Second},
{false, false, 60 * time.Second},
}
)

Expand All @@ -124,7 +129,7 @@ func TestWatchFileAdded(t *testing.T) {

func TestWatchFileChanged(t *testing.T) {
for _, testCase := range testCases {
watchFileChanged(testCase.watchDir, testCase.symlink, t)
watchFileChanged(testCase.watchDir, testCase.symlink, testCase.period, t)
}
}

Expand Down Expand Up @@ -275,7 +280,7 @@ func watchFileAdded(watchDir bool, symlink bool, t *testing.T) {
}
}

func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
func watchFileChanged(watchDir bool, symlink bool, period time.Duration, t *testing.T) {
hostname := types.NodeName("random-test-hostname")
var testCases = getTestCases(hostname)

Expand Down Expand Up @@ -314,22 +319,23 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
}()

if watchDir {
NewSourceFile(dirName, hostname, 100*time.Millisecond, ch)
NewSourceFile(dirName, hostname, period, ch)
} else {
NewSourceFile(file, hostname, 100*time.Millisecond, ch)
NewSourceFile(file, hostname, period, ch)
}

// await fsnotify to be ready
time.Sleep(time.Second)

// expect an update by SourceFile.resetStoreFromPath()
expectUpdate(t, ch, testCase)

pod := testCase.pod.(*v1.Pod)
pod.Spec.Containers[0].Name = "image2"

testCase.expected.Pods[0].Spec.Containers[0].Name = "image2"
changeFile := func() {
// Edit the file content
testCase.lock.Lock()
defer testCase.lock.Unlock()

pod := testCase.pod.(*v1.Pod)
pod.Spec.Containers[0].Name = "image2"

testCase.expected.Pods[0].Spec.Containers[0].Name = "image2"
if symlink {
file = testCase.writeToFile(linkedDirName, fileName, t)
return
Expand Down Expand Up @@ -374,8 +380,6 @@ func expectUpdate(t *testing.T, ch chan interface{}, testCase *testCase) {
}
}

testCase.lock.Lock()
defer testCase.lock.Unlock()
if !apiequality.Semantic.DeepEqual(testCase.expected, update) {
t.Fatalf("%s: Expected: %#v, Got: %#v", testCase.desc, testCase.expected, update)
}
Expand Down

0 comments on commit ea07644

Please sign in to comment.