Skip to content

Commit 8ce8d4c

Browse files
committed
add test for MutateContainers
1 parent dc14678 commit 8ce8d4c

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-logr/logr v0.3.0
1010
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
1111
github.com/prometheus/client_golang v1.7.1
12+
github.com/stretchr/testify v1.5.1
1213
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
1314
golang.org/x/tools v0.0.0-20200714190737-9048b464a08d
1415
k8s.io/api v0.20.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,11 @@ github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM
475475
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
476476
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
477477
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
478+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
478479
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
479480
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
480481
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
482+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
481483
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
482484
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
483485
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=

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

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package manifest
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

8+
"github.com/stretchr/testify/assert"
9+
710
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
811
)
912

@@ -386,3 +389,247 @@ metadata:
386389
})
387390
}
388391
}
392+
393+
func Test_MutateContainers(t *testing.T) {
394+
tests := []struct {
395+
name string
396+
inputManifest string
397+
expectedObject []*Object
398+
error bool
399+
errorString string
400+
fn func(map[string]interface{}) error
401+
}{
402+
{
403+
name: "normal success pattern",
404+
inputManifest: `---
405+
apiVersion: apps/v1
406+
kind: Deployment
407+
metadata:
408+
name: frontend
409+
labels:
410+
app: test-app
411+
spec:
412+
selector:
413+
matchLabels:
414+
app: guestbook
415+
tier: frontend
416+
replicas: 3
417+
template:
418+
metadata:
419+
labels:
420+
app: guestbook
421+
tier: frontend
422+
spec:
423+
containers:
424+
- name: php-redis
425+
image: gcr.io/google-samples/gb-frontend:v4`,
426+
expectedObject: []*Object{
427+
{
428+
object: &unstructured.Unstructured{
429+
Object: map[string]interface{}{
430+
"apiVersion": "apps/v1",
431+
"kind": "Deployment",
432+
"metadata": map[string]interface{}{
433+
"labels": map[string]interface{}{
434+
"app": "test-app",
435+
},
436+
"name": "frontend",
437+
},
438+
"spec": map[string]interface{}{
439+
"replicas": 3,
440+
"selector": map[string]interface{}{
441+
"matchLabels": map[string]interface{}{
442+
"app": "guestbook",
443+
"tier": "frontend",
444+
},
445+
},
446+
"template": map[string]interface{}{
447+
"metadata": map[string]interface{}{
448+
"labels": map[string]interface{}{
449+
"app": "guestbook",
450+
"tier": "frontend",
451+
},
452+
},
453+
"spec": map[string]interface{}{
454+
"containers": []interface{}{
455+
map[string]interface{}{
456+
"image": "gcr.io/google-samples/gb-frontend:v4",
457+
"name": "php-redis",
458+
},
459+
},
460+
},
461+
},
462+
},
463+
},
464+
},
465+
},
466+
},
467+
error: false,
468+
},
469+
{
470+
name: "object has no containers key",
471+
inputManifest: `---
472+
apiVersion: apps/v1
473+
kind: Deployment
474+
metadata:
475+
name: frontend
476+
labels:
477+
app: test-app
478+
spec:
479+
selector:
480+
matchLabels:
481+
app: guestbook
482+
tier: frontend
483+
replicas: 3
484+
template:
485+
metadata:
486+
labels:
487+
app: guestbook
488+
tier: frontend
489+
spec: invalid-value`,
490+
expectedObject: nil,
491+
error: true,
492+
errorString: "error reading containers: spec.template.spec.containers accessor error: invalid-value is of the type string, expected map[string]interface{}",
493+
fn: func(m map[string]interface{}) error {
494+
return nil
495+
},
496+
},
497+
{
498+
name: "no manifest",
499+
inputManifest: "",
500+
expectedObject: nil,
501+
error: true,
502+
errorString: "containers not found",
503+
fn: func(m map[string]interface{}) error {
504+
return nil
505+
},
506+
},
507+
{
508+
name: "object has no containers list",
509+
inputManifest: `---
510+
apiVersion: apps/v1
511+
kind: Deployment
512+
metadata:
513+
name: frontend
514+
labels:
515+
app: test-app
516+
spec:
517+
selector:
518+
matchLabels:
519+
app: guestbook
520+
tier: frontend
521+
replicas: 3
522+
template:
523+
metadata:
524+
labels:
525+
app: guestbook
526+
tier: frontend
527+
spec:
528+
containers: invalid-value`,
529+
expectedObject: nil,
530+
error: true,
531+
errorString: "containers was not a list",
532+
fn: func(m map[string]interface{}) error {
533+
return nil
534+
},
535+
},
536+
{
537+
name: "object has no containers normal structure",
538+
inputManifest: `---
539+
apiVersion: apps/v1
540+
kind: Deployment
541+
metadata:
542+
name: frontend
543+
labels:
544+
app: test-app
545+
spec:
546+
selector:
547+
matchLabels:
548+
app: guestbook
549+
tier: frontend
550+
replicas: 3
551+
template:
552+
metadata:
553+
labels:
554+
app: guestbook
555+
tier: frontend
556+
spec:
557+
containers:
558+
- dummy-value`,
559+
expectedObject: nil,
560+
error: true,
561+
errorString: "container was not an object",
562+
fn: func(m map[string]interface{}) error {
563+
return nil
564+
},
565+
},
566+
{
567+
name: "mutate function return error",
568+
inputManifest: `---
569+
apiVersion: apps/v1
570+
kind: Deployment
571+
metadata:
572+
name: frontend
573+
labels:
574+
app: test-app
575+
spec:
576+
selector:
577+
matchLabels:
578+
app: guestbook
579+
tier: frontend
580+
replicas: 3
581+
template:
582+
metadata:
583+
labels:
584+
app: guestbook
585+
tier: frontend
586+
spec:
587+
containers:
588+
- name: php-redis
589+
image: gcr.io/google-samples/gb-frontend:v4`,
590+
expectedObject: nil,
591+
error: true,
592+
errorString: "error occures in mutate function",
593+
fn: func(m map[string]interface{}) error {
594+
return fmt.Errorf("error occures in mutate function")
595+
},
596+
},
597+
}
598+
for _, tt := range tests {
599+
t.Run(tt.name, func(t *testing.T) {
600+
ctx := context.Background()
601+
objects, err := ParseObjects(ctx, tt.inputManifest)
602+
if err != nil {
603+
t.Fatalf("unexpected err: %v", err)
604+
}
605+
if tt.error == false {
606+
for _, o := range objects.Items {
607+
err = o.MutateContainers(func(m map[string]interface{}) error {
608+
return nil
609+
})
610+
actualBytes, _ := o.JSON()
611+
actualStr := string(actualBytes)
612+
613+
expectedBytes, _ := tt.expectedObject[0].JSON()
614+
expectedStr := string(expectedBytes)
615+
616+
if expectedStr != actualStr {
617+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
618+
}
619+
}
620+
} else {
621+
if tt.inputManifest == "" {
622+
o, _ := NewObject(&unstructured.Unstructured{})
623+
err = o.MutateContainers(tt.fn)
624+
assert.EqualError(t, err, tt.errorString)
625+
} else {
626+
for _, o := range objects.Items {
627+
err = o.MutateContainers(tt.fn)
628+
assert.EqualError(t, err, tt.errorString)
629+
}
630+
}
631+
}
632+
633+
})
634+
}
635+
}

0 commit comments

Comments
 (0)