From 5398a3b7ec321ac7e0a2115f86a200c87ac331e4 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Wed, 7 Jun 2017 02:28:51 +0000 Subject: [PATCH] Add mount/unmount in os interface Signed-off-by: Lantao Liu --- pkg/os/os.go | 16 ++++++++++-- pkg/os/testing/fake_os.go | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/pkg/os/os.go b/pkg/os/os.go index 1caed5eeb..bc279d9b6 100644 --- a/pkg/os/os.go +++ b/pkg/os/os.go @@ -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) +} diff --git a/pkg/os/testing/fake_os.go b/pkg/os/testing/fake_os.go index 10709915a..84d8cef20 100644 --- a/pkg/os/testing/fake_os.go +++ b/pkg/os/testing/fake_os.go @@ -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 }