mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 01:39:06 +00:00
Convert file system based test to in-memory.
This commit is contained in:
230
pkg/target/bigger_test.go
Normal file
230
pkg/target/bigger_test.go
Normal file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
)
|
||||
|
||||
// TODO(monopole): Add a feature test example covering secret generation.
|
||||
|
||||
// WARNING: These tests use a fake file system, and any attempt to use a
|
||||
// feature that spawns shells will fail, because said shells expect a working
|
||||
// directory corresponding to a real directory on disk - see
|
||||
// these lines in secretfactory.go:
|
||||
// cmd := exec.CommandContext(ctx, commands[0], commands[1:]...)
|
||||
// cmd.Dir = f.wd
|
||||
// Worse, the fake directory might match a real directory on the your system,
|
||||
// making the failure less obvious (and maybe hurting something if your secret
|
||||
// generation technique writes data to disk). So no use of secret generation
|
||||
// in these particular tests.
|
||||
// To eventually fix this, we could write the data to a real filesystem, and
|
||||
// clean up after, or use some other trick compatible with exec.
|
||||
|
||||
func writeBase(t *testing.T, ldr loadertest.FakeLoader) {
|
||||
writeK(t, ldr, "/app/base", `
|
||||
namePrefix: baseprefix-
|
||||
commonLabels:
|
||||
foo: bar
|
||||
commonAnnotations:
|
||||
baseAnno: This is a base annotation
|
||||
resources:
|
||||
- deployment/deployment.yaml
|
||||
- service/service.yaml
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/service/service.yaml", `
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: mungebot-service
|
||||
labels:
|
||||
app: mungebot
|
||||
spec:
|
||||
ports:
|
||||
- port: 7002
|
||||
selector:
|
||||
app: mungebot
|
||||
`)
|
||||
writeF(t, ldr, "/app/base/deployment/deployment.yaml", `
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mungebot
|
||||
labels:
|
||||
app: mungebot
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: mungebot
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
env:
|
||||
- name: foo
|
||||
value: bar
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
func TestBigBase(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/base")
|
||||
writeBase(t, ldr)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("Empty map.")
|
||||
}
|
||||
s, err := m.EncodeAsYaml()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
expected := `apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
annotations:
|
||||
baseAnno: This is a base annotation
|
||||
labels:
|
||||
app: mungebot
|
||||
foo: bar
|
||||
name: baseprefix-mungebot-service
|
||||
spec:
|
||||
ports:
|
||||
- port: 7002
|
||||
selector:
|
||||
app: mungebot
|
||||
foo: bar
|
||||
---
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
annotations:
|
||||
baseAnno: This is a base annotation
|
||||
labels:
|
||||
app: mungebot
|
||||
foo: bar
|
||||
name: baseprefix-mungebot
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
foo: bar
|
||||
template:
|
||||
metadata:
|
||||
annotations:
|
||||
baseAnno: This is a base annotation
|
||||
labels:
|
||||
app: mungebot
|
||||
foo: bar
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: foo
|
||||
value: bar
|
||||
image: nginx
|
||||
name: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`
|
||||
if string(s) != expected {
|
||||
t.Fatalf("Actual results:\n%s\nnot equal to expected:\n%s\n",
|
||||
s, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBigOverlay(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader("/app/overlay")
|
||||
writeBase(t, ldr)
|
||||
writeK(t, ldr, "/app/overlay", `
|
||||
namePrefix: test-infra-
|
||||
commonLabels:
|
||||
app: mungebot
|
||||
org: kubernetes
|
||||
repo: test-infra
|
||||
commonAnnotations:
|
||||
note: This is a test annotation
|
||||
bases:
|
||||
- ../base
|
||||
patchesStrategicMerge:
|
||||
- deployment/deployment.yaml
|
||||
configMapGenerator:
|
||||
- name: app-env
|
||||
env: configmap/app.env
|
||||
- name: app-config
|
||||
files:
|
||||
- configmap/app-init.ini
|
||||
imageTags:
|
||||
- name: nginx
|
||||
newTag: 1.8.0`)
|
||||
|
||||
writeF(t, ldr, "/app/overlay/configmap/app.env", `
|
||||
DB_USERNAME=admin
|
||||
DB_PASSWORD=somepw
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/configmap/app-init.ini", `
|
||||
FOO=bar
|
||||
BAR=baz
|
||||
`)
|
||||
writeF(t, ldr, "/app/overlay/deployment/deployment.yaml", `
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mungebot
|
||||
spec:
|
||||
replicas: 2
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
env:
|
||||
- name: FOO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: app-env
|
||||
key: somekey
|
||||
- name: busybox
|
||||
image: busybox
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: someConfigMap
|
||||
- configMapRef:
|
||||
name: app-env
|
||||
volumeMounts:
|
||||
- mountPath: /tmp/env
|
||||
name: app-env
|
||||
volumes:
|
||||
- configMap:
|
||||
name: app-env
|
||||
name: app-env
|
||||
`)
|
||||
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
|
||||
if err != nil {
|
||||
t.Fatalf("Err: %v", err)
|
||||
}
|
||||
if m == nil {
|
||||
t.Fatalf("Empty map.")
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,13 @@ var rf = resmap.NewFactory(resource.NewFactory(
|
||||
kunstruct.NewKunstructuredFactoryImpl()))
|
||||
|
||||
func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
|
||||
// Warning: the following filesystem - a fake - must be rooted at /.
|
||||
// This fs root is used as the working directory for the shell spawned by
|
||||
// the secretgenerator, and has nothing to do with the filesystem used
|
||||
// to load relative paths from the fake filesystem.
|
||||
// This trick only works for secret generator commands that don't actually
|
||||
// try to read the file system, because these tests don't write to the
|
||||
// real "/" directory. See use of exec package in the secretfactory.
|
||||
fakeFs := fs.MakeFakeFS()
|
||||
fakeFs.Mkdir("/")
|
||||
kt, err := NewKustTarget(
|
||||
@@ -245,7 +252,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 "/testpath/deployment.yaml"`) {
|
||||
if !strings.Contains(err.Error(), `cannot read file`) {
|
||||
t.Fatalf("unexpected error: %q", err)
|
||||
}
|
||||
}
|
||||
@@ -304,31 +311,34 @@ func TestDisableNameSuffixHash(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func write(t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
|
||||
err := ldr.AddFile(
|
||||
filepath.Join(dir, constants.KustomizationFileName),
|
||||
[]byte(`
|
||||
func writeF(t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
|
||||
err := ldr.AddFile(dir, []byte(content))
|
||||
if err != nil {
|
||||
t.Fatalf("failed write to %s; %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeK(
|
||||
t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
|
||||
writeF(t, ldr, filepath.Join(dir, constants.KustomizationFileName), `
|
||||
apiVersion: v1
|
||||
kind: Kustomization
|
||||
`+content))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to setup fake loader.")
|
||||
}
|
||||
`+content)
|
||||
}
|
||||
|
||||
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
|
||||
ldr := loadertest.NewFakeLoader(
|
||||
"/app/overlays/aws-sandbox2.us-east-1")
|
||||
write(t, ldr, "/app/base", "")
|
||||
write(t, ldr, "/app/overlays/aws", `
|
||||
writeK(t, ldr, "/app/base", "")
|
||||
writeK(t, ldr, "/app/overlays/aws", `
|
||||
bases:
|
||||
- ../../base
|
||||
`)
|
||||
write(t, ldr, "/app/overlays/aws-nonprod", `
|
||||
writeK(t, ldr, "/app/overlays/aws-nonprod", `
|
||||
bases:
|
||||
- ../aws
|
||||
`)
|
||||
write(t, ldr, "/app/overlays/aws-sandbox2.us-east-1", `
|
||||
writeK(t, ldr, "/app/overlays/aws-sandbox2.us-east-1", `
|
||||
bases:
|
||||
- ../aws-nonprod
|
||||
`)
|
||||
|
||||
Reference in New Issue
Block a user