Add mount/unmount in os interface

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2017-06-07 02:28:51 +00:00
parent e9a930b28b
commit 5398a3b7ec
2 changed files with 69 additions and 2 deletions

View File

@ -21,9 +21,9 @@ import (
"io/ioutil"
"os"
"golang.org/x/net/context"
"github.com/tonistiigi/fifo"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
)
// OS collects system level operations that need to be mocked out
@ -35,6 +35,8 @@ type OS interface {
Stat(name string) (os.FileInfo, error)
CopyFile(src, dest string, perm os.FileMode) error
WriteFile(filename string, data []byte, perm os.FileMode) error
Mount(source string, target string, fstype string, flags uintptr, data string) error
Unmount(target string, flags int) error
}
// RealOS is used to dispatch the real system level operations.
@ -82,3 +84,13 @@ func (RealOS) CopyFile(src, dest string, perm os.FileMode) error {
func (RealOS) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
// Mount will call unix.Mount to mount the file.
func (RealOS) Mount(source string, target string, fstype string, flags uintptr, data string) error {
return unix.Mount(source, target, fstype, flags, data)
}
// Unmount will call unix.Unmount to unmount the file.
func (RealOS) Unmount(target string, flags int) error {
return unix.Unmount(target, flags)
}

View File

@ -26,6 +26,14 @@ import (
osInterface "github.com/kubernetes-incubator/cri-containerd/pkg/os"
)
// CalledDetail is the struct contains called function name and arguments.
type CalledDetail struct {
// Name of the function called.
Name string
// Arguments of the function called.
Arguments []interface{}
}
// FakeOS mocks out certain OS calls to avoid perturbing the filesystem
// If a member of the form `*Fn` is set, that function will be called in place
// of the real call.
@ -37,6 +45,9 @@ type FakeOS struct {
StatFn func(string) (os.FileInfo, error)
CopyFileFn func(string, string, os.FileMode) error
WriteFileFn func(string, []byte, os.FileMode) error
MountFn func(source string, target string, fstype string, flags uintptr, data string) error
UnmountFn func(target string, flags int) error
calls []CalledDetail
errors map[string]error
}
@ -77,6 +88,19 @@ func (f *FakeOS) ClearErrors() {
f.errors = make(map[string]error)
}
func (f *FakeOS) appendCalls(name string, args ...interface{}) {
f.Lock()
defer f.Unlock()
f.calls = append(f.calls, CalledDetail{Name: name, Arguments: args})
}
// GetCalls get detail of calls.
func (f *FakeOS) GetCalls() []CalledDetail {
f.Lock()
defer f.Unlock()
return append([]CalledDetail{}, f.calls...)
}
// NewFakeOS creates a FakeOS.
func NewFakeOS() *FakeOS {
return &FakeOS{
@ -86,6 +110,7 @@ func NewFakeOS() *FakeOS {
// MkdirAll is a fake call that invokes MkdirAllFn or just returns nil.
func (f *FakeOS) MkdirAll(path string, perm os.FileMode) error {
f.appendCalls("MkdirAll", path, perm)
if err := f.getError("MkdirAll"); err != nil {
return err
}
@ -98,6 +123,7 @@ func (f *FakeOS) MkdirAll(path string, perm os.FileMode) error {
// RemoveAll is a fake call that invokes RemoveAllFn or just returns nil.
func (f *FakeOS) RemoveAll(path string) error {
f.appendCalls("RemoveAll", path)
if err := f.getError("RemoveAll"); err != nil {
return err
}
@ -110,6 +136,7 @@ func (f *FakeOS) RemoveAll(path string) error {
// OpenFifo is a fake call that invokes OpenFifoFn or just returns nil.
func (f *FakeOS) OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
f.appendCalls("OpenFifo", ctx, fn, flag, perm)
if err := f.getError("OpenFifo"); err != nil {
return nil, err
}
@ -122,6 +149,7 @@ func (f *FakeOS) OpenFifo(ctx context.Context, fn string, flag int, perm os.File
// Stat is a fake call that invokes StatFn or just return nil.
func (f *FakeOS) Stat(name string) (os.FileInfo, error) {
f.appendCalls("Stat", name)
if err := f.getError("Stat"); err != nil {
return nil, err
}
@ -134,6 +162,7 @@ func (f *FakeOS) Stat(name string) (os.FileInfo, error) {
// CopyFile is a fake call that invokes CopyFileFn or just return nil.
func (f *FakeOS) CopyFile(src, dest string, perm os.FileMode) error {
f.appendCalls("CopyFile", src, dest, perm)
if err := f.getError("CopyFile"); err != nil {
return err
}
@ -146,6 +175,7 @@ func (f *FakeOS) CopyFile(src, dest string, perm os.FileMode) error {
// WriteFile is a fake call that invokes WriteFileFn or just return nil.
func (f *FakeOS) WriteFile(filename string, data []byte, perm os.FileMode) error {
f.appendCalls("WriteFile", filename, data, perm)
if err := f.getError("WriteFile"); err != nil {
return err
}
@ -153,5 +183,30 @@ func (f *FakeOS) WriteFile(filename string, data []byte, perm os.FileMode) error
if f.WriteFileFn != nil {
return f.WriteFileFn(filename, data, perm)
}
}
// Mount is a fake call that invokes MountFn or just return nil.
func (f *FakeOS) Mount(source string, target string, fstype string, flags uintptr, data string) error {
f.appendCalls("Mount", source, target, fstype, flags, data)
if err := f.getError("Mount"); err != nil {
return err
}
if f.MountFn != nil {
return f.MountFn(source, target, fstype, flags, data)
}
return nil
}
// Unmount is a fake call that invokes UnmountFn or just return nil.
func (f *FakeOS) Unmount(target string, flags int) error {
f.appendCalls("Unmount", target, flags)
if err := f.getError("Unmount"); err != nil {
return err
}
if f.UnmountFn != nil {
return f.UnmountFn(target, flags)
}
return nil
}