@@ -17,6 +17,7 @@ limitations under the License.
1717package controller
1818
1919import (
20+ "context"
2021 "testing"
2122 "time"
2223
@@ -536,7 +537,7 @@ func TestProviderConfigSecretChanges(t *testing.T) {
536537 expectSameHash bool
537538 }{
538539 {
539- name : "With the same configmap data, the hash annotation doesn't change" ,
540+ name : "With the same config secret data, the hash annotation doesn't change" ,
540541 cmData : map [string ][]byte {
541542 "some-key" : []byte ("some data" ),
542543 "another-key" : []byte ("another data" ),
@@ -548,7 +549,7 @@ func TestProviderConfigSecretChanges(t *testing.T) {
548549 expectSameHash : true ,
549550 },
550551 {
551- name : "With the same configmap data, the hash annotation doesn't change " ,
552+ name : "With different config secret data, the hash annotation changes " ,
552553 cmData : map [string ][]byte {
553554 "some-key" : []byte ("some data" ),
554555 "another-key" : []byte ("another data" ),
@@ -569,18 +570,6 @@ func TestProviderConfigSecretChanges(t *testing.T) {
569570 g .Expect (env .CleanupAndWait (ctx , objs ... )).To (Succeed ())
570571 }()
571572
572- dataHash , err := calculateHash (tc .cmData )
573- g .Expect (err ).ToNot (HaveOccurred ())
574-
575- updatedDataHash , err := calculateHash (tc .updatedCMData )
576- g .Expect (err ).ToNot (HaveOccurred ())
577-
578- if tc .expectSameHash {
579- g .Expect (updatedDataHash ).To (Equal (dataHash ))
580- } else {
581- g .Expect (updatedDataHash ).ToNot (Equal (dataHash ))
582- }
583-
584573 cmSecretName := "test-config"
585574
586575 provider := & operatorv1.CoreProvider {
@@ -630,11 +619,14 @@ func TestProviderConfigSecretChanges(t *testing.T) {
630619 g .Expect (env .CreateAndWait (ctx , secret .DeepCopy ())).To (Succeed ())
631620 objs = append (objs , secret )
632621
622+ initialHash , err := calculateHash (ctx , env .Client , provider )
623+ g .Expect (err ).ToNot (HaveOccurred ())
624+
633625 t .Log ("creating test provider" , provider .GetName ())
634626 g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
635627 objs = append (objs , provider )
636628
637- g .Eventually (generateExpectedResultChecker (provider , appliedConfigHashAnnotation , dataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
629+ g .Eventually (generateExpectedResultChecker (provider , initialHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
638630
639631 g .Eventually (func () error {
640632 if err := env .Client .Get (ctx , client .ObjectKeyFromObject (secret ), secret ); err != nil {
@@ -647,6 +639,21 @@ func TestProviderConfigSecretChanges(t *testing.T) {
647639 return env .Client .Update (ctx , secret )
648640 }).Should (Succeed ())
649641
642+ var updatedDataHash string
643+ if tc .expectSameHash {
644+ g .Eventually (func () string {
645+ updatedDataHash , err = calculateHash (ctx , env .Client , provider )
646+ g .Expect (err ).ToNot (HaveOccurred ())
647+ return updatedDataHash
648+ }, 15 * time .Second ).Should (Equal (initialHash ))
649+ } else {
650+ g .Eventually (func () string {
651+ updatedDataHash , err = calculateHash (ctx , env .Client , provider )
652+ g .Expect (err ).ToNot (HaveOccurred ())
653+ return updatedDataHash
654+ }, 15 * time .Second ).ShouldNot (Equal (initialHash ))
655+ }
656+
650657 g .Eventually (func () error {
651658 if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
652659 return err
@@ -664,7 +671,7 @@ func TestProviderConfigSecretChanges(t *testing.T) {
664671 return env .Client .Update (ctx , provider )
665672 }).Should (Succeed ())
666673
667- g .Eventually (generateExpectedResultChecker (provider , appliedConfigHashAnnotation , updatedDataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
674+ g .Eventually (generateExpectedResultChecker (provider , updatedDataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
668675 })
669676 }
670677}
@@ -758,12 +765,6 @@ func TestProviderSpecChanges(t *testing.T) {
758765 t .Run (tc .name , func (t * testing.T ) {
759766 g := NewWithT (t )
760767
761- specHash , err := calculateHash (tc .spec )
762- g .Expect (err ).ToNot (HaveOccurred ())
763-
764- updatedSpecHash , err := calculateHash (tc .updatedSpec )
765- g .Expect (err ).ToNot (HaveOccurred ())
766-
767768 provider := & operatorv1.CoreProvider {
768769 ObjectMeta : metav1.ObjectMeta {
769770 Name : "cluster-api" ,
@@ -773,6 +774,15 @@ func TestProviderSpecChanges(t *testing.T) {
773774 },
774775 }
775776
777+ updatedProvider := provider .DeepCopy ()
778+ updatedProvider .SetSpec (tc .updatedSpec )
779+
780+ specHash , err := calculateHash (context .Background (), env .Client , provider )
781+ g .Expect (err ).ToNot (HaveOccurred ())
782+
783+ updatedSpecHash , err := calculateHash (context .Background (), env .Client , updatedProvider )
784+ g .Expect (err ).ToNot (HaveOccurred ())
785+
776786 namespace := "test-provider-spec-changes"
777787
778788 ns , err := env .CreateNamespace (ctx , namespace )
@@ -785,7 +795,7 @@ func TestProviderSpecChanges(t *testing.T) {
785795 t .Log ("creating test provider" , provider .GetName ())
786796 g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
787797
788- g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , specHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
798+ g .Eventually (generateExpectedResultChecker (provider , specHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
789799
790800 g .Eventually (func () error {
791801 if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
@@ -808,9 +818,9 @@ func TestProviderSpecChanges(t *testing.T) {
808818 }).Should (Succeed ())
809819
810820 if ! tc .expectError {
811- g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , updatedSpecHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
821+ g .Eventually (generateExpectedResultChecker (provider , updatedSpecHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
812822 } else {
813- g .Eventually (generateExpectedResultChecker (provider , appliedSpecHashAnnotation , "" , corev1 .ConditionFalse ), timeout ).Should (BeEquivalentTo (true ))
823+ g .Eventually (generateExpectedResultChecker (provider , "" , corev1 .ConditionFalse ), timeout ).Should (BeEquivalentTo (true ))
814824 }
815825
816826 // Clean up
@@ -819,14 +829,14 @@ func TestProviderSpecChanges(t *testing.T) {
819829 }
820830}
821831
822- func generateExpectedResultChecker (provider genericprovider.GenericProvider , hashKey , specHash string , condStatus corev1.ConditionStatus ) func () bool {
832+ func generateExpectedResultChecker (provider genericprovider.GenericProvider , specHash string , condStatus corev1.ConditionStatus ) func () bool {
823833 return func () bool {
824834 if err := env .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
825835 return false
826836 }
827837
828838 // In case of error we don't want the spec annotation to be updated
829- if provider .GetAnnotations ()[hashKey ] != specHash {
839+ if provider .GetAnnotations ()[appliedSpecHashAnnotation ] != specHash {
830840 return false
831841 }
832842
0 commit comments