Skip to content

Commit c961b12

Browse files
🌱 Add unit tests for SetupPullSecretCache (#3219)
* 🌱 Add unit tests for SetupPullSecretCache 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 <andretorresdg@alumni.usp.br> * Use a neutral namespace for the global pull secret test case Signed-off-by: Andre Devay <andretorresdg@alumni.usp.br> --------- Signed-off-by: Andre Devay <andretorresdg@alumni.usp.br>
1 parent 0d6a5ef commit c961b12

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package pullsecretcache_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/fields"
9+
"k8s.io/apimachinery/pkg/labels"
10+
"k8s.io/apimachinery/pkg/types"
11+
"sigs.k8s.io/controller-runtime/pkg/cache"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
14+
"github.com/operator-framework/operator-controller/internal/shared/util/pullsecretcache"
15+
)
16+
17+
// byObjectFor returns the cache.ByObject entry whose key has the same type as T.
18+
func byObjectFor[T client.Object](t *testing.T, opts cache.Options) cache.ByObject {
19+
t.Helper()
20+
var found []cache.ByObject
21+
for k, v := range opts.ByObject {
22+
if _, ok := k.(T); ok {
23+
found = append(found, v)
24+
}
25+
}
26+
require.Len(t, found, 1, "expected exactly one ByObject entry for %T", *new(T))
27+
return found[0]
28+
}
29+
30+
func requireSelectors(t *testing.T, cfg cache.Config, wantLabel labels.Selector, wantField fields.Selector) {
31+
t.Helper()
32+
// Compare the selectors themselves: labels.Everything() and labels.Nothing()
33+
// (and their fields counterparts) both render as "".
34+
require.Equal(t, wantLabel, cfg.LabelSelector)
35+
require.Equal(t, wantField, cfg.FieldSelector)
36+
}
37+
38+
func TestSetupPullSecretCache(t *testing.T) {
39+
saKey := types.NamespacedName{Namespace: "olmv1-system", Name: "operator-controller-controller-manager"}
40+
nameSelector := func(name string) fields.Selector {
41+
return fields.SelectorFromSet(fields.Set{"metadata.name": name})
42+
}
43+
44+
for _, tc := range []struct {
45+
name string
46+
globalPullSecret *types.NamespacedName
47+
wantSecretNS map[string]fields.Selector
48+
}{
49+
{
50+
name: "no global pull secret",
51+
globalPullSecret: nil,
52+
wantSecretNS: map[string]fields.Selector{
53+
saKey.Namespace: fields.Everything(),
54+
},
55+
},
56+
{
57+
// The service account namespace already caches every Secret, so the
58+
// global pull secret must not narrow it down to a single name.
59+
name: "global pull secret in the service account namespace",
60+
globalPullSecret: &types.NamespacedName{Namespace: saKey.Namespace, Name: "global-pull-secret"},
61+
wantSecretNS: map[string]fields.Selector{
62+
saKey.Namespace: fields.Everything(),
63+
},
64+
},
65+
{
66+
name: "global pull secret in another namespace",
67+
globalPullSecret: &types.NamespacedName{Namespace: "pull-secrets", Name: "pull-secret"},
68+
wantSecretNS: map[string]fields.Selector{
69+
saKey.Namespace: fields.Everything(),
70+
"pull-secrets": nameSelector("pull-secret"),
71+
},
72+
},
73+
} {
74+
t.Run(tc.name, func(t *testing.T) {
75+
existing := &corev1.ConfigMap{}
76+
opts := cache.Options{
77+
ByObject: map[client.Object]cache.ByObject{
78+
existing: {Label: labels.Everything()},
79+
},
80+
}
81+
82+
require.NoError(t, pullsecretcache.SetupPullSecretCache(&opts, tc.globalPullSecret, saKey))
83+
84+
// Entries for other types are left untouched.
85+
require.Len(t, opts.ByObject, 3)
86+
require.Contains(t, opts.ByObject, client.Object(existing))
87+
88+
// Only the operator's own ServiceAccount is cached.
89+
sa := byObjectFor[*corev1.ServiceAccount](t, opts)
90+
require.Len(t, sa.Namespaces, 1)
91+
require.Contains(t, sa.Namespaces, saKey.Namespace)
92+
requireSelectors(t, sa.Namespaces[saKey.Namespace], labels.Everything(), nameSelector(saKey.Name))
93+
94+
secret := byObjectFor[*corev1.Secret](t, opts)
95+
require.Len(t, secret.Namespaces, len(tc.wantSecretNS))
96+
for ns, wantField := range tc.wantSecretNS {
97+
require.Contains(t, secret.Namespaces, ns)
98+
requireSelectors(t, secret.Namespaces[ns], labels.Everything(), wantField)
99+
}
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)