@@ -102,17 +102,11 @@ func apiResourcesForObjects(objs []runtime.Object) []*metav1.APIResourceList {
102
102
}
103
103
104
104
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 ... )
106
105
k8sClientFake := k8sfake .NewSimpleClientset (k8sObjs ... )
107
106
k8sClientFake .Resources = apiResourcesForObjects (append (extObjs , regObjs ... ))
108
107
opClientFake := operatorclient .NewClient (k8sClientFake , apiextensionsfake .NewSimpleClientset (extObjs ... ), apiregistrationfake .NewSimpleClientset (regObjs ... ))
109
108
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 ... )
116
110
117
111
var nsList []string
118
112
for ix := range namespaces {
@@ -1101,35 +1095,37 @@ func TestSyncOperatorGroups(t *testing.T) {
1101
1095
})
1102
1096
1103
1097
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
1109
1105
initialApis []runtime.Object
1110
- namespaces []v1.Namespace
1111
- inputGroup v1alpha2.OperatorGroup
1112
1106
expectedStatus v1alpha2.OperatorGroupStatus
1113
1107
expectedAnnotation map [string ]string
1114
1108
}{
1115
1109
{
1116
1110
name : "operator group with no matching namespace, no CSVs" ,
1117
1111
expectedEqual : true ,
1118
- namespaces : []v1. Namespace {
1119
- {
1112
+ initialObjs : []runtime. Object {
1113
+ & v1alpha2. OperatorGroup {
1120
1114
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
+ },
1122
1122
},
1123
1123
},
1124
1124
},
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 ,
1133
1129
},
1134
1130
},
1135
1131
},
@@ -1138,25 +1134,27 @@ func TestSyncOperatorGroups(t *testing.T) {
1138
1134
{
1139
1135
name : "operator group with matching namespace, no CSVs" ,
1140
1136
expectedEqual : true ,
1141
- namespaces : []v1. Namespace {
1142
- {
1137
+ initialObjs : []runtime. Object {
1138
+ & v1alpha2. OperatorGroup {
1143
1139
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
+ },
1147
1149
},
1148
1150
},
1149
1151
},
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" },
1160
1158
},
1161
1159
},
1162
1160
},
@@ -1176,16 +1174,19 @@ func TestSyncOperatorGroups(t *testing.T) {
1176
1174
{
1177
1175
name : "operator group with matching namespace, CSV present" ,
1178
1176
expectedEqual : false ,
1179
- namespaces : []v1. Namespace {
1180
- {
1177
+ initialObjs : []runtime. Object {
1178
+ & v1alpha2. OperatorGroup {
1181
1179
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
+ },
1185
1188
},
1186
1189
},
1187
- },
1188
- initialCsvs : []runtime.Object {
1189
1190
csv ("csv1" ,
1190
1191
testNS ,
1191
1192
"" ,
@@ -1195,20 +1196,15 @@ func TestSyncOperatorGroups(t *testing.T) {
1195
1196
v1alpha1 .CSVPhaseSucceeded ,
1196
1197
),
1197
1198
},
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" },
1210
1205
},
1211
1206
},
1207
+ deployment ("csv1-dep1" , testNS ),
1212
1208
},
1213
1209
expectedStatus : v1alpha2.OperatorGroupStatus {
1214
1210
Namespaces : []* v1.Namespace {
@@ -1227,16 +1223,19 @@ func TestSyncOperatorGroups(t *testing.T) {
1227
1223
{
1228
1224
name : "operator group with matching namespace, CSV present" ,
1229
1225
expectedEqual : true ,
1230
- namespaces : []v1. Namespace {
1231
- {
1226
+ initialObjs : []runtime. Object {
1227
+ & v1alpha2. OperatorGroup {
1232
1228
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
+ },
1236
1237
},
1237
1238
},
1238
- },
1239
- initialCsvs : []runtime.Object {
1240
1239
csv ("csv1" ,
1241
1240
testNS ,
1242
1241
"" ,
@@ -1246,20 +1245,15 @@ func TestSyncOperatorGroups(t *testing.T) {
1246
1245
v1alpha1 .CSVPhaseSucceeded ,
1247
1246
),
1248
1247
},
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" },
1261
1254
},
1262
1255
},
1256
+ ownedDeployment ,
1263
1257
},
1264
1258
expectedStatus : v1alpha2.OperatorGroupStatus {
1265
1259
Namespaces : []* v1.Namespace {
@@ -1279,25 +1273,25 @@ func TestSyncOperatorGroups(t *testing.T) {
1279
1273
1280
1274
for _ , tc := range tests {
1281
1275
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 )
1283
1278
require .NoError (t , err )
1284
1279
1285
1280
stopCh := make (chan struct {})
1286
1281
defer func () { stopCh <- struct {}{} }()
1287
1282
ready , _ := op .Run (stopCh )
1288
1283
<- ready
1289
1284
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 )
1293
1287
1294
- err = op .syncOperatorGroups (& tc . inputGroup )
1288
+ err = op .syncOperatorGroups (operatorGroup )
1295
1289
require .NoError (t , err )
1296
- assert .Equal (t , tc .expectedStatus , tc . inputGroup .Status )
1290
+ assert .Equal (t , tc .expectedStatus , operatorGroup .Status )
1297
1291
1298
1292
if tc .expectedAnnotation != nil {
1299
1293
// assuming CSVs are in correct namespace
1300
- for _ , ns := range tc . namespaces {
1294
+ for _ , ns := range namespaceList {
1301
1295
deployments , err := op .lister .AppsV1 ().DeploymentLister ().Deployments (ns .GetName ()).List (labels .Everything ())
1302
1296
if err != nil {
1303
1297
t .Fatal (err )
0 commit comments