mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
kubelet_test: Deflake TestWatchFileChanged
- Adjust the period to trigger PodUpdate event - Fix lock race between 'changeFile' and 'expectUpdate' - Await fsnotify to be ready
This commit is contained in:
parent
4e95e1df04
commit
d12072a23a
@ -108,11 +108,16 @@ var (
|
|||||||
testCases = []struct {
|
testCases = []struct {
|
||||||
watchDir bool
|
watchDir bool
|
||||||
symlink bool
|
symlink bool
|
||||||
|
period time.Duration
|
||||||
}{
|
}{
|
||||||
{true, true},
|
// set the period to be long enough for the file to be changed
|
||||||
{true, false},
|
// and short enough to trigger the event
|
||||||
{false, true},
|
{true, true, 3 * time.Second},
|
||||||
{false, false},
|
|
||||||
|
// 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) {
|
func TestWatchFileChanged(t *testing.T) {
|
||||||
for _, testCase := range testCases {
|
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")
|
hostname := types.NodeName("random-test-hostname")
|
||||||
var testCases = getTestCases(hostname)
|
var testCases = getTestCases(hostname)
|
||||||
|
|
||||||
@ -314,22 +319,23 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if watchDir {
|
if watchDir {
|
||||||
NewSourceFile(dirName, hostname, 100*time.Millisecond, ch)
|
NewSourceFile(dirName, hostname, period, ch)
|
||||||
} else {
|
} 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()
|
// expect an update by SourceFile.resetStoreFromPath()
|
||||||
expectUpdate(t, ch, testCase)
|
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() {
|
changeFile := func() {
|
||||||
// Edit the file content
|
// 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 {
|
if symlink {
|
||||||
file = testCase.writeToFile(linkedDirName, fileName, t)
|
file = testCase.writeToFile(linkedDirName, fileName, t)
|
||||||
return
|
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) {
|
if !apiequality.Semantic.DeepEqual(testCase.expected, update) {
|
||||||
t.Fatalf("%s: Expected: %#v, Got: %#v", testCase.desc, testCase.expected, update)
|
t.Fatalf("%s: Expected: %#v, Got: %#v", testCase.desc, testCase.expected, update)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user