@@ -23,6 +23,7 @@ import (
23
23
"time"
24
24
25
25
. "github.com/onsi/gomega"
26
+ "github.com/pkg/errors"
26
27
admissionregistration "k8s.io/api/admissionregistration/v1"
27
28
appsv1 "k8s.io/api/apps/v1"
28
29
corev1 "k8s.io/api/core/v1"
@@ -808,6 +809,67 @@ func Test_certManagerClient_EnsureLatestVersion(t *testing.T) {
808
809
}
809
810
}
810
811
812
+ func Test_certManagerClient_EnsureInstalled (t * testing.T ) {
813
+ tests := []struct {
814
+ name string
815
+ retryReadinessCheck bool
816
+ expectedError error
817
+ expectedCertManagerCalls int
818
+ }{
819
+ {
820
+ name : "Retries checking for existing cert-manager" ,
821
+ retryReadinessCheck : true ,
822
+ // because of current EnsureInstalled logic, where the code to check for existing cert-manager
823
+ // and code to do a new installation if that check failed call the same methods/functions
824
+ // (or those methods/functions aren't really mockable from unit test POV), counting the number
825
+ // of calls to cm.configClient.CertManager() seems like a reasonable way to differntiate between paths
826
+ // and test the retry logic
827
+ expectedCertManagerCalls : 1 ,
828
+ },
829
+ {
830
+ name : "Checks for existing cert-manager only once" ,
831
+ retryReadinessCheck : false ,
832
+ expectedCertManagerCalls : 3 ,
833
+ },
834
+ }
835
+
836
+ for _ , tt := range tests {
837
+ t .Run (tt .name , func (t * testing.T ) {
838
+ g := NewWithT (t )
839
+
840
+ fakeConfigClient := newFakeConfig ()
841
+ // make the proxy NewClient() calls return two errors on two initial calls, then nil's
842
+ proxy := test .NewFakeProxy ().WithNewClientErrors (
843
+ errors .New ("fail1" ),
844
+ errors .New ("fail2" ),
845
+ )
846
+ pollImmediateWaiter := func (ctx context.Context , _ time.Duration , _ time.Duration , f wait.ConditionWithContextFunc ) error {
847
+ // mimic non-test behavior
848
+ return wait .PollUntilContextTimeout (ctx , 0 , 10 * time .Second , true , f )
849
+ }
850
+ repo := repository .NewMemoryRepository ().
851
+ WithPaths ("root" , "components.yaml" ).
852
+ WithDefaultVersion (config .CertManagerDefaultVersion ).
853
+ WithFile (config .CertManagerDefaultVersion , "components.yaml" , certManagerDeploymentYaml )
854
+ repositoryClientFactory := func (ctx context.Context , provider config.Provider , configClient config.Client , _ ... repository.Option ) (repository.Client , error ) {
855
+ return repository .New (ctx , provider , configClient , repository .InjectRepository (repo ))
856
+ }
857
+
858
+ cm := newCertManagerClient (fakeConfigClient , repositoryClientFactory , proxy , pollImmediateWaiter )
859
+
860
+ err := cm .EnsureInstalled (context .TODO (), tt .retryReadinessCheck )
861
+
862
+ if tt .expectedError != nil {
863
+ g .Expect (err ).To (HaveOccurred ())
864
+ g .Expect (err ).To (MatchError (tt .expectedError ))
865
+ } else {
866
+ g .Expect (err ).NotTo (HaveOccurred ())
867
+ }
868
+ g .Expect (fakeConfigClient .certManagerCalls ).To (Equal (tt .expectedCertManagerCalls ))
869
+ })
870
+ }
871
+ }
872
+
811
873
func newFakeConfig () * fakeConfigClient {
812
874
fakeReader := test .NewFakeReader ()
813
875
@@ -821,23 +883,26 @@ func newFakeConfig() *fakeConfigClient {
821
883
type fakeConfigClient struct {
822
884
fakeReader * test.FakeReader
823
885
internalclient config.Client
886
+
887
+ certManagerCalls int
824
888
}
825
889
826
890
var _ config.Client = & fakeConfigClient {}
827
891
828
- func (f fakeConfigClient ) CertManager () config.CertManagerClient {
892
+ func (f * fakeConfigClient ) CertManager () config.CertManagerClient {
893
+ f .certManagerCalls ++
829
894
return f .internalclient .CertManager ()
830
895
}
831
896
832
- func (f fakeConfigClient ) Providers () config.ProvidersClient {
897
+ func (f * fakeConfigClient ) Providers () config.ProvidersClient {
833
898
return f .internalclient .Providers ()
834
899
}
835
900
836
- func (f fakeConfigClient ) Variables () config.VariablesClient {
901
+ func (f * fakeConfigClient ) Variables () config.VariablesClient {
837
902
return f .internalclient .Variables ()
838
903
}
839
904
840
- func (f fakeConfigClient ) ImageMeta () config.ImageMetaClient {
905
+ func (f * fakeConfigClient ) ImageMeta () config.ImageMetaClient {
841
906
return f .internalclient .ImageMeta ()
842
907
}
843
908
0 commit comments