Skip to content

Commit a898457

Browse files
committed
refactor test code for readability
1 parent 38b7f42 commit a898457

File tree

1 file changed

+81
-42
lines changed

1 file changed

+81
-42
lines changed

pkg/resmap/resmap_test.go

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -119,53 +119,92 @@ func TestDemandOneMatchForId(t *testing.T) {
119119
}
120120

121121
func TestFilterBy(t *testing.T) {
122-
rm := ResMap{resid.NewResIdWithPrefixNamespace(cmap, "cm1", "prefix1", "ns1"): rf.FromMap(
123-
map[string]interface{}{
124-
"apiVersion": "v1",
125-
"kind": "ConfigMap",
126-
"metadata": map[string]interface{}{
127-
"name": "cm1",
122+
tests := map[string]struct {
123+
resMap ResMap
124+
filter resid.ResId
125+
expected ResMap
126+
}{
127+
"different namespace": {
128+
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace1"): rf.FromMap(
129+
map[string]interface{}{
130+
"apiVersion": "v1",
131+
"kind": "ConfigMap",
132+
"metadata": map[string]interface{}{
133+
"name": "config-map",
134+
},
135+
}),
128136
},
129-
}),
130-
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"): rf.FromMap(
131-
map[string]interface{}{
132-
"apiVersion": "v1",
133-
"kind": "ConfigMap",
134-
"metadata": map[string]interface{}{
135-
"name": "cm2",
136-
},
137-
}),
138-
}
139-
rm1 := ResMap{
140-
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns2"): rf.FromMap(
141-
map[string]interface{}{
142-
"apiVersion": "v1",
143-
"kind": "ConfigMap",
144-
"metadata": map[string]interface{}{
145-
"name": "cm2",
146-
},
147-
}),
148-
}
149-
150-
for k, v := range rm {
151-
rm1[k] = v
152-
}
153-
154-
empty := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns3"))
155-
if len(empty) != 0 {
156-
t.Fatalf("Expected empty filtered map but got %v", empty)
157-
}
158-
159-
ns1map := rm1.FilterBy(resid.NewResIdWithPrefixNamespace(cmap, "cm4", "prefix1", "ns1"))
160-
if !reflect.DeepEqual(rm, ns1map) {
161-
t.Fatalf("Expected %v but got back %v", rm, ns1map)
137+
filter: resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace2"),
138+
expected: ResMap{},
139+
},
140+
"different prefix": {
141+
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix1", "namespace"): rf.FromMap(
142+
map[string]interface{}{
143+
"apiVersion": "v1",
144+
"kind": "ConfigMap",
145+
"metadata": map[string]interface{}{
146+
"name": "config-map",
147+
},
148+
}),
149+
},
150+
filter: resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix2", "namespace"),
151+
expected: ResMap{},
152+
},
153+
"same namespace, same prefix": {
154+
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map1", "prefix", "namespace"): rf.FromMap(
155+
map[string]interface{}{
156+
"apiVersion": "v1",
157+
"kind": "ConfigMap",
158+
"metadata": map[string]interface{}{
159+
"name": "config-map1",
160+
},
161+
}),
162+
},
163+
filter: resid.NewResIdWithPrefixNamespace(cmap, "config-map2", "prefix", "namespace"),
164+
expected: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map1", "prefix", "namespace"): rf.FromMap(
165+
map[string]interface{}{
166+
"apiVersion": "v1",
167+
"kind": "ConfigMap",
168+
"metadata": map[string]interface{}{
169+
"name": "config-map1",
170+
},
171+
}),
172+
},
173+
},
174+
"filter by cluster-level Gvk": {
175+
resMap: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace"): rf.FromMap(
176+
map[string]interface{}{
177+
"apiVersion": "v1",
178+
"kind": "ConfigMap",
179+
"metadata": map[string]interface{}{
180+
"name": "config-map",
181+
},
182+
}),
183+
},
184+
filter: resid.NewResId(gvk.Gvk{Kind: "ClusterRoleBinding"}, "cluster-role-binding"),
185+
expected: ResMap{resid.NewResIdWithPrefixNamespace(cmap, "config-map", "prefix", "namespace"): rf.FromMap(
186+
map[string]interface{}{
187+
"apiVersion": "v1",
188+
"kind": "ConfigMap",
189+
"metadata": map[string]interface{}{
190+
"name": "config-map",
191+
},
192+
}),
193+
},
194+
},
162195
}
163196

164-
clmap := rm1.FilterBy(resid.NewResId(gvk.Gvk{Kind: "ClusterRoleBinding"}, "crb"))
165-
if !reflect.DeepEqual(rm1, clmap) {
166-
t.Fatalf("Expected %v but got back %v", rm1, clmap)
197+
for name, test := range tests {
198+
test := test
199+
t.Run(name, func(t *testing.T) {
200+
got := test.resMap.FilterBy(test.filter)
201+
if !reflect.DeepEqual(test.expected, got) {
202+
t.Fatalf("Expected %v but got back %v", test.expected, got)
203+
}
204+
})
167205
}
168206
}
207+
169208
func TestDeepCopy(t *testing.T) {
170209
rm1 := ResMap{
171210
resid.NewResId(cmap, "cm1"): rf.FromMap(

0 commit comments

Comments
 (0)