mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 17:34:21 +00:00
More tests, better errors.
This commit is contained in:
@@ -27,7 +27,7 @@ func (fi fileInfo) Mode() os.FileMode { return 0777 }
|
||||
// ModTime returns a bogus time
|
||||
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
|
||||
|
||||
// IsADir returns if it is a directory
|
||||
// IsDir returns true if it is a directory
|
||||
func (fi fileInfo) IsDir() bool { return fi.dir }
|
||||
|
||||
// Sys should return underlying data source, but it now returns nil
|
||||
|
||||
@@ -11,7 +11,8 @@ import (
|
||||
const (
|
||||
Separator = string(filepath.Separator)
|
||||
doubleSep = Separator + Separator
|
||||
DotDir = "."
|
||||
SelfDir = "."
|
||||
ParentDir = ".."
|
||||
)
|
||||
|
||||
// FileSystem groups basic os filesystem methods.
|
||||
@@ -41,7 +42,8 @@ type FileSystem interface {
|
||||
Glob(pattern string) ([]string, error)
|
||||
// ReadFile returns the contents of the file at the given path.
|
||||
ReadFile(path string) ([]byte, error)
|
||||
// WriteFile writes the data to a file at the given path.
|
||||
// WriteFile writes the data to a file at the given path,
|
||||
// overwriting anything that's already there.
|
||||
WriteFile(path string, data []byte) error
|
||||
// Walk walks the file system with the given WalkFunc.
|
||||
Walk(path string, walkFn filepath.WalkFunc) error
|
||||
|
||||
@@ -120,7 +120,7 @@ func (fs *fsInMemory) 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)
|
||||
return nil, fmt.Errorf("'%s' doesn't exist", name)
|
||||
}
|
||||
|
||||
// WriteFile always succeeds and does nothing.
|
||||
|
||||
@@ -19,3 +19,12 @@ func StripTrailingSeps(s string) string {
|
||||
}
|
||||
return s[:k]
|
||||
}
|
||||
|
||||
// StripLeadingSeps trims leading filepath separators from input.
|
||||
func StripLeadingSeps(s string) string {
|
||||
k := 0
|
||||
for k < len(s) && s[k] == filepath.Separator {
|
||||
k++
|
||||
}
|
||||
return s[k:]
|
||||
}
|
||||
|
||||
@@ -84,9 +84,9 @@ func TestFilePathSplit(t *testing.T) {
|
||||
file: "",
|
||||
},
|
||||
{
|
||||
full: DotDir,
|
||||
full: SelfDir,
|
||||
dir: "",
|
||||
file: DotDir,
|
||||
file: SelfDir,
|
||||
},
|
||||
{
|
||||
full: "rabbit.jpg",
|
||||
@@ -163,3 +163,43 @@ func TestStripTrailingSeps(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripLeadingSeps(t *testing.T) {
|
||||
cases := []struct {
|
||||
full string
|
||||
rem string
|
||||
}{
|
||||
{
|
||||
full: "foo",
|
||||
rem: "foo",
|
||||
},
|
||||
{
|
||||
full: "",
|
||||
rem: "",
|
||||
},
|
||||
{
|
||||
full: "/foo",
|
||||
rem: "foo",
|
||||
},
|
||||
{
|
||||
full: "///foo///bar///",
|
||||
rem: "foo///bar///",
|
||||
},
|
||||
{
|
||||
full: "/////",
|
||||
rem: "",
|
||||
},
|
||||
{
|
||||
full: "/",
|
||||
rem: "",
|
||||
},
|
||||
}
|
||||
for _, p := range cases {
|
||||
dir := StripLeadingSeps(p.full)
|
||||
if dir != p.rem {
|
||||
t.Fatalf(
|
||||
"in '%s', got dir='%s' (expected '%s')",
|
||||
p.full, dir, p.rem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/go-openapi/spec"
|
||||
"github.com/pkg/errors"
|
||||
"k8s.io/kube-openapi/pkg/common"
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
@@ -78,7 +79,7 @@ func makeConfigFromApiMap(m nameToApiMap) (*builtinconfig.TransformerConfig, err
|
||||
// openAPI definition once
|
||||
// "x-kubernetes-group-version-kind" is available in CRD
|
||||
func makeGvkFromTypeName(n string) resid.Gvk {
|
||||
names := strings.Split(n, ".")
|
||||
names := strings.Split(n, filesys.SelfDir)
|
||||
kind := names[len(names)-1]
|
||||
return resid.Gvk{Kind: kind}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ func TestResourceNotFound(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatalf("Didn't get the expected error for an unknown resource")
|
||||
}
|
||||
if !strings.Contains(err.Error(), `cannot read file`) {
|
||||
if !strings.Contains(err.Error(), "'/whatever/deployment.yaml' doesn't exist") {
|
||||
t.Fatalf("unexpected error: %q", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ metadata:
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
|
||||
th := kusttest_test.NewKustTestHarness(t, ".")
|
||||
th := kusttest_test.NewKustTestHarness(t, filesys.SelfDir)
|
||||
th.AssertActualEqualsExpected(m, `
|
||||
apiVersion: v1
|
||||
kind: WorkDir
|
||||
|
||||
@@ -139,7 +139,7 @@ func CurrentWorkingDir() string {
|
||||
if len(pwd) > 0 {
|
||||
return pwd
|
||||
}
|
||||
return "."
|
||||
return filesys.SelfDir
|
||||
}
|
||||
|
||||
func pwdEnv() string {
|
||||
|
||||
@@ -93,20 +93,18 @@ type fileLoader struct {
|
||||
cleaner func() error
|
||||
}
|
||||
|
||||
const CWD = "."
|
||||
|
||||
// NewFileLoaderAtCwd returns a loader that loads from ".".
|
||||
// NewFileLoaderAtCwd returns a loader that loads from PWD.
|
||||
// A convenience for kustomize edit commands.
|
||||
func NewFileLoaderAtCwd(fSys filesys.FileSystem) *fileLoader {
|
||||
return newLoaderOrDie(
|
||||
RestrictionRootOnly, fSys, CWD)
|
||||
RestrictionRootOnly, fSys, filesys.SelfDir)
|
||||
}
|
||||
|
||||
// NewFileLoaderAtRoot returns a loader that loads from "/".
|
||||
// A convenience for tests.
|
||||
func NewFileLoaderAtRoot(fSys filesys.FileSystem) *fileLoader {
|
||||
return newLoaderOrDie(
|
||||
RestrictionRootOnly, fSys, string(filepath.Separator))
|
||||
RestrictionRootOnly, fSys, filesys.Separator)
|
||||
}
|
||||
|
||||
// Root returns the absolute path that is prepended to any
|
||||
|
||||
@@ -131,7 +131,7 @@ func TestLoaderBadRelative(t *testing.T) {
|
||||
}
|
||||
|
||||
// It's not okay to stay at the same place.
|
||||
l2, err = l1.New(".")
|
||||
l2, err = l1.New(filesys.SelfDir)
|
||||
if err == nil {
|
||||
t.Fatalf("expected err, but got root %s", l2.Root())
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func RestrictionRootOnly(
|
||||
return "", err
|
||||
}
|
||||
if f == "" {
|
||||
return "", fmt.Errorf("'%s' must be a file", path)
|
||||
return "", fmt.Errorf("'%s' must resolve to a file", path)
|
||||
}
|
||||
if !d.HasPrefix(root) {
|
||||
return "", fmt.Errorf(
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package loader
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -25,9 +26,11 @@ func TestRestrictionNone(t *testing.T) {
|
||||
|
||||
func TestRestrictionRootOnly(t *testing.T) {
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
root := filesys.ConfirmedDir("/tmp/foo")
|
||||
root := filesys.ConfirmedDir(
|
||||
filesys.Separator + filepath.Join("tmp", "foo"))
|
||||
path := filepath.Join(string(root), "whatever", "beans")
|
||||
|
||||
path := "/tmp/foo/whatever/beans"
|
||||
fSys.Create(path)
|
||||
p, err := RestrictionRootOnly(fSys, root, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -37,18 +40,21 @@ func TestRestrictionRootOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
// Legal.
|
||||
path = "/tmp/foo/whatever/../../foo/whatever"
|
||||
path = filepath.Join(
|
||||
string(root), "whatever", "..", "..", "foo", "whatever", "beans")
|
||||
p, err = RestrictionRootOnly(fSys, root, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path = "/tmp/foo/whatever"
|
||||
path = filepath.Join(
|
||||
string(root), "whatever", "beans")
|
||||
if p != path {
|
||||
t.Fatalf("expected '%s', got '%s'", path, p)
|
||||
}
|
||||
|
||||
// Illegal.
|
||||
path = "/tmp/illegal"
|
||||
// Illegal; file exists but is out of bounds.
|
||||
path = filepath.Join(filesys.Separator+"tmp", "illegal")
|
||||
fSys.Create(path)
|
||||
_, err = RestrictionRootOnly(fSys, root, path)
|
||||
if err == nil {
|
||||
t.Fatal("should have an error")
|
||||
|
||||
@@ -8,13 +8,12 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/loadertest"
|
||||
"sigs.k8s.io/kustomize/api/kv"
|
||||
"sigs.k8s.io/kustomize/api/loader"
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
. "sigs.k8s.io/kustomize/api/resmap"
|
||||
resmaptest_test "sigs.k8s.io/kustomize/api/testutils/resmaptest"
|
||||
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
|
||||
@@ -209,8 +208,11 @@ BAR=baz
|
||||
// files/literal/env etc.
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
if fErr := l.AddFile(tc.filepath, []byte(tc.content)); fErr != nil {
|
||||
t.Fatalf("Error adding fake file: %v\n", fErr)
|
||||
if tc.filepath != "" {
|
||||
if fErr := l.AddFile(
|
||||
tc.filepath, []byte(tc.content)); fErr != nil {
|
||||
t.Fatalf("error adding file '%s': %v\n", tc.filepath, fErr)
|
||||
}
|
||||
}
|
||||
r, err := rmF.NewResMapFromConfigMapArgs(kvLdr, nil, tc.input)
|
||||
if err != nil {
|
||||
@@ -238,7 +240,7 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
|
||||
},
|
||||
}
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
fSys.Mkdir(".")
|
||||
fSys.Mkdir(filesys.SelfDir)
|
||||
|
||||
actual, err := rmF.NewResMapFromSecretArgs(
|
||||
kv.NewLoader(
|
||||
|
||||
Reference in New Issue
Block a user