@@ -3,8 +3,10 @@ package resourcemerge
33import (
44 "testing"
55
6+ "github.com/ghodss/yaml"
67 corev1 "k8s.io/api/core/v1"
78 "k8s.io/apimachinery/pkg/api/equality"
9+ "k8s.io/apimachinery/pkg/util/diff"
810 "k8s.io/utils/pointer"
911)
1012
@@ -16,14 +18,98 @@ func TestEnsurePodSpec(t *testing.T) {
1618
1719 expectedModified bool
1820 expected corev1.PodSpec
19- }{{
20- name : "empty inputs" ,
21- existing : corev1.PodSpec {},
22- input : corev1.PodSpec {},
21+ }{
22+ {
23+ name : "empty inputs" ,
24+ existing : corev1.PodSpec {},
25+ input : corev1.PodSpec {},
2326
24- expectedModified : false ,
25- expected : corev1.PodSpec {},
26- }}
27+ expectedModified : false ,
28+ expected : corev1.PodSpec {},
29+ },
30+ {
31+ name : "remove regular containers from existing" ,
32+ existing : corev1.PodSpec {
33+ Containers : []corev1.Container {
34+ corev1.Container {Name : "test" }}},
35+ input : corev1.PodSpec {},
36+
37+ expectedModified : true ,
38+ expected : corev1.PodSpec {},
39+ },
40+ {
41+ name : "remove regular and init containers from existing" ,
42+ existing : corev1.PodSpec {
43+ InitContainers : []corev1.Container {
44+ corev1.Container {Name : "test-init" }},
45+ Containers : []corev1.Container {
46+ corev1.Container {Name : "test" }}},
47+ input : corev1.PodSpec {},
48+
49+ expectedModified : true ,
50+ expected : corev1.PodSpec {},
51+ },
52+ {
53+ name : "remove init containers from existing" ,
54+ existing : corev1.PodSpec {
55+ InitContainers : []corev1.Container {
56+ corev1.Container {Name : "test-init" }}},
57+ input : corev1.PodSpec {},
58+
59+ expectedModified : true ,
60+ expected : corev1.PodSpec {},
61+ },
62+ {
63+ name : "append regular and init containers" ,
64+ existing : corev1.PodSpec {
65+ InitContainers : []corev1.Container {
66+ corev1.Container {Name : "test-init-a" }},
67+ Containers : []corev1.Container {
68+ corev1.Container {Name : "test-a" }}},
69+ input : corev1.PodSpec {
70+ InitContainers : []corev1.Container {
71+ corev1.Container {Name : "test-init-a" },
72+ corev1.Container {Name : "test-init-b" },
73+ },
74+ Containers : []corev1.Container {
75+ corev1.Container {Name : "test-a" },
76+ corev1.Container {Name : "test-b" },
77+ },
78+ },
79+
80+ expectedModified : true ,
81+ expected : corev1.PodSpec {
82+ InitContainers : []corev1.Container {
83+ corev1.Container {Name : "test-init-a" },
84+ corev1.Container {Name : "test-init-b" },
85+ },
86+ Containers : []corev1.Container {
87+ corev1.Container {Name : "test-a" },
88+ corev1.Container {Name : "test-b" },
89+ },
90+ },
91+ },
92+ {
93+ name : "match regular and init containers" ,
94+ existing : corev1.PodSpec {
95+ InitContainers : []corev1.Container {
96+ corev1.Container {Name : "test-init" }},
97+ Containers : []corev1.Container {
98+ corev1.Container {Name : "test" }}},
99+ input : corev1.PodSpec {
100+ InitContainers : []corev1.Container {
101+ corev1.Container {Name : "test-init" }},
102+ Containers : []corev1.Container {
103+ corev1.Container {Name : "test" }}},
104+
105+ expectedModified : false ,
106+ expected : corev1.PodSpec {
107+ InitContainers : []corev1.Container {
108+ corev1.Container {Name : "test-init" }},
109+ Containers : []corev1.Container {
110+ corev1.Container {Name : "test" }}},
111+ },
112+ }
27113
28114 for _ , test := range tests {
29115 t .Run (test .name , func (t * testing.T ) {
@@ -34,8 +120,16 @@ func TestEnsurePodSpec(t *testing.T) {
34120 }
35121
36122 if ! equality .Semantic .DeepEqual (test .existing , test .expected ) {
37- t .Errorf ("mismatch PodSpec got : %v want: %v " , test .existing , test .expected )
123+ t .Errorf ("unexpected : %s " , diff . ObjectReflectDiff ( test .expected , test .existing ) )
38124 }
39125 })
40126 }
41127}
128+
129+ func yamlOrDie (data interface {}) string {
130+ bytes , err := yaml .Marshal (data )
131+ if err != nil {
132+ panic (err )
133+ }
134+ return string (bytes )
135+ }
0 commit comments