@@ -251,6 +251,82 @@ func TestProcessReconcileRequest(t *testing.T) {
251251 },
252252 wantErr : "failed to check if serviceaccount" ,
253253 },
254+ {
255+ name : "trust namespace does not exist sets degraded true" ,
256+ getTrustManager : func () * v1alpha1.TrustManager {
257+ return testTrustManager ().Build ()
258+ },
259+ preReq : func (r * Reconciler , m * fakes.FakeCtrlClient ) {
260+ m .GetCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) error {
261+ switch o := obj .(type ) {
262+ case * v1alpha1.TrustManager :
263+ testTrustManager ().Build ().DeepCopyInto (o )
264+ }
265+ return nil
266+ })
267+ // Namespace does not exist - validateTrustNamespace will fail
268+ m .ExistsCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) (bool , error ) {
269+ switch obj .(type ) {
270+ case * corev1.Namespace :
271+ return false , nil
272+ }
273+ return false , nil
274+ })
275+ },
276+ wantConditions : []metav1.Condition {
277+ {
278+ Type : v1alpha1 .Degraded ,
279+ Status : metav1 .ConditionTrue ,
280+ Reason : v1alpha1 .ReasonFailed ,
281+ },
282+ {
283+ Type : v1alpha1 .Ready ,
284+ Status : metav1 .ConditionFalse ,
285+ Reason : v1alpha1 .ReasonFailed ,
286+ },
287+ },
288+ },
289+ {
290+ name : "custom trust namespace configures resources correctly" ,
291+ getTrustManager : func () * v1alpha1.TrustManager {
292+ tm := testTrustManager ().Build ()
293+ tm .Spec .TrustManagerConfig .TrustNamespace = "custom-trust-ns"
294+ return tm
295+ },
296+ preReq : func (r * Reconciler , m * fakes.FakeCtrlClient ) {
297+ m .GetCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) error {
298+ switch o := obj .(type ) {
299+ case * v1alpha1.TrustManager :
300+ tm := testTrustManager ().Build ()
301+ tm .Spec .TrustManagerConfig .TrustNamespace = "custom-trust-ns"
302+ tm .DeepCopyInto (o )
303+ }
304+ return nil
305+ })
306+ // Custom namespace exists; all other resources return not-found so they
307+ // are created via SSA Patch (which succeeds by default).
308+ m .ExistsCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) (bool , error ) {
309+ switch obj .(type ) {
310+ case * corev1.Namespace :
311+ return true , nil
312+ }
313+ return false , nil
314+ })
315+ },
316+ wantConditions : []metav1.Condition {
317+ {
318+ Type : v1alpha1 .Degraded ,
319+ Status : metav1 .ConditionFalse ,
320+ Reason : v1alpha1 .ReasonReady ,
321+ },
322+ {
323+ Type : v1alpha1 .Ready ,
324+ Status : metav1 .ConditionTrue ,
325+ Reason : v1alpha1 .ReasonReady ,
326+ Message : "reconciliation successful" ,
327+ },
328+ },
329+ },
254330 }
255331
256332 for _ , tt := range tests {
@@ -289,3 +365,60 @@ func TestProcessReconcileRequest(t *testing.T) {
289365 })
290366 }
291367}
368+
369+ func TestStatusTrustNamespaceUpdate (t * testing.T ) {
370+ t .Setenv (trustManagerImageNameEnvVarName , testImage )
371+
372+ tests := []struct {
373+ name string
374+ trustNamespace string
375+ wantTrustNamespace string
376+ }{
377+ {
378+ name : "default trust namespace is set in status" ,
379+ trustNamespace : "" ,
380+ wantTrustNamespace : defaultTrustNamespace ,
381+ },
382+ {
383+ name : "custom trust namespace is set in status" ,
384+ trustNamespace : "custom-trust-ns" ,
385+ wantTrustNamespace : "custom-trust-ns" ,
386+ },
387+ }
388+
389+ for _ , tt := range tests {
390+ t .Run (tt .name , func (t * testing.T ) {
391+ r := testReconciler (t )
392+ mock := & fakes.FakeCtrlClient {}
393+
394+ mock .GetCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) error {
395+ switch o := obj .(type ) {
396+ case * v1alpha1.TrustManager :
397+ tm := testTrustManager ().WithTrustNamespace (tt .trustNamespace ).Build ()
398+ tm .DeepCopyInto (o )
399+ }
400+ return nil
401+ })
402+
403+ mock .ExistsCalls (func (ctx context.Context , key client.ObjectKey , obj client.Object ) (bool , error ) {
404+ switch obj .(type ) {
405+ case * corev1.Namespace :
406+ return true , nil
407+ }
408+ return false , nil
409+ })
410+
411+ r .CtrlClient = mock
412+
413+ tm := testTrustManager ().WithTrustNamespace (tt .trustNamespace ).Build ()
414+ _ , err := r .processReconcileRequest (tm , types.NamespacedName {Name : tm .GetName ()})
415+ if err != nil {
416+ t .Fatalf ("unexpected error: %v" , err )
417+ }
418+
419+ if tm .Status .TrustNamespace != tt .wantTrustNamespace {
420+ t .Errorf ("expected status.trustNamespace %q, got %q" , tt .wantTrustNamespace , tm .Status .TrustNamespace )
421+ }
422+ })
423+ }
424+ }
0 commit comments