More tests.

This commit is contained in:
jregan
2019-11-29 08:31:51 -08:00
parent 680a0812c6
commit a49a764705
5 changed files with 46 additions and 67 deletions

View File

@@ -0,0 +1,306 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"testing"
)
func makeBaseReferencingCustomConfig(th testingHarness) {
th.WriteK("/app/base", `
namePrefix: x-
commonLabels:
app: myApp
vars:
- name: APRIL_DIET
objref:
kind: Giraffe
name: april
fieldref:
fieldpath: spec.diet
- name: KOKO_DIET
objref:
kind: Gorilla
name: koko
fieldref:
fieldpath: spec.diet
resources:
- animalPark.yaml
- giraffes.yaml
- gorilla.yaml
configurations:
- config/defaults.yaml
- config/custom.yaml
`)
th.WriteF("/app/base/giraffes.yaml", `
kind: Giraffe
metadata:
name: april
spec:
diet: mimosa
location: NE
---
kind: Giraffe
metadata:
name: may
spec:
diet: acacia
location: SE
`)
th.WriteF("/app/base/gorilla.yaml", `
kind: Gorilla
metadata:
name: koko
spec:
diet: bambooshoots
location: SW
`)
th.WriteF("/app/base/animalPark.yaml", `
kind: AnimalPark
metadata:
name: sandiego
spec:
gorillaRef:
name: koko
giraffeRef:
name: april
food:
- "$(APRIL_DIET)"
- "$(KOKO_DIET)"
`)
}
func TestCustomConfig(t *testing.T) {
th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th)
th.WriteLegacyConfigs("/app/base/config/defaults.yaml")
th.WriteF("/app/base/config/custom.yaml", `
nameReference:
- kind: Gorilla
fieldSpecs:
- kind: AnimalPark
path: spec/gorillaRef/name
- kind: Giraffe
fieldSpecs:
- kind: AnimalPark
path: spec/giraffeRef/name
varReference:
- path: spec/food
kind: AnimalPark
`)
m := th.Run("/app/base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
kind: AnimalPark
metadata:
labels:
app: myApp
name: x-sandiego
spec:
food:
- mimosa
- bambooshoots
giraffeRef:
name: x-april
gorillaRef:
name: x-koko
---
kind: Giraffe
metadata:
labels:
app: myApp
name: x-april
spec:
diet: mimosa
location: NE
---
kind: Giraffe
metadata:
labels:
app: myApp
name: x-may
spec:
diet: acacia
location: SE
---
kind: Gorilla
metadata:
labels:
app: myApp
name: x-koko
spec:
diet: bambooshoots
location: SW
`)
}
func TestCustomConfigWithDefaultOverspecification(t *testing.T) {
th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th)
th.WriteLegacyConfigs("/app/base/config/defaults.yaml")
// Specifying namePrefix here conflicts with (is the same as)
// the defaults written above. This is intentional in the
// test to assure duplicate config doesn't cause problems.
th.WriteF("/app/base/config/custom.yaml", `
namePrefix:
- path: metadata/name
nameReference:
- kind: Gorilla
fieldSpecs:
- kind: AnimalPark
path: spec/gorillaRef/name
- kind: Giraffe
fieldSpecs:
- kind: AnimalPark
path: spec/giraffeRef/name
varReference:
- path: spec/food
kind: AnimalPark
`)
m := th.Run("/app/base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
kind: AnimalPark
metadata:
labels:
app: myApp
name: x-sandiego
spec:
food:
- mimosa
- bambooshoots
giraffeRef:
name: x-april
gorillaRef:
name: x-koko
---
kind: Giraffe
metadata:
labels:
app: myApp
name: x-april
spec:
diet: mimosa
location: NE
---
kind: Giraffe
metadata:
labels:
app: myApp
name: x-may
spec:
diet: acacia
location: SE
---
kind: Gorilla
metadata:
labels:
app: myApp
name: x-koko
spec:
diet: bambooshoots
location: SW
`)
}
func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) {
th := makeTestHarness(t)
makeBaseReferencingCustomConfig(th)
th.WriteLegacyConfigs("/app/base/config/defaults.yaml")
th.WriteF("/app/base/config/custom.yaml", `
nameReference:
- kind: Gorilla
fieldSpecs:
- kind: AnimalPark
path: spec/gorillaRef/name
- kind: Giraffe
fieldSpecs:
- kind: AnimalPark
path: spec/giraffeRef/name
varReference:
- path: spec/food
kind: AnimalPark
`)
th.WriteK("/app/overlay", `
namePrefix: o-
commonLabels:
movie: planetOfTheApes
patchesStrategicMerge:
- animalPark.yaml
resources:
- ../base
- ursus.yaml
`)
th.WriteF("/app/overlay/ursus.yaml", `
kind: Gorilla
metadata:
name: ursus
spec:
diet: heston
location: Arizona
`)
// The following replaces the gorillaRef in the AnimalPark.
th.WriteF("/app/overlay/animalPark.yaml", `
kind: AnimalPark
metadata:
name: sandiego
spec:
gorillaRef:
name: ursus
`)
m := th.Run("/app/overlay", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
kind: AnimalPark
metadata:
labels:
app: myApp
movie: planetOfTheApes
name: o-x-sandiego
spec:
food:
- mimosa
- bambooshoots
giraffeRef:
name: o-x-april
gorillaRef:
name: o-ursus
---
kind: Giraffe
metadata:
labels:
app: myApp
movie: planetOfTheApes
name: o-x-april
spec:
diet: mimosa
location: NE
---
kind: Giraffe
metadata:
labels:
app: myApp
movie: planetOfTheApes
name: o-x-may
spec:
diet: acacia
location: SE
---
kind: Gorilla
metadata:
labels:
app: myApp
movie: planetOfTheApes
name: o-x-koko
spec:
diet: bambooshoots
location: SW
---
kind: Gorilla
metadata:
labels:
movie: planetOfTheApes
name: o-ursus
spec:
diet: heston
location: Arizona
`)
}

View File

@@ -1,7 +1,6 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// TODO: move most of the tests in the api/target package to this package.
package krusty_test
import (
@@ -11,12 +10,11 @@ import (
"sigs.k8s.io/kustomize/api/krusty"
)
// TODO: move most of the tests in api/internal/target
// to this package, as they are all high level tests and
// examples appropriate to this level and package.
// The following test isn't much more than a usage example;
// everything is actually tested down in api/internal/target.
func TestSomething(t *testing.T) {
// A simple usage example - just shows what happens when
// there are no files to read. For more substantial tests
// and examples, see other tests in this package.
// TODO: https://github.com/kubernetes-sigs/kustomize/issues/1862
func TestEmptyFileSystem(t *testing.T) {
fSys := filesys.MakeFsInMemory()
b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions())
_, err := b.Run("noSuchThing")

View File

@@ -11,6 +11,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/konfig/builtinpluginconsts"
. "sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
@@ -176,3 +177,15 @@ func tabToSpace(input string) string {
}
return strings.Join(result, "")
}
func (th testingHarness) WriteLegacyConfigs(fName string) {
m := builtinpluginconsts.GetDefaultFieldSpecsAsMap()
var content []byte
for _, tCfg := range m {
content = append(content, []byte(tCfg)...)
}
err := th.fSys.WriteFile(fName, content)
if err != nil {
th.t.Fatalf("unable to add file %s", fName)
}
}

View File

@@ -0,0 +1,177 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"testing"
)
func makeStatefulSetKustomization(th testingHarness) {
th.WriteK("/app", `
commonLabels:
notIn: arrays
resources:
- statefulset.yaml
- statefulset-with-template.yaml
`)
th.WriteF("/app/statefulset.yaml", `
kind: StatefulSet
apiVersion: apps/v1
metadata:
name: test
labels:
notIn: arrays
spec:
serviceName: test
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
`)
th.WriteF("/app/statefulset-with-template.yaml", `
kind: StatefulSet
apiVersion: apps/v1
metadata:
name: persisted-test
labels:
notIn: arrays
spec:
serviceName: test
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
- name: data
mountPath: /usr/share/nginx/data
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: my-storage-class
resources:
requests:
storage: 1Gi
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: my-storage-class
resources:
requests:
storage: 100Gi
`)
}
func TestTransformersNoCreateArrays(t *testing.T) {
th := makeTestHarness(t)
makeStatefulSetKustomization(th)
m := th.Run("/app", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
notIn: arrays
name: test
spec:
replicas: 1
selector:
matchLabels:
app: test
notIn: arrays
serviceName: test
template:
metadata:
labels:
app: test
notIn: arrays
spec:
containers:
- image: k8s.gcr.io/nginx-slim:0.8
name: nginx
ports:
- containerPort: 80
name: web
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
notIn: arrays
name: persisted-test
spec:
replicas: 1
selector:
matchLabels:
app: test
notIn: arrays
serviceName: test
template:
metadata:
labels:
app: test
notIn: arrays
spec:
containers:
- image: k8s.gcr.io/nginx-slim:0.8
name: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- mountPath: /usr/share/nginx/html
name: www
- mountPath: /usr/share/nginx/data
name: data
volumeClaimTemplates:
- metadata:
labels:
notIn: arrays
name: www
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: my-storage-class
- metadata:
labels:
notIn: arrays
name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi
storageClassName: my-storage-class
`)
}

View File

@@ -0,0 +1,397 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"testing"
)
func makeTransfomersImageBase(th testingHarness) {
th.WriteK("/app/base", `
resources:
- deploy1.yaml
- random.yaml
images:
- name: nginx
newTag: v2
- name: my-nginx
newTag: previous
- name: myprivaterepohostname:1234/my/image
newTag: v1.0.1
- name: foobar
digest: sha256:24a0c4b4
- name: alpine
newName: myprivaterepohostname:1234/my/cool-alpine
- name: gcr.io:8080/my-project/my-cool-app
newName: my-cool-app
- name: postgres
newName: my-postgres
newTag: v3
- name: docker
newName: my-docker
digest: sha256:25a0d4b4
`)
th.WriteF("/app/base/deploy1.yaml", `
group: apps
apiVersion: v1
kind: Deployment
metadata:
name: deploy1
spec:
template:
spec:
initContainers:
- name: nginx2
image: my-nginx:1.8.0
- name: init-alpine
image: alpine:1.8.0
containers:
- name: ngnix
image: nginx:1.7.9
- name: repliaced-with-digest
image: foobar:1
- name: postgresdb
image: postgres:1.8.0
`)
th.WriteF("/app/base/random.yaml", `
kind: randomKind
metadata:
name: random
spec:
template:
spec:
containers:
- name: ngnix1
image: nginx
spec2:
template:
spec:
containers:
- name: nginx3
image: nginx:v1
- name: nginx4
image: my-nginx:latest
spec3:
template:
spec:
initContainers:
- name: postgresdb
image: postgres:alpine-9
- name: init-docker
image: docker:17-git
- name: myImage
image: myprivaterepohostname:1234/my/image:latest
- name: myImage2
image: myprivaterepohostname:1234/my/image
- name: my-app
image: my-app-image:v1
- name: my-cool-app
image: gcr.io:8080/my-project/my-cool-app:latest
`)
}
func TestIssue1281_JsonPatchAndImageTag(t *testing.T) {
th := makeTestHarness(t)
th.WriteK("/app", `
resources:
- deployment.yaml
images:
- name: abbott
newTag: v2
- name: costello
newTag: v8
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: ben
path: patch.json
`)
th.WriteF("/app/deployment.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: ben
spec:
template:
spec:
dnsPolicy: "None"
containers:
- name: awesome
image: abbott
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: alice
spec:
template:
spec:
containers:
- name: tomato
image: abbott
`)
th.WriteF("/app/patch.json", `
[ {"op": "add",
"path": "/spec/replica", "value": "3"},
{"op": "replace",
"path": "/spec/template/spec/containers/0",
"value": { "image": "costello" } } ]
`)
m := th.Run("/app", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: ben
spec:
replica: "3"
template:
spec:
containers:
- image: costello:v8
dnsPolicy: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: alice
spec:
template:
spec:
containers:
- image: abbott:v2
name: tomato
`)
}
func TestTransfomersImageDefaultConfig(t *testing.T) {
th := makeTestHarness(t)
makeTransfomersImageBase(th)
m := th.Run("/app/base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: v1
group: apps
kind: Deployment
metadata:
name: deploy1
spec:
template:
spec:
containers:
- image: nginx:v2
name: ngnix
- image: foobar@sha256:24a0c4b4
name: repliaced-with-digest
- image: my-postgres:v3
name: postgresdb
initContainers:
- image: my-nginx:previous
name: nginx2
- image: myprivaterepohostname:1234/my/cool-alpine:1.8.0
name: init-alpine
---
kind: randomKind
metadata:
name: random
spec:
template:
spec:
containers:
- image: nginx:v2
name: ngnix1
spec2:
template:
spec:
containers:
- image: nginx:v2
name: nginx3
- image: my-nginx:previous
name: nginx4
spec3:
template:
spec:
initContainers:
- image: my-postgres:v3
name: postgresdb
- image: my-docker@sha256:25a0d4b4
name: init-docker
- image: myprivaterepohostname:1234/my/image:v1.0.1
name: myImage
- image: myprivaterepohostname:1234/my/image:v1.0.1
name: myImage2
- image: my-app-image:v1
name: my-app
- image: my-cool-app:latest
name: my-cool-app
`)
}
func makeTransfomersImageCustomBase(th testingHarness) {
th.WriteK("/app/base", `
resources:
- custom.yaml
configurations:
- config/custom.yaml
images:
- name: nginx
newTag: v2
- name: my-nginx
newTag: previous
- name: myprivaterepohostname:1234/my/image
newTag: v1.0.1
- name: foobar
digest: sha256:24a0c4b4
- name: alpine
newName: myprivaterepohostname:1234/my/cool-alpine
- name: gcr.io:8080/my-project/my-cool-app
newName: my-cool-app
- name: postgres
newName: my-postgres
newTag: v3
- name: docker
newName: my-docker
digest: sha256:25a0d4b4
`)
th.WriteF("/app/base/custom.yaml", `
kind: customKind
metadata:
name: custom
spec:
template:
spec:
myContainers:
- name: ngnix1
image: nginx
spec2:
template:
spec:
myContainers:
- name: nginx3
image: nginx:v1
- name: nginx4
image: my-nginx:latest
spec3:
template:
spec:
myInitContainers:
- name: postgresdb
image: postgres:alpine-9
- name: init-docker
image: docker:17-git
- name: myImage
image: myprivaterepohostname:1234/my/image:latest
- name: myImage2
image: myprivaterepohostname:1234/my/image
- name: my-app
image: my-app-image:v1
- name: my-cool-app
image: gcr.io:8080/my-project/my-cool-app:latest
`)
th.WriteF("/app/base/config/custom.yaml", `
images:
- kind: Custom
path: spec/template/spec/myContainers/image
- kind: Custom
path: spec2/template/spec/myContainers/image
- kind: Custom
path: spec3/template/spec/myInitContainers/image
`)
}
func TestTransfomersImageCustomConfig(t *testing.T) {
th := makeTestHarness(t)
makeTransfomersImageCustomBase(th)
m := th.Run("/app/base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
kind: customKind
metadata:
name: custom
spec:
template:
spec:
myContainers:
- image: nginx
name: ngnix1
spec2:
template:
spec:
myContainers:
- image: nginx:v1
name: nginx3
- image: my-nginx:latest
name: nginx4
spec3:
template:
spec:
myInitContainers:
- image: postgres:alpine-9
name: postgresdb
- image: docker:17-git
name: init-docker
- image: myprivaterepohostname:1234/my/image:latest
name: myImage
- image: myprivaterepohostname:1234/my/image
name: myImage2
- image: my-app-image:v1
name: my-app
- image: gcr.io:8080/my-project/my-cool-app:latest
name: my-cool-app
`)
}
func makeTransfomersImageKnativeBase(th testingHarness) {
th.WriteK("/app/base", `
resources:
- knative.yaml
configurations:
- config/knative.yaml
images:
- name: solsa-echo
newTag: foo
`)
th.WriteF("/app/base/knative.yaml", `
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: echo
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: solsa-echo
`)
th.WriteF("/app/base/config/knative.yaml", `
images:
- path: spec/runLatest/configuration/revisionTemplate/spec/container/image
apiVersion: serving.knative.dev/v1alpha1
kind: Service
`)
}
func TestTransfomersImageKnativeConfig(t *testing.T) {
th := makeTestHarness(t)
makeTransfomersImageKnativeBase(th)
m := th.Run("/app/base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: echo
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: solsa-echo:foo
`)
}