@@ -163,7 +163,7 @@ func TestProviderReconciler_Reconcile(t *testing.T) {
163163 g .Expect (conditions .GetReason (resultP , meta .ReadyCondition )).To (BeIdenticalTo (meta .InvalidURLReason ))
164164 })
165165
166- t .Run ("recovers from staleness " , func (t * testing.T ) {
166+ t .Run ("recovers from being stalled " , func (t * testing.T ) {
167167 g := NewWithT (t )
168168
169169 g .Expect (k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )).To (Succeed ())
@@ -180,14 +180,92 @@ func TestProviderReconciler_Reconcile(t *testing.T) {
180180 g .Expect (conditions .Has (resultP , meta .StalledCondition )).To (BeFalse ())
181181 })
182182
183- t .Run ("finalizes suspended object" , func (t * testing.T ) {
183+ t .Run ("HTTP connections are blocked" , func (t * testing.T ) {
184+ g := NewWithT (t )
185+ g .Expect (k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )).To (Succeed ())
186+
187+ resultP .Spec .Proxy = "http://proxy.internal"
188+ g .Expect (k8sClient .Update (context .Background (), resultP )).To (Succeed ())
189+
190+ g .Eventually (func () bool {
191+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )
192+ return ! conditions .IsReady (resultP )
193+ }, timeout , time .Second ).Should (BeTrue ())
194+
195+ g .Expect (conditions .Has (resultP , meta .ReconcilingCondition )).To (BeFalse ())
196+ g .Expect (conditions .Has (resultP , meta .StalledCondition )).To (BeTrue ())
197+ g .Expect (conditions .GetObservedGeneration (resultP , meta .StalledCondition )).To (BeIdenticalTo (resultP .Generation ))
198+ g .Expect (conditions .GetReason (resultP , meta .StalledCondition )).To (BeIdenticalTo (meta .InsecureConnectionsDisallowedReason ))
199+ g .Expect (conditions .GetReason (resultP , meta .ReadyCondition )).To (BeIdenticalTo (meta .InsecureConnectionsDisallowedReason ))
200+ })
201+
202+ t .Run ("becomes not ready with InvalidURLReason if secret has an invalid address" , func (t * testing.T ) {
184203 g := NewWithT (t )
185204
205+ secret := & corev1.Secret {
206+ ObjectMeta : metav1.ObjectMeta {
207+ Name : secretName ,
208+ Namespace : namespaceName ,
209+ },
210+ StringData : map [string ]string {
211+ "token" : "test" ,
212+ "address" : "http//invalid" ,
213+ },
214+ }
215+ g .Expect (k8sClient .Update (context .Background (), secret )).To (Succeed ())
216+
186217 g .Expect (k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )).To (Succeed ())
218+ resultP .Spec .SecretRef = & meta.LocalObjectReference {
219+ Name : secretName ,
220+ }
221+ resultP .Spec .Proxy = ""
222+ resultP .Spec .Address = ""
223+ g .Expect (k8sClient .Update (context .Background (), resultP )).To (Succeed ())
187224
188- resultP .Spec .Suspend = true
225+ g .Eventually (func () bool {
226+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )
227+ return ! conditions .IsStalled (resultP )
228+ }, timeout , time .Second ).Should (BeTrue ())
229+
230+ g .Expect (conditions .GetReason (resultP , meta .ReadyCondition )).To (BeIdenticalTo (meta .InvalidURLReason ))
231+
232+ g .Expect (conditions .Has (resultP , meta .ReconcilingCondition )).To (BeTrue ())
233+ g .Expect (conditions .GetReason (resultP , meta .ReconcilingCondition )).To (BeIdenticalTo (meta .ProgressingWithRetryReason ))
234+ g .Expect (conditions .GetObservedGeneration (resultP , meta .ReconcilingCondition )).To (BeIdenticalTo (resultP .Generation ))
235+ })
236+
237+ t .Run ("is not stalled if there is a secret ref even if spec.address is invalid" , func (t * testing.T ) {
238+ g := NewWithT (t )
239+
240+ g .Expect (k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )).To (Succeed ())
241+
242+ resultP .Spec .Address = "http://invalid|"
189243 g .Expect (k8sClient .Update (context .Background (), resultP )).To (Succeed ())
190244
245+ g .Eventually (func () bool {
246+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )
247+ return ! conditions .IsReady (resultP )
248+ }, timeout , time .Second ).Should (BeTrue ())
249+
250+ g .Expect (conditions .Has (resultP , meta .StalledCondition )).To (BeFalse ())
251+ g .Expect (conditions .Has (resultP , meta .ReconcilingCondition )).To (BeTrue ())
252+ g .Expect (conditions .GetReason (resultP , meta .ReconcilingCondition )).To (BeIdenticalTo (meta .ProgressingWithRetryReason ))
253+ })
254+
255+ t .Run ("finalizes suspended object" , func (t * testing.T ) {
256+ g := NewWithT (t )
257+
258+ g .Eventually (func () bool {
259+ if err := k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP ); err != nil {
260+ return false
261+ }
262+ resultP .Spec .Suspend = true
263+ if err := k8sClient .Update (context .Background (), resultP ); err != nil {
264+ return false
265+ }
266+ return true
267+ }, timeout , time .Second ).Should (BeTrue ())
268+
191269 g .Eventually (func () bool {
192270 _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (provider ), resultP )
193271 return resultP .Spec .Suspend == true
@@ -201,3 +279,63 @@ func TestProviderReconciler_Reconcile(t *testing.T) {
201279 }, timeout , time .Second ).Should (BeTrue ())
202280 })
203281}
282+
283+ func Test_parseURLs (t * testing.T ) {
284+ tests := []struct {
285+ name string
286+ address string
287+ proxy string
288+ blockHTTP bool
289+ err error
290+ errMsg string
291+ }{
292+ {
293+ name : "valid address and proxy" ,
294+ address : "http://example.com" ,
295+ proxy : "http://proxy.com" ,
296+ },
297+ {
298+ name : "invalid address" ,
299+ address : "http//invalid" ,
300+ errMsg : "invalid address" ,
301+ },
302+ {
303+ name : "invalid proxy" ,
304+ address : "http://example.com" ,
305+ proxy : "http//invalid" ,
306+ errMsg : "invalid proxy" ,
307+ },
308+ {
309+ name : "block http proxy" ,
310+ address : "http://example.com" ,
311+ proxy : "http://proxy.com" ,
312+ blockHTTP : true ,
313+ err : insecureHTTPError ,
314+ errMsg : "consider changing proxy" ,
315+ },
316+ {
317+ name : "block http address" ,
318+ address : "http://example.com" ,
319+ blockHTTP : true ,
320+ err : insecureHTTPError ,
321+ errMsg : "consider changing address" ,
322+ },
323+ }
324+
325+ for _ , tt := range tests {
326+ t .Run (tt .name , func (t * testing.T ) {
327+ g := NewWithT (t )
328+ err := parseURLs (tt .address , tt .proxy , tt .blockHTTP )
329+
330+ if tt .errMsg == "" {
331+ g .Expect (err ).ToNot (HaveOccurred ())
332+ } else {
333+ g .Expect (err ).To (HaveOccurred ())
334+ g .Expect (err .Error ()).To (ContainSubstring (tt .errMsg ))
335+ }
336+ if tt .err != nil {
337+ g .Expect (err ).To (MatchError (tt .err ))
338+ }
339+ })
340+ }
341+ }
0 commit comments