Skip to content

Commit

Permalink
Merge pull request #573 from monopole/enforceRelocatability
Browse files Browse the repository at this point in the history
Enforce relocatable kustomizations.
  • Loading branch information
monopole authored Nov 22, 2018
2 parents 0042c4b + 4f9d00c commit c51646e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 36 deletions.
4 changes: 2 additions & 2 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 Down
5 changes: 4 additions & 1 deletion pkg/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"sigs.k8s.io/kustomize/pkg/ifc"
)

const enforceRelocatable = false
// TODO: 2018/Nov/20 remove this before next release.
// Leave only the true path. Retaining only for
// quick revert.
const enforceRelocatable = true

// fileLoader loads files, returning an array of bytes.
// It also enforces two kustomization requirements:
Expand Down
20 changes: 10 additions & 10 deletions pkg/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ type testData struct {

var testCases = []testData{
{
path: "/foo/project/fileA.yaml",
path: "foo/project/fileA.yaml",
expectedContent: "fileA content",
},
{
path: "/foo/project/subdir1/fileB.yaml",
path: "foo/project/subdir1/fileB.yaml",
expectedContent: "fileB content",
},
{
path: "/foo/project/subdir2/fileC.yaml",
path: "foo/project/subdir2/fileC.yaml",
expectedContent: "fileC content",
},
{
path: "/foo/project2/fileD.yaml",
path: "foo/project/fileD.yaml",
expectedContent: "fileD content",
},
}

func MakeFakeFs(td []testData) fs.FileSystem {
fSys := fs.MakeFakeFS()
for _, x := range td {
fSys.WriteFile(x.path, []byte(x.expectedContent))
fSys.WriteFile("/"+x.path, []byte(x.expectedContent))
}
return fSys
}
Expand All @@ -70,23 +70,23 @@ func TestLoaderLoad(t *testing.T) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}
l2, err := l1.New("/foo/project")
l2, err := l1.New("foo/project")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
if "/foo/project" != l2.Root() {
t.Fatalf("incorrect root: %s\n", l2.Root())
}
for _, x := range testCases {
b, err := l2.Load(strings.TrimPrefix(x.path, "/foo/project/"))
b, err := l2.Load(strings.TrimPrefix(x.path, "foo/project/"))
if err != nil {
t.Fatalf("unexpected load error %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}
l2, err = l1.New("/foo/project/") // Assure trailing slash stripped
l2, err = l1.New("foo/project/") // Assure trailing slash stripped
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
Expand All @@ -96,7 +96,7 @@ func TestLoaderLoad(t *testing.T) {
}

func TestLoaderNewSubDir(t *testing.T) {
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("/foo/project")
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("foo/project")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
Expand All @@ -118,7 +118,7 @@ func TestLoaderNewSubDir(t *testing.T) {
}

func TestLoaderBadRelative(t *testing.T) {
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("/foo/project/subdir1")
l1, err := NewFileLoaderAtRoot(MakeFakeFs(testCases)).New("foo/project/subdir1")
if err != nil {
t.Fatalf("unexpected err: %v\n", err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/patch/transformer/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestNewPatchJson6902FactoryJSON(t *testing.T) {
target:
kind: Deployment
name: some-name
path: /testpath/patch.json
path: patch.json
`)
p := patch.Json6902{}
err = yaml.Unmarshal(jsonPatch, &p)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestNewPatchJson6902FactoryYAML(t *testing.T) {
target:
name: some-name
kind: Deployment
path: /testpath/patch.yaml
path: patch.yaml
`)
p := patch.Json6902{}
err = yaml.Unmarshal(jsonPatch, &p)
Expand Down Expand Up @@ -162,12 +162,12 @@ func TestNewPatchJson6902FactoryMulti(t *testing.T) {
- target:
kind: foo
name: some-name
path: /testpath/patch.json
path: patch.json
- target:
kind: foo
name: some-name
path: /testpath/patch.yaml
path: patch.yaml
`)
var p []patch.Json6902
err = yaml.Unmarshal(jsonPatches, &p)
Expand Down Expand Up @@ -276,12 +276,12 @@ func TestNewPatchJson6902FactoryMultiConflict(t *testing.T) {
- target:
kind: foo
name: some-name
path: /testpath/patch.json
path: patch.json
- target:
kind: foo
name: some-name
path: /testpath/patch.yaml
path: patch.yaml
`)
var p []patch.Json6902
err = yaml.Unmarshal(jsonPatches, &p)
Expand Down
12 changes: 6 additions & 6 deletions pkg/resmap/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ metadata:
---
`

l := loadertest.NewFakeLoader("/home/seans/project")
if ferr := l.AddFile("/home/seans/project/deployment.yaml", []byte(resourceStr)); ferr != nil {
l := loadertest.NewFakeLoader("/whatever/project")
if ferr := l.AddFile("/whatever/project/deployment.yaml", []byte(resourceStr)); ferr != nil {
t.Fatalf("Error adding fake file: %v\n", ferr)
}
expected := ResMap{resid.NewResId(deploy, "dply1"): rf.FromMap(
Expand Down Expand Up @@ -85,7 +85,7 @@ metadata:
}

m, _ := rmF.FromFiles(
l, []string{"/home/seans/project/deployment.yaml"})
l, []string{"deployment.yaml"})
if len(m) != 3 {
t.Fatalf("%#v should contain 3 appResource, but got %d", m, len(m))
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestNewFromConfigMaps(t *testing.T) {
expected ResMap
}

l := loadertest.NewFakeLoader("/home/seans/project/")
l := loadertest.NewFakeLoader("/whatever/project/")
testCases := []testCase{
{
description: "construct config map from env",
Expand All @@ -157,7 +157,7 @@ func TestNewFromConfigMaps(t *testing.T) {
},
},
},
filepath: "/home/seans/project/app.env",
filepath: "/whatever/project/app.env",
content: "DB_USERNAME=admin\nDB_PASSWORD=somepw",
expected: ResMap{
resid.NewResId(cmap, "envConfigMap"): rf.FromMap(
Expand All @@ -183,7 +183,7 @@ func TestNewFromConfigMaps(t *testing.T) {
},
},
},
filepath: "/home/seans/project/app-init.ini",
filepath: "/whatever/project/app-init.ini",
content: "FOO=bar\nBAR=baz\n",
expected: ResMap{
resid.NewResId(cmap, "fileConfigMap"): rf.FromMap(
Expand Down
14 changes: 7 additions & 7 deletions pkg/resource/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (

func TestSliceFromPatches(t *testing.T) {

patchGood1 := patch.StrategicMerge("/foo/patch1.yaml")
patchGood1 := patch.StrategicMerge("patch1.yaml")
patch1 := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: pooh
`
patchGood2 := patch.StrategicMerge("/foo/patch2.yaml")
patchGood2 := patch.StrategicMerge("patch2.yaml")
patch2 := `
apiVersion: v1
kind: ConfigMap
Expand All @@ -45,14 +45,14 @@ metadata:
---
---
`
patchBad := patch.StrategicMerge("/foo/patch3.yaml")
patchBad := patch.StrategicMerge("patch3.yaml")
patch3 := `
WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT: woot
`
l := loadertest.NewFakeLoader("/foo")
l.AddFile(string(patchGood1), []byte(patch1))
l.AddFile(string(patchGood2), []byte(patch2))
l.AddFile(string(patchBad), []byte(patch3))
l := loadertest.NewFakeLoader("/")
l.AddFile("/"+string(patchGood1), []byte(patch1))
l.AddFile("/"+string(patchGood2), []byte(patch2))
l.AddFile("/"+string(patchBad), []byte(patch3))

tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion pkg/transformers/config/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namePrefix:

func TestMakeTransformerConfigFromFiles(t *testing.T) {
ldr, expected, _ := makeFakeLoaderAndOutput()
tcfg, err := NewFactory(ldr).FromFiles([]string{"/transformerconfig/test/config.yaml"})
tcfg, err := NewFactory(ldr).FromFiles([]string{"transformerconfig/test/config.yaml"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/transformers/config/factorycrd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ func TestLoadCRDs(t *testing.T) {
NameReference: nbrs,
}

actualTc, _ := NewFactory(makeLoader(t)).LoadCRDs(
[]string{"/testpath/crd.json"})

actualTc, err := NewFactory(makeLoader(t)).LoadCRDs(
[]string{"crd.json"})
if err != nil {
t.Fatalf("unexpected error:%v", err)
}
if !reflect.DeepEqual(actualTc, expectedTc) {
t.Fatalf("expected\n %v\n but got\n %v\n", expectedTc, actualTc)
}
Expand Down

0 comments on commit c51646e

Please sign in to comment.