@@ -288,7 +288,7 @@ func TestRecordManagedAnnotations(t *testing.T) {
288
288
},
289
289
},
290
290
{
291
- name : "object has has annotations" ,
291
+ name : "object has annotations" ,
292
292
object : & unstructured.Unstructured {
293
293
Object : map [string ]interface {}{
294
294
"apiVersion" : "apps/v1" ,
@@ -322,7 +322,7 @@ func TestRecordManagedAnnotations(t *testing.T) {
322
322
},
323
323
},
324
324
{
325
- name : "object has has annotations and labels" ,
325
+ name : "object has annotations and labels" ,
326
326
object : & unstructured.Unstructured {
327
327
Object : map [string ]interface {}{
328
328
"apiVersion" : "apps/v1" ,
@@ -361,6 +361,47 @@ func TestRecordManagedAnnotations(t *testing.T) {
361
361
},
362
362
},
363
363
},
364
+ {
365
+ name : "object has recorded annotations and labels" ,
366
+ object : & unstructured.Unstructured {
367
+ Object : map [string ]interface {}{
368
+ "apiVersion" : "apps/v1" ,
369
+ "kind" : "Deployment" ,
370
+ "metadata" : map [string ]interface {}{
371
+ "name" : "demo-deployment-1" ,
372
+ "annotations" : map [string ]interface {}{
373
+ workv1alpha2 .ManagedAnnotation : "foo,resourcetemplate.karmada.io/managed-annotations,resourcetemplate.karmada.io/managed-labels" ,
374
+ "foo" : "foo" ,
375
+ },
376
+ "labels" : map [string ]interface {}{
377
+ "bar" : "bar" ,
378
+ },
379
+ },
380
+ "spec" : map [string ]interface {}{
381
+ "replicas" : 2 ,
382
+ },
383
+ },
384
+ },
385
+ expected : & unstructured.Unstructured {
386
+ Object : map [string ]interface {}{
387
+ "apiVersion" : "apps/v1" ,
388
+ "kind" : "Deployment" ,
389
+ "metadata" : map [string ]interface {}{
390
+ "name" : "demo-deployment-1" ,
391
+ "annotations" : map [string ]interface {}{
392
+ workv1alpha2 .ManagedAnnotation : "foo,resourcetemplate.karmada.io/managed-annotations,resourcetemplate.karmada.io/managed-labels" ,
393
+ "foo" : "foo" ,
394
+ },
395
+ "labels" : map [string ]interface {}{
396
+ "bar" : "bar" ,
397
+ },
398
+ },
399
+ "spec" : map [string ]interface {}{
400
+ "replicas" : 2 ,
401
+ },
402
+ },
403
+ },
404
+ },
364
405
}
365
406
for _ , tt := range tests {
366
407
t .Run (tt .name , func (t * testing.T ) {
@@ -425,3 +466,223 @@ func TestMergeAnnotation(t *testing.T) {
425
466
})
426
467
}
427
468
}
469
+
470
+ func TestDedupeAndMergeAnnotations (t * testing.T ) {
471
+ tests := []struct {
472
+ name string
473
+ existAnnotation map [string ]string
474
+ newAnnotation map [string ]string
475
+ expectedAnnotation map [string ]string
476
+ }{
477
+ {
478
+ name : "nil both annotation" ,
479
+ existAnnotation : nil ,
480
+ newAnnotation : nil ,
481
+ expectedAnnotation : nil ,
482
+ },
483
+ {
484
+ name : "nil existing annotation" ,
485
+ existAnnotation : nil ,
486
+ newAnnotation : map [string ]string {
487
+ "newAnnotationKey" : "newAnnotationValues" ,
488
+ },
489
+ expectedAnnotation : map [string ]string {
490
+ "newAnnotationKey" : "newAnnotationValues" ,
491
+ },
492
+ },
493
+ {
494
+ name : "nil new annotation" ,
495
+ existAnnotation : map [string ]string {
496
+ "existAnnotationKey" : "existAnnotationValues" ,
497
+ },
498
+ newAnnotation : nil ,
499
+ expectedAnnotation : map [string ]string {
500
+ "existAnnotationKey" : "existAnnotationValues" ,
501
+ },
502
+ },
503
+ {
504
+ name : "same annotation" ,
505
+ existAnnotation : map [string ]string {
506
+ "existAnnotationKey" : "existAnnotationValues" ,
507
+ },
508
+ newAnnotation : map [string ]string {
509
+ "existAnnotationKey" : "existAnnotationValues" ,
510
+ },
511
+ expectedAnnotation : map [string ]string {
512
+ "existAnnotationKey" : "existAnnotationValues" ,
513
+ },
514
+ },
515
+ {
516
+ name : "different annotation" ,
517
+ existAnnotation : map [string ]string {
518
+ "existAnnotationKey" : "existAnnotationValues" ,
519
+ },
520
+ newAnnotation : map [string ]string {
521
+ "newAnnotationKey" : "newAnnotationValues" ,
522
+ },
523
+ expectedAnnotation : map [string ]string {
524
+ "existAnnotationKey" : "existAnnotationValues" ,
525
+ "newAnnotationKey" : "newAnnotationValues" ,
526
+ },
527
+ },
528
+ }
529
+ for _ , tt := range tests {
530
+ t .Run (tt .name , func (t * testing.T ) {
531
+ tt .existAnnotation = DedupeAndMergeAnnotations (tt .existAnnotation , tt .newAnnotation )
532
+ if ! reflect .DeepEqual (tt .existAnnotation , tt .expectedAnnotation ) {
533
+ t .Errorf ("DedupeAndMergeAnnotations(), existAnnotation = %v, want %v" , tt .existAnnotation , tt .expectedAnnotation )
534
+ }
535
+ })
536
+ }
537
+ }
538
+
539
+ func TestRemoveAnnotations (t * testing.T ) {
540
+ type args struct {
541
+ obj * unstructured.Unstructured
542
+ key string
543
+ }
544
+ tests := []struct {
545
+ name string
546
+ args args
547
+ expected * unstructured.Unstructured
548
+ }{
549
+ {
550
+ name : "empty key" ,
551
+ args : args {
552
+ obj : & unstructured.Unstructured {
553
+ Object : map [string ]interface {}{
554
+ "apiVersion" : "apps/v1" ,
555
+ "kind" : "Deployment" ,
556
+ "metadata" : map [string ]interface {}{
557
+ "name" : "demo-deployment" ,
558
+ "annotations" : map [string ]interface {}{"foo" : "bar" },
559
+ },
560
+ "spec" : map [string ]interface {}{
561
+ "replicas" : 2 ,
562
+ },
563
+ },
564
+ },
565
+ key : "" ,
566
+ },
567
+ expected : & unstructured.Unstructured {
568
+ Object : map [string ]interface {}{
569
+ "apiVersion" : "apps/v1" ,
570
+ "kind" : "Deployment" ,
571
+ "metadata" : map [string ]interface {}{
572
+ "name" : "demo-deployment" ,
573
+ "annotations" : map [string ]interface {}{"foo" : "bar" },
574
+ },
575
+ "spec" : map [string ]interface {}{
576
+ "replicas" : 2 ,
577
+ },
578
+ },
579
+ },
580
+ },
581
+ {
582
+ name : "nil object annotations" ,
583
+ args : args {
584
+ obj : & unstructured.Unstructured {
585
+ Object : map [string ]interface {}{
586
+ "apiVersion" : "apps/v1" ,
587
+ "kind" : "Deployment" ,
588
+ "metadata" : map [string ]interface {}{
589
+ "name" : "demo-deployment" ,
590
+ },
591
+ "spec" : map [string ]interface {}{
592
+ "replicas" : 2 ,
593
+ },
594
+ },
595
+ },
596
+ key : "foo" ,
597
+ },
598
+ expected : & unstructured.Unstructured {
599
+ Object : map [string ]interface {}{
600
+ "apiVersion" : "apps/v1" ,
601
+ "kind" : "Deployment" ,
602
+ "metadata" : map [string ]interface {}{
603
+ "name" : "demo-deployment" ,
604
+ },
605
+ "spec" : map [string ]interface {}{
606
+ "replicas" : 2 ,
607
+ },
608
+ },
609
+ },
610
+ },
611
+ {
612
+ name : "same key" ,
613
+ args : args {
614
+ obj : & unstructured.Unstructured {
615
+ Object : map [string ]interface {}{
616
+ "apiVersion" : "apps/v1" ,
617
+ "kind" : "Deployment" ,
618
+ "metadata" : map [string ]interface {}{
619
+ "name" : "demo-deployment" ,
620
+ "annotations" : map [string ]interface {}{"foo" : "bar" },
621
+ },
622
+ "spec" : map [string ]interface {}{
623
+ "replicas" : 2 ,
624
+ },
625
+ },
626
+ },
627
+ key : "foo" ,
628
+ },
629
+ expected : & unstructured.Unstructured {
630
+ Object : map [string ]interface {}{
631
+ "apiVersion" : "apps/v1" ,
632
+ "kind" : "Deployment" ,
633
+ "metadata" : map [string ]interface {}{
634
+ "name" : "demo-deployment" ,
635
+ "annotations" : map [string ]interface {}{},
636
+ },
637
+ "spec" : map [string ]interface {}{
638
+ "replicas" : 2 ,
639
+ },
640
+ },
641
+ },
642
+ },
643
+ {
644
+ name : "different key" ,
645
+ args : args {
646
+ obj : & unstructured.Unstructured {
647
+ Object : map [string ]interface {}{
648
+ "apiVersion" : "apps/v1" ,
649
+ "kind" : "Deployment" ,
650
+ "metadata" : map [string ]interface {}{
651
+ "name" : "demo-deployment" ,
652
+ "annotations" : map [string ]interface {}{"foo" : "bar" },
653
+ },
654
+ "spec" : map [string ]interface {}{
655
+ "replicas" : 2 ,
656
+ },
657
+ },
658
+ },
659
+ key : "foo1" ,
660
+ },
661
+ expected : & unstructured.Unstructured {
662
+ Object : map [string ]interface {}{
663
+ "apiVersion" : "apps/v1" ,
664
+ "kind" : "Deployment" ,
665
+ "metadata" : map [string ]interface {}{
666
+ "name" : "demo-deployment" ,
667
+ "annotations" : map [string ]interface {}{"foo" : "bar" },
668
+ },
669
+ "spec" : map [string ]interface {}{
670
+ "replicas" : 2 ,
671
+ },
672
+ },
673
+ },
674
+ },
675
+ }
676
+ for _ , tt := range tests {
677
+ t .Run (tt .name , func (t * testing.T ) {
678
+ if tt .args .key == "" {
679
+ RemoveAnnotations (tt .args .obj )
680
+ } else {
681
+ RemoveAnnotations (tt .args .obj , tt .args .key )
682
+ }
683
+ if ! reflect .DeepEqual (tt .args .obj , tt .expected ) {
684
+ t .Errorf ("RemoveAnnotations(), tt.args.obj = %v, want %v" , tt .args .obj , tt .expected )
685
+ }
686
+ })
687
+ }
688
+ }
0 commit comments