From a221218681788e3bbae9f3abf2a1653253716054 Mon Sep 17 00:00:00 2001 From: yue9944882 <291271447@qq.com> Date: Mon, 4 Jun 2018 11:58:43 +0800 Subject: [PATCH 1/2] fixes data races --- pkg/kubelet/config/file_linux_test.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/kubelet/config/file_linux_test.go b/pkg/kubelet/config/file_linux_test.go index 70bc2bffae2..e747bef7189 100644 --- a/pkg/kubelet/config/file_linux_test.go +++ b/pkg/kubelet/config/file_linux_test.go @@ -131,6 +131,7 @@ func TestWatchFileChanged(t *testing.T) { } type testCase struct { + lock *sync.Mutex desc string linkedFile string pod runtime.Object @@ -141,6 +142,7 @@ func getTestCases(hostname types.NodeName) []*testCase { grace := int64(30) return []*testCase{ { + lock: &sync.Mutex{}, desc: "Simple pod", pod: &v1.Pod{ TypeMeta: metav1.TypeMeta{ @@ -304,11 +306,10 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { } var file string - lock := &sync.Mutex{} ch := make(chan interface{}) func() { - lock.Lock() - defer lock.Unlock() + testCase.lock.Lock() + defer testCase.lock.Unlock() if symlink { file = testCase.writeToFile(linkedDirName, fileName, t) @@ -332,8 +333,8 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { changeFile := func() { // Edit the file content - lock.Lock() - defer lock.Unlock() + testCase.lock.Lock() + defer testCase.lock.Unlock() pod := testCase.pod.(*v1.Pod) pod.Spec.Containers[0].Name = "image2" @@ -352,9 +353,7 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { expectUpdate(t, ch, testCase) if watchDir { - from := fileName - fileName = fileName + "_ch" - go changeFileName(dirName, from, fileName, t) + go changeFileName(dirName, fileName, fileName+"_ch", t) // expect an update by MOVED_FROM inotify event cause changing file name expectEmptyUpdate(t, ch) // expect an update by MOVED_TO inotify event cause changing file name @@ -365,8 +364,12 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { } func deleteFile(dir, file string, ch chan interface{}, t *testing.T) { + path := filepath.Join(dir, file) + if _, err := os.Stat(path); err != nil { + // The file might absent if it's renamed, deleted.. + return + } go func() { - path := filepath.Join(dir, file) err := os.Remove(path) if err != nil { t.Errorf("unable to remove test file %s: %s", path, err) @@ -397,6 +400,8 @@ 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) } From d467b29c5cbe882c38e255c0f09c741ab8a80de9 Mon Sep 17 00:00:00 2001 From: yue9944882 <291271447@qq.com> Date: Fri, 8 Jun 2018 14:28:19 +0800 Subject: [PATCH 2/2] remove duplicated cleaning up func --- pkg/kubelet/config/file_linux_test.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/pkg/kubelet/config/file_linux_test.go b/pkg/kubelet/config/file_linux_test.go index e747bef7189..b449b8aafa9 100644 --- a/pkg/kubelet/config/file_linux_test.go +++ b/pkg/kubelet/config/file_linux_test.go @@ -321,10 +321,6 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { if watchDir { NewSourceFile(dirName, hostname, 100*time.Millisecond, ch) - defer func() { - // Remove the file - deleteFile(dirName, fileName, ch, t) - }() } else { NewSourceFile(file, hostname, 100*time.Millisecond, ch) } @@ -363,22 +359,6 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) { } } -func deleteFile(dir, file string, ch chan interface{}, t *testing.T) { - path := filepath.Join(dir, file) - if _, err := os.Stat(path); err != nil { - // The file might absent if it's renamed, deleted.. - return - } - go func() { - err := os.Remove(path) - if err != nil { - t.Errorf("unable to remove test file %s: %s", path, err) - } - }() - - expectEmptyUpdate(t, ch) -} - func expectUpdate(t *testing.T, ch chan interface{}, testCase *testCase) { timer := time.After(5 * time.Second) for {