-
Notifications
You must be signed in to change notification settings - Fork 147
/
testUtils_test.go
385 lines (334 loc) · 11.5 KB
/
testUtils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package hyperconverged
import (
"context"
"fmt"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
consolev1 "github.com/openshift/api/console/v1"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
csvv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorsapiv2 "github.com/operator-framework/api/pkg/operators/v2"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
networkaddonsv1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1"
hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/alerts"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/common"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/commontestutils"
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/operands"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/components"
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
"github.com/kubevirt/hyperconverged-cluster-operator/version"
kubevirtcorev1 "kubevirt.io/api/core/v1"
cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
sspv1beta2 "kubevirt.io/ssp-operator/api/v1beta2"
)
// Mock TestRequest to simulate Reconcile() being called on an event for a watched resource
var (
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: name,
Namespace: namespace,
},
}
)
func initReconciler(client client.Client, old *ReconcileHyperConverged) *ReconcileHyperConverged {
s := commontestutils.GetScheme()
eventEmitter := commontestutils.NewEventEmitterMock()
ci := commontestutils.ClusterInfoMock{}
operandHandler := operands.NewOperandHandler(client, s, ci, eventEmitter)
upgradeMode := false
firstLoop := true
upgradeableCondition := newStubOperatorCondition()
if old != nil {
upgradeMode = old.upgradeMode
firstLoop = old.firstLoop
upgradeableCondition = old.upgradeableCondition
}
// Create a ReconcileHyperConverged object with the scheme and fake client
return &ReconcileHyperConverged{
client: client,
scheme: s,
operandHandler: operandHandler,
eventEmitter: eventEmitter,
firstLoop: firstLoop,
ownVersion: version.Version,
upgradeMode: upgradeMode,
upgradeableCondition: upgradeableCondition,
}
}
type stubCondition struct {
condition *metav1.Condition
}
func newStubOperatorCondition() hcoutil.Condition {
cond := &stubCondition{}
return cond
}
func (nc *stubCondition) Set(_ context.Context, status metav1.ConditionStatus, reason, message string) error {
nc.condition = &metav1.Condition{
Type: operatorsapiv2.Upgradeable,
Status: status,
Reason: reason,
Message: message,
}
return nil
}
func (nc *stubCondition) validate(status metav1.ConditionStatus, reason, message string) {
ExpectWithOffset(2, nc.condition).ToNot(BeNil())
ExpectWithOffset(2, nc.condition.Status).To(Equal(status))
ExpectWithOffset(2, nc.condition.Reason).To(Equal(reason))
ExpectWithOffset(2, nc.condition.Message).To(ContainSubstring(message))
}
func validateOperatorCondition(r *ReconcileHyperConverged, status metav1.ConditionStatus, reason, message string) {
cond := r.upgradeableCondition.(*stubCondition)
cond.validate(status, reason, message)
}
type BasicExpected struct {
namespace *corev1.Namespace
hco *hcov1beta1.HyperConverged
pc *schedulingv1.PriorityClass
kv *kubevirtcorev1.KubeVirt
cdi *cdiv1beta1.CDI
cna *networkaddonsv1.NetworkAddonsConfig
ssp *sspv1beta2.SSP
mService *corev1.Service
serviceMonitor *monitoringv1.ServiceMonitor
cliDownload *consolev1.ConsoleCLIDownload
cliDownloadsRoute *routev1.Route
cliDownloadsService *corev1.Service
virtioWinConfig *corev1.ConfigMap
virtioWinRole *rbacv1.Role
virtioWinRoleBinding *rbacv1.RoleBinding
hcoCRD *apiextensionsv1.CustomResourceDefinition
consolePluginDeploy *appsv1.Deployment
consoleProxyDeploy *appsv1.Deployment
consolePluginSvc *corev1.Service
consoleProxySvc *corev1.Service
consolePlugin *consolev1.ConsolePlugin
consoleConfig *operatorv1.Console
csv *csvv1alpha1.ClusterServiceVersion
}
func (be BasicExpected) toArray() []client.Object {
return []client.Object{
be.namespace,
be.hco,
be.pc,
be.kv,
be.cdi,
be.cna,
be.ssp,
be.mService,
be.serviceMonitor,
be.cliDownload,
be.cliDownloadsRoute,
be.cliDownloadsService,
be.virtioWinConfig,
be.virtioWinRole,
be.virtioWinRoleBinding,
be.hcoCRD,
be.consolePluginDeploy,
be.consoleProxyDeploy,
be.consolePluginSvc,
be.consoleProxySvc,
be.consolePlugin,
be.consoleConfig,
be.csv,
}
}
func (be BasicExpected) initClient() *commontestutils.HcoTestClient {
return commontestutils.InitClient(be.toArray())
}
func getBasicDeployment() *BasicExpected {
res := &BasicExpected{}
hco := &hcov1beta1.HyperConverged{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{hcoutil.AppLabel: name},
},
Spec: hcov1beta1.HyperConvergedSpec{},
Status: hcov1beta1.HyperConvergedStatus{
Conditions: []metav1.Condition{
{
Type: hcov1beta1.ConditionReconcileComplete,
Status: metav1.ConditionTrue,
Reason: common.ReconcileCompleted,
Message: common.ReconcileCompletedMessage,
},
},
Versions: []hcov1beta1.Version{
{
Name: hcoVersionName,
Version: version.Version,
},
},
},
}
res.hco = hco
components.GetOperatorCR().Spec.DeepCopyInto(&res.hco.Spec)
res.namespace = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: hco.Namespace,
Annotations: map[string]string{
hcoutil.OpenshiftNodeSelectorAnn: "",
},
},
}
res.pc = operands.NewKubeVirtPriorityClass(hco)
deploymentRef := metav1.OwnerReference{
APIVersion: appsv1.SchemeGroupVersion.String(),
Kind: "Deployment",
Name: "hco-operator",
UID: "1234567890",
BlockOwnerDeletion: ptr.To(false),
Controller: ptr.To(false),
}
res.mService = alerts.NewMetricsService(namespace, deploymentRef)
res.serviceMonitor = alerts.NewServiceMonitor(namespace, deploymentRef)
expectedKV, err := operands.NewKubeVirt(hco, namespace)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
expectedKV.Status.Conditions = []kubevirtcorev1.KubeVirtCondition{
{
Type: kubevirtcorev1.KubeVirtConditionAvailable,
Status: corev1.ConditionTrue,
},
{
Type: kubevirtcorev1.KubeVirtConditionProgressing,
Status: corev1.ConditionFalse,
},
{
Type: kubevirtcorev1.KubeVirtConditionDegraded,
Status: corev1.ConditionFalse,
},
}
res.kv = expectedKV
expectedCDI, err := operands.NewCDI(hco)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
expectedCDI.Status.Conditions = getGenericCompletedConditions()
res.cdi = expectedCDI
expectedCNA, err := operands.NewNetworkAddons(hco)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
expectedCNA.Status.Conditions = getGenericCompletedConditions()
res.cna = expectedCNA
expectedSSP, _, err := operands.NewSSP(hco)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
expectedSSP.Status.Conditions = getGenericCompletedConditions()
res.ssp = expectedSSP
expectedCliDownload := operands.NewConsoleCLIDownload(hco)
res.cliDownload = expectedCliDownload
expectedCliDownloadsRoute := operands.NewCliDownloadsRoute(hco)
res.cliDownloadsRoute = expectedCliDownloadsRoute
expectedCliDownloadsService := operands.NewCliDownloadsService(hco)
res.cliDownloadsService = expectedCliDownloadsService
expectedVirtioWinConfig, err := operands.NewVirtioWinCm(hco)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
res.virtioWinConfig = expectedVirtioWinConfig
expectedVirtioWinRole := operands.NewVirtioWinCmReaderRole(hco)
res.virtioWinRole = expectedVirtioWinRole
expectedVirtioWinRoleBinding := operands.NewVirtioWinCmReaderRoleBinding(hco)
res.virtioWinRoleBinding = expectedVirtioWinRoleBinding
expectedConsolePluginDeployment := operands.NewKvUIPluginDeployment(hco)
res.consolePluginDeploy = expectedConsolePluginDeployment
expectedConsoleProxyDeployment := operands.NewKvUIProxyDeployment(hco)
res.consoleProxyDeploy = expectedConsoleProxyDeployment
expectedConsolePluginService := operands.NewKvUIPluginSvc(hco)
res.consolePluginSvc = expectedConsolePluginService
expectedConsoleProxyService := operands.NewKvUIProxySvc(hco)
res.consoleProxySvc = expectedConsoleProxyService
expectedConsolePlugin := operands.NewKVConsolePlugin(hco)
res.consolePlugin = expectedConsolePlugin
expectedConsoleConfig := &operatorv1.Console{
TypeMeta: metav1.TypeMeta{
Kind: "Console",
APIVersion: "operator.openshift.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
}
res.consoleConfig = expectedConsoleConfig
hcoCrd := &apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "hyperconvergeds.hco.kubevirt.io",
},
}
res.hcoCRD = hcoCrd
csv := hcoutil.GetClusterInfo().GetCSV()
res.csv = csv
return res
}
// returns the HCO after reconcile, and the returned requeue
func doReconcile(cl client.Client, hco *hcov1beta1.HyperConverged, old *ReconcileHyperConverged) (*hcov1beta1.HyperConverged, *ReconcileHyperConverged, bool) {
r := initReconciler(cl, old)
r.firstLoop = false
r.ownVersion = os.Getenv(hcoutil.HcoKvIoVersionName)
if r.ownVersion == "" {
r.ownVersion = version.Version
}
res, err := r.Reconcile(context.TODO(), request)
Expect(err).ToNot(HaveOccurred())
foundResource := &hcov1beta1.HyperConverged{}
Expect(
cl.Get(context.TODO(),
types.NamespacedName{Name: hco.Name, Namespace: hco.Namespace},
foundResource),
).ToNot(HaveOccurred())
return foundResource, r, res.Requeue
}
func getGenericCompletedConditions() []conditionsv1.Condition {
return []conditionsv1.Condition{
{
Type: conditionsv1.ConditionAvailable,
Status: corev1.ConditionTrue,
},
{
Type: conditionsv1.ConditionProgressing,
Status: corev1.ConditionFalse,
},
{
Type: conditionsv1.ConditionDegraded,
Status: corev1.ConditionFalse,
},
}
}
func getGenericProgressingConditions() []conditionsv1.Condition {
return []conditionsv1.Condition{
{
Type: conditionsv1.ConditionAvailable,
Status: corev1.ConditionFalse,
},
{
Type: conditionsv1.ConditionProgressing,
Status: corev1.ConditionTrue,
},
{
Type: conditionsv1.ConditionDegraded,
Status: corev1.ConditionFalse,
},
}
}
func checkAvailability(hco *hcov1beta1.HyperConverged, expected metav1.ConditionStatus) {
found := false
for _, cond := range hco.Status.Conditions {
if cond.Type == hcov1beta1.ConditionAvailable {
found = true
ExpectWithOffset(1, cond.Status).To(Equal(expected))
break
}
}
if !found {
Fail(fmt.Sprintf(`Can't find 'Available' condition; %+v`, hco.Status.Conditions))
}
}