Skip to content

Commit cd201c5

Browse files
author
Jeff Peeler
committed
fix(unit): create OperatorGroups with initial fakes
Instead of creating operator groups in a "second phase", create the resources at the same time along with everything else. This removes a small amount of namespace specific code from NewFakeOperator and makes things more consistent.
1 parent ac50ddc commit cd201c5

File tree

1 file changed

+79
-85
lines changed

1 file changed

+79
-85
lines changed

pkg/controller/operators/olm/operator_test.go

Lines changed: 79 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,11 @@ func apiResourcesForObjects(objs []runtime.Object) []*metav1.APIResourceList {
102102
}
103103

104104
func NewFakeOperator(clientObjs []runtime.Object, k8sObjs []runtime.Object, extObjs []runtime.Object, regObjs []runtime.Object, resolver install.StrategyResolverInterface, namespaces []v1.Namespace) (*Operator, error) {
105-
clientFake := fake.NewSimpleClientset(clientObjs...)
106105
k8sClientFake := k8sfake.NewSimpleClientset(k8sObjs...)
107106
k8sClientFake.Resources = apiResourcesForObjects(append(extObjs, regObjs...))
108107
opClientFake := operatorclient.NewClient(k8sClientFake, apiextensionsfake.NewSimpleClientset(extObjs...), apiregistrationfake.NewSimpleClientset(regObjs...))
109108
annotations := map[string]string{"test": "annotation"}
110-
for _, ns := range namespaces {
111-
_, err := opClientFake.KubernetesInterface().CoreV1().Namespaces().Create(&ns)
112-
if err != nil {
113-
return nil, err
114-
}
115-
}
109+
clientFake := fake.NewSimpleClientset(clientObjs...)
116110

117111
var nsList []string
118112
for ix := range namespaces {
@@ -1101,35 +1095,37 @@ func TestSyncOperatorGroups(t *testing.T) {
11011095
})
11021096

11031097
tests := []struct {
1104-
name string
1105-
expectedEqual bool
1106-
initialCsvs []runtime.Object
1107-
initialCrds []runtime.Object
1108-
initialObjs []runtime.Object
1098+
name string
1099+
expectedEqual bool
1100+
// first item in initialObjs must be an OperatorGroup
1101+
initialObjs []runtime.Object
1102+
initialCrds []runtime.Object
1103+
// first item in initialK8sObjs must be a namespace
1104+
initialK8sObjs []runtime.Object
11091105
initialApis []runtime.Object
1110-
namespaces []v1.Namespace
1111-
inputGroup v1alpha2.OperatorGroup
11121106
expectedStatus v1alpha2.OperatorGroupStatus
11131107
expectedAnnotation map[string]string
11141108
}{
11151109
{
11161110
name: "operator group with no matching namespace, no CSVs",
11171111
expectedEqual: true,
1118-
namespaces: []v1.Namespace{
1119-
{
1112+
initialObjs: []runtime.Object{
1113+
&v1alpha2.OperatorGroup{
11201114
ObjectMeta: metav1.ObjectMeta{
1121-
Name: testNS,
1115+
Name: "operator-group-1",
1116+
Namespace: testNS,
1117+
},
1118+
Spec: v1alpha2.OperatorGroupSpec{
1119+
Selector: metav1.LabelSelector{
1120+
MatchLabels: aLabel,
1121+
},
11221122
},
11231123
},
11241124
},
1125-
inputGroup: v1alpha2.OperatorGroup{
1126-
ObjectMeta: metav1.ObjectMeta{
1127-
Name: "operator-group-1",
1128-
Namespace: testNS,
1129-
},
1130-
Spec: v1alpha2.OperatorGroupSpec{
1131-
Selector: metav1.LabelSelector{
1132-
MatchLabels: aLabel,
1125+
initialK8sObjs: []runtime.Object{
1126+
&v1.Namespace{
1127+
ObjectMeta: metav1.ObjectMeta{
1128+
Name: testNS,
11331129
},
11341130
},
11351131
},
@@ -1138,25 +1134,27 @@ func TestSyncOperatorGroups(t *testing.T) {
11381134
{
11391135
name: "operator group with matching namespace, no CSVs",
11401136
expectedEqual: true,
1141-
namespaces: []v1.Namespace{
1142-
{
1137+
initialObjs: []runtime.Object{
1138+
&v1alpha2.OperatorGroup{
11431139
ObjectMeta: metav1.ObjectMeta{
1144-
Name: testNS,
1145-
Labels: aLabel,
1146-
Annotations: map[string]string{"test": "annotation"},
1140+
Name: "operator-group-1",
1141+
Namespace: testNS,
1142+
},
1143+
Spec: v1alpha2.OperatorGroupSpec{
1144+
Selector: metav1.LabelSelector{
1145+
MatchLabels: map[string]string{
1146+
"app": "matchLabel",
1147+
},
1148+
},
11471149
},
11481150
},
11491151
},
1150-
inputGroup: v1alpha2.OperatorGroup{
1151-
ObjectMeta: metav1.ObjectMeta{
1152-
Name: "operator-group-1",
1153-
Namespace: testNS,
1154-
},
1155-
Spec: v1alpha2.OperatorGroupSpec{
1156-
Selector: metav1.LabelSelector{
1157-
MatchLabels: map[string]string{
1158-
"app": "matchLabel",
1159-
},
1152+
initialK8sObjs: []runtime.Object{
1153+
&v1.Namespace{
1154+
ObjectMeta: metav1.ObjectMeta{
1155+
Name: testNS,
1156+
Labels: aLabel,
1157+
Annotations: map[string]string{"test": "annotation"},
11601158
},
11611159
},
11621160
},
@@ -1176,16 +1174,19 @@ func TestSyncOperatorGroups(t *testing.T) {
11761174
{
11771175
name: "operator group with matching namespace, CSV present",
11781176
expectedEqual: false,
1179-
namespaces: []v1.Namespace{
1180-
{
1177+
initialObjs: []runtime.Object{
1178+
&v1alpha2.OperatorGroup{
11811179
ObjectMeta: metav1.ObjectMeta{
1182-
Name: testNS,
1183-
Labels: aLabel,
1184-
Annotations: map[string]string{"test": "annotation"},
1180+
Name: "operator-group-1",
1181+
Namespace: testNS,
1182+
Labels: aLabel,
1183+
},
1184+
Spec: v1alpha2.OperatorGroupSpec{
1185+
Selector: metav1.LabelSelector{
1186+
MatchLabels: aLabel,
1187+
},
11851188
},
11861189
},
1187-
},
1188-
initialCsvs: []runtime.Object{
11891190
csv("csv1",
11901191
testNS,
11911192
"",
@@ -1195,20 +1196,15 @@ func TestSyncOperatorGroups(t *testing.T) {
11951196
v1alpha1.CSVPhaseSucceeded,
11961197
),
11971198
},
1198-
initialObjs: []runtime.Object{
1199-
deployment("csv1-dep1", testNS),
1200-
},
1201-
inputGroup: v1alpha2.OperatorGroup{
1202-
ObjectMeta: metav1.ObjectMeta{
1203-
Name: "operator-group-1",
1204-
Namespace: testNS,
1205-
Labels: aLabel,
1206-
},
1207-
Spec: v1alpha2.OperatorGroupSpec{
1208-
Selector: metav1.LabelSelector{
1209-
MatchLabels: aLabel,
1199+
initialK8sObjs: []runtime.Object{
1200+
&v1.Namespace{
1201+
ObjectMeta: metav1.ObjectMeta{
1202+
Name: testNS,
1203+
Labels: aLabel,
1204+
Annotations: map[string]string{"test": "annotation"},
12101205
},
12111206
},
1207+
deployment("csv1-dep1", testNS),
12121208
},
12131209
expectedStatus: v1alpha2.OperatorGroupStatus{
12141210
Namespaces: []*v1.Namespace{
@@ -1227,16 +1223,19 @@ func TestSyncOperatorGroups(t *testing.T) {
12271223
{
12281224
name: "operator group with matching namespace, CSV present",
12291225
expectedEqual: true,
1230-
namespaces: []v1.Namespace{
1231-
{
1226+
initialObjs: []runtime.Object{
1227+
&v1alpha2.OperatorGroup{
12321228
ObjectMeta: metav1.ObjectMeta{
1233-
Name: testNS,
1234-
Labels: aLabel,
1235-
Annotations: map[string]string{"test": "annotation"},
1229+
Name: "operator-group-1",
1230+
Namespace: testNS,
1231+
Labels: aLabel,
1232+
},
1233+
Spec: v1alpha2.OperatorGroupSpec{
1234+
Selector: metav1.LabelSelector{
1235+
MatchLabels: aLabel,
1236+
},
12361237
},
12371238
},
1238-
},
1239-
initialCsvs: []runtime.Object{
12401239
csv("csv1",
12411240
testNS,
12421241
"",
@@ -1246,20 +1245,15 @@ func TestSyncOperatorGroups(t *testing.T) {
12461245
v1alpha1.CSVPhaseSucceeded,
12471246
),
12481247
},
1249-
initialObjs: []runtime.Object{
1250-
ownedDeployment,
1251-
},
1252-
inputGroup: v1alpha2.OperatorGroup{
1253-
ObjectMeta: metav1.ObjectMeta{
1254-
Name: "operator-group-1",
1255-
Namespace: testNS,
1256-
Labels: aLabel,
1257-
},
1258-
Spec: v1alpha2.OperatorGroupSpec{
1259-
Selector: metav1.LabelSelector{
1260-
MatchLabels: aLabel,
1248+
initialK8sObjs: []runtime.Object{
1249+
&v1.Namespace{
1250+
ObjectMeta: metav1.ObjectMeta{
1251+
Name: testNS,
1252+
Labels: aLabel,
1253+
Annotations: map[string]string{"test": "annotation"},
12611254
},
12621255
},
1256+
ownedDeployment,
12631257
},
12641258
expectedStatus: v1alpha2.OperatorGroupStatus{
12651259
Namespaces: []*v1.Namespace{
@@ -1279,25 +1273,25 @@ func TestSyncOperatorGroups(t *testing.T) {
12791273

12801274
for _, tc := range tests {
12811275
t.Run(tc.name, func(t *testing.T) {
1282-
op, err := NewFakeOperator(tc.initialCsvs, tc.initialObjs, tc.initialCrds, tc.initialApis, &install.StrategyResolver{}, tc.namespaces)
1276+
namespaceList := []v1.Namespace{*tc.initialK8sObjs[0].(*v1.Namespace)}
1277+
op, err := NewFakeOperator(tc.initialObjs, tc.initialK8sObjs, tc.initialCrds, tc.initialApis, &install.StrategyResolver{}, namespaceList)
12831278
require.NoError(t, err)
12841279

12851280
stopCh := make(chan struct{})
12861281
defer func() { stopCh <- struct{}{} }()
12871282
ready, _ := op.Run(stopCh)
12881283
<-ready
12891284

1290-
// Could not put this in initialObjs - got "no kind is registered for the type v1alpha2.OperatorGroup"
1291-
_, err = op.client.OperatorsV1alpha2().OperatorGroups(tc.inputGroup.Namespace).Create(&tc.inputGroup)
1292-
require.NoError(t, err)
1285+
operatorGroup, ok := tc.initialObjs[0].(*v1alpha2.OperatorGroup)
1286+
require.True(t, ok)
12931287

1294-
err = op.syncOperatorGroups(&tc.inputGroup)
1288+
err = op.syncOperatorGroups(operatorGroup)
12951289
require.NoError(t, err)
1296-
assert.Equal(t, tc.expectedStatus, tc.inputGroup.Status)
1290+
assert.Equal(t, tc.expectedStatus, operatorGroup.Status)
12971291

12981292
if tc.expectedAnnotation != nil {
12991293
// assuming CSVs are in correct namespace
1300-
for _, ns := range tc.namespaces {
1294+
for _, ns := range namespaceList {
13011295
deployments, err := op.lister.AppsV1().DeploymentLister().Deployments(ns.GetName()).List(labels.Everything())
13021296
if err != nil {
13031297
t.Fatal(err)

0 commit comments

Comments
 (0)