@@ -286,10 +286,11 @@ func TestSanitizeCheckpointPod(t *testing.T) {
286286 pod * v1.Pod
287287 expected * v1.Pod
288288 }
289+ trueVar := true
289290
290291 cases := []testCase {
291292 {
292- desc : "Pod name and namespace are preserved & checkpoint annotation added" ,
293+ desc : "Pod name and namespace are preserved, checkpoint annotation added, owner points to parent " ,
293294 pod : & v1.Pod {
294295 ObjectMeta : metav1.ObjectMeta {
295296 Name : "podname" ,
@@ -298,14 +299,15 @@ func TestSanitizeCheckpointPod(t *testing.T) {
298299 },
299300 expected : & v1.Pod {
300301 ObjectMeta : metav1.ObjectMeta {
301- Name : "podname" ,
302- Namespace : "podnamespace" ,
303- Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
302+ Name : "podname" ,
303+ Namespace : "podnamespace" ,
304+ Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
305+ OwnerReferences : []metav1.OwnerReference {{Name : "podname" , Controller : & trueVar }},
304306 },
305307 },
306308 },
307309 {
308- desc : "Existing annotations are removed, checkpoint annotation added" ,
310+ desc : "Existing annotations are removed, checkpoint annotation added, owner points to parent " ,
309311 pod : & v1.Pod {
310312 ObjectMeta : metav1.ObjectMeta {
311313 Name : "podname" ,
@@ -315,9 +317,10 @@ func TestSanitizeCheckpointPod(t *testing.T) {
315317 },
316318 expected : & v1.Pod {
317319 ObjectMeta : metav1.ObjectMeta {
318- Name : "podname" ,
319- Namespace : "podnamespace" ,
320- Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
320+ Name : "podname" ,
321+ Namespace : "podnamespace" ,
322+ Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
323+ OwnerReferences : []metav1.OwnerReference {{Name : "podname" , Controller : & trueVar }},
321324 },
322325 },
323326 },
@@ -332,9 +335,10 @@ func TestSanitizeCheckpointPod(t *testing.T) {
332335 },
333336 expected : & v1.Pod {
334337 ObjectMeta : metav1.ObjectMeta {
335- Name : "podname" ,
336- Namespace : "podnamespace" ,
337- Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
338+ Name : "podname" ,
339+ Namespace : "podnamespace" ,
340+ Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
341+ OwnerReferences : []metav1.OwnerReference {{Name : "podname" , Controller : & trueVar }},
338342 },
339343 },
340344 },
@@ -348,10 +352,61 @@ func TestSanitizeCheckpointPod(t *testing.T) {
348352 Spec : v1.PodSpec {ServiceAccountName : "foo" , DeprecatedServiceAccount : "bar" },
349353 },
350354 expected : & v1.Pod {
355+ ObjectMeta : metav1.ObjectMeta {
356+ Name : "podname" ,
357+ Namespace : "podnamespace" ,
358+ Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
359+ OwnerReferences : []metav1.OwnerReference {{Name : "podname" , Controller : & trueVar }},
360+ },
361+ },
362+ },
363+ {
364+ desc : "Labels are preserved" ,
365+ pod : & v1.Pod {
366+ ObjectMeta : metav1.ObjectMeta {
367+ Name : "podname" ,
368+ Namespace : "podnamespace" ,
369+ Labels : map [string ]string {"foo" : "bar" },
370+ },
371+ },
372+ expected : & v1.Pod {
373+ ObjectMeta : metav1.ObjectMeta {
374+ Name : "podname" ,
375+ Namespace : "podnamespace" ,
376+ Labels : map [string ]string {"foo" : "bar" },
377+ Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
378+ OwnerReferences : []metav1.OwnerReference {{Name : "podname" , Controller : & trueVar }},
379+ },
380+ },
381+ },
382+ {
383+ desc : "OwnerReference of checkpoint points to parent pod" ,
384+ pod : & v1.Pod {
385+ TypeMeta : metav1.TypeMeta {
386+ APIVersion : "v1" ,
387+ Kind : "Pod" ,
388+ },
389+ ObjectMeta : metav1.ObjectMeta {
390+ Name : "podname" ,
391+ Namespace : "podnamespace" ,
392+ UID : "pod-uid" ,
393+ OwnerReferences : []metav1.OwnerReference {
394+ {APIVersion : "v1" , Kind : "Daemonset" , Name : "daemonname" , UID : "daemon-uid" },
395+ },
396+ },
397+ },
398+ expected : & v1.Pod {
399+ TypeMeta : metav1.TypeMeta {
400+ APIVersion : "v1" ,
401+ Kind : "Pod" ,
402+ },
351403 ObjectMeta : metav1.ObjectMeta {
352404 Name : "podname" ,
353405 Namespace : "podnamespace" ,
354406 Annotations : map [string ]string {checkpointParentAnnotation : "podname" },
407+ OwnerReferences : []metav1.OwnerReference {
408+ {APIVersion : "v1" , Kind : "Pod" , Name : "podname" , UID : "pod-uid" , Controller : & trueVar },
409+ },
355410 },
356411 },
357412 },
@@ -360,12 +415,13 @@ func TestSanitizeCheckpointPod(t *testing.T) {
360415 for _ , tc := range cases {
361416 got , err := sanitizeCheckpointPod (tc .pod )
362417 if err != nil {
363- t .Errorf ("Unexpected error: %v" , err )
418+ t .Errorf ("\n Unexpected error: %v\n " , err )
364419 }
365420 if ! api .Semantic .DeepEqual (tc .expected , got ) {
366- t .Errorf ("For Test: %s\n \n Expected:\n %+ v\n Got:\n %+ v\n " , tc .desc , tc .expected , got )
421+ t .Errorf ("\n For Test: %s\n \n Expected:\n %# v\n Got:\n %# v\n " , tc .desc , tc .expected , got )
367422 }
368423 }
424+
369425}
370426
371427func TestPodListToParentPods (t * testing.T ) {
@@ -603,7 +659,7 @@ func TestCopyPod(t *testing.T) {
603659 t .Errorf ("Unexpected error: %v" , err )
604660 }
605661 if ! api .Semantic .DeepEqual (pod , * got ) {
606- t .Errorf ("Expected:\n %+ v\n Got:\n %+ v" , pod , got )
662+ t .Errorf ("Expected:\n %# v\n Got:\n %# v" , pod , got )
607663 }
608664}
609665
0 commit comments