Skip to content

Commit 503f991

Browse files
committed
add test for MutatePodSpec
1 parent 8ce8d4c commit 503f991

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

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

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,211 @@ spec:
633633
})
634634
}
635635
}
636+
637+
func Test_MutatePodSpec(t *testing.T) {
638+
tests := []struct {
639+
name string
640+
inputManifest string
641+
expectedObject []*Object
642+
error bool
643+
errorString string
644+
fn func(map[string]interface{}) error
645+
}{
646+
{
647+
name: "normal success pattern",
648+
inputManifest: `---
649+
apiVersion: apps/v1
650+
kind: Deployment
651+
metadata:
652+
name: frontend
653+
labels:
654+
app: test-app
655+
spec:
656+
selector:
657+
matchLabels:
658+
app: guestbook
659+
tier: frontend
660+
replicas: 3
661+
template:
662+
metadata:
663+
labels:
664+
app: guestbook
665+
tier: frontend
666+
spec:
667+
containers:
668+
- name: php-redis
669+
image: gcr.io/google-samples/gb-frontend:v4`,
670+
expectedObject: []*Object{
671+
{
672+
object: &unstructured.Unstructured{
673+
Object: map[string]interface{}{
674+
"apiVersion": "apps/v1",
675+
"kind": "Deployment",
676+
"metadata": map[string]interface{}{
677+
"labels": map[string]interface{}{
678+
"app": "test-app",
679+
},
680+
"name": "frontend",
681+
},
682+
"spec": map[string]interface{}{
683+
"replicas": 3,
684+
"selector": map[string]interface{}{
685+
"matchLabels": map[string]interface{}{
686+
"app": "guestbook",
687+
"tier": "frontend",
688+
},
689+
},
690+
"template": map[string]interface{}{
691+
"metadata": map[string]interface{}{
692+
"labels": map[string]interface{}{
693+
"app": "guestbook",
694+
"tier": "frontend",
695+
},
696+
},
697+
"spec": map[string]interface{}{
698+
"containers": []interface{}{
699+
map[string]interface{}{
700+
"image": "gcr.io/google-samples/gb-frontend:v4",
701+
"name": "php-redis",
702+
},
703+
},
704+
},
705+
},
706+
},
707+
},
708+
},
709+
},
710+
},
711+
error: false,
712+
},
713+
{
714+
name: "object has no spec key in template key",
715+
inputManifest: `---
716+
apiVersion: apps/v1
717+
kind: Deployment
718+
metadata:
719+
name: frontend
720+
labels:
721+
app: test-app
722+
spec:
723+
selector:
724+
matchLabels:
725+
app: guestbook
726+
tier: frontend
727+
replicas: 3
728+
template: invalid-value`,
729+
expectedObject: nil,
730+
error: true,
731+
errorString: "error reading containers: spec.template.spec accessor error: invalid-value is of the type string, expected map[string]interface{}",
732+
fn: func(m map[string]interface{}) error {
733+
return nil
734+
},
735+
},
736+
{
737+
name: "no manifest",
738+
inputManifest: "",
739+
expectedObject: nil,
740+
error: true,
741+
errorString: "pod spec not found",
742+
fn: func(m map[string]interface{}) error {
743+
return nil
744+
},
745+
},
746+
{
747+
name: "object has no containers normal structure",
748+
inputManifest: `---
749+
apiVersion: apps/v1
750+
kind: Deployment
751+
metadata:
752+
name: frontend
753+
labels:
754+
app: test-app
755+
spec:
756+
selector:
757+
matchLabels:
758+
app: guestbook
759+
tier: frontend
760+
replicas: 3
761+
template:
762+
metadata:
763+
labels:
764+
app: guestbook
765+
tier: frontend
766+
spec: invalid-value`,
767+
expectedObject: nil,
768+
error: true,
769+
errorString: "pod spec was not an object",
770+
fn: func(m map[string]interface{}) error {
771+
return nil
772+
},
773+
},
774+
{
775+
name: "mutate function return error",
776+
inputManifest: `---
777+
apiVersion: apps/v1
778+
kind: Deployment
779+
metadata:
780+
name: frontend
781+
labels:
782+
app: test-app
783+
spec:
784+
selector:
785+
matchLabels:
786+
app: guestbook
787+
tier: frontend
788+
replicas: 3
789+
template:
790+
metadata:
791+
labels:
792+
app: guestbook
793+
tier: frontend
794+
spec:
795+
containers:
796+
- name: php-redis
797+
image: gcr.io/google-samples/gb-frontend:v4`,
798+
expectedObject: nil,
799+
error: true,
800+
errorString: "error occures in mutate function",
801+
fn: func(m map[string]interface{}) error {
802+
return fmt.Errorf("error occures in mutate function")
803+
},
804+
},
805+
}
806+
for _, tt := range tests {
807+
t.Run(tt.name, func(t *testing.T) {
808+
ctx := context.Background()
809+
objects, err := ParseObjects(ctx, tt.inputManifest)
810+
if err != nil {
811+
t.Fatalf("unexpected err: %v", err)
812+
}
813+
if tt.error == false {
814+
for _, o := range objects.Items {
815+
err = o.MutatePodSpec(func(m map[string]interface{}) error {
816+
return nil
817+
})
818+
actualBytes, _ := o.JSON()
819+
actualStr := string(actualBytes)
820+
821+
expectedBytes, _ := tt.expectedObject[0].JSON()
822+
expectedStr := string(expectedBytes)
823+
824+
if expectedStr != actualStr {
825+
t.Fatalf("unexpected result, expected ========\n%v\n\nactual ========\n%v\n", expectedStr, actualStr)
826+
}
827+
}
828+
} else {
829+
if tt.inputManifest == "" {
830+
o, _ := NewObject(&unstructured.Unstructured{})
831+
err = o.MutatePodSpec(tt.fn)
832+
assert.EqualError(t, err, tt.errorString)
833+
} else {
834+
for _, o := range objects.Items {
835+
err = o.MutatePodSpec(tt.fn)
836+
assert.EqualError(t, err, tt.errorString)
837+
}
838+
}
839+
}
840+
841+
})
842+
}
843+
}

0 commit comments

Comments
 (0)