@@ -17,7 +17,6 @@ limitations under the License.
1717package controller
1818
1919import (
20- "context"
2120 "testing"
2221 "time"
2322
@@ -745,14 +744,13 @@ func TestProviderConfigSecretChanges(t *testing.T) {
745744 g .Expect (env .CreateAndWait (ctx , secret .DeepCopy ())).To (Succeed ())
746745 objs = append (objs , secret )
747746
748- initialHash , err := calculateHash (ctx , env .Client , provider )
749- g .Expect (err ).ToNot (HaveOccurred ())
750-
751747 t .Log ("creating test provider" , provider .GetName ())
752748 g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
753749 objs = append (objs , provider )
754750
755- g .Eventually (generateExpectedResultChecker (provider , initialHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
751+ g .Eventually (generateExpectedResultChecker (provider , corev1 .ConditionTrue , func (s string ) bool { return s != "" }), timeout ).Should (BeEquivalentTo (true ))
752+
753+ initialHash := provider .GetAnnotations ()[appliedSpecHashAnnotation ]
756754
757755 g .Eventually (func () error {
758756 if err := env .Client .Get (ctx , client .ObjectKeyFromObject (secret ), secret ); err != nil {
@@ -765,52 +763,32 @@ func TestProviderConfigSecretChanges(t *testing.T) {
765763 return env .Client .Update (ctx , secret )
766764 }).Should (Succeed ())
767765
768- var updatedDataHash string
769-
770766 if tc .expectSameHash {
771- g .Eventually (func () string {
772- updatedDataHash , err = calculateHash (ctx , env . Client , provider )
773- g .Expect (err ).ToNot (HaveOccurred ())
767+ g .Consistently (func (g Gomega ) {
768+ err := env . Client . Get (ctx , client . ObjectKeyFromObject ( provider ) , provider )
769+ g .Expect (client . IgnoreNotFound ( err ) ).ToNot (HaveOccurred ())
774770
775- return updatedDataHash
776- }, 15 * time .Second ).Should (Equal ( initialHash ))
771+ g . Expect ( provider . GetAnnotations ()[ appliedSpecHashAnnotation ]). To ( Equal ( initialHash ))
772+ }, 15 * time .Second ).Should (Succeed ( ))
777773 } else {
778- g .Eventually (func () string {
779- updatedDataHash , err = calculateHash (ctx , env . Client , provider )
780- g .Expect (err ).ToNot (HaveOccurred ())
774+ g .Eventually (func (g Gomega ) {
775+ err := env . Client . Get (ctx , client . ObjectKeyFromObject ( provider ) , provider )
776+ g .Expect (client . IgnoreNotFound ( err ) ).ToNot (HaveOccurred ())
781777
782- return updatedDataHash
783- }, 15 * time .Second ).ShouldNot (Equal ( initialHash ))
778+ g . Expect ( provider . GetAnnotations ()[ appliedSpecHashAnnotation ]). ToNot ( Equal ( initialHash ))
779+ }, 15 * time .Second ).ShouldNot (Succeed ( ))
784780 }
785-
786- g .Eventually (func () error {
787- if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
788- return err
789- }
790-
791- // Set a label to ensure that provider was changed
792- labels := provider .GetLabels ()
793- if labels == nil {
794- labels = map [string ]string {}
795- }
796- labels ["my-label" ] = "some-value"
797- provider .SetLabels (labels )
798- provider .SetManagedFields (nil )
799-
800- return env .Client .Update (ctx , provider )
801- }).Should (Succeed ())
802-
803- g .Eventually (generateExpectedResultChecker (provider , updatedDataHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
804781 })
805782 }
806783}
807784
808785func TestProviderSpecChanges (t * testing.T ) {
809786 testCases := []struct {
810- name string
811- spec operatorv1.ProviderSpec
812- updatedSpec operatorv1.ProviderSpec
813- expectError bool
787+ name string
788+ spec operatorv1.ProviderSpec
789+ updatedSpec operatorv1.ProviderSpec
790+ expectHashChange bool
791+ expectError bool
814792 }{
815793 {
816794 name : "same spec, hash annotation doesn't change" ,
@@ -860,9 +838,10 @@ func TestProviderSpecChanges(t *testing.T) {
860838 },
861839 },
862840 },
841+ expectHashChange : true ,
863842 },
864843 {
865- name : "upgrade to a non-existent version, hash annotation is empty " ,
844+ name : "upgrade to a non-existent version, hash annotation unchanged " ,
866845 expectError : true ,
867846 spec : operatorv1.ProviderSpec {
868847 Version : testCurrentVersion ,
@@ -906,12 +885,6 @@ func TestProviderSpecChanges(t *testing.T) {
906885 updatedProvider := provider .DeepCopy ()
907886 updatedProvider .SetSpec (tc .updatedSpec )
908887
909- specHash , err := calculateHash (context .Background (), env .Client , provider )
910- g .Expect (err ).ToNot (HaveOccurred ())
911-
912- updatedSpecHash , err := calculateHash (context .Background (), env .Client , updatedProvider )
913- g .Expect (err ).ToNot (HaveOccurred ())
914-
915888 namespace := "test-provider-spec-changes"
916889
917890 ns , err := env .CreateNamespace (ctx , namespace )
@@ -929,7 +902,11 @@ func TestProviderSpecChanges(t *testing.T) {
929902 g .Expect (env .Cleanup (ctx , provider , dummyConfigMap (namespace ))).To (Succeed ())
930903 }()
931904
932- g .Eventually (generateExpectedResultChecker (provider , specHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
905+ g .Eventually (generateExpectedResultChecker (provider , corev1 .ConditionTrue , func (s string ) bool {
906+ return s != ""
907+ }), timeout ).Should (BeEquivalentTo (true ))
908+
909+ currentHash := provider .GetAnnotations ()[appliedSpecHashAnnotation ]
933910
934911 g .Eventually (func () error {
935912 if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
@@ -952,22 +929,28 @@ func TestProviderSpecChanges(t *testing.T) {
952929 }).Should (Succeed ())
953930
954931 if ! tc .expectError {
955- g .Eventually (generateExpectedResultChecker (provider , updatedSpecHash , corev1 .ConditionTrue ), timeout ).Should (BeEquivalentTo (true ))
932+ g .Eventually (generateExpectedResultChecker (provider , corev1 .ConditionTrue , func (s string ) bool {
933+ if tc .expectHashChange {
934+ return s != currentHash
935+ }
936+
937+ return s == currentHash
938+ }), timeout ).Should (BeEquivalentTo (true ))
956939 } else {
957- g .Eventually (generateExpectedResultChecker (provider , "" , corev1 .ConditionFalse ), timeout ).Should (BeEquivalentTo (true ))
940+ g .Eventually (generateExpectedResultChecker (provider , corev1 .ConditionFalse , func ( s string ) bool { return s == currentHash } ), timeout ).Should (BeEquivalentTo (true ))
958941 }
959942 })
960943 }
961944}
962945
963- func generateExpectedResultChecker (provider genericprovider.GenericProvider , specHash string , condStatus corev1.ConditionStatus ) func () bool {
946+ func generateExpectedResultChecker (provider genericprovider.GenericProvider , condStatus corev1.ConditionStatus , hashCheck func ( string ) bool ) func () bool {
964947 return func () bool {
965948 if err := env .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
966949 return false
967950 }
968951
969952 // In case of error we don't want the spec annotation to be updated
970- if provider .GetAnnotations ()[appliedSpecHashAnnotation ] != specHash {
953+ if ! hashCheck ( provider .GetAnnotations ()[appliedSpecHashAnnotation ]) {
971954 return false
972955 }
973956
0 commit comments