@@ -17,6 +17,7 @@ limitations under the License.
1717package util
1818
1919import (
20+ "reflect"
2021 "testing"
2122
2223 v1 "k8s.io/api/core/v1"
@@ -102,3 +103,249 @@ func TestMergeVolumes(t *testing.T) {
102103 }
103104 }
104105}
106+
107+ func TestSetPodConditionIfMsgChanged (t * testing.T ) {
108+ tests := []struct {
109+ pod * v1.Pod
110+ condition v1.PodCondition
111+ conditions []v1.PodCondition
112+ }{
113+ // case 0: existed condition status changed
114+ {
115+ pod : & v1.Pod {
116+ Status : v1.PodStatus {
117+ Conditions : []v1.PodCondition {
118+ {
119+ Type : "type-0" ,
120+ Status : v1 .ConditionTrue ,
121+ Message : "Msg-0" ,
122+ },
123+ {
124+ Type : "type-1" ,
125+ Status : v1 .ConditionFalse ,
126+ },
127+ },
128+ },
129+ },
130+ condition : v1.PodCondition {
131+ Type : "type-0" ,
132+ Status : v1 .ConditionFalse ,
133+ },
134+ conditions : []v1.PodCondition {
135+ {
136+ Type : "type-0" ,
137+ Status : v1 .ConditionFalse ,
138+ },
139+ {
140+ Type : "type-1" ,
141+ Status : v1 .ConditionFalse ,
142+ },
143+ },
144+ },
145+
146+ // case 1: add a new condition
147+ {
148+ pod : & v1.Pod {
149+ Status : v1.PodStatus {
150+ Conditions : []v1.PodCondition {
151+ {
152+ Type : "type-0" ,
153+ Status : v1 .ConditionTrue ,
154+ Message : "Msg-0" ,
155+ },
156+ {
157+ Type : "type-1" ,
158+ Status : v1 .ConditionFalse ,
159+ },
160+ },
161+ },
162+ },
163+ condition : v1.PodCondition {
164+ Type : "type-2" ,
165+ Status : v1 .ConditionFalse ,
166+ },
167+ conditions : []v1.PodCondition {
168+ {
169+ Type : "type-0" ,
170+ Status : v1 .ConditionTrue ,
171+ Message : "Msg-0" ,
172+ },
173+ {
174+ Type : "type-1" ,
175+ Status : v1 .ConditionFalse ,
176+ },
177+ {
178+ Type : "type-2" ,
179+ Status : v1 .ConditionFalse ,
180+ },
181+ },
182+ },
183+
184+ // case 2: existed condition status not changed, but message changed
185+ {
186+ pod : & v1.Pod {
187+ Status : v1.PodStatus {
188+ Conditions : []v1.PodCondition {
189+ {
190+ Type : "type-0" ,
191+ Status : v1 .ConditionTrue ,
192+ Message : "Msg-0" ,
193+ },
194+ {
195+ Type : "type-1" ,
196+ Status : v1 .ConditionFalse ,
197+ },
198+ },
199+ },
200+ },
201+ condition : v1.PodCondition {
202+ Type : "type-0" ,
203+ Status : v1 .ConditionTrue ,
204+ Message : "Msg-Changed" ,
205+ },
206+ conditions : []v1.PodCondition {
207+ {
208+ Type : "type-0" ,
209+ Status : v1 .ConditionTrue ,
210+ Message : "Msg-Changed" ,
211+ },
212+ {
213+ Type : "type-1" ,
214+ Status : v1 .ConditionFalse ,
215+ },
216+ },
217+ },
218+ }
219+
220+ for i , test := range tests {
221+ expect := test .conditions
222+ SetPodConditionIfMsgChanged (test .pod , test .condition )
223+ actual := test .pod .Status .Conditions
224+ if ! reflect .DeepEqual (expect , actual ) {
225+ t .Fatalf ("case %d: expect Conditions(%s), but get %s" , i , expect , actual )
226+ }
227+ }
228+ }
229+
230+ func TestSetPodCondition (t * testing.T ) {
231+ tests := []struct {
232+ pod * v1.Pod
233+ condition v1.PodCondition
234+ conditions []v1.PodCondition
235+ }{
236+ // case 0: existed condition status changed
237+ {
238+ pod : & v1.Pod {
239+ Status : v1.PodStatus {
240+ Conditions : []v1.PodCondition {
241+ {
242+ Type : "type-0" ,
243+ Status : v1 .ConditionTrue ,
244+ Message : "Msg-0" ,
245+ },
246+ {
247+ Type : "type-1" ,
248+ Status : v1 .ConditionFalse ,
249+ },
250+ },
251+ },
252+ },
253+ condition : v1.PodCondition {
254+ Type : "type-0" ,
255+ Status : v1 .ConditionFalse ,
256+ },
257+ conditions : []v1.PodCondition {
258+ {
259+ Type : "type-0" ,
260+ Status : v1 .ConditionFalse ,
261+ },
262+ {
263+ Type : "type-1" ,
264+ Status : v1 .ConditionFalse ,
265+ },
266+ },
267+ },
268+
269+ // case 1: add a new condition
270+ {
271+ pod : & v1.Pod {
272+ Status : v1.PodStatus {
273+ Conditions : []v1.PodCondition {
274+ {
275+ Type : "type-0" ,
276+ Status : v1 .ConditionTrue ,
277+ Message : "Msg-0" ,
278+ },
279+ {
280+ Type : "type-1" ,
281+ Status : v1 .ConditionFalse ,
282+ },
283+ },
284+ },
285+ },
286+ condition : v1.PodCondition {
287+ Type : "type-2" ,
288+ Status : v1 .ConditionFalse ,
289+ },
290+ conditions : []v1.PodCondition {
291+ {
292+ Type : "type-0" ,
293+ Status : v1 .ConditionTrue ,
294+ Message : "Msg-0" ,
295+ },
296+ {
297+ Type : "type-1" ,
298+ Status : v1 .ConditionFalse ,
299+ },
300+ {
301+ Type : "type-2" ,
302+ Status : v1 .ConditionFalse ,
303+ },
304+ },
305+ },
306+
307+ // case 2: existed condition status not changed, message should not be changed
308+ {
309+ pod : & v1.Pod {
310+ Status : v1.PodStatus {
311+ Conditions : []v1.PodCondition {
312+ {
313+ Type : "type-0" ,
314+ Status : v1 .ConditionTrue ,
315+ Message : "Msg-0" ,
316+ },
317+ {
318+ Type : "type-1" ,
319+ Status : v1 .ConditionFalse ,
320+ },
321+ },
322+ },
323+ },
324+ condition : v1.PodCondition {
325+ Type : "type-0" ,
326+ Status : v1 .ConditionTrue ,
327+ Message : "Msg-Changed" ,
328+ },
329+ conditions : []v1.PodCondition {
330+ {
331+ Type : "type-0" ,
332+ Status : v1 .ConditionTrue ,
333+ Message : "Msg-0" ,
334+ },
335+ {
336+ Type : "type-1" ,
337+ Status : v1 .ConditionFalse ,
338+ },
339+ },
340+ },
341+ }
342+
343+ for i , test := range tests {
344+ expect := test .conditions
345+ SetPodCondition (test .pod , test .condition )
346+ actual := test .pod .Status .Conditions
347+ if ! reflect .DeepEqual (expect , actual ) {
348+ t .Fatalf ("case %d: expect Conditions(%s), but get %s" , i , expect , actual )
349+ }
350+ }
351+ }
0 commit comments