-
Notifications
You must be signed in to change notification settings - Fork 52
CM-902: Add table-driven unit tests and improve coverage #385
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 8 commits into
openshift:master
from
anandkuma77:CM-902-add-testcases
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efa9a57
CM-902: Add and improve unit tests
bfb2ac6
test: strengthen NewClient and validateTrustManagerConfig coverage
b4fe999
fix: align unit tests with ObjectMetadataModified and trustManagerBui…
d149df8
fix: harden NewClient and tests from review feedback
add71f8
test(deployment): require NotFound and substring for infra error case
0ea04a5
Remove unit tests for generated bindata and applyconfiguration code
a3e30f9
client: drop nil manager check from NewClient
e1e688b
Fix review comments on unit tests
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| package certmanager | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/informers" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
| "k8s.io/client-go/tools/cache" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| configinformersv1 "github.com/openshift/client-go/config/informers/externalversions/config/v1" | ||
| configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" | ||
|
|
||
| "github.com/openshift/cert-manager-operator/pkg/operator/operatorclient" | ||
| ) | ||
|
|
||
| const testSecretName = "cloud-creds" | ||
|
|
||
| // The following helpers return the full err.Error() the hook produces for each failure mode | ||
| // (same NotFound GroupResource/name and fmt.Errorf wording as withCloudCredentials; test-only). | ||
| func expectedCloudSecretRetryingNotFound(secretName string) string { | ||
| nf := apierrors.NewNotFound(corev1.Resource("secret"), secretName) | ||
| return fmt.Errorf("(Retrying) cloud secret %q doesn't exist due to %w", secretName, nf).Error() | ||
| } | ||
|
|
||
| func expectedUnsupportedCloudProvider(platform configv1.PlatformType) string { | ||
| return fmt.Errorf("unsupported cloud provider %q for mounting cloud credentials secret", platform).Error() | ||
| } | ||
|
|
||
| func expectedInfrastructureClusterNotFound() string { | ||
| return apierrors.NewNotFound(configv1.Resource("infrastructure"), "cluster").Error() | ||
| } | ||
|
|
||
| func TestWithCloudCredentials(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| deploymentName string | ||
| secretName string | ||
| secretInStore bool | ||
| decoySecretOnly bool // lister has a different secret name so Get(secretName) fails (not brittle on tt.name) | ||
| platformType configv1.PlatformType | ||
| wantErr string // full hook err.Error(); empty => expect nil error | ||
| wantVolumes int | ||
| wantMountPath string | ||
| wantAWSEnv bool | ||
| noInfra bool // if true, infra indexer is left empty so Get("cluster") fails | ||
| }{ | ||
| { | ||
| name: "non-controller deployment no-op", | ||
| deploymentName: certmanagerWebhookDeployment, | ||
| secretName: testSecretName, | ||
| platformType: configv1.AWSPlatformType, | ||
| wantVolumes: 0, | ||
| }, | ||
| { | ||
| name: "empty secret name returns nil", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: "", | ||
| platformType: configv1.AWSPlatformType, | ||
| wantVolumes: 0, | ||
| }, | ||
| { | ||
| name: "secret not found returns retry error", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: "missing-secret", | ||
| secretInStore: false, | ||
| decoySecretOnly: true, | ||
| platformType: configv1.AWSPlatformType, | ||
| wantErr: expectedCloudSecretRetryingNotFound("missing-secret"), | ||
| wantVolumes: 0, | ||
| }, | ||
| { | ||
| name: "AWS adds volume, mount and env", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: testSecretName, | ||
| secretInStore: true, | ||
| platformType: configv1.AWSPlatformType, | ||
| wantVolumes: 1, | ||
| wantMountPath: awsCredentialsDir, | ||
| wantAWSEnv: true, | ||
| }, | ||
| { | ||
| name: "GCP adds volume and mount, no AWS env", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: testSecretName, | ||
| secretInStore: true, | ||
| platformType: configv1.GCPPlatformType, | ||
| wantVolumes: 1, | ||
| wantMountPath: gcpCredentialsDir, | ||
| wantAWSEnv: false, | ||
| }, | ||
| { | ||
| name: "unsupported platform returns error", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: testSecretName, | ||
| secretInStore: true, | ||
| platformType: configv1.PlatformType("Unsupported"), | ||
| wantErr: expectedUnsupportedCloudProvider(configv1.PlatformType("Unsupported")), | ||
| wantVolumes: 0, | ||
| }, | ||
| { | ||
| name: "infra not found returns error", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: testSecretName, | ||
| secretInStore: true, | ||
| platformType: configv1.AWSPlatformType, | ||
| wantErr: expectedInfrastructureClusterNotFound(), | ||
| noInfra: true, | ||
| wantVolumes: 0, | ||
| }, | ||
| { | ||
| name: "Azure platform is unsupported", | ||
| deploymentName: certmanagerControllerDeployment, | ||
| secretName: testSecretName, | ||
| secretInStore: true, | ||
| platformType: configv1.AzurePlatformType, | ||
| wantErr: expectedUnsupportedCloudProvider(configv1.AzurePlatformType), | ||
| wantVolumes: 0, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| var kubeClient *fake.Clientset | ||
| if tt.secretInStore { | ||
| secret := &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: tt.secretName, Namespace: operatorclient.TargetNamespace}, | ||
| } | ||
| kubeClient = fake.NewSimpleClientset(secret) | ||
| } else if tt.decoySecretOnly { | ||
| kubeClient = fake.NewSimpleClientset(&corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "other", Namespace: operatorclient.TargetNamespace}, | ||
| }) | ||
| } else { | ||
| kubeClient = fake.NewSimpleClientset() | ||
| } | ||
| kubeInformers := informers.NewSharedInformerFactory(kubeClient, 0) | ||
| if tt.secretInStore || tt.wantErr != "" { | ||
| secret := &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: tt.secretName, Namespace: operatorclient.TargetNamespace}, | ||
| } | ||
| if tt.decoySecretOnly { | ||
| secret.Name = "other" | ||
| } | ||
| kubeInformers.Core().V1().Secrets().Informer().GetStore().Add(secret) | ||
| } | ||
| stopCh := make(chan struct{}) | ||
| defer close(stopCh) | ||
| kubeInformers.Start(stopCh) | ||
| if tt.secretInStore || tt.wantErr != "" { | ||
| if !cache.WaitForCacheSync(stopCh, kubeInformers.Core().V1().Secrets().Informer().HasSynced) { | ||
| t.Fatal("secret informer did not sync") | ||
| } | ||
| } | ||
|
|
||
| infraIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) | ||
| if !tt.noInfra { | ||
| infra := &configv1.Infrastructure{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, | ||
| Status: configv1.InfrastructureStatus{ | ||
| PlatformStatus: &configv1.PlatformStatus{Type: tt.platformType}, | ||
| }, | ||
| } | ||
| _ = infraIndexer.Add(infra) | ||
| } | ||
| infraInformer := &fakeInfrastructureInformer{lister: configlistersv1.NewInfrastructureLister(infraIndexer)} | ||
|
|
||
| hook := withCloudCredentials( | ||
| kubeInformers.Core().V1().Secrets(), | ||
| infraInformer, | ||
| tt.deploymentName, | ||
| tt.secretName, | ||
| ) | ||
| deployment := &appsv1.Deployment{ | ||
| Spec: appsv1.DeploymentSpec{ | ||
| Template: corev1.PodTemplateSpec{ | ||
| Spec: corev1.PodSpec{ | ||
| Containers: []corev1.Container{{Name: "controller"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := hook(nil, deployment) | ||
|
|
||
| if tt.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error %q, got nil", tt.wantErr) | ||
| } | ||
| if got := err.Error(); got != tt.wantErr { | ||
| t.Errorf("error = %q, want %q", got, tt.wantErr) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if n := len(deployment.Spec.Template.Spec.Volumes); n != tt.wantVolumes { | ||
| t.Errorf("volumes count = %d, want %d", n, tt.wantVolumes) | ||
| } | ||
| if tt.wantVolumes > 0 { | ||
| if deployment.Spec.Template.Spec.Volumes[0].Name != cloudCredentialsVolumeName { | ||
| t.Errorf("volume name = %q, want %q", deployment.Spec.Template.Spec.Volumes[0].Name, cloudCredentialsVolumeName) | ||
| } | ||
| if tt.wantMountPath != "" { | ||
| mounts := deployment.Spec.Template.Spec.Containers[0].VolumeMounts | ||
| if len(mounts) == 0 { | ||
| t.Fatalf("expected VolumeMount for mount path %q, got none (containers[0].VolumeMounts is empty)", tt.wantMountPath) | ||
| } | ||
| if mounts[0].MountPath != tt.wantMountPath { | ||
| t.Errorf("mount path = %q, want %q", mounts[0].MountPath, tt.wantMountPath) | ||
| } | ||
| } | ||
| var hasAWS bool | ||
| for _, e := range deployment.Spec.Template.Spec.Containers[0].Env { | ||
| if e.Name == "AWS_SDK_LOAD_CONFIG" { | ||
| hasAWS = true | ||
| break | ||
| } | ||
| } | ||
| if hasAWS != tt.wantAWSEnv { | ||
| t.Errorf("AWS_SDK_LOAD_CONFIG present = %v, want %v", hasAWS, tt.wantAWSEnv) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // fakeInfrastructureInformer implements configinformersv1.InfrastructureInformer using a fixed lister. | ||
| type fakeInfrastructureInformer struct { | ||
| lister configlistersv1.InfrastructureLister | ||
| } | ||
|
|
||
| func (f *fakeInfrastructureInformer) Informer() cache.SharedIndexInformer { | ||
| return nil | ||
| } | ||
|
|
||
| func (f *fakeInfrastructureInformer) Lister() configlistersv1.InfrastructureLister { | ||
| return f.lister | ||
| } | ||
|
|
||
| var _ configinformersv1.InfrastructureInformer = (*fakeInfrastructureInformer)(nil) | ||
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.