e2e storage: add Rename to PodIO

This is useful for atomic file creation: first create a temporary file, then
rename it.
This commit is contained in:
Patrick Ohly 2022-09-30 10:20:51 +02:00
parent 462f41212a
commit 5b63199f46
2 changed files with 15 additions and 0 deletions

View File

@ -71,6 +71,9 @@ type DirIO interface {
Mkdir(path string) error Mkdir(path string) error
// RemoveAll removes the path and everything contained inside it. It's not an error if the path does not exist. // RemoveAll removes the path and everything contained inside it. It's not an error if the path does not exist.
RemoveAll(path string) error RemoveAll(path string) error
// Rename changes the name of a file or directory. The parent directory
// of newPath must exist.
Rename(oldPath, newPath string) error
} }
type OSDirIO struct{} type OSDirIO struct{}
@ -97,6 +100,10 @@ func (o OSDirIO) RemoveAll(path string) error {
return os.RemoveAll(path) return os.RemoveAll(path)
} }
func (o OSDirIO) Rename(oldPath, newPath string) error {
return os.Rename(oldPath, newPath)
}
// Service is the CSI Mock service provider. // Service is the CSI Mock service provider.
type Service interface { type Service interface {
csi.ControllerServer csi.ControllerServer

View File

@ -70,6 +70,14 @@ func (p PodDirIO) CreateFile(path string, content io.Reader) error {
return nil return nil
} }
func (p PodDirIO) Rename(oldPath, newPath string) error {
_, stderr, err := p.execute([]string{"mv", oldPath, newPath}, nil)
if err != nil {
return fmt.Errorf("rename %q -> %q: stderr=%q, %v", oldPath, newPath, stderr, err)
}
return nil
}
func (p PodDirIO) RemoveAll(path string) error { func (p PodDirIO) RemoveAll(path string) error {
_, stderr, err := p.execute([]string{"rm", "-rf", path}, nil) _, stderr, err := p.execute([]string{"rm", "-rf", path}, nil)
if err != nil { if err != nil {