@@ -17,7 +17,6 @@ limitations under the License.
17
17
package controller
18
18
19
19
import (
20
- "context"
21
20
"testing"
22
21
"time"
23
22
@@ -745,14 +744,13 @@ func TestProviderConfigSecretChanges(t *testing.T) {
745
744
g .Expect (env .CreateAndWait (ctx , secret .DeepCopy ())).To (Succeed ())
746
745
objs = append (objs , secret )
747
746
748
- initialHash , err := calculateHash (ctx , env .Client , provider )
749
- g .Expect (err ).ToNot (HaveOccurred ())
750
-
751
747
t .Log ("creating test provider" , provider .GetName ())
752
748
g .Expect (env .CreateAndWait (ctx , provider .DeepCopy ())).To (Succeed ())
753
749
objs = append (objs , provider )
754
750
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 ]
756
754
757
755
g .Eventually (func () error {
758
756
if err := env .Client .Get (ctx , client .ObjectKeyFromObject (secret ), secret ); err != nil {
@@ -765,52 +763,32 @@ func TestProviderConfigSecretChanges(t *testing.T) {
765
763
return env .Client .Update (ctx , secret )
766
764
}).Should (Succeed ())
767
765
768
- var updatedDataHash string
769
-
770
766
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 ())
774
770
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 ( ))
777
773
} 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 ())
781
777
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 ( ))
784
780
}
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 ))
804
781
})
805
782
}
806
783
}
807
784
808
785
func TestProviderSpecChanges (t * testing.T ) {
809
786
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
814
792
}{
815
793
{
816
794
name : "same spec, hash annotation doesn't change" ,
@@ -860,9 +838,10 @@ func TestProviderSpecChanges(t *testing.T) {
860
838
},
861
839
},
862
840
},
841
+ expectHashChange : true ,
863
842
},
864
843
{
865
- name : "upgrade to a non-existent version, hash annotation is empty " ,
844
+ name : "upgrade to a non-existent version, hash annotation unchanged " ,
866
845
expectError : true ,
867
846
spec : operatorv1.ProviderSpec {
868
847
Version : testCurrentVersion ,
@@ -906,12 +885,6 @@ func TestProviderSpecChanges(t *testing.T) {
906
885
updatedProvider := provider .DeepCopy ()
907
886
updatedProvider .SetSpec (tc .updatedSpec )
908
887
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
-
915
888
namespace := "test-provider-spec-changes"
916
889
917
890
ns , err := env .CreateNamespace (ctx , namespace )
@@ -929,7 +902,11 @@ func TestProviderSpecChanges(t *testing.T) {
929
902
g .Expect (env .Cleanup (ctx , provider , dummyConfigMap (namespace ))).To (Succeed ())
930
903
}()
931
904
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 ]
933
910
934
911
g .Eventually (func () error {
935
912
if err := env .Client .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
@@ -952,22 +929,28 @@ func TestProviderSpecChanges(t *testing.T) {
952
929
}).Should (Succeed ())
953
930
954
931
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 ))
956
939
} 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 ))
958
941
}
959
942
})
960
943
}
961
944
}
962
945
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 {
964
947
return func () bool {
965
948
if err := env .Get (ctx , client .ObjectKeyFromObject (provider ), provider ); err != nil {
966
949
return false
967
950
}
968
951
969
952
// 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 ]) {
971
954
return false
972
955
}
973
956
0 commit comments