mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Add more resid test coverage.
This commit is contained in:
@@ -22,23 +22,30 @@ import (
|
||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||
)
|
||||
|
||||
// ResId conflates GroupVersionKind with a textual name to uniquely identify a kubernetes resource (object).
|
||||
// ResId is an immutable identifier of a k8s resource object.
|
||||
type ResId struct {
|
||||
// Gvk of the resource.
|
||||
gvKind gvk.Gvk
|
||||
// original name of the resource before transformation.
|
||||
|
||||
// name of the resource before transformation.
|
||||
name string
|
||||
// namePrefix of the resource
|
||||
// an untransformed resource has no prefix, fully transformed resource has an arbitrary number of prefixes
|
||||
// concatenated together.
|
||||
|
||||
// namePrefix of the resource.
|
||||
// An untransformed resource has no prefix.
|
||||
// A fully transformed resource has an arbitrary
|
||||
// number of prefixes concatenated together.
|
||||
prefix string
|
||||
// nameSuffix of the resource
|
||||
// an untransformed resource has no suffix, fully transformed resource has an arbitrary number of suffixes
|
||||
// concatenated together.
|
||||
|
||||
// nameSuffix of the resource.
|
||||
// An untransformed resource has no suffix.
|
||||
// A fully transformed resource has an arbitrary
|
||||
// number of suffixes concatenated together.
|
||||
suffix string
|
||||
// namespace the resource belongs to
|
||||
// an untransformed resource has no namespace, fully transformed resource has the namespace from
|
||||
// the top most overlay
|
||||
|
||||
// Namespace the resource belongs to.
|
||||
// An untransformed resource has no namespace.
|
||||
// A fully transformed resource has the namespace
|
||||
// from the top most overlay.
|
||||
namespace string
|
||||
}
|
||||
|
||||
@@ -108,10 +115,16 @@ func (n ResId) GvknString() string {
|
||||
return n.gvKind.String() + separator + n.name
|
||||
}
|
||||
|
||||
// GvknEquals return if two ResId have the same Group/Version/Kind and name
|
||||
// The comparison excludes prefix and suffix
|
||||
// GvknEquals returns true if the other id matches
|
||||
// Group/Version/Kind/name.
|
||||
func (n ResId) GvknEquals(id ResId) bool {
|
||||
return n.gvKind.Equals(id.gvKind) && n.name == id.name
|
||||
return n.name == id.name && n.gvKind.Equals(id.gvKind)
|
||||
}
|
||||
|
||||
// NsGvknEquals returns true if the other id matches
|
||||
// namespace/Group/Version/Kind/name.
|
||||
func (n ResId) NsGvknEquals(id ResId) bool {
|
||||
return n.namespace == id.namespace && n.GvknEquals(id)
|
||||
}
|
||||
|
||||
// Gvk returns Group/Version/Kind of the resource.
|
||||
|
||||
@@ -10,31 +10,80 @@ var stringTests = []struct {
|
||||
x ResId
|
||||
s string
|
||||
}{
|
||||
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"g_v_k|ns|p|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_v_k|ns|p|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_~V_k|ns|p|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_~V_~K|ns|p|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", prefix: "p", suffix: "s"},
|
||||
"~G_~V_~K|~X|p|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", suffix: "s"},
|
||||
"~G_~V_~K|~X|~P|nm|s"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
suffix: "s"},
|
||||
"~G_~V_~K|~X|~P|~N|s"},
|
||||
{ResId{gvKind: gvk.Gvk{}},
|
||||
"~G_~V_~K|~X|~P|~N|~S"},
|
||||
{ResId{},
|
||||
"~G_~V_~K|~X|~P|~N|~S"},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"g_v_k|ns|p|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_v_k|ns|p|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_k|ns|p|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|ns|p|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|~X|p|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|~X|~P|nm|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|~X|~P|~N|s",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
},
|
||||
"~G_~V_~K|~X|~P|~N|~S",
|
||||
},
|
||||
{
|
||||
ResId{},
|
||||
"~G_~V_~K|~X|~P|~N|~S",
|
||||
},
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
@@ -49,31 +98,80 @@ var gvknStringTests = []struct {
|
||||
x ResId
|
||||
s string
|
||||
}{
|
||||
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"g_v_k|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_v_k|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_~V_k|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", prefix: "p", suffix: "s", namespace: "ns"},
|
||||
"~G_~V_~K|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", prefix: "p", suffix: "s"},
|
||||
"~G_~V_~K|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
name: "nm", suffix: "s"},
|
||||
"~G_~V_~K|nm"},
|
||||
{ResId{gvKind: gvk.Gvk{},
|
||||
suffix: "s"},
|
||||
"~G_~V_~K|"},
|
||||
{ResId{gvKind: gvk.Gvk{}},
|
||||
"~G_~V_~K|"},
|
||||
{ResId{},
|
||||
"~G_~V_~K|"},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"g_v_k|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_v_k|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_k|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
namespace: "ns",
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
prefix: "p",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
name: "nm",
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|nm",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
suffix: "s",
|
||||
},
|
||||
"~G_~V_~K|",
|
||||
},
|
||||
{
|
||||
ResId{
|
||||
gvKind: gvk.Gvk{},
|
||||
},
|
||||
"~G_~V_~K|",
|
||||
},
|
||||
{
|
||||
ResId{},
|
||||
"~G_~V_~K|",
|
||||
},
|
||||
}
|
||||
|
||||
func TestGvknString(t *testing.T) {
|
||||
@@ -85,47 +183,147 @@ func TestGvknString(t *testing.T) {
|
||||
}
|
||||
|
||||
var GvknEqualsTest = []struct {
|
||||
x1 ResId
|
||||
x2 ResId
|
||||
id1 ResId
|
||||
id2 ResId
|
||||
gVknResult bool
|
||||
nSgVknResult bool
|
||||
}{
|
||||
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
|
||||
ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
|
||||
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
|
||||
ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
|
||||
{ResId{gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
|
||||
ResId{gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
|
||||
{ResId{name: "nm", prefix: "AA", suffix: "aa", namespace: "X"},
|
||||
ResId{name: "nm", prefix: "BB", suffix: "bb", namespace: "Z"}},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: true,
|
||||
},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
namespace: "Z",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: false,
|
||||
},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: false,
|
||||
},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
namespace: "Z",
|
||||
gvKind: gvk.Gvk{Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: false,
|
||||
},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
namespace: "Z",
|
||||
gvKind: gvk.Gvk{Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: false,
|
||||
},
|
||||
{
|
||||
id1: ResId{
|
||||
namespace: "X",
|
||||
name: "nm",
|
||||
prefix: "AA",
|
||||
suffix: "aa",
|
||||
},
|
||||
id2: ResId{
|
||||
namespace: "Z",
|
||||
name: "nm",
|
||||
prefix: "BB",
|
||||
suffix: "bb",
|
||||
},
|
||||
gVknResult: true,
|
||||
nSgVknResult: false,
|
||||
},
|
||||
}
|
||||
|
||||
func TestEquals(t *testing.T) {
|
||||
for _, hey := range GvknEqualsTest {
|
||||
if !hey.x1.GvknEquals(hey.x2) {
|
||||
t.Fatalf("%v should equal %v", hey.x1, hey.x2)
|
||||
for _, tst := range GvknEqualsTest {
|
||||
if tst.id1.GvknEquals(tst.id2) != tst.gVknResult {
|
||||
t.Fatalf("GvknEquals(\n%v,\n%v\n) should be %v",
|
||||
tst.id1, tst.id2, tst.gVknResult)
|
||||
}
|
||||
if tst.id1.NsGvknEquals(tst.id2) != tst.nSgVknResult {
|
||||
t.Fatalf("NsGvknEquals(\n%v,\n%v\n) should be %v",
|
||||
tst.id1, tst.id2, tst.nSgVknResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyWithNewPrefixSuffix(t *testing.T) {
|
||||
r1 := ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "a",
|
||||
suffix: "b",
|
||||
namespace: "X"}
|
||||
}
|
||||
r2 := r1.CopyWithNewPrefixSuffix("p-", "-s")
|
||||
expected := ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "p-a",
|
||||
suffix: "b-s",
|
||||
namespace: "X"}
|
||||
}
|
||||
if !r2.GvknEquals(expected) {
|
||||
t.Fatalf("%v should equal %v", r2, expected)
|
||||
}
|
||||
@@ -133,18 +331,20 @@ func TestCopyWithNewPrefixSuffix(t *testing.T) {
|
||||
|
||||
func TestCopyWithNewNamespace(t *testing.T) {
|
||||
r1 := ResId{
|
||||
namespace: "X",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "a",
|
||||
suffix: "b",
|
||||
namespace: "X"}
|
||||
}
|
||||
r2 := r1.CopyWithNewNamespace("zzz")
|
||||
expected := ResId{
|
||||
namespace: "zzz",
|
||||
gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
||||
name: "nm",
|
||||
prefix: "a",
|
||||
suffix: "b",
|
||||
namespace: "zzz"}
|
||||
}
|
||||
if !r2.GvknEquals(expected) {
|
||||
t.Fatalf("%v should equal %v", r2, expected)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user