Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Oct 28, 2018
1 parent 383b3e7 commit 885c195
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 89 deletions.
8 changes: 4 additions & 4 deletions k8sdeps/configmapandsecret/configmapfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestConstructConfigMap(t *testing.T) {
input: types.ConfigMapArgs{
Name: "envConfigMap",
DataSources: types.DataSources{
EnvSource: "configmap/app.env",
EnvSource: "/configmap/app.env",
},
},
options: nil,
Expand All @@ -105,7 +105,7 @@ func TestConstructConfigMap(t *testing.T) {
input: types.ConfigMapArgs{
Name: "fileConfigMap",
DataSources: types.DataSources{
FileSources: []string{"configmap/app-init.ini"},
FileSources: []string{"/configmap/app-init.ini"},
},
},
options: nil,
Expand All @@ -129,8 +129,8 @@ func TestConstructConfigMap(t *testing.T) {
}

fSys := fs.MakeFakeFS()
fSys.WriteFile("configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
fSys.WriteFile("configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
f := NewConfigMapFactory(fSys, loader.NewFileLoader(fSys))
for _, tc := range testCases {
cm, err := f.MakeConfigMap(&tc.input, tc.options)
Expand Down
55 changes: 33 additions & 22 deletions pkg/fs/fakefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import (
"path/filepath"
"sigs.k8s.io/kustomize/pkg/constants"
"sort"
"strings"
)

var _ FileSystem = &FakeFS{}
var _ FileSystem = &fakeFs{}

// FakeFS implements FileSystem using a fake in-memory filesystem.
type FakeFS struct {
// fakeFs implements FileSystem using a fake in-memory filesystem.
type fakeFs struct {
m map[string]*FakeFile
}

// MakeFakeFS returns an instance of FakeFS with no files in it.
func MakeFakeFS() *FakeFS {
return &FakeFS{m: map[string]*FakeFile{}}
// MakeFakeFS returns an instance of fakeFs with no files in it.
func MakeFakeFS() *fakeFs {
result := &fakeFs{m: map[string]*FakeFile{}}
result.Mkdir("/")
return result
}

// kustomizationContent is used in tests.
Expand All @@ -54,40 +57,40 @@ secretGenerator: []
`

// Create assures a fake file appears in the in-memory file system.
func (fs *FakeFS) Create(name string) (File, error) {
func (fs *fakeFs) Create(name string) (File, error) {
f := &FakeFile{}
f.open = true
fs.m[name] = f
return fs.m[name], nil
}

// Mkdir assures a fake directory appears in the in-memory file system.
func (fs *FakeFS) Mkdir(name string) error {
func (fs *fakeFs) Mkdir(name string) error {
fs.m[name] = makeDir(name)
return nil
}

// MkdirAll delegates to Mkdir
func (fs *FakeFS) MkdirAll(name string) error {
func (fs *fakeFs) MkdirAll(name string) error {
return fs.Mkdir(name)
}

// Open returns a fake file in the open state.
func (fs *FakeFS) Open(name string) (File, error) {
func (fs *fakeFs) Open(name string) (File, error) {
if _, found := fs.m[name]; !found {
return nil, fmt.Errorf("file %q cannot be opened", name)
}
return fs.m[name], nil
}

// Exists returns true if file is known.
func (fs *FakeFS) Exists(name string) bool {
func (fs *fakeFs) Exists(name string) bool {
_, found := fs.m[name]
return found
}

// Glob returns the list of matching files
func (fs *FakeFS) Glob(pattern string) ([]string, error) {
func (fs *fakeFs) Glob(pattern string) ([]string, error) {
var result []string
for p := range fs.m {
if fs.pathMatch(p, pattern) {
Expand All @@ -99,45 +102,53 @@ func (fs *FakeFS) Glob(pattern string) ([]string, error) {
}

// IsDir returns true if the file exists and is a directory.
func (fs *FakeFS) IsDir(name string) bool {
func (fs *fakeFs) IsDir(name string) bool {
f, found := fs.m[name]
if !found {
return false
if found && f.dir {
return true
}
return f.dir
if !strings.HasSuffix(name, "/") {
name = name + "/"
}
for k := range fs.m {
if strings.HasPrefix(k, name) {
return true
}
}
return false
}

// ReadFile always returns an empty bytes and error depending on content of m.
func (fs *FakeFS) ReadFile(name string) ([]byte, error) {
func (fs *fakeFs) ReadFile(name string) ([]byte, error) {
if ff, found := fs.m[name]; found {
return ff.content, nil
}
return nil, fmt.Errorf("cannot read file %q", name)
}

func (fs *FakeFS) ReadTestKustomization() ([]byte, error) {
func (fs *fakeFs) ReadTestKustomization() ([]byte, error) {
return fs.ReadFile(constants.KustomizationFileName)
}

// WriteFile always succeeds and does nothing.
func (fs *FakeFS) WriteFile(name string, c []byte) error {
func (fs *fakeFs) WriteFile(name string, c []byte) error {
ff := &FakeFile{}
ff.Write(c)
fs.m[name] = ff
return nil
}

// WriteTestKustomization writes a standard test file.
func (fs *FakeFS) WriteTestKustomization() {
func (fs *fakeFs) WriteTestKustomization() {
fs.WriteTestKustomizationWith([]byte(kustomizationContent))
}

// WriteTestKustomizationWith writes a standard test file.
func (fs *FakeFS) WriteTestKustomizationWith(bytes []byte) {
func (fs *fakeFs) WriteTestKustomizationWith(bytes []byte) {
fs.WriteFile(constants.KustomizationFileName, bytes)
}

func (fs *FakeFS) pathMatch(path, pattern string) bool {
func (fs *fakeFs) pathMatch(path, pattern string) bool {
match, _ := filepath.Match(pattern, path)
return match
}
25 changes: 25 additions & 0 deletions pkg/fs/fakefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestExists(t *testing.T) {
if x.Exists("foo") {
t.Fatalf("expected no foo")
}
x.Mkdir("/")
if !x.IsDir("/") {
t.Fatalf("expected dir at /")
}
}

func TestIsDir(t *testing.T) {
Expand All @@ -44,6 +48,27 @@ func TestIsDir(t *testing.T) {
}
}

func TestIsDirDeeper(t *testing.T) {
x := MakeFakeFS()
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
x.WriteFile("/foo/project/subdir/file.yaml", []byte("Unused"))
if !x.IsDir("/") {
t.Fatalf("/ should be a dir")
}
if !x.IsDir("/foo") {
t.Fatalf("/foo should be a dir")
}
if !x.IsDir("/foo/project") {
t.Fatalf("/foo/project should be a dir")
}
if x.IsDir("/fo") {
t.Fatalf("/fo should not be a dir")
}
if x.IsDir("/x") {
t.Fatalf("/x should not be a dir")
}
}

func TestCreate(t *testing.T) {
x := MakeFakeFS()
f, err := x.Create("foo")
Expand Down
Loading

0 comments on commit 885c195

Please sign in to comment.