Skip to content

Commit f2f1125

Browse files
committed
add tests for resmap
1 parent 5036a12 commit f2f1125

File tree

2 files changed

+219
-20
lines changed

2 files changed

+219
-20
lines changed

pkg/resmap/resmap.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"github.com/ghodss/yaml"
2727
"github.com/golang/glog"
2828
"github.com/pkg/errors"
29-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30-
"sigs.k8s.io/kustomize/pkg/gvk"
3129
"sigs.k8s.io/kustomize/pkg/ifc"
3230
internal "sigs.k8s.io/kustomize/pkg/internal/error"
3331
"sigs.k8s.io/kustomize/pkg/resid"
@@ -125,20 +123,6 @@ func (m ResMap) DeepCopy() ResMap {
125123
return mcopy
126124
}
127125

128-
func (m ResMap) insert(newName string, obj *unstructured.Unstructured) error {
129-
oldName := obj.GetName()
130-
gvKind := gvk.FromSchemaGvk(obj.GroupVersionKind())
131-
id := resid.NewResId(gvKind, oldName)
132-
133-
if _, found := m[id]; found {
134-
return fmt.Errorf(
135-
"the <name: %q, GroupVersionKind: %v> already exists in the map", oldName, gvKind)
136-
}
137-
obj.SetName(newName)
138-
m[id] = resource.NewResourceFromUnstruct(*obj)
139-
return nil
140-
}
141-
142126
// FilterBy returns a ResMap containing ResIds with the same namespace and nameprefix
143127
// with the inputId
144128
func (m ResMap) FilterBy(inputId resid.ResId) ResMap {

pkg/resmap/resmap_test.go

Lines changed: 219 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,202 @@ metadata:
7070
}
7171
}
7272

73+
func TestDemandOneMatchForId(t *testing.T) {
74+
rm1 := ResMap{
75+
resid.NewResIdWithPrefixNamespace(cmap, "cm1", "prefix1", "ns1"): resource.NewResourceFromMap(
76+
map[string]interface{}{
77+
"apiVersion": "v1",
78+
"kind": "ConfigMap",
79+
"metadata": map[string]interface{}{
80+
"name": "cm1",
81+
},
82+
}),
83+
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"): resource.NewResourceFromMap(
84+
map[string]interface{}{
85+
"apiVersion": "v1",
86+
"kind": "ConfigMap",
87+
"metadata": map[string]interface{}{
88+
"name": "cm2",
89+
},
90+
}),
91+
}
92+
93+
_, ok := rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"))
94+
if !ok {
95+
t.Fatal("Expected single map entry but got none")
96+
}
97+
98+
// confirm that ns and prefix are not included in match
99+
_, ok = rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix", "ns"))
100+
if !ok {
101+
t.Fatal("Expected single map entry but got none")
102+
}
103+
104+
// confirm that name is matched correctly
105+
result, ok := rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns1"))
106+
if ok {
107+
t.Fatalf("Expected no map entries but got %v", result)
108+
}
109+
110+
cmap2 := gvk.Gvk{Version: "v2", Kind: "ConfigMap"}
111+
112+
// confirm that gvk is matched correctly
113+
result, ok = rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap2, "cm2", "prefix1", "ns1"))
114+
if ok {
115+
t.Fatalf("Expected no map entries but got %v", result)
116+
}
117+
118+
}
119+
120+
func TestFilterBy(t *testing.T) {
121+
rm := ResMap{resid.NewResIdWithPrefixNamespace(cmap, "cm1", "prefix1", "ns1"): resource.NewResourceFromMap(
122+
map[string]interface{}{
123+
"apiVersion": "v1",
124+
"kind": "ConfigMap",
125+
"metadata": map[string]interface{}{
126+
"name": "cm1",
127+
},
128+
}),
129+
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"): resource.NewResourceFromMap(
130+
map[string]interface{}{
131+
"apiVersion": "v1",
132+
"kind": "ConfigMap",
133+
"metadata": map[string]interface{}{
134+
"name": "cm2",
135+
},
136+
}),
137+
}
138+
rm1 := ResMap{
139+
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns2"): resource.NewResourceFromMap(
140+
map[string]interface{}{
141+
"apiVersion": "v1",
142+
"kind": "ConfigMap",
143+
"metadata": map[string]interface{}{
144+
"name": "cm2",
145+
},
146+
}),
147+
}
148+
149+
for k, v := range rm {
150+
rm1[k] = v
151+
}
152+
153+
empty := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns3"))
154+
if len(empty) != 0 {
155+
t.Fatalf("Expected empty filtered map but got %v", empty)
156+
}
157+
158+
ns1map := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns1"))
159+
if !reflect.DeepEqual(rm, ns1map) {
160+
t.Fatalf("Expected %v but got back %v", rm, ns1map)
161+
}
162+
}
163+
func TestDeepCopy(t *testing.T) {
164+
rm1 := ResMap{
165+
resid.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
166+
map[string]interface{}{
167+
"apiVersion": "v1",
168+
"kind": "ConfigMap",
169+
"metadata": map[string]interface{}{
170+
"name": "cm1",
171+
},
172+
}),
173+
resid.NewResId(cmap, "cm2"): resource.NewResourceFromMap(
174+
map[string]interface{}{
175+
"apiVersion": "v1",
176+
"kind": "ConfigMap",
177+
"metadata": map[string]interface{}{
178+
"name": "cm2",
179+
},
180+
}),
181+
}
182+
183+
rm2 := rm1.DeepCopy()
184+
185+
if !reflect.DeepEqual(rm1, rm2) {
186+
t.Fatalf("%v doesn't equal it's deep copy %v", rm1, rm2)
187+
}
188+
}
189+
190+
func TestErrorIfNotEqual(t *testing.T) {
191+
192+
rm1 := ResMap{
193+
resid.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
194+
map[string]interface{}{
195+
"apiVersion": "v1",
196+
"kind": "ConfigMap",
197+
"metadata": map[string]interface{}{
198+
"name": "cm1",
199+
},
200+
}),
201+
resid.NewResId(cmap, "cm2"): resource.NewResourceFromMap(
202+
map[string]interface{}{
203+
"apiVersion": "v1",
204+
"kind": "ConfigMap",
205+
"metadata": map[string]interface{}{
206+
"name": "cm2",
207+
},
208+
}),
209+
}
210+
211+
err := rm1.ErrorIfNotEqual(rm1)
212+
if err != nil {
213+
t.Fatalf("%v should equal itself %v", rm1, err)
214+
}
215+
216+
rm2 := ResMap{
217+
resid.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
218+
map[string]interface{}{
219+
"apiVersion": "v1",
220+
"kind": "ConfigMap",
221+
"metadata": map[string]interface{}{
222+
"name": "cm1",
223+
},
224+
}),
225+
}
226+
227+
// test the different number of keys path
228+
err = rm1.ErrorIfNotEqual(rm2)
229+
if err == nil {
230+
t.Fatalf("%v should not equal %v %v", rm1, rm2, err)
231+
}
232+
233+
rm3 := ResMap{
234+
resid.NewResId(cmap, "cm2"): resource.NewResourceFromMap(
235+
map[string]interface{}{
236+
"apiVersion": "v1",
237+
"kind": "ConfigMap",
238+
"metadata": map[string]interface{}{
239+
"name": "cm1",
240+
},
241+
}),
242+
}
243+
244+
// test the different key values path
245+
err = rm2.ErrorIfNotEqual(rm3)
246+
if err == nil {
247+
t.Fatalf("%v should not equal %v %v", rm1, rm2, err)
248+
}
249+
250+
rm4 := ResMap{
251+
resid.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
252+
map[string]interface{}{
253+
"apiVersion": "v1",
254+
"kind": "ConfigMap",
255+
"metadata": map[string]interface{}{
256+
"name": "cm3",
257+
},
258+
}),
259+
}
260+
261+
// test the deepcopy path
262+
err = rm2.ErrorIfNotEqual(rm4)
263+
if err == nil {
264+
t.Fatalf("%v should not equal %v %v", rm1, rm2, err)
265+
}
266+
267+
}
268+
73269
func TestNewMapFromFiles(t *testing.T) {
74270

75271
resourceStr := `apiVersion: apps/v1
@@ -225,7 +421,8 @@ func TestMergeWithoutOverride(t *testing.T) {
225421
}
226422
}
227423

228-
func TestMergeWithOverride(t *testing.T) {
424+
func generateMergeFixtures(b ifc.GenerationBehavior) []ResMap {
425+
229426
input1 := ResMap{
230427
resid.NewResId(cmap, "cmap"): resource.NewResourceFromMap(
231428
map[string]interface{}{
@@ -256,8 +453,11 @@ func TestMergeWithOverride(t *testing.T) {
256453
}),
257454
}
258455
input1[resid.NewResId(cmap, "cmap")].SetBehavior(ifc.BehaviorCreate)
259-
input2[resid.NewResId(cmap, "cmap")].SetBehavior(ifc.BehaviorMerge)
260-
input := []ResMap{input1, input2}
456+
input2[resid.NewResId(cmap, "cmap")].SetBehavior(b)
457+
return []ResMap{input1, input2}
458+
}
459+
460+
func TestMergeWithOverride(t *testing.T) {
261461
expected := ResMap{
262462
resid.NewResId(cmap, "cmap"): resource.NewResourceFromMap(
263463
map[string]interface{}{
@@ -276,7 +476,7 @@ func TestMergeWithOverride(t *testing.T) {
276476
}),
277477
}
278478
expected[resid.NewResId(cmap, "cmap")].SetBehavior(ifc.BehaviorCreate)
279-
merged, err := MergeWithOverride(input...)
479+
merged, err := MergeWithOverride(generateMergeFixtures(ifc.BehaviorMerge)...)
280480
if err != nil {
281481
t.Fatalf("unexpected error: %v", err)
282482
}
@@ -299,4 +499,19 @@ func TestMergeWithOverride(t *testing.T) {
299499
if !reflect.DeepEqual(merged2, expected) {
300500
t.Fatalf("%#v doesn't equal expected %#v", merged2, expected)
301501
}
502+
503+
inputs := generateMergeFixtures(ifc.BehaviorReplace)
504+
replaced, err := MergeWithOverride(inputs...)
505+
if err != nil {
506+
t.Fatalf("unexpected error: %v", err)
507+
}
508+
expectedReplaced := inputs[1]
509+
if !reflect.DeepEqual(replaced, expectedReplaced) {
510+
t.Fatalf("%#v doesn't equal expected %#v", replaced, expectedReplaced)
511+
}
512+
513+
_, err = MergeWithOverride(generateMergeFixtures(ifc.BehaviorUnspecified)...)
514+
if err == nil {
515+
t.Fatal("Merging with GenerationBehavior BehaviorUnspecified should return an error but does not")
516+
}
302517
}

0 commit comments

Comments
 (0)