mv namespace_test to api/krusty/, Provide another high level example.

This commit is contained in:
Dingshujie
2019-11-30 14:19:50 +08:00
parent 189f65dab9
commit e5c314a3ea
2 changed files with 16 additions and 41 deletions

View File

@@ -1,16 +1,14 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package target_test package krusty_test
import ( import (
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
func TestNamespacedGenerator(t *testing.T) { func TestNamespacedGenerator(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app") th := makeTestHarness(t)
th.WriteK("/app", ` th.WriteK("/app", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
@@ -34,10 +32,7 @@ secretGenerator:
literals: literals:
- password.txt=anotherSecret - password.txt=anotherSecret
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
@@ -76,7 +71,7 @@ type: Opaque
} }
func TestNamespacedGeneratorWithOverlays(t *testing.T) { func TestNamespacedGeneratorWithOverlays(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/overlay") th := makeTestHarness(t)
th.WriteK("/app/base", ` th.WriteK("/app/base", `
namespace: base namespace: base
@@ -97,10 +92,7 @@ configMapGenerator:
literals: literals:
- overlay=true - overlay=true
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/app/overlay", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:

View File

@@ -1,18 +1,15 @@
// Copyright 2019 The Kubernetes Authors. // Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package target_test package krusty_test
import ( import (
"strings" "strings"
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
func TestNamespacedSecrets(t *testing.T) { func TestNamespacedSecrets(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app") th := makeTestHarness(t)
th.WriteF("/app/secrets.yaml", ` th.WriteF("/app/secrets.yaml", `
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
@@ -51,16 +48,12 @@ resources:
- secrets.yaml - secrets.yaml
- role.yaml - role.yaml
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
// This validates Fix #1444. This should not be an error anymore - // This validates Fix #1444. This should not be an error anymore -
// the secrets have the same name but are in different namespaces. // the secrets have the same name but are in different namespaces.
// The ClusterRole (by def) is not in a namespace, // The ClusterRole (by def) is not in a namespace,
// an in this case applies to *any* Secret resource // an in this case applies to *any* Secret resource
// named "dummy" // named "dummy"
if err != nil { m := th.Run("/app", th.MakeDefaultOptions())
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
@@ -100,7 +93,7 @@ rules:
// PrefixSuffixTransformer and namereference transformers are // PrefixSuffixTransformer and namereference transformers are
// able to deal with simultaneous change of namespace and name. // able to deal with simultaneous change of namespace and name.
func TestNameAndNsTransformation(t *testing.T) { func TestNameAndNsTransformation(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/nameandns") th := makeTestHarness(t)
th.WriteK("/nameandns", ` th.WriteK("/nameandns", `
namePrefix: p1- namePrefix: p1-
@@ -206,11 +199,7 @@ kind: PersistentVolume
metadata: metadata:
name: pv1 name: pv1
`) `)
m := th.Run("/nameandns", th.MakeDefaultOptions())
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
kind: ConfigMap kind: ConfigMap
@@ -474,11 +463,11 @@ spec:
// using the same name in different namespaces are treated as ambiguous if the namespace is // using the same name in different namespaces are treated as ambiguous if the namespace is
// not specified // not specified
func TestVariablesAmbiguous(t *testing.T) { func TestVariablesAmbiguous(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/myapp") th := makeTestHarness(t)
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyApp) th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyApp)
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources) th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources) th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
_, err := th.MakeKustTarget().MakeCustomizedResMap() err := th.RunWithErr("/namespaceNeedInVar/myapp", th.MakeDefaultOptions())
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
@@ -531,7 +520,7 @@ vars:
// to TestVariablesAmbiguous problem. It requires to separate the variables // to TestVariablesAmbiguous problem. It requires to separate the variables
// and resources into multiple kustomization context/folders instead of one. // and resources into multiple kustomization context/folders instead of one.
func TestVariablesAmbiguousWorkaround(t *testing.T) { func TestVariablesAmbiguousWorkaround(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/workaround") th := makeTestHarness(t)
th.WriteK("/namespaceNeedInVar/dev", namespaceNeedInVarDevFolder) th.WriteK("/namespaceNeedInVar/dev", namespaceNeedInVarDevFolder)
th.WriteF("/namespaceNeedInVar/dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources) th.WriteF("/namespaceNeedInVar/dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
th.WriteK("/namespaceNeedInVar/test", namespaceNeedInVarTestFolder) th.WriteK("/namespaceNeedInVar/test", namespaceNeedInVarTestFolder)
@@ -541,10 +530,7 @@ resources:
- ../dev - ../dev
- ../test - ../test
`) `)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/namespaceNeedInVar/workaround", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput) th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
} }
@@ -590,13 +576,10 @@ vars:
// TestVariablesDisambiguatedWithNamespace demonstrates that adding the namespace // TestVariablesDisambiguatedWithNamespace demonstrates that adding the namespace
// to the variable declarations allows to disambiguate the variables. // to the variable declarations allows to disambiguate the variables.
func TestVariablesDisambiguatedWithNamespace(t *testing.T) { func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/namespaceNeedInVar/myapp") th := makeTestHarness(t)
th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyAppWithNamespace) th.WriteK("/namespaceNeedInVar/myapp", namespaceNeedInVarMyAppWithNamespace)
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources) th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources) th.WriteF("/namespaceNeedInVar/myapp/elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
m, err := th.MakeKustTarget().MakeCustomizedResMap() m := th.Run("/namespaceNeedInVar/myapp", th.MakeDefaultOptions())
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput) th.AssertActualEqualsExpected(m, namespaceNeedInVarExpectedOutput)
} }