mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #64675 from yue9944882/fix-data-race-cli-file-linux
Automatic merge from submit-queue (batch tested with PRs 61330, 64793, 64675, 65059, 65368). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Fixes data races for pkg/kubelet/config/file_linux_test.go **What this PR does / why we need it**: **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #64655 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
f09a938bcd
@ -131,6 +131,7 @@ func TestWatchFileChanged(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
|
lock *sync.Mutex
|
||||||
desc string
|
desc string
|
||||||
linkedFile string
|
linkedFile string
|
||||||
pod runtime.Object
|
pod runtime.Object
|
||||||
@ -141,6 +142,7 @@ func getTestCases(hostname types.NodeName) []*testCase {
|
|||||||
grace := int64(30)
|
grace := int64(30)
|
||||||
return []*testCase{
|
return []*testCase{
|
||||||
{
|
{
|
||||||
|
lock: &sync.Mutex{},
|
||||||
desc: "Simple pod",
|
desc: "Simple pod",
|
||||||
pod: &v1.Pod{
|
pod: &v1.Pod{
|
||||||
TypeMeta: metav1.TypeMeta{
|
TypeMeta: metav1.TypeMeta{
|
||||||
@ -304,11 +306,10 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var file string
|
var file string
|
||||||
lock := &sync.Mutex{}
|
|
||||||
ch := make(chan interface{})
|
ch := make(chan interface{})
|
||||||
func() {
|
func() {
|
||||||
lock.Lock()
|
testCase.lock.Lock()
|
||||||
defer lock.Unlock()
|
defer testCase.lock.Unlock()
|
||||||
|
|
||||||
if symlink {
|
if symlink {
|
||||||
file = testCase.writeToFile(linkedDirName, fileName, t)
|
file = testCase.writeToFile(linkedDirName, fileName, t)
|
||||||
@ -320,10 +321,6 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
|
|
||||||
if watchDir {
|
if watchDir {
|
||||||
NewSourceFile(dirName, hostname, 100*time.Millisecond, ch)
|
NewSourceFile(dirName, hostname, 100*time.Millisecond, ch)
|
||||||
defer func() {
|
|
||||||
// Remove the file
|
|
||||||
deleteFile(dirName, fileName, ch, t)
|
|
||||||
}()
|
|
||||||
} else {
|
} else {
|
||||||
NewSourceFile(file, hostname, 100*time.Millisecond, ch)
|
NewSourceFile(file, hostname, 100*time.Millisecond, ch)
|
||||||
}
|
}
|
||||||
@ -332,8 +329,8 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
|
|
||||||
changeFile := func() {
|
changeFile := func() {
|
||||||
// Edit the file content
|
// Edit the file content
|
||||||
lock.Lock()
|
testCase.lock.Lock()
|
||||||
defer lock.Unlock()
|
defer testCase.lock.Unlock()
|
||||||
|
|
||||||
pod := testCase.pod.(*v1.Pod)
|
pod := testCase.pod.(*v1.Pod)
|
||||||
pod.Spec.Containers[0].Name = "image2"
|
pod.Spec.Containers[0].Name = "image2"
|
||||||
@ -352,9 +349,7 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
expectUpdate(t, ch, testCase)
|
expectUpdate(t, ch, testCase)
|
||||||
|
|
||||||
if watchDir {
|
if watchDir {
|
||||||
from := fileName
|
go changeFileName(dirName, fileName, fileName+"_ch", t)
|
||||||
fileName = fileName + "_ch"
|
|
||||||
go changeFileName(dirName, from, fileName, t)
|
|
||||||
// expect an update by MOVED_FROM inotify event cause changing file name
|
// expect an update by MOVED_FROM inotify event cause changing file name
|
||||||
expectEmptyUpdate(t, ch)
|
expectEmptyUpdate(t, ch)
|
||||||
// expect an update by MOVED_TO inotify event cause changing file name
|
// expect an update by MOVED_TO inotify event cause changing file name
|
||||||
@ -364,18 +359,6 @@ func watchFileChanged(watchDir bool, symlink bool, t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteFile(dir, file string, ch chan interface{}, t *testing.T) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
expectEmptyUpdate(t, ch)
|
|
||||||
}
|
|
||||||
|
|
||||||
func expectUpdate(t *testing.T, ch chan interface{}, testCase *testCase) {
|
func expectUpdate(t *testing.T, ch chan interface{}, testCase *testCase) {
|
||||||
timer := time.After(5 * time.Second)
|
timer := time.After(5 * time.Second)
|
||||||
for {
|
for {
|
||||||
@ -397,6 +380,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) {
|
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