diff --git a/pkg/kubelet/container/testing/os.go b/pkg/kubelet/container/testing/os.go index 6dc59726864..cf84b257689 100644 --- a/pkg/kubelet/container/testing/os.go +++ b/pkg/kubelet/container/testing/os.go @@ -19,6 +19,7 @@ package testing import ( "errors" "os" + "sync" "time" ) @@ -34,6 +35,7 @@ type FakeOS struct { HostName string Removes []string Files map[string][]*os.FileInfo + FilesLock sync.RWMutex } // Mkdir is a fake call that just returns nil. @@ -53,7 +55,7 @@ func (f *FakeOS) Symlink(oldname string, newname string) error { } // Stat is a fake that returns an error -func (f FakeOS) Stat(path string) (os.FileInfo, error) { +func (f *FakeOS) Stat(path string) (os.FileInfo, error) { if f.StatFn != nil { return f.StatFn(path) } @@ -74,6 +76,8 @@ func (f *FakeOS) RemoveAll(path string) error { // Create is a fake call that creates a virtual file and returns nil. func (f *FakeOS) Create(path string) (*os.File, error) { + f.FilesLock.Lock() + defer f.FilesLock.Unlock() if f.Files == nil { f.Files = make(map[string][]*os.FileInfo) } @@ -82,7 +86,7 @@ func (f *FakeOS) Create(path string) (*os.File, error) { } // Chmod is a fake call that returns nil. -func (FakeOS) Chmod(path string, perm os.FileMode) error { +func (*FakeOS) Chmod(path string, perm os.FileMode) error { return nil } @@ -92,12 +96,12 @@ func (f *FakeOS) Hostname() (name string, err error) { } // Chtimes is a fake call that returns nil. -func (FakeOS) Chtimes(path string, atime time.Time, mtime time.Time) error { +func (*FakeOS) Chtimes(path string, atime time.Time, mtime time.Time) error { return nil } // Pipe is a fake call that returns nil. -func (FakeOS) Pipe() (r *os.File, w *os.File, err error) { +func (*FakeOS) Pipe() (r *os.File, w *os.File, err error) { return nil, nil, nil } @@ -113,6 +117,8 @@ func (f *FakeOS) ReadDir(dirname string) ([]os.DirEntry, error) { func (f *FakeOS) Glob(pattern string) ([]string, error) { if f.GlobFn != nil { var res []string + f.FilesLock.RLock() + defer f.FilesLock.RUnlock() for k := range f.Files { if f.GlobFn(pattern, k) { res = append(res, k) @@ -124,16 +130,16 @@ func (f *FakeOS) Glob(pattern string) ([]string, error) { } // Open is a fake call that returns nil. -func (FakeOS) Open(name string) (*os.File, error) { +func (*FakeOS) Open(name string) (*os.File, error) { return nil, nil } // OpenFile is a fake call that return nil. -func (FakeOS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { +func (*FakeOS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { return nil, nil } // Rename is a fake call that return nil. -func (FakeOS) Rename(oldpath, newpath string) error { +func (*FakeOS) Rename(oldpath, newpath string) error { return nil }