@@ -102,80 +102,6 @@ func TestExtractNumOfClustersFromPolicySnapshot(t *testing.T) {
102102 }
103103}
104104
105- func TestExtractSubindexFromClusterResourceSnapshot (t * testing.T ) {
106- testCases := []struct {
107- name string
108- snapshot * fleetv1beta1.ClusterResourceSnapshot
109- wantExist bool
110- wantSubindex int
111- wantError bool
112- }{
113- {
114- name : "valid annotation" ,
115- snapshot : & fleetv1beta1.ClusterResourceSnapshot {
116- ObjectMeta : metav1.ObjectMeta {
117- Name : snapshotName ,
118- Annotations : map [string ]string {
119- fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "1" ,
120- },
121- },
122- },
123- wantExist : true ,
124- wantSubindex : 1 ,
125- },
126- {
127- name : "no annotation" ,
128- snapshot : & fleetv1beta1.ClusterResourceSnapshot {
129- ObjectMeta : metav1.ObjectMeta {
130- Name : snapshotName ,
131- },
132- },
133- wantExist : false ,
134- wantSubindex : - 1 ,
135- },
136- {
137- name : "invalid annotation: not an integer" ,
138- snapshot : & fleetv1beta1.ClusterResourceSnapshot {
139- ObjectMeta : metav1.ObjectMeta {
140- Name : snapshotName ,
141- Annotations : map [string ]string {
142- fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "abc" ,
143- },
144- },
145- },
146- wantError : true ,
147- },
148- {
149- name : "invalid annotation: negative integer" ,
150- snapshot : & fleetv1beta1.ClusterResourceSnapshot {
151- ObjectMeta : metav1.ObjectMeta {
152- Name : snapshotName ,
153- Annotations : map [string ]string {
154- fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "-1" ,
155- },
156- },
157- },
158- wantError : true ,
159- },
160- }
161-
162- for _ , tc := range testCases {
163- t .Run (tc .name , func (t * testing.T ) {
164- gotExist , gotSubindex , err := ExtractSubindexFromClusterResourceSnapshot (tc .snapshot )
165- if tc .wantError {
166- if err == nil {
167- t .Fatalf ("ExtractSubindexFromClusterResourceSnapshot() = %v, %v, want error" , gotExist , gotSubindex )
168- }
169- return
170- }
171-
172- if gotExist != tc .wantExist || gotSubindex != tc .wantSubindex {
173- t .Fatalf ("ExtractSubindexFromClusterResourceSnapshot() = %v, %v, want %v, %v" , gotExist , gotSubindex , tc .wantExist , tc .wantSubindex )
174- }
175- })
176- }
177- }
178-
179105// TestExtractObservedCRPGenerationFromPolicySnapshot tests the ExtractObservedCRPGenerationFromPolicySnapshot function.
180106func TestExtractObservedCRPGenerationFromPolicySnapshot (t * testing.T ) {
181107 testCases := []struct {
@@ -451,3 +377,180 @@ func TestExtractNextResourceSnapshotCandidateDetectionTimeFromResourceSnapshot(t
451377 })
452378 }
453379}
380+ func TestParseResourceGroupHashFromAnnotation (t * testing.T ) {
381+ testCases := []struct {
382+ name string
383+ snapshot * fleetv1beta1.ClusterResourceSnapshot
384+ want string
385+ wantError bool
386+ }{
387+ {
388+ name : "valid annotation" ,
389+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
390+ ObjectMeta : metav1.ObjectMeta {
391+ Name : snapshotName ,
392+ Annotations : map [string ]string {
393+ fleetv1beta1 .ResourceGroupHashAnnotation : "abc123" ,
394+ },
395+ },
396+ },
397+ want : "abc123" ,
398+ },
399+ {
400+ name : "no annotations" ,
401+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
402+ ObjectMeta : metav1.ObjectMeta {
403+ Name : snapshotName ,
404+ },
405+ },
406+ wantError : true ,
407+ },
408+ {
409+ name : "annotation not set" ,
410+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
411+ ObjectMeta : metav1.ObjectMeta {
412+ Name : snapshotName ,
413+ Annotations : map [string ]string {
414+ "other-annotation" : "value" ,
415+ },
416+ },
417+ },
418+ wantError : true ,
419+ },
420+ {
421+ name : "empty annotation value" ,
422+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
423+ ObjectMeta : metav1.ObjectMeta {
424+ Name : snapshotName ,
425+ Annotations : map [string ]string {
426+ fleetv1beta1 .ResourceGroupHashAnnotation : "" ,
427+ },
428+ },
429+ },
430+ want : "" ,
431+ },
432+ }
433+
434+ for _ , tc := range testCases {
435+ t .Run (tc .name , func (t * testing.T ) {
436+ got , err := ParseResourceGroupHashFromAnnotation (tc .snapshot )
437+ if gotErr := err != nil ; gotErr != tc .wantError {
438+ t .Fatalf ("ParseResourceGroupHashFromAnnotation() got err %v, want err %v" , err , tc .wantError )
439+ }
440+ if ! tc .wantError && got != tc .want {
441+ t .Fatalf ("ParseResourceGroupHashFromAnnotation() got %s, want %s" , got , tc .want )
442+ }
443+ })
444+ }
445+ }
446+
447+ func TestExtractSubindexFromResourceSnapshot (t * testing.T ) {
448+ testCases := []struct {
449+ name string
450+ snapshot * fleetv1beta1.ClusterResourceSnapshot
451+ wantExist bool
452+ wantSubindex int
453+ wantError bool
454+ }{
455+ {
456+ name : "valid annotation" ,
457+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
458+ ObjectMeta : metav1.ObjectMeta {
459+ Name : snapshotName ,
460+ Annotations : map [string ]string {
461+ fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "1" ,
462+ },
463+ },
464+ },
465+ wantExist : true ,
466+ wantSubindex : 1 ,
467+ },
468+ {
469+ name : "no annotations" ,
470+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
471+ ObjectMeta : metav1.ObjectMeta {
472+ Name : snapshotName ,
473+ },
474+ },
475+ wantExist : false ,
476+ wantSubindex : 0 ,
477+ },
478+ {
479+ name : "annotation not set" ,
480+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
481+ ObjectMeta : metav1.ObjectMeta {
482+ Name : snapshotName ,
483+ Annotations : map [string ]string {
484+ "other-annotation" : "value" ,
485+ },
486+ },
487+ },
488+ wantExist : false ,
489+ wantSubindex : 0 ,
490+ },
491+ {
492+ name : "empty annotation value" ,
493+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
494+ ObjectMeta : metav1.ObjectMeta {
495+ Name : snapshotName ,
496+ Annotations : map [string ]string {
497+ fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "" ,
498+ },
499+ },
500+ },
501+ wantExist : false ,
502+ wantSubindex : 0 ,
503+ },
504+ {
505+ name : "invalid annotation: not an integer" ,
506+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
507+ ObjectMeta : metav1.ObjectMeta {
508+ Name : snapshotName ,
509+ Annotations : map [string ]string {
510+ fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "abc" ,
511+ },
512+ },
513+ },
514+ wantError : true ,
515+ },
516+ {
517+ name : "invalid annotation: negative integer" ,
518+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
519+ ObjectMeta : metav1.ObjectMeta {
520+ Name : snapshotName ,
521+ Annotations : map [string ]string {
522+ fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "-1" ,
523+ },
524+ },
525+ },
526+ wantError : true ,
527+ },
528+ {
529+ name : "valid annotation with zero value" ,
530+ snapshot : & fleetv1beta1.ClusterResourceSnapshot {
531+ ObjectMeta : metav1.ObjectMeta {
532+ Name : snapshotName ,
533+ Annotations : map [string ]string {
534+ fleetv1beta1 .SubindexOfResourceSnapshotAnnotation : "0" ,
535+ },
536+ },
537+ },
538+ wantExist : true ,
539+ wantSubindex : 0 ,
540+ },
541+ }
542+
543+ for _ , tc := range testCases {
544+ t .Run (tc .name , func (t * testing.T ) {
545+ gotExist , gotSubindex , err := ExtractSubindexFromResourceSnapshot (tc .snapshot )
546+ if gotErr := err != nil ; gotErr != tc .wantError {
547+ t .Fatalf ("ExtractSubindexFromResourceSnapshot() got err %v, want err %v" , err , tc .wantError )
548+ }
549+ if ! tc .wantError {
550+ if gotExist != tc .wantExist || gotSubindex != tc .wantSubindex {
551+ t .Fatalf ("ExtractSubindexFromResourceSnapshot() = %v, %v, want %v, %v" , gotExist , gotSubindex , tc .wantExist , tc .wantSubindex )
552+ }
553+ }
554+ })
555+ }
556+ }
0 commit comments