mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 16:06:51 +00:00
rkt: Add tests for GarbageCollect().
This commit is contained in:
@@ -17,7 +17,9 @@ limitations under the License.
|
||||
package container
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// OSInterface collects system level operations that need to be mocked out
|
||||
@@ -26,6 +28,12 @@ type OSInterface interface {
|
||||
Mkdir(path string, perm os.FileMode) error
|
||||
Symlink(oldname string, newname string) error
|
||||
Stat(path string) (os.FileInfo, error)
|
||||
Remove(path string) error
|
||||
Create(path string) (*os.File, error)
|
||||
Hostname() (name string, err error)
|
||||
Chtimes(path string, atime time.Time, mtime time.Time) error
|
||||
Pipe() (r *os.File, w *os.File, err error)
|
||||
ReadDir(dirname string) ([]os.FileInfo, error)
|
||||
}
|
||||
|
||||
// RealOS is used to dispatch the real system level operaitons.
|
||||
@@ -45,3 +53,34 @@ func (RealOS) Symlink(oldname string, newname string) error {
|
||||
func (RealOS) Stat(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
||||
// Remove will call os.Remove to remove the path.
|
||||
func (RealOS) Remove(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
// Create will call os.Create to create and return a file
|
||||
// at path.
|
||||
func (RealOS) Create(path string) (*os.File, error) {
|
||||
return os.Create(path)
|
||||
}
|
||||
|
||||
// Hostname will call os.Hostname to return the hostname.
|
||||
func (RealOS) Hostname() (name string, err error) {
|
||||
return os.Hostname()
|
||||
}
|
||||
|
||||
// Chtimes will call os.Chtimes to change the atime and mtime of the path
|
||||
func (RealOS) Chtimes(path string, atime time.Time, mtime time.Time) error {
|
||||
return os.Chtimes(path, atime, mtime)
|
||||
}
|
||||
|
||||
// Pipe will call os.Pipe to return a connected pair of pipe.
|
||||
func (RealOS) Pipe() (r *os.File, w *os.File, err error) {
|
||||
return os.Pipe()
|
||||
}
|
||||
|
||||
// ReadDir will call ioutil.ReadDir to return the files under the directory.
|
||||
func (RealOS) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||
return ioutil.ReadDir(dirname)
|
||||
}
|
||||
|
||||
@@ -19,14 +19,25 @@ package testing
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FakeOS mocks out certain OS calls to avoid perturbing the filesystem
|
||||
// on the test machine.
|
||||
// If a member of the form `*Fn` is set, that function will be called in place
|
||||
// of the real call.
|
||||
type FakeOS struct {
|
||||
StatFn func(string) (os.FileInfo, error)
|
||||
StatFn func(string) (os.FileInfo, error)
|
||||
ReadDirFn func(string) ([]os.FileInfo, error)
|
||||
HostName string
|
||||
Removes []string
|
||||
Files map[string][]*os.FileInfo
|
||||
}
|
||||
|
||||
func NewFakeOS() *FakeOS {
|
||||
return &FakeOS{
|
||||
Removes: []string{},
|
||||
Files: make(map[string][]*os.FileInfo),
|
||||
}
|
||||
}
|
||||
|
||||
// Mkdir is a fake call that just returns nil.
|
||||
@@ -46,3 +57,37 @@ func (f FakeOS) Stat(path string) (os.FileInfo, error) {
|
||||
}
|
||||
return nil, errors.New("unimplemented testing mock")
|
||||
}
|
||||
|
||||
// Remove is a fake call that returns nil.
|
||||
func (f *FakeOS) Remove(path string) error {
|
||||
f.Removes = append(f.Removes, path)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create is a fake call that returns nil.
|
||||
func (FakeOS) Create(path string) (*os.File, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Hostname is a fake call that returns nil.
|
||||
func (f *FakeOS) Hostname() (name string, err error) {
|
||||
return f.HostName, nil
|
||||
}
|
||||
|
||||
// Chtimes is a fake call that returns nil.
|
||||
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) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
// ReadDir is a fake call that returns the files under the directory.
|
||||
func (f *FakeOS) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||
if f.ReadDirFn != nil {
|
||||
return f.ReadDirFn(dirname)
|
||||
}
|
||||
return nil, errors.New("unimplemented testing mock")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user