Skip to content

Commit dc14678

Browse files
committed
Add unit tests to objects.go
This commit adds some unit tests for objects.go because that has some core functions for this framework. It's good to increase coverage for framework.
1 parent b43c259 commit dc14678

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed

pkg/patterns/declarative/pkg/manifest/objects_test.go

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,238 @@ configMapGenerator:
151151
})
152152
}
153153
}
154+
155+
func Test_ObjectAddLabels(t *testing.T) {
156+
inputManifest := `---
157+
apiVersion: apps/v1
158+
kind: Deployment
159+
metadata:
160+
labels:
161+
app: test-app
162+
name: frontend
163+
spec:
164+
replicas: 1
165+
selector:
166+
matchLabels:
167+
app: test-app
168+
strategy: {}
169+
template:
170+
metadata:
171+
labels:
172+
app: test-app
173+
spec:
174+
containers:
175+
- image: busybox
176+
name: busybox`
177+
tests := []struct {
178+
name string
179+
inputManifest string
180+
inputLabels map[string]string
181+
expectedLabels map[string]string
182+
}{
183+
{
184+
name: "add labels which are all new one",
185+
inputManifest: inputManifest,
186+
inputLabels: map[string]string{"sample-key1": "sample-value1", "sample-key2": "sample-value2"},
187+
expectedLabels: map[string]string{"app": "test-app", "sample-key1": "sample-value1", "sample-key2": "sample-value2"},
188+
},
189+
{
190+
// If call AddLabels with a key which has exists already, value will be overwritten.
191+
name: "add label which has already exist in manifest",
192+
inputManifest: inputManifest,
193+
inputLabels: map[string]string{"app": "test-app2"},
194+
expectedLabels: map[string]string{"app": "test-app2"},
195+
},
196+
}
197+
198+
for _, tt := range tests {
199+
t.Run(tt.name, func(t *testing.T) {
200+
ctx := context.Background()
201+
objects, err := ParseObjects(ctx, tt.inputManifest)
202+
if err != nil {
203+
t.Fatalf("unexpected error: %v", err)
204+
}
205+
for _, o := range objects.Items {
206+
o.AddLabels(tt.inputLabels)
207+
if len(tt.expectedLabels) != len(o.object.GetLabels()) {
208+
t.Errorf("Expected length of labels to be %v but is %v", len(tt.expectedLabels), len(o.object.GetLabels()))
209+
}
210+
for k, v := range tt.expectedLabels {
211+
if o.object.GetLabels()[k] != v {
212+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", tt.expectedLabels, o.object.GetLabels())
213+
}
214+
}
215+
}
216+
})
217+
}
218+
}
219+
220+
func Test_ParseJSONToObject(t *testing.T) {
221+
tests := []struct {
222+
name string
223+
inputManifest string
224+
expectedObject *Object
225+
error bool
226+
}{
227+
{
228+
name: "valid json manifest",
229+
inputManifest: `{
230+
"apiVersion": "v1",
231+
"kind": "ServiceAccount",
232+
"metadata": {
233+
"name": "foo-operator",
234+
"namespace": "kube-system"
235+
}
236+
}`,
237+
expectedObject: &Object{
238+
object: &unstructured.Unstructured{
239+
Object: map[string]interface{}{
240+
"apiVersion": "v1",
241+
"kind": "ServiceAccount",
242+
"metadata": map[string]interface{}{
243+
"name": "foo-operator",
244+
"namespace": "kube-system",
245+
},
246+
},
247+
},
248+
},
249+
error: false,
250+
},
251+
{
252+
name: "parse json error will occur",
253+
inputManifest: `{
254+
"apiVersion": "v1",
255+
"kind": "ServiceAccount",
256+
"metadata": {
257+
"name": "foo-operator",
258+
"invalid-key":
259+
}
260+
}`,
261+
expectedObject: nil,
262+
error: true,
263+
},
264+
{
265+
name: "unexpected type error will occur",
266+
inputManifest: `{
267+
"apiVersion": "v1",
268+
"invalid-kind": "ServiceAccount",
269+
"metadata": {
270+
"name": "foo-operator",
271+
"namespace": "kube-system"
272+
}
273+
}`,
274+
expectedObject: nil,
275+
error: true,
276+
},
277+
}
278+
for _, tt := range tests {
279+
t.Run(tt.name, func(t *testing.T) {
280+
if tt.error == true {
281+
_, err := ParseJSONToObject([]byte(tt.inputManifest))
282+
if err == nil {
283+
t.Fatalf("expect error occur, but error doesn't occur")
284+
}
285+
} else {
286+
object, err := ParseJSONToObject([]byte(tt.inputManifest))
287+
if err != nil {
288+
t.Fatalf("unexpected err: %v", err)
289+
}
290+
actual, _ := object.object.MarshalJSON()
291+
expected, _ := tt.expectedObject.object.MarshalJSON()
292+
if string(actual) != string(expected) {
293+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", tt.expectedObject.object, object.object)
294+
}
295+
}
296+
})
297+
}
298+
}
299+
300+
func Test_SetNestedStringMap(t *testing.T) {
301+
tests := []struct {
302+
name string
303+
inputManifest string
304+
inputMap map[string]string
305+
expectedObject []*Object
306+
}{
307+
{
308+
name: "normal pattern",
309+
inputManifest: `---
310+
apiVersion: v1
311+
kind: ServiceAccount
312+
metadata:
313+
name: foo-operator
314+
namespace: kube-system`,
315+
inputMap: map[string]string{"foo": "bar"},
316+
expectedObject: []*Object{
317+
{
318+
object: &unstructured.Unstructured{
319+
Object: map[string]interface{}{
320+
"apiVersion": "v1",
321+
"kind": "ServiceAccount",
322+
"metadata": map[string]interface{}{
323+
"name": "foo-operator",
324+
"namespace": "kube-system",
325+
"labels": map[string]interface{}{
326+
"foo": "bar",
327+
},
328+
},
329+
},
330+
},
331+
},
332+
},
333+
},
334+
{
335+
name: "nil object pattern",
336+
inputManifest: "",
337+
inputMap: map[string]string{"foo": "bar"},
338+
expectedObject: []*Object{
339+
{
340+
object: &unstructured.Unstructured{
341+
Object: map[string]interface{}{
342+
"metadata": map[string]interface{}{
343+
"labels": map[string]interface{}{
344+
"foo": "bar",
345+
},
346+
},
347+
},
348+
},
349+
},
350+
},
351+
},
352+
}
353+
for _, tt := range tests {
354+
t.Run(tt.name, func(t *testing.T) {
355+
ctx := context.Background()
356+
if len(tt.inputManifest) != 0 {
357+
objects, err := ParseObjects(ctx, tt.inputManifest)
358+
if err != nil {
359+
t.Fatalf("unexpected err: %v", err)
360+
}
361+
for _, o := range objects.Items {
362+
o.SetNestedStringMap(tt.inputMap, "metadata", "labels")
363+
actualBytes, _ := o.JSON()
364+
actualStr := string(actualBytes)
365+
366+
expectedBytes, _ := tt.expectedObject[0].JSON()
367+
expectedStr := string(expectedBytes)
368+
369+
if expectedStr != actualStr {
370+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
371+
}
372+
}
373+
} else { // Test for object.Object == nil pattern
374+
o, _ := NewObject(&unstructured.Unstructured{})
375+
o.SetNestedStringMap(tt.inputMap, "metadata", "labels")
376+
actualBytes, _ := o.JSON()
377+
actualStr := string(actualBytes)
378+
379+
expectedBytes, _ := tt.expectedObject[0].JSON()
380+
expectedStr := string(expectedBytes)
381+
382+
if expectedStr != actualStr {
383+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
384+
}
385+
}
386+
})
387+
}
388+
}

0 commit comments

Comments
 (0)