Skip to content

Commit 10e72cd

Browse files
Arun Mauryaclaude
andcommitted
test(trustmanager): add unit tests for trust namespace configuration
Add missing unit test coverage for configurable trust namespace feature. The trust namespace configuration was already fully implemented in PR openshift#379. This commit adds comprehensive unit tests to verify the implementation and satisfy CM-869 acceptance criteria. Tests added: - Trust namespace validation when namespace doesn't exist - Custom trust namespace configuration with successful reconciliation - Status field updates for both default and custom namespace values Without these tests, the trust namespace validation logic was not adequately covered, making it harder to catch regressions. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 359b345 commit 10e72cd

3 files changed

Lines changed: 139 additions & 1 deletion

File tree

pkg/controller/trustmanager/controller_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

pkg/controller/trustmanager/test_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (b *trustManagerBuilder) WithAffinity(affinity *corev1.Affinity) *trustMana
6969
return b
7070
}
7171

72+
func (b *trustManagerBuilder) WithTrustNamespace(trustNamespace string) *trustManagerBuilder {
73+
b.Spec.TrustManagerConfig.TrustNamespace = trustNamespace
74+
return b
75+
}
76+
7277
func (b *trustManagerBuilder) Build() *v1alpha1.TrustManager {
7378
return b.TrustManager
7479
}

pkg/controller/trustmanager/webhooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func getValidatingWebhookConfigObject(resourceLabels, resourceAnnotations map[st
5252
return webhookConfig
5353
}
5454

55-
// updateWebhookClientConfig sets the webhook clientConfig service name and namespace
55+
// updateWebhookClientConfig sets the webhook clientConfig service name and namespace.
5656
func updateWebhookClientConfig(webhookConfig *admissionregistrationv1.ValidatingWebhookConfiguration) {
5757
for i := range webhookConfig.Webhooks {
5858
if webhookConfig.Webhooks[i].ClientConfig.Service != nil {

0 commit comments

Comments
 (0)