From d12072a23a97e309eccf8596cde34343fdd55244 Mon Sep 17 00:00:00 2001 From: Geon-Ju Kim Date: Mon, 8 Mar 2021 09:56:51 +0900 Subject: [PATCH] kubelet_test: Deflake TestWatchFileChanged - Adjust the period to trigger PodUpdate event - Fix lock race between 'changeFile' and 'expectUpdate' - Await fsnotify to be ready --- pkg/kubelet/config/file_linux_test.go | 38 +++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/pkg/kubelet/config/file_linux_test.go b/pkg/kubelet/config/file_linux_test.go index 2c1c0018cd7..e3cab80ea49 100644 --- a/pkg/kubelet/config/file_linux_test.go +++ b/pkg/kubelet/config/file_linux_test.go @@ -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}, } ) @@ -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) } } @@ -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) @@ -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 @@ -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) }