@@ -97,6 +97,114 @@ func TestCompareContainerVolumeMounts(t *testing.T) {
9797 })
9898}
9999
100+ func TestCompareAnnotations (t * testing.T ) {
101+ type testCase struct {
102+ name string
103+ jenkinsAnnotations map [string ]string
104+ ignoredAnnotations []string
105+ podAnnotations map [string ]string
106+ expectedShouldMatch bool
107+ }
108+
109+ runTest := func (t * testing.T , tc testCase ) {
110+
111+ t .Run (tc .name , func (t * testing.T ) {
112+ jenkins := & v1alpha2.Jenkins {
113+ Spec : v1alpha2.JenkinsSpec {
114+ Master : v1alpha2.JenkinsMaster {
115+ Annotations : tc .jenkinsAnnotations ,
116+ },
117+ },
118+ }
119+
120+ if len (tc .ignoredAnnotations ) > 0 {
121+ jenkins .Spec .Lifecycle = v1alpha2.JenkinsLifecycle {
122+ Ignore : v1alpha2.JenkinsLifecycleIgnore {
123+ IgnoredAnnotations : tc .ignoredAnnotations ,
124+ },
125+ }
126+ }
127+
128+ pod := corev1.Pod {
129+ ObjectMeta : metav1.ObjectMeta {
130+ Annotations : tc .podAnnotations ,
131+ },
132+ }
133+
134+ reconciler := New (configuration.Configuration {Jenkins : jenkins }, client.JenkinsAPIConnectionSettings {})
135+ result := reconciler .compareAnnotations (pod )
136+
137+ assert .Equal (t , tc .expectedShouldMatch , result ,
138+ "Expected compareAnnotations to return %v but got %v" , tc .expectedShouldMatch , result )
139+ })
140+ }
141+
142+ testCases := []testCase {
143+ {
144+ name : "no annotation - additional annotations - not different" ,
145+ jenkinsAnnotations : map [string ]string {},
146+ ignoredAnnotations : nil ,
147+ podAnnotations : map [string ]string {"one" : "two" },
148+ expectedShouldMatch : true ,
149+ },
150+ {
151+ name : "one additional annotation - change not ignored - not different" ,
152+ jenkinsAnnotations : map [string ]string {"one" : "two" },
153+ ignoredAnnotations : nil ,
154+ podAnnotations : map [string ]string {"one" : "two" , "additional" : "annotation" },
155+ expectedShouldMatch : true ,
156+ },
157+ {
158+
159+ name : "annotations different - different" ,
160+ jenkinsAnnotations : map [string ]string {"one" : "two" },
161+ ignoredAnnotations : nil ,
162+ podAnnotations : map [string ]string {"two" : "three" },
163+ expectedShouldMatch : false ,
164+ },
165+
166+ {
167+ name : "annotations different - ignored - not different" ,
168+ jenkinsAnnotations : map [string ]string {"one" : "two" },
169+ ignoredAnnotations : []string {"one" },
170+ podAnnotations : map [string ]string {"two" : "three" },
171+ expectedShouldMatch : true ,
172+ },
173+ {
174+ name : "one annotation different - change not ignored - different" ,
175+ jenkinsAnnotations : map [string ]string {"one" : "two" },
176+ ignoredAnnotations : nil ,
177+ podAnnotations : map [string ]string {"one" : "different" },
178+ expectedShouldMatch : false ,
179+ },
180+ {
181+ name : "one annotation different - change ignored - not different" ,
182+ jenkinsAnnotations : map [string ]string {"one" : "two" },
183+ ignoredAnnotations : []string {"one" },
184+ podAnnotations : map [string ]string {"one" : "different" },
185+ expectedShouldMatch : true ,
186+ },
187+ {
188+ name : "one additional annotation - different" ,
189+ jenkinsAnnotations : map [string ]string {"one" : "two" , "ignore" : "me" },
190+ ignoredAnnotations : nil ,
191+ podAnnotations : map [string ]string {"one" : "two" , "ignore" : "this" },
192+ expectedShouldMatch : false ,
193+ },
194+ {
195+ name : "one additional annotation - change ignored - not different" ,
196+ jenkinsAnnotations : map [string ]string {"one" : "two" , "ignore" : "me" },
197+ ignoredAnnotations : []string {"ignore" },
198+ podAnnotations : map [string ]string {"one" : "two" , "ignore" : "this" },
199+ expectedShouldMatch : true ,
200+ },
201+ }
202+
203+ for _ , tc := range testCases {
204+ runTest (t , tc )
205+ }
206+ }
207+
100208func TestCompareVolumes (t * testing.T ) {
101209 t .Run ("defaults" , func (t * testing.T ) {
102210 jenkins := & v1alpha2.Jenkins {}
0 commit comments