Skip to content

Commit 26a24c7

Browse files
committed
add unit tests
1 parent db34956 commit 26a24c7

File tree

5 files changed

+270
-104
lines changed

5 files changed

+270
-104
lines changed

pkg/controller/operators/catalog/operator_test.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,11 +1929,36 @@ func TestValidateV1CRDCompatibility(t *testing.T) {
19291929
func TestSyncRegistryServer(t *testing.T) {
19301930
namespace := "ns"
19311931

1932+
configmapCatalog := &v1alpha1.CatalogSource{
1933+
ObjectMeta: metav1.ObjectMeta{
1934+
Name: "cool-catalog",
1935+
Namespace: "cool-namespace",
1936+
UID: types.UID("catalog-uid"),
1937+
},
1938+
Spec: v1alpha1.CatalogSourceSpec{
1939+
ConfigMap: "cool-configmap",
1940+
SourceType: v1alpha1.SourceTypeInternal,
1941+
},
1942+
}
1943+
grpcCatalog := &v1alpha1.CatalogSource{
1944+
ObjectMeta: metav1.ObjectMeta{
1945+
Name: "cool-catalog",
1946+
Namespace: "cool-namespace",
1947+
UID: types.UID("catalog-uid"),
1948+
Labels: map[string]string{"olm.catalogSource": "cool-catalog"},
1949+
},
1950+
Spec: v1alpha1.CatalogSourceSpec{
1951+
Image: "catalog-image",
1952+
SourceType: v1alpha1.SourceTypeGrpc,
1953+
},
1954+
}
1955+
19321956
tests := []struct {
1933-
testName string
1934-
err error
1935-
catSrc *v1alpha1.CatalogSource
1936-
clientObjs []runtime.Object
1957+
testName string
1958+
err error
1959+
catSrc *v1alpha1.CatalogSource
1960+
clientObjs []runtime.Object
1961+
cleanRegistryServerError bool
19371962
}{
19381963
{
19391964
testName: "EmptyRegistryPoll",
@@ -1946,6 +1971,18 @@ func TestSyncRegistryServer(t *testing.T) {
19461971
},
19471972
},
19481973
},
1974+
{
1975+
testName: "CleanRegistryServerGetsCalledGrpc",
1976+
err: fmt.Errorf("could not clean stale resources: message to ensure function is getting called"),
1977+
catSrc: grpcCatalog,
1978+
cleanRegistryServerError: true,
1979+
},
1980+
{
1981+
testName: "CleanRegistryServerGetsCalledConfigMap",
1982+
err: fmt.Errorf("could not clean stale resources: message to ensure function is getting called"),
1983+
catSrc: configmapCatalog,
1984+
cleanRegistryServerError: true,
1985+
},
19491986
}
19501987

19511988
for _, tt := range tests {
@@ -1963,6 +2000,12 @@ func TestSyncRegistryServer(t *testing.T) {
19632000
EnsureRegistryServerStub: func(logger *logrus.Entry, source *v1alpha1.CatalogSource) error {
19642001
return nil
19652002
},
2003+
CleanRegistryServerStub: func(e *logrus.Entry, cs *v1alpha1.CatalogSource) error {
2004+
if tt.cleanRegistryServerError {
2005+
return fmt.Errorf("message to ensure function is getting called")
2006+
}
2007+
return nil
2008+
},
19662009
}
19672010
},
19682011
}

pkg/controller/registry/reconciler/configmap_test.go

Lines changed: 46 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/ghodss/yaml"
11-
k8slabels "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/util/labels"
1211
"github.com/sirupsen/logrus"
1312
"github.com/stretchr/testify/require"
1413
corev1 "k8s.io/api/core/v1"
@@ -29,12 +28,6 @@ import (
2928
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
3029
)
3130

32-
const (
33-
registryImageName = "test:image"
34-
runAsUser = 1001
35-
testNamespace = "testns"
36-
)
37-
3831
type fakeReconcilerConfig struct {
3932
now nowFunc
4033
k8sObjs []runtime.Object
@@ -185,66 +178,6 @@ func validConfigMapCatalogSource(configMap *corev1.ConfigMap) *v1alpha1.CatalogS
185178
}
186179
}
187180

188-
func objectsForCatalogSource(t *testing.T, catsrc *v1alpha1.CatalogSource) []runtime.Object {
189-
// the registry pod security context is derived from the defaultNamespace by default
190-
// therefore a namespace resource must always be present
191-
var objs = []runtime.Object{
192-
defaultNamespace(),
193-
}
194-
195-
switch catsrc.Spec.SourceType {
196-
case v1alpha1.SourceTypeInternal, v1alpha1.SourceTypeConfigmap:
197-
decorated := configMapCatalogSourceDecorator{catsrc, runAsUser}
198-
service, err := decorated.Service()
199-
if err != nil {
200-
t.Fatal(err)
201-
}
202-
serviceAccount := decorated.ServiceAccount()
203-
pod, err := decorated.Pod(registryImageName, defaultPodSecurityConfig)
204-
if err != nil {
205-
t.Fatal(err)
206-
}
207-
objs = append(objs,
208-
pod,
209-
service,
210-
serviceAccount,
211-
)
212-
case v1alpha1.SourceTypeGrpc:
213-
if catsrc.Spec.Image != "" {
214-
decorated := grpcCatalogSourceDecorator{CatalogSource: catsrc, createPodAsUser: runAsUser, opmImage: ""}
215-
serviceAccount := decorated.ServiceAccount()
216-
service, err := decorated.Service()
217-
if err != nil {
218-
t.Fatal(err)
219-
}
220-
pod, err := decorated.Pod(serviceAccount, defaultPodSecurityConfig)
221-
if err != nil {
222-
t.Fatal(err)
223-
}
224-
objs = append(objs,
225-
pod,
226-
service,
227-
serviceAccount,
228-
)
229-
}
230-
}
231-
232-
blockOwnerDeletion := false
233-
isController := false
234-
for _, o := range objs {
235-
mo := o.(metav1.Object)
236-
mo.SetOwnerReferences([]metav1.OwnerReference{{
237-
APIVersion: "operators.coreos.com/v1alpha1",
238-
Kind: "CatalogSource",
239-
Name: catsrc.GetName(),
240-
UID: catsrc.GetUID(),
241-
BlockOwnerDeletion: &blockOwnerDeletion,
242-
Controller: &isController,
243-
}})
244-
}
245-
return objs
246-
}
247-
248181
func modifyObjName(objs []runtime.Object, kind runtime.Object, newName string) []runtime.Object {
249182
var out []runtime.Object
250183
t := reflect.TypeOf(kind)
@@ -260,21 +193,6 @@ func modifyObjName(objs []runtime.Object, kind runtime.Object, newName string) [
260193
return out
261194
}
262195

263-
func setLabel(objs []runtime.Object, kind runtime.Object, label, value string) []runtime.Object {
264-
var out []runtime.Object
265-
t := reflect.TypeOf(kind)
266-
for _, obj := range objs {
267-
o := obj.DeepCopyObject()
268-
if reflect.TypeOf(o) == t {
269-
if accessor, err := meta.Accessor(o); err == nil {
270-
k8slabels.AddLabel(accessor.GetLabels(), label, value)
271-
}
272-
}
273-
out = append(out, o)
274-
}
275-
return out
276-
}
277-
278196
func TestConfigMapRegistryReconciler(t *testing.T) {
279197
now := func() metav1.Time { return metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC) }
280198

@@ -527,3 +445,49 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
527445
})
528446
}
529447
}
448+
449+
func TestConfigMapRegistryCleaner(t *testing.T) {
450+
validConfigMap := validConfigMap()
451+
validCatalogSource := validConfigMapCatalogSource(validConfigMap)
452+
type cluster struct {
453+
k8sObjs []runtime.Object
454+
}
455+
type in struct {
456+
cluster cluster
457+
catsrc *v1alpha1.CatalogSource
458+
}
459+
type out struct {
460+
err error
461+
}
462+
tests := []struct {
463+
testName string
464+
in in
465+
out out
466+
}{
467+
{
468+
testName: "ConfigMap/ExistingRegistry/DeletedPod",
469+
in: in{
470+
cluster: cluster{
471+
k8sObjs: withPodDeletedButNotRemoved(objectsForCatalogSource(t, validCatalogSource)),
472+
},
473+
catsrc: validCatalogSource,
474+
},
475+
},
476+
}
477+
for _, tt := range tests {
478+
t.Run(tt.testName, func(t *testing.T) {
479+
stopc := make(chan struct{})
480+
defer close(stopc)
481+
482+
factory, _ := fakeReconcilerFactory(t, stopc, withK8sObjs(tt.in.cluster.k8sObjs...))
483+
rec := factory.ReconcilerForSource(tt.in.catsrc)
484+
485+
err := rec.CleanRegistryServer(logrus.NewEntry(logrus.New()), tt.in.catsrc)
486+
487+
require.Equal(t, tt.out.err, err)
488+
if tt.out.err != nil {
489+
return
490+
}
491+
})
492+
}
493+
}

pkg/controller/registry/reconciler/grpc_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,50 @@ func TestGrpcRegistryChecker(t *testing.T) {
645645
}
646646
}
647647

648+
func TestGrpcRegistryCleaner(t *testing.T) {
649+
type cluster struct {
650+
k8sObjs []runtime.Object
651+
}
652+
type in struct {
653+
cluster cluster
654+
catsrc *v1alpha1.CatalogSource
655+
}
656+
type out struct {
657+
err error
658+
}
659+
tests := []struct {
660+
testName string
661+
in in
662+
out out
663+
}{
664+
{
665+
testName: "Grpc/ExistingRegistry/DeletedPod",
666+
in: in{
667+
cluster: cluster{
668+
k8sObjs: withPodDeletedButNotRemoved(objectsForCatalogSource(t, validGrpcCatalogSource("old-img", ""))),
669+
},
670+
catsrc: validGrpcCatalogSource("new-img", ""),
671+
},
672+
},
673+
}
674+
for _, tt := range tests {
675+
t.Run(tt.testName, func(t *testing.T) {
676+
stopc := make(chan struct{})
677+
defer close(stopc)
678+
679+
factory, _ := fakeReconcilerFactory(t, stopc, withK8sObjs(tt.in.cluster.k8sObjs...))
680+
rec := factory.ReconcilerForSource(tt.in.catsrc)
681+
682+
err := rec.CleanRegistryServer(logrus.NewEntry(logrus.New()), tt.in.catsrc)
683+
684+
require.Equal(t, tt.out.err, err)
685+
if tt.out.err != nil {
686+
return
687+
}
688+
})
689+
}
690+
}
691+
648692
func TestGetPodImageID(t *testing.T) {
649693
var table = []struct {
650694
description string

pkg/controller/registry/reconciler/reconciler_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1717
)
1818

19-
const workloadUserID = 1001
20-
const defaultPodSecurityConfig = v1alpha1.Restricted
21-
2219
func TestPodMemoryTarget(t *testing.T) {
2320
q := resource.MustParse("5Mi")
2421
var testCases = []struct {
@@ -1125,18 +1122,3 @@ func baseClusterState() []runtime.Object {
11251122
defaultNamespace(),
11261123
}
11271124
}
1128-
1129-
// defaultNamespace returns a kubernetes namespace with the assumes default settings,
1130-
// e.g. Pod Security Admission security policy label
1131-
func defaultNamespace() *corev1.Namespace {
1132-
return &corev1.Namespace{
1133-
ObjectMeta: metav1.ObjectMeta{
1134-
Name: testNamespace,
1135-
Labels: map[string]string{
1136-
// catalogsource pod security configuration depends on the defaultNamespace psa configuration
1137-
// adding restricted PSA label as this is the default
1138-
"pod-security.kubernetes.io/enforce": "restricted",
1139-
},
1140-
},
1141-
}
1142-
}

0 commit comments

Comments
 (0)