Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions internal/shared/util/pullsecretcache/pullsecretcache_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading