@@ -424,6 +424,142 @@ releaseSeries:
424
424
}
425
425
}
426
426
427
+ func TestProviderConfigSecretChanges (t * testing.T ) {
428
+ testCases := []struct {
429
+ name string
430
+ cmData map [string ][]byte
431
+ updatedCMData map [string ][]byte
432
+ expectSameHash bool
433
+ }{
434
+ {
435
+ name : "With the same configmap data, the hash annotation doesn't change" ,
436
+ cmData : map [string ][]byte {
437
+ "some-key" : []byte ("some data" ),
438
+ "another-key" : []byte ("another data" ),
439
+ },
440
+ updatedCMData : map [string ][]byte {
441
+ "another-key" : []byte ("another data" ),
442
+ "some-key" : []byte ("some data" ),
443
+ },
444
+ expectSameHash : true ,
445
+ },
446
+ {
447
+ name : "With the same configmap data, the hash annotation doesn't change" ,
448
+ cmData : map [string ][]byte {
449
+ "some-key" : []byte ("some data" ),
450
+ "another-key" : []byte ("another data" ),
451
+ },
452
+ updatedCMData : map [string ][]byte {
453
+ "another-key" : []byte ("another data" ),
454
+ "some-key" : []byte ("some updated data" ),
455
+ },
456
+ expectSameHash : false ,
457
+ },
458
+ }
459
+
460
+ for _ , tc := range testCases {
461
+ t .Run (tc .name , func (t * testing.T ) {
462
+ g := NewWithT (t )
463
+
464
+ dataHash , err := calculateHash (tc .cmData )
465
+ g .Expect (err ).ToNot (HaveOccurred ())
466
+
467
+ updatedDataHash , err := calculateHash (tc .updatedCMData )
468
+ g .Expect (err ).ToNot (HaveOccurred ())
469
+
470
+ if tc .expectSameHash {
471
+ g .Expect (updatedDataHash ).To (Equal (dataHash ))
472
+ } else {
473
+ g .Expect (updatedDataHash ).ToNot (Equal (dataHash ))
474
+ }
475
+
476
+ cmSecretName := "test-config"
477
+
478
+ provider := & operatorv1.CoreProvider {
479
+ ObjectMeta : metav1.ObjectMeta {
480
+ Name : "cluster-api" ,
481
+ },
482
+ Spec : operatorv1.CoreProviderSpec {
483
+ ProviderSpec : operatorv1.ProviderSpec {
484
+ Version : testCurrentVersion ,
485
+ FetchConfig : & operatorv1.FetchConfiguration {
486
+ Selector : & metav1.LabelSelector {
487
+ MatchLabels : map [string ]string {
488
+ "test" : "dummy-config" ,
489
+ },
490
+ },
491
+ },
492
+ ConfigSecret : & operatorv1.SecretReference {
493
+ Name : cmSecretName ,
494
+ },
495
+ },
496
+ },
497
+ }
498
+
499
+ providerNamespace , err := env .CreateNamespace (ctx , "test-provider" )
500
+ t .Log ("Ensure namespace exists" , providerNamespace .Name )
501
+ g .Expect (err ).ToNot (HaveOccurred ())
502
+
503
+ configNamespace , err := env .CreateNamespace (ctx , "test-provider-config-changes" )
504
+ t .Log ("Ensure namespace exists" , configNamespace .Name )
505
+ g .Expect (err ).ToNot (HaveOccurred ())
506
+
507
+ g .Expect (env .CreateAndWait (ctx , dummyConfigMap (providerNamespace .Name , testCurrentVersion ))).To (Succeed ())
508
+
509
+ provider .Namespace = providerNamespace .Name
510
+ provider .Spec .ConfigSecret .Namespace = configNamespace .Name
511
+
512
+ secret := & corev1.Secret {
513
+ ObjectMeta : metav1.ObjectMeta {
514
+ Namespace : configNamespace .Name ,
515
+ Name : cmSecretName ,
516
+ },
517
+ Data : tc .cmData ,
518
+ }
519
+
520
+ g .Expect (env .CreateAndWait (ctx , secret .DeepCopy ())).To (Succeed ())
521
+
522
+ t .Log ("creating test provider" , provider .GetName ())
523
+ g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
524
+
525
+ g .Eventually (generateExpectedResultChecker (provider , appliedConfigHashAnnotation , dataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
526
+
527
+ g .Eventually (func () error {
528
+ if err := env .Client .Get (ctx , client .ObjectKeyFromObject (secret ), secret ); err != nil {
529
+ return err
530
+ }
531
+
532
+ // Change provider config data
533
+ secret .Data = tc .updatedCMData
534
+
535
+ return env .Client .Update (ctx , secret )
536
+ }).Should (Succeed ())
537
+
538
+ g .Eventually (func () error {
539
+ if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
540
+ return err
541
+ }
542
+
543
+ // Set a label to ensure that provider was changed
544
+ labels := provider .GetLabels ()
545
+ if labels == nil {
546
+ labels = map [string ]string {}
547
+ }
548
+ labels ["my-label" ] = "some-value"
549
+ provider .SetLabels (labels )
550
+ provider .SetManagedFields (nil )
551
+
552
+ return env .Client .Update (ctx , provider )
553
+ }).Should (Succeed ())
554
+
555
+ g .Eventually (generateExpectedResultChecker (provider , appliedConfigHashAnnotation , updatedDataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
556
+
557
+ // Clean up
558
+ g .Expect (env .Cleanup (ctx , provider , secret , providerNamespace , configNamespace )).To (Succeed ())
559
+ })
560
+ }
561
+ }
562
+
427
563
func TestProviderSpecChanges (t * testing.T ) {
428
564
testCases := []struct {
429
565
name string
@@ -540,7 +676,7 @@ func TestProviderSpecChanges(t *testing.T) {
540
676
t .Log ("creating test provider" , provider .GetName ())
541
677
g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
542
678
543
- g .Eventually (generateExpectedResultChecker (provider , specHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
679
+ g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , specHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
544
680
545
681
g .Eventually (func () error {
546
682
if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
@@ -563,9 +699,9 @@ func TestProviderSpecChanges(t *testing.T) {
563
699
}).Should (Succeed ())
564
700
565
701
if ! tc .expectError {
566
- g .Eventually (generateExpectedResultChecker (provider , updatedSpecHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
702
+ g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , updatedSpecHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
567
703
} else {
568
- g .Eventually (generateExpectedResultChecker (provider , "" , corev1 .ConditionFalse ), timeout ).Should (BeEquivalentTo (true ))
704
+ g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , "" , corev1 .ConditionFalse ), timeout ).Should (BeEquivalentTo (true ))
569
705
}
570
706
571
707
// Clean up
@@ -574,14 +710,14 @@ func TestProviderSpecChanges(t *testing.T) {
574
710
}
575
711
}
576
712
577
- func generateExpectedResultChecker (provider genericprovider.GenericProvider , specHash string , condStatus corev1.ConditionStatus ) func () bool {
713
+ func generateExpectedResultChecker (provider genericprovider.GenericProvider , hashKey , specHash string , condStatus corev1.ConditionStatus ) func () bool {
578
714
return func () bool {
579
715
if err := env .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
580
716
return false
581
717
}
582
718
583
719
// In case of error we don't want the spec annotation to be updated
584
- if provider .GetAnnotations ()[appliedSpecHashAnnotation ] != specHash {
720
+ if provider .GetAnnotations ()[hashKey ] != specHash {
585
721
return false
586
722
}
587
723
0 commit comments