From c65b9e5ce524dd20f83a165f79b7f1713c87524d Mon Sep 17 00:00:00 2001 From: Andre Devay Date: Tue, 1 Dec 2026 12:55:07 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20Add=20unit=20tests=20for=20Setup?= =?UTF-8?q?PullSecretCache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SetupPullSecretCache decides which ServiceAccounts and Secrets the operator-controller and catalogd managers cache, and therefore which pull secrets they can read, but it had no tests. Cover the three configurations: no global pull secret, a global pull secret in the service account namespace (which must not narrow the "all Secrets" cache of that namespace to one name), and a global pull secret in another namespace (cached by name only). Also check that only the operator's own ServiceAccount is cached and that unrelated ByObject entries are preserved. Signed-off-by: Andre Devay --- .../pullsecretcache/pullsecretcache_test.go | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 internal/shared/util/pullsecretcache/pullsecretcache_test.go diff --git a/internal/shared/util/pullsecretcache/pullsecretcache_test.go b/internal/shared/util/pullsecretcache/pullsecretcache_test.go new file mode 100644 index 0000000000..45d6932497 --- /dev/null +++ b/internal/shared/util/pullsecretcache/pullsecretcache_test.go @@ -0,0 +1,102 @@ +package pullsecretcache_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/operator-framework/operator-controller/internal/shared/util/pullsecretcache" +) + +// byObjectFor returns the cache.ByObject entry whose key has the same type as T. +func byObjectFor[T client.Object](t *testing.T, opts cache.Options) cache.ByObject { + t.Helper() + var found []cache.ByObject + for k, v := range opts.ByObject { + if _, ok := k.(T); ok { + found = append(found, v) + } + } + require.Len(t, found, 1, "expected exactly one ByObject entry for %T", *new(T)) + return found[0] +} + +func requireSelectors(t *testing.T, cfg cache.Config, wantLabel labels.Selector, wantField fields.Selector) { + t.Helper() + // Compare the selectors themselves: labels.Everything() and labels.Nothing() + // (and their fields counterparts) both render as "". + require.Equal(t, wantLabel, cfg.LabelSelector) + require.Equal(t, wantField, cfg.FieldSelector) +} + +func TestSetupPullSecretCache(t *testing.T) { + saKey := types.NamespacedName{Namespace: "olmv1-system", Name: "operator-controller-controller-manager"} + nameSelector := func(name string) fields.Selector { + return fields.SelectorFromSet(fields.Set{"metadata.name": name}) + } + + for _, tc := range []struct { + name string + globalPullSecret *types.NamespacedName + wantSecretNS map[string]fields.Selector + }{ + { + name: "no global pull secret", + globalPullSecret: nil, + wantSecretNS: map[string]fields.Selector{ + saKey.Namespace: fields.Everything(), + }, + }, + { + // The service account namespace already caches every Secret, so the + // global pull secret must not narrow it down to a single name. + name: "global pull secret in the service account namespace", + globalPullSecret: &types.NamespacedName{Namespace: saKey.Namespace, Name: "global-pull-secret"}, + wantSecretNS: map[string]fields.Selector{ + saKey.Namespace: fields.Everything(), + }, + }, + { + name: "global pull secret in another namespace", + globalPullSecret: &types.NamespacedName{Namespace: "openshift-config", Name: "pull-secret"}, + wantSecretNS: map[string]fields.Selector{ + saKey.Namespace: fields.Everything(), + "openshift-config": nameSelector("pull-secret"), + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + existing := &corev1.ConfigMap{} + opts := cache.Options{ + ByObject: map[client.Object]cache.ByObject{ + existing: {Label: labels.Everything()}, + }, + } + + require.NoError(t, pullsecretcache.SetupPullSecretCache(&opts, tc.globalPullSecret, saKey)) + + // Entries for other types are left untouched. + require.Len(t, opts.ByObject, 3) + require.Contains(t, opts.ByObject, client.Object(existing)) + + // Only the operator's own ServiceAccount is cached. + sa := byObjectFor[*corev1.ServiceAccount](t, opts) + require.Len(t, sa.Namespaces, 1) + require.Contains(t, sa.Namespaces, saKey.Namespace) + requireSelectors(t, sa.Namespaces[saKey.Namespace], labels.Everything(), nameSelector(saKey.Name)) + + secret := byObjectFor[*corev1.Secret](t, opts) + require.Len(t, secret.Namespaces, len(tc.wantSecretNS)) + for ns, wantField := range tc.wantSecretNS { + require.Contains(t, secret.Namespaces, ns) + requireSelectors(t, secret.Namespaces[ns], labels.Everything(), wantField) + } + }) + } +}