11package certmanager
22
33import (
4- "strings "
4+ "fmt "
55 "testing"
66
77 appsv1 "k8s.io/api/apps/v1"
@@ -15,24 +15,40 @@ import (
1515 configv1 "github.com/openshift/api/config/v1"
1616 configinformersv1 "github.com/openshift/client-go/config/informers/externalversions/config/v1"
1717 configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
18+
19+ "github.com/openshift/cert-manager-operator/pkg/operator/operatorclient"
1820)
1921
2022const testSecretName = "cloud-creds"
2123
24+ // The following helpers return the full err.Error() the hook produces for each failure mode
25+ // (same NotFound GroupResource/name and fmt.Errorf wording as withCloudCredentials; test-only).
26+ func expectedCloudSecretRetryingNotFound (secretName string ) string {
27+ nf := apierrors .NewNotFound (corev1 .Resource ("secret" ), secretName )
28+ return fmt .Errorf ("(Retrying) cloud secret %q doesn't exist due to %w" , secretName , nf ).Error ()
29+ }
30+
31+ func expectedUnsupportedCloudProvider (platform configv1.PlatformType ) string {
32+ return fmt .Errorf ("unsupported cloud provider %q for mounting cloud credentials secret" , platform ).Error ()
33+ }
34+
35+ func expectedInfrastructureClusterNotFound () string {
36+ return apierrors .NewNotFound (configv1 .Resource ("infrastructure" ), "cluster" ).Error ()
37+ }
38+
2239func TestWithCloudCredentials (t * testing.T ) {
2340 tests := []struct {
24- name string
25- deploymentName string
26- secretName string
27- secretInStore bool
28- decoySecretOnly bool // lister has a different secret name so Get(secretName) fails (not brittle on tt.name)
29- platformType configv1.PlatformType
30- wantErr string
31- wantNotFoundOK bool // if true, err must be NotFound and still contain wantErr in the message
32- wantVolumes int
33- wantMountPath string
34- wantAWSEnv bool
35- noInfra bool // if true, infra indexer is left empty so Get("cluster") fails
41+ name string
42+ deploymentName string
43+ secretName string
44+ secretInStore bool
45+ decoySecretOnly bool // lister has a different secret name so Get(secretName) fails (not brittle on tt.name)
46+ platformType configv1.PlatformType
47+ wantErr string // full hook err.Error(); empty => expect nil error
48+ wantVolumes int
49+ wantMountPath string
50+ wantAWSEnv bool
51+ noInfra bool // if true, infra indexer is left empty so Get("cluster") fails
3652 }{
3753 {
3854 name : "non-controller deployment no-op" ,
@@ -55,7 +71,7 @@ func TestWithCloudCredentials(t *testing.T) {
5571 secretInStore : false ,
5672 decoySecretOnly : true ,
5773 platformType : configv1 .AWSPlatformType ,
58- wantErr : "Retrying" ,
74+ wantErr : expectedCloudSecretRetryingNotFound ( "missing-secret" ) ,
5975 wantVolumes : 0 ,
6076 },
6177 {
@@ -84,27 +100,26 @@ func TestWithCloudCredentials(t *testing.T) {
84100 secretName : testSecretName ,
85101 secretInStore : true ,
86102 platformType : configv1 .PlatformType ("Unsupported" ),
87- wantErr : "unsupported cloud provider" ,
103+ wantErr : expectedUnsupportedCloudProvider ( configv1 . PlatformType ( "Unsupported" )) ,
88104 wantVolumes : 0 ,
89105 },
90106 {
91- name : "infra not found returns error" ,
92- deploymentName : certmanagerControllerDeployment ,
93- secretName : testSecretName ,
94- secretInStore : true ,
95- platformType : configv1 .AWSPlatformType ,
96- wantErr : "cluster" ,
97- wantNotFoundOK : true ,
98- noInfra : true ,
99- wantVolumes : 0 ,
107+ name : "infra not found returns error" ,
108+ deploymentName : certmanagerControllerDeployment ,
109+ secretName : testSecretName ,
110+ secretInStore : true ,
111+ platformType : configv1 .AWSPlatformType ,
112+ wantErr : expectedInfrastructureClusterNotFound (),
113+ noInfra : true ,
114+ wantVolumes : 0 ,
100115 },
101116 {
102117 name : "Azure platform is unsupported" ,
103118 deploymentName : certmanagerControllerDeployment ,
104119 secretName : testSecretName ,
105120 secretInStore : true ,
106121 platformType : configv1 .AzurePlatformType ,
107- wantErr : "unsupported cloud provider" ,
122+ wantErr : expectedUnsupportedCloudProvider ( configv1 . AzurePlatformType ) ,
108123 wantVolumes : 0 ,
109124 },
110125 }
@@ -113,20 +128,20 @@ func TestWithCloudCredentials(t *testing.T) {
113128 var kubeClient * fake.Clientset
114129 if tt .secretInStore {
115130 secret := & corev1.Secret {
116- ObjectMeta : metav1.ObjectMeta {Name : tt .secretName , Namespace : "cert-manager" },
131+ ObjectMeta : metav1.ObjectMeta {Name : tt .secretName , Namespace : operatorclient . TargetNamespace },
117132 }
118133 kubeClient = fake .NewSimpleClientset (secret )
119134 } else if tt .decoySecretOnly {
120135 kubeClient = fake .NewSimpleClientset (& corev1.Secret {
121- ObjectMeta : metav1.ObjectMeta {Name : "other" , Namespace : "cert-manager" },
136+ ObjectMeta : metav1.ObjectMeta {Name : "other" , Namespace : operatorclient . TargetNamespace },
122137 })
123138 } else {
124139 kubeClient = fake .NewSimpleClientset ()
125140 }
126141 kubeInformers := informers .NewSharedInformerFactory (kubeClient , 0 )
127142 if tt .secretInStore || tt .wantErr != "" {
128143 secret := & corev1.Secret {
129- ObjectMeta : metav1.ObjectMeta {Name : tt .secretName , Namespace : "cert-manager" },
144+ ObjectMeta : metav1.ObjectMeta {Name : tt .secretName , Namespace : operatorclient . TargetNamespace },
130145 }
131146 if tt .decoySecretOnly {
132147 secret .Name = "other"
@@ -173,21 +188,10 @@ func TestWithCloudCredentials(t *testing.T) {
173188
174189 if tt .wantErr != "" {
175190 if err == nil {
176- t .Fatalf ("expected error containing %q, got nil" , tt .wantErr )
191+ t .Fatalf ("expected error %q, got nil" , tt .wantErr )
177192 }
178- matchSubstring := strings .Contains (err .Error (), tt .wantErr )
179- var ok bool
180- if tt .wantNotFoundOK {
181- ok = apierrors .IsNotFound (err ) && matchSubstring
182- } else {
183- ok = matchSubstring
184- }
185- if ! ok {
186- if tt .wantNotFoundOK {
187- t .Errorf ("error = %v, want NotFound with message containing %q" , err , tt .wantErr )
188- } else {
189- t .Errorf ("error = %v, want substring %q" , err , tt .wantErr )
190- }
193+ if got := err .Error (); got != tt .wantErr {
194+ t .Errorf ("error = %q, want %q" , got , tt .wantErr )
191195 }
192196 return
193197 }
0 commit comments