More tests/examples.

This commit is contained in:
jregan
2019-11-30 11:28:42 -08:00
parent fc8c45cff5
commit 1f85ce454d
8 changed files with 389 additions and 314 deletions

View File

@@ -4,8 +4,12 @@
package krusty_test
import (
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/api/konfig"
. "sigs.k8s.io/kustomize/api/internal/target"
)
@@ -32,6 +36,23 @@ metadata:
}
}
func TestTargetMustHaveOnlyOneKustomizationFile(t *testing.T) {
th := makeTestHarness(t)
for _, n := range konfig.RecognizedKustomizationFileNames() {
th.WriteF(filepath.Join("/app", n), `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
`)
}
err := th.RunWithErr("/app", th.MakeDefaultOptions())
if err == nil {
t.Fatalf("expected an error")
}
if !strings.Contains(err.Error(), "Found multiple kustomization files under: /app") {
t.Fatalf("unexpected error: %q", err)
}
}
func TestBaseMustHaveKustomizationFile(t *testing.T) {
th := makeTestHarness(t)
th.WriteK("/app", `
@@ -57,3 +78,18 @@ spec:
t.Fatalf("unexpected error: %q", err)
}
}
func TestResourceNotFound(t *testing.T) {
th := makeTestHarness(t)
th.WriteK("/app", `
resources:
- deployment.yaml
`)
err := th.RunWithErr("/app", th.MakeDefaultOptions())
if err == nil {
t.Fatalf("expected an error")
}
if !strings.Contains(err.Error(), "'/app/deployment.yaml' doesn't exist") {
t.Fatalf("unexpected error: %q", err)
}
}

View File

@@ -0,0 +1,27 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"testing"
)
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
th := makeTestHarness(t)
th.WriteK("/app/base", "")
th.WriteK("/app/overlays/aws", `
resources:
- ../../base
`)
th.WriteK("/app/overlays/aws-nonprod", `
resources:
- ../aws
`)
th.WriteK("/app/overlays/aws-sandbox2.us-east-1", `
resources:
- ../aws-nonprod
`)
m := th.Run("/app/overlays/aws-sandbox2.us-east-1", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, "")
}

View File

@@ -0,0 +1,99 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
)
func findSecret(m resmap.ResMap) *resource.Resource {
for _, r := range m.Resources() {
if r.OrgId().Kind == "Secret" {
return r
}
}
return nil
}
func TestDisableNameSuffixHash(t *testing.T) {
th := makeTestHarness(t)
const kustomizationContent = `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
nameSuffix: -bar
namespace: ns1
commonLabels:
app: nginx
commonAnnotations:
note: This is a test annotation
resources:
- deployment.yaml
- namespace.yaml
generatorOptions:
disableNameSuffixHash: false
configMapGenerator:
- name: literalConfigMap
literals:
- DB_USERNAME=admin
- DB_PASSWORD=somepw
secretGenerator:
- name: secret
literals:
- DB_USERNAME=admin
- DB_PASSWORD=somepw
type: Opaque
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: dply1
path: jsonpatch.json
`
th.WriteK("/whatever", kustomizationContent)
th.WriteF("/whatever/deployment.yaml", `
apiVersion: apps/v1
metadata:
name: dply1
kind: Deployment
`)
th.WriteF("/whatever/namespace.yaml", `
apiVersion: v1
kind: Namespace
metadata:
name: ns1
`)
th.WriteF("/whatever/jsonpatch.json", `[
{"op": "add", "path": "/spec/replica", "value": "3"}
]`)
m := th.Run("/whatever", th.MakeDefaultOptions())
secret := findSecret(m)
if secret == nil {
t.Errorf("Expected to find a Secret")
}
if secret.GetName() != "foo-secret-bar-9btc7bt4kb" {
t.Errorf("unexpected secret resource name: %s", secret.GetName())
}
th.WriteK("/whatever",
strings.Replace(kustomizationContent,
"disableNameSuffixHash: false",
"disableNameSuffixHash: true", -1))
m = th.Run("/whatever", th.MakeDefaultOptions())
secret = findSecret(m)
if secret == nil {
t.Errorf("Expected to find a Secret")
}
if secret.GetName() != "foo-secret-bar" { // No hash at end.
t.Errorf("unexpected secret resource name: %s", secret.GetName())
}
}

View File

@@ -59,13 +59,14 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
return nil, err
}
defer ldr.Cleanup()
kt, err := target.NewKustTarget(
kt := target.NewKustTarget(
ldr,
validator.NewKustValidator(),
rf,
pf,
pLdr.NewLoader(b.options.PluginConfig, rf),
)
err = kt.Load()
if err != nil {
return nil, err
}