Merge pull request #115576 from silenceshell/fix-fake-os-files-concurrent-map-write

fix concurrent-map-write of FakeOS.Create
This commit is contained in:
Kubernetes Prow Robot 2023-03-14 09:39:26 -07:00 committed by GitHub
commit cb5ad1e044
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,7 @@ package testing
import ( import (
"errors" "errors"
"os" "os"
"sync"
"time" "time"
) )
@ -34,6 +35,7 @@ type FakeOS struct {
HostName string HostName string
Removes []string Removes []string
Files map[string][]*os.FileInfo Files map[string][]*os.FileInfo
FilesLock sync.RWMutex
} }
// Mkdir is a fake call that just returns nil. // 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 // 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 { if f.StatFn != nil {
return f.StatFn(path) 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. // Create is a fake call that creates a virtual file and returns nil.
func (f *FakeOS) Create(path string) (*os.File, error) { func (f *FakeOS) Create(path string) (*os.File, error) {
f.FilesLock.Lock()
defer f.FilesLock.Unlock()
if f.Files == nil { if f.Files == nil {
f.Files = make(map[string][]*os.FileInfo) 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. // 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 return nil
} }
@ -92,12 +96,12 @@ func (f *FakeOS) Hostname() (name string, err error) {
} }
// Chtimes is a fake call that returns nil. // 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 return nil
} }
// Pipe is a fake call that returns 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 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) { func (f *FakeOS) Glob(pattern string) ([]string, error) {
if f.GlobFn != nil { if f.GlobFn != nil {
var res []string var res []string
f.FilesLock.RLock()
defer f.FilesLock.RUnlock()
for k := range f.Files { for k := range f.Files {
if f.GlobFn(pattern, k) { if f.GlobFn(pattern, k) {
res = append(res, 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. // 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 return nil, nil
} }
// OpenFile is a fake call that return 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 return nil, nil
} }
// Rename is a fake call that return 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 return nil
} }