-
Notifications
You must be signed in to change notification settings - Fork 40
CM-869: Add test coverage for configurable TrustNamespace #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 14 commits into
openshift:master
from
arun717:trustnamespace_impl
Apr 2, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
10e72cd
test(trustmanager): add unit tests for trust namespace configuration
7a0aea0
test(e2e): add e2e tests for trust namespace configuration
cfb6441
test(trustmanager): fix for missing namespace case.
9d0373c
Handling PR review comments.
9cf508c
Handling remaining PR review comments on trustmanager_test.go
89dbd8d
Addressed Review comments iteration 3
06eb460
Rebase after resolving merge conflict.
0edc861
Handling CodeRabbit comment
f5fa48c
Resolving Merge Conflicts manually.
e7a8560
Addressed new round of pr review comments
197e490
Undo comment for test
fe6fc5b
Addressing the latest review comment of removal of a test.
1162cb0
Merge conflict manual resolution.
4725276
Removing duplicate method definition.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
pkg/controller/trustmanager/install_trustmanager_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package trustmanager | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/openshift/cert-manager-operator/api/operator/v1alpha1" | ||
| "github.com/openshift/cert-manager-operator/pkg/controller/common/fakes" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| func TestUpdateStatusObservedState(t *testing.T) { | ||
| t.Setenv(trustManagerImageNameEnvVarName, testImage) | ||
|
|
||
| // Observed status after sync from testTrustManager() defaults (empty status fields + default spec). | ||
| wantStatusSyncedFromDefaultSpec := v1alpha1.TrustManagerStatus{ | ||
| TrustManagerImage: testImage, | ||
| TrustNamespace: defaultTrustNamespace, | ||
| SecretTargetsPolicy: "", | ||
| DefaultCAPackagePolicy: "", | ||
| FilterExpiredCertificatesPolicy: "", | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| trustManager func() *v1alpha1.TrustManager | ||
| wantStatusUpdate int | ||
| wantStatus v1alpha1.TrustManagerStatus | ||
| }{ | ||
| { | ||
| name: "updates all observed fields when status is empty", | ||
| trustManager: func() *v1alpha1.TrustManager { | ||
| return testTrustManager().Build() | ||
| }, | ||
| wantStatusUpdate: 1, | ||
| wantStatus: wantStatusSyncedFromDefaultSpec, | ||
| }, | ||
| { | ||
| name: "updates all observed fields for custom spec", | ||
| trustManager: func() *v1alpha1.TrustManager { | ||
| return testTrustManager(). | ||
| WithTrustNamespace("custom-trust-ns"). | ||
| WithSecretTargets(v1alpha1.SecretTargetsPolicyCustom, []string{"allowed-secret"}). | ||
| WithDefaultCAPackage(v1alpha1.DefaultCAPackagePolicyEnabled). | ||
| WithFilterExpiredCertificates(v1alpha1.FilterExpiredCertificatesPolicyEnabled). | ||
| Build() | ||
| }, | ||
arun717 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| wantStatusUpdate: 1, | ||
| wantStatus: v1alpha1.TrustManagerStatus{ | ||
| TrustManagerImage: testImage, | ||
| TrustNamespace: "custom-trust-ns", | ||
| SecretTargetsPolicy: v1alpha1.SecretTargetsPolicyCustom, | ||
| DefaultCAPackagePolicy: v1alpha1.DefaultCAPackagePolicyEnabled, | ||
| FilterExpiredCertificatesPolicy: v1alpha1.FilterExpiredCertificatesPolicyEnabled, | ||
| }, | ||
| }, | ||
| { | ||
| name: "no-op when observed state already matches spec and env", | ||
| trustManager: func() *v1alpha1.TrustManager { | ||
| tm := testTrustManager().Build() | ||
| tm.Status.TrustManagerImage = testImage | ||
| tm.Status.TrustNamespace = defaultTrustNamespace | ||
| tm.Status.SecretTargetsPolicy = tm.Spec.TrustManagerConfig.SecretTargets.Policy | ||
| tm.Status.DefaultCAPackagePolicy = tm.Spec.TrustManagerConfig.DefaultCAPackage.Policy | ||
| tm.Status.FilterExpiredCertificatesPolicy = tm.Spec.TrustManagerConfig.FilterExpiredCertificates | ||
| return tm | ||
| }, | ||
| wantStatusUpdate: 0, | ||
| wantStatus: wantStatusSyncedFromDefaultSpec, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| r := testReconciler(t) | ||
| mock := &fakes.FakeCtrlClient{} | ||
| tm := tt.trustManager() | ||
|
|
||
| mock.GetCalls(func(ctx context.Context, key client.ObjectKey, obj client.Object) error { | ||
| switch o := obj.(type) { | ||
| case *v1alpha1.TrustManager: | ||
| tm.DeepCopyInto(o) | ||
| } | ||
| return nil | ||
| }) | ||
| mock.StatusUpdateCalls(func(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error { | ||
| return nil | ||
| }) | ||
| r.CtrlClient = mock | ||
|
|
||
| if err := r.updateStatusObservedState(tm); err != nil { | ||
| t.Fatalf("updateStatusObservedState: %v", err) | ||
| } | ||
| if got := mock.StatusUpdateCallCount(); got != tt.wantStatusUpdate { | ||
| t.Errorf("StatusUpdateCallCount() = %d, want %d", got, tt.wantStatusUpdate) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(tm.Status, tt.wantStatus) { | ||
| t.Errorf("TrustManager.Status mismatch (-want +got):\n%s", cmp.Diff(tt.wantStatus, tm.Status)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.