Files
kustomize/pkg/target/resourceconflict_test.go
Jeffrey Regan 3a01a63a01 Simplify code base.
- In ResMap, drop concept of internal Id to Resource
   map.  The ResMap is now (just) a list, allowing only
   very particular edits.

 - Resources should now be maintained in the order
   loaded.  A later PR can adjust tests to remove the
   internal legacy sorting, and confirm order-out is
   predictable from order-in.  The PR would suppress
   the sort in tests, and reorder the output to make
   all tests pass again, and confirm that the new order
   matched depth-first input traversal.  The FromMap
   fixture function was removed from all test inputs to
   establish a predictable input order.

 - Resources now have two 'Ids', OriginalId and
   CurrentId.  The former is fixed as
   GVK-name-namespace at load time, the latter changes
   during transformations.  The latter can be used to
   narrow name references when the former maps to
   multiple resources.  We allow bases to be loaded
   more than once in a build (a diamond pattern), so
   the OriginalId is not unique across the resources
   set.  The CurrentId is (and must be) unique, but is
   constantly mutating.  Failing to make this
   distinction clear, and attempting to maintain a
   mapping from a single mutating Id to a resource was
   making the code too complex.

 - Drop prefix/suffix from ResId - the ResId is now
   immutable.  A later PR can remove the distinction
   with ItemId.

 - This PR increases coverage of ResMap is since this
   is a large refactor.  Higher level tests didn't need
   much change outside reordering of results at the
   resource level.
2019-06-17 10:50:45 -07:00

228 lines
4.7 KiB
Go

// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package target_test
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
)
func writeBase(th *kusttest_test.KustTestHarness) {
th.WriteK("/app/base", `
resources:
- serviceaccount.yaml
- rolebinding.yaml
namePrefix: pfx-
nameSuffix: -sfx
`)
th.WriteF("/app/base/serviceaccount.yaml", `
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount
`)
th.WriteF("/app/base/rolebinding.yaml", `
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: serviceaccount
`)
}
func writeMidOverlays(th *kusttest_test.KustTestHarness) {
// Mid-level overlays
th.WriteK("/app/overlays/a", `
bases:
- ../../base
namePrefix: a-
nameSuffix: -suffixA
`)
th.WriteK("/app/overlays/b", `
bases:
- ../../base
namePrefix: b-
nameSuffix: -suffixB
`)
}
func writeTopOverlay(th *kusttest_test.KustTestHarness) {
// Top overlay, combining the mid-level overlays
th.WriteK("/app/combined", `
bases:
- ../overlays/a
- ../overlays/b
`)
}
func TestBase(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/base")
writeBase(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: ServiceAccount
metadata:
name: pfx-serviceaccount-sfx
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: pfx-rolebinding-sfx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: pfx-serviceaccount-sfx
`)
}
func TestMidLevelA(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/overlays/a")
writeBase(th)
writeMidOverlays(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: ServiceAccount
metadata:
name: a-pfx-serviceaccount-sfx-suffixA
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: a-pfx-rolebinding-sfx-suffixA
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: a-pfx-serviceaccount-sfx-suffixA
`)
}
func TestMidLevelB(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/overlays/b")
writeBase(th)
writeMidOverlays(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: ServiceAccount
metadata:
name: b-pfx-serviceaccount-sfx-suffixB
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: b-pfx-rolebinding-sfx-suffixB
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: b-pfx-serviceaccount-sfx-suffixB
`)
}
func TestMultibasesNoConflict(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/combined")
writeBase(th)
writeMidOverlays(th)
writeTopOverlay(th)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: ServiceAccount
metadata:
name: a-pfx-serviceaccount-sfx-suffixA
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: b-pfx-serviceaccount-sfx-suffixB
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: a-pfx-rolebinding-sfx-suffixA
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: a-pfx-serviceaccount-sfx-suffixA
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: b-pfx-rolebinding-sfx-suffixB
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role
subjects:
- kind: ServiceAccount
name: b-pfx-serviceaccount-sfx-suffixB
`)
}
func TestMultibasesWithConflict(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/combined")
writeBase(th)
writeMidOverlays(th)
writeTopOverlay(th)
th.WriteK("/app/overlays/a", `
bases:
- ../../base
namePrefix: a-
nameSuffix: -suffixA
resources:
- serviceaccount.yaml
`)
// Expect an error because this resource in the overlay
// matches a resource in the base.
th.WriteF("/app/overlays/a/serviceaccount.yaml", `
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount
`)
_, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil {
t.Fatalf("Expected resource conflict.")
}
if !strings.Contains(
err.Error(), "multiple matches for ~G_v1_ServiceAccount") {
t.Fatalf("Unexpected err: %v", err)
}
}