mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 09:02:53 +00:00
Tweak file path handling and logging in test.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/types"
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||||
)
|
)
|
||||||
@@ -28,13 +29,13 @@ func (f *Factory) MakeConfigMap(
|
|||||||
args *types.ConfigMapArgs) (*v1.ConfigMap, error) {
|
args *types.ConfigMapArgs) (*v1.ConfigMap, error) {
|
||||||
all, err := f.ldr.LoadKvPairs(args.GeneratorArgs)
|
all, err := f.ldr.LoadKvPairs(args.GeneratorArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(err, "loading KV pairs")
|
||||||
}
|
}
|
||||||
cm := makeFreshConfigMap(args)
|
cm := makeFreshConfigMap(args)
|
||||||
for _, p := range all {
|
for _, p := range all {
|
||||||
err = f.addKvToConfigMap(cm, p)
|
err = f.addKvToConfigMap(cm, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(err, "trouble mapping")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.options != nil {
|
if f.options != nil {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package configmapandsecret
|
package configmapandsecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -86,7 +87,9 @@ func TestConstructConfigMap(t *testing.T) {
|
|||||||
GeneratorArgs: types.GeneratorArgs{
|
GeneratorArgs: types.GeneratorArgs{
|
||||||
Name: "envConfigMap",
|
Name: "envConfigMap",
|
||||||
DataSources: types.DataSources{
|
DataSources: types.DataSources{
|
||||||
EnvSources: []string{"configmap/app.env"},
|
EnvSources: []string{
|
||||||
|
filepath.Join("configmap", "app.env"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -99,7 +102,10 @@ func TestConstructConfigMap(t *testing.T) {
|
|||||||
GeneratorArgs: types.GeneratorArgs{
|
GeneratorArgs: types.GeneratorArgs{
|
||||||
Name: "fileConfigMap",
|
Name: "fileConfigMap",
|
||||||
DataSources: types.DataSources{
|
DataSources: types.DataSources{
|
||||||
FileSources: []string{"configmap/app-init.ini", "configmap/app.bin"},
|
FileSources: []string{
|
||||||
|
filepath.Join("configmap", "app-init.ini"),
|
||||||
|
filepath.Join("configmap", "app.bin"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -126,9 +132,15 @@ func TestConstructConfigMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fSys := fs.MakeFakeFS()
|
fSys := fs.MakeFakeFS()
|
||||||
fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
|
fSys.WriteFile(
|
||||||
fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
|
fSys.RPath("configmap", "app.env"),
|
||||||
fSys.WriteFile("/configmap/app.bin", []byte{0xff, 0xfd})
|
[]byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
|
||||||
|
fSys.WriteFile(
|
||||||
|
fSys.RPath("configmap", "app-init.ini"),
|
||||||
|
[]byte("FOO=bar\nBAR=baz\n"))
|
||||||
|
fSys.WriteFile(
|
||||||
|
fSys.RPath("configmap", "app.bin"),
|
||||||
|
[]byte{0xff, 0xfd})
|
||||||
ldr := loader.NewFileLoaderAtRoot(validators.MakeFakeValidator(), fSys)
|
ldr := loader.NewFileLoaderAtRoot(validators.MakeFakeValidator(), fSys)
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
f := NewFactory(ldr, tc.options)
|
f := NewFactory(ldr, tc.options)
|
||||||
|
|||||||
@@ -36,12 +36,15 @@ type fakeFs struct {
|
|||||||
// MakeFakeFS returns an instance of fakeFs with no files in it.
|
// MakeFakeFS returns an instance of fakeFs with no files in it.
|
||||||
func MakeFakeFS() *fakeFs {
|
func MakeFakeFS() *fakeFs {
|
||||||
result := &fakeFs{m: map[string]*FakeFile{}}
|
result := &fakeFs{m: map[string]*FakeFile{}}
|
||||||
result.Mkdir("/")
|
result.Mkdir(separator)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// kustomizationContent is used in tests.
|
const (
|
||||||
const kustomizationContent = `apiVersion: kustomize.config.k8s.io/v1beta1
|
separator = string(filepath.Separator)
|
||||||
|
doubleSep = separator + separator
|
||||||
|
// kustomizationContent is used in tests.
|
||||||
|
kustomizationContent = `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
namePrefix: some-prefix
|
namePrefix: some-prefix
|
||||||
nameSuffix: some-suffix
|
nameSuffix: some-suffix
|
||||||
@@ -60,6 +63,7 @@ configMapGenerator: []
|
|||||||
# There could be secrets in Base, if just using a fork/rebase workflow
|
# There could be secrets in Base, if just using a fork/rebase workflow
|
||||||
secretGenerator: []
|
secretGenerator: []
|
||||||
`
|
`
|
||||||
|
)
|
||||||
|
|
||||||
// Create assures a fake file appears in the in-memory file system.
|
// 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) {
|
||||||
@@ -69,6 +73,11 @@ func (fs *fakeFs) Create(name string) (File, error) {
|
|||||||
return fs.m[name], nil
|
return fs.m[name], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RPath returns a rooted path, e.g. "/hey/foo" as opposed to "hey/foo".
|
||||||
|
func (fs *fakeFs) RPath(elem ...string) string {
|
||||||
|
return separator + filepath.Join(elem...)
|
||||||
|
}
|
||||||
|
|
||||||
// Mkdir assures a fake directory appears in the in-memory file system.
|
// 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)
|
fs.m[name] = makeDir(name)
|
||||||
@@ -139,8 +148,8 @@ func (fs *fakeFs) IsDir(name string) bool {
|
|||||||
if found && f.dir {
|
if found && f.dir {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if !strings.HasSuffix(name, "/") {
|
if !strings.HasSuffix(name, separator) {
|
||||||
name = name + "/"
|
name = name + separator
|
||||||
}
|
}
|
||||||
for k := range fs.m {
|
for k := range fs.m {
|
||||||
if strings.HasPrefix(k, name) {
|
if strings.HasPrefix(k, name) {
|
||||||
@@ -210,7 +219,8 @@ func (fs *fakeFs) lstat(path string) (*Fakefileinfo, error) {
|
|||||||
func (fs *fakeFs) join(elem ...string) string {
|
func (fs *fakeFs) join(elem ...string) string {
|
||||||
for i, e := range elem {
|
for i, e := range elem {
|
||||||
if e != "" {
|
if e != "" {
|
||||||
return strings.Replace(strings.Join(elem[i:], "/"), "//", "/", -1)
|
return strings.Replace(
|
||||||
|
strings.Join(elem[i:], separator), doubleSep, separator, -1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
@@ -218,15 +228,15 @@ func (fs *fakeFs) join(elem ...string) string {
|
|||||||
|
|
||||||
func (fs *fakeFs) readDirNames(path string) []string {
|
func (fs *fakeFs) readDirNames(path string) []string {
|
||||||
var names []string
|
var names []string
|
||||||
if !strings.HasSuffix(path, "/") {
|
if !strings.HasSuffix(path, separator) {
|
||||||
path += "/"
|
path += separator
|
||||||
}
|
}
|
||||||
pathSegments := strings.Count(path, "/")
|
pathSegments := strings.Count(path, separator)
|
||||||
for name := range fs.m {
|
for name := range fs.m {
|
||||||
if name == path {
|
if name == path {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.Count(name, "/") > pathSegments {
|
if strings.Count(name, separator) > pathSegments {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(name, path) {
|
if strings.HasPrefix(name, path) {
|
||||||
|
|||||||
Reference in New Issue
Block a user