-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor the secretg generator controlle4
The commit is meant to: - make the code easier to understand, reducing complexity caused by nested if/else and error conditions (align happy path to the left) - remove shadowed error vars to avoid reporting misleading errors - add some basic unit test for the reconcile loop
- Loading branch information
1 parent
b91bd29
commit 0cb1dba
Showing
2 changed files
with
216 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
153 changes: 153 additions & 0 deletions
153
controllers/secretgenerator/secretgenerator_controller_test.go
This file contains 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,153 @@ | ||
package secretgenerator_test | ||
|
||
import ( | ||
"context" | ||
"github.com/onsi/gomega/gstruct" | ||
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/secretgenerator" | ||
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func newFakeClient(objs ...client.Object) (client.Client, error) { | ||
scheme := runtime.NewScheme() | ||
utilruntime.Must(corev1.AddToScheme(scheme)) | ||
utilruntime.Must(appsv1.AddToScheme(scheme)) | ||
|
||
fakeMapper := meta.NewDefaultRESTMapper(scheme.PreferredVersionAllGroups()) | ||
for gvk := range scheme.AllKnownTypes() { | ||
fakeMapper.Add(gvk, meta.RESTScopeNamespace) | ||
} | ||
|
||
return fake.NewClientBuilder(). | ||
WithScheme(scheme). | ||
WithRESTMapper(fakeMapper). | ||
WithObjects(objs...). | ||
Build(), | ||
nil | ||
} | ||
|
||
func TestGenerateSecret(t *testing.T) { | ||
g := NewWithT(t) | ||
ctx := context.Background() | ||
|
||
secretName := "foo" | ||
secretNs := "ns" | ||
|
||
// secret expected to be found | ||
existingSecret := corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: secretNs, | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
Annotations: map[string]string{ | ||
annotations.SecretNameAnnotation: "bar", | ||
annotations.SecretTypeAnnotation: "random", | ||
}, | ||
}, | ||
} | ||
|
||
// secret to be generated | ||
generatedSecret := corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName + "-generated", | ||
Namespace: secretNs, | ||
}, | ||
} | ||
|
||
cli, err := newFakeClient(&existingSecret) | ||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
r := secretgenerator.SecretGeneratorReconciler{ | ||
Client: cli, | ||
} | ||
|
||
_, err = r.Reconcile(ctx, reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: existingSecret.Name, | ||
Namespace: existingSecret.Namespace, | ||
}, | ||
}) | ||
|
||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
err = cli.Get(ctx, client.ObjectKeyFromObject(&generatedSecret), &generatedSecret) | ||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
g.Expect(generatedSecret.StringData).To( | ||
HaveKey(existingSecret.Annotations[annotations.SecretNameAnnotation])) | ||
g.Expect(generatedSecret.Labels).To( | ||
gstruct.MatchAllKeys(gstruct.Keys{ | ||
"foo": Equal("bar"), | ||
}), | ||
) | ||
} | ||
|
||
func TestExistingSecret(t *testing.T) { | ||
g := NewWithT(t) | ||
ctx := context.Background() | ||
|
||
secretName := "foo" | ||
secretNs := "ns" | ||
|
||
// secret expected to be found | ||
existingSecret := corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: secretNs, | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
Annotations: map[string]string{ | ||
annotations.SecretNameAnnotation: "bar", | ||
annotations.SecretTypeAnnotation: "random", | ||
}, | ||
}, | ||
} | ||
|
||
// secret to be generated | ||
generatedSecret := corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName + "-generated", | ||
Namespace: secretNs, | ||
}, | ||
} | ||
|
||
cli, err := newFakeClient(&existingSecret, &generatedSecret) | ||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
r := secretgenerator.SecretGeneratorReconciler{ | ||
Client: cli, | ||
} | ||
|
||
_, err = r.Reconcile(ctx, reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: existingSecret.Name, | ||
Namespace: existingSecret.Namespace, | ||
}, | ||
}) | ||
|
||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
err = cli.Get(ctx, client.ObjectKeyFromObject(&generatedSecret), &generatedSecret) | ||
g.Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
// assuming an existing secret is left untouched | ||
g.Expect(generatedSecret.Labels).To(BeEmpty()) | ||
g.Expect(generatedSecret.StringData).To(BeEmpty()) | ||
} |