forked from red-hat-storage/ocs-operator
/
ocsinitialization_controller_test.go
409 lines (371 loc) · 12.2 KB
/
ocsinitialization_controller_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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package ocsinitialization
import (
"context"
"fmt"
"testing"
secv1 "github.com/openshift/api/security/v1"
fakeSecClient "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1/fake"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
v1 "github.com/openshift/ocs-operator/api/v1"
statusutil "github.com/openshift/ocs-operator/controllers/util"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
testingClient "k8s.io/client-go/testing"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
var successfulReconcileConditions = map[conditionsv1.ConditionType]corev1.ConditionStatus{
conditionsv1.ConditionAvailable: corev1.ConditionTrue,
conditionsv1.ConditionProgressing: corev1.ConditionFalse,
conditionsv1.ConditionDegraded: corev1.ConditionFalse,
conditionsv1.ConditionUpgradeable: corev1.ConditionTrue,
v1.ConditionReconcileComplete: corev1.ConditionTrue,
}
func getTestParams(mockNamespace bool, t *testing.T) (v1.OCSInitialization, reconcile.Request, OCSInitializationReconciler) {
var request reconcile.Request
if mockNamespace {
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "test",
Namespace: "test-ns",
},
}
} else {
request = reconcile.Request{NamespacedName: InitNamespacedName()}
}
ocs := v1.OCSInitialization{
ObjectMeta: metav1.ObjectMeta{
Name: request.Name,
Namespace: request.Namespace,
},
}
reconciler := getReconciler(t, &ocs)
//The fake client stores the objects after adding a resource version to
//them. This is a breaking change introduced in
//https://github.com/kubernetes-sigs/controller-runtime/pull/1306.
//Therefore we cannot use the fake object that we provided as input to the
//the fake client and should use the object obtained from the Get
//operation.
_ = reconciler.Client.Get(context.TODO(), request.NamespacedName, &ocs)
return ocs, request, reconciler
}
func getReconciler(t *testing.T, objs ...client.Object) OCSInitializationReconciler {
scheme := createFakeScheme(t)
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
secClient := &fakeSecClient.FakeSecurityV1{Fake: &testingClient.Fake{}}
log := logf.Log.WithName("controller_storagecluster_test")
return OCSInitializationReconciler{
Scheme: scheme,
Client: client,
SecurityClient: secClient,
Log: log,
RookImage: "rook/ceph",
}
}
func createFakeScheme(t *testing.T) *runtime.Scheme {
scheme, err := v1.SchemeBuilder.Build()
if err != nil {
assert.Fail(t, "unable to build scheme")
}
err = appsv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add appsv1 scheme")
}
err = corev1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add corev1 scheme")
}
err = monitoringv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add monitoringv1 scheme")
}
err = secv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add securityv1 scheme")
}
return scheme
}
func TestReconcilerImplementsInterface(t *testing.T) {
reconciler := OCSInitializationReconciler{}
var i interface{} = &reconciler
_, ok := i.(reconcile.Reconciler)
assert.True(t, ok)
}
func TestNonWatchedResourceNotFound(t *testing.T) {
testcases := []struct {
label string
name string
namespace string
}{
{
label: "Case 1", // non-watched resource (name = "foo") not found
name: "foo",
namespace: "test-ns",
},
{
label: "Case 2", // non-watched resource (namespace = "foo") not found
name: "test",
namespace: "foo",
},
}
for _, tc := range testcases {
_, request, reconciler := getTestParams(true, t)
request.Name = tc.name
request.Namespace = tc.namespace
_, err := reconciler.Reconcile(context.TODO(), request)
assert.NoErrorf(t, err, "[%s]: failed to reconcile with non watched resource", tc.label)
}
}
//nolint //ignoring errcheck causing the failures
func TestNonWatchedResourceFound(t *testing.T) {
testcases := []struct {
label string
name string
namespace string
}{
{
label: "Case 1", // non-watched resource (name = "foo") already created
name: "foo",
namespace: "test-ns",
},
{
label: "Case 2", // non-watched resource (namespace = "foo") already created
name: "test",
namespace: "foo",
},
}
for _, tc := range testcases {
ocs, request, reconciler := getTestParams(true, t)
request.Name = tc.name
request.Namespace = tc.namespace
ocs.Name = tc.name
ocs.Namespace = tc.namespace
err := reconciler.Client.Create(nil, &ocs)
assert.NoErrorf(t, err, "[%s]: failed CREATE of non watched resource", tc.label)
_, err = reconciler.Reconcile(context.TODO(), request)
assert.NoErrorf(t, err, "[%s]: failed to reconcile with non watched resource", tc.label)
actual := &v1.OCSInitialization{}
err = reconciler.Client.Get(nil, request.NamespacedName, actual)
assert.NoErrorf(t, err, "[%s]: failed GET of actual resource", tc.label)
assert.Equalf(t, statusutil.PhaseIgnored, actual.Status.Phase, "[%s]: failed to update phase of non watched resource that already exists OCS:\n%v", tc.label, actual)
}
}
//nolint //ignoring errcheck as causing failures
func TestCreateWatchedResource(t *testing.T) {
testcases := []struct {
label string
alreadyCreated bool
}{
{
label: "Case 1", // ocsInit resource not created already before reconcile
alreadyCreated: false,
},
{
label: "Case 2", // ocsInit resource already created before reconcile
alreadyCreated: true,
},
}
for _, tc := range testcases {
ocs, request, reconciler := getTestParams(false, t)
if tc.alreadyCreated {
reconciler.Client.Create(nil, &ocs)
} else {
err := reconciler.Client.Delete(nil, &ocs)
assert.NoError(t, err)
err = reconciler.Client.Get(nil, request.NamespacedName, &ocs)
assert.Error(t, err)
}
_, err := reconciler.Reconcile(context.TODO(), request)
assert.NoError(t, err)
obj := v1.OCSInitialization{}
_ = reconciler.Client.Get(nil, request.NamespacedName, &obj)
assert.Equalf(t, obj.Name, request.Name, "[%s]: failed to create ocsInit resource with correct name", tc.label)
assert.Equalf(t, obj.Namespace, request.Namespace, "[%s]: failed to create ocsInit resource with correct namespace", tc.label)
}
}
// TestCreateSCCs ensures that the reconciler creates the SCCs if they are missing.
func TestCreateSCCs(t *testing.T) {
testcases := []struct {
label string
sscCreated bool
}{
{
label: "Case 1", // sscs already created before reconcile
sscCreated: true,
},
{
label: "Case 2",
sscCreated: false, // sscs not created before reconcile
},
}
for _, tc := range testcases {
ocs, request, reconciler := getTestParams(false, t)
if tc.sscCreated {
ocs.Status.SCCsCreated = true
err := reconciler.Client.Update(context.TODO(), &ocs)
assert.NoErrorf(t, err, "[%s]: failed to update ocsInit status", tc.label)
}
_, err := reconciler.Reconcile(context.TODO(), request)
assert.NoErrorf(t, err, "[%s]: failed to reconcile ocsInit", tc.label)
obj := v1.OCSInitialization{}
_ = reconciler.Client.Get(context.TODO(), request.NamespacedName, &obj)
for cType, status := range successfulReconcileConditions {
found := assertCondition(obj, cType, status)
if !found {
assert.Fail(t, fmt.Sprintf("Expected status condition %s %s not found", cType, status))
}
}
}
}
func TestReconcileCompleteConditions(t *testing.T) {
_, request, reconciler := getTestParams(false, t)
_, err := reconciler.Reconcile(context.TODO(), request)
assert.NoError(t, err)
obj := v1.OCSInitialization{}
_ = reconciler.Client.Get(context.TODO(), request.NamespacedName, &obj)
assert.NotEmpty(t, obj.Status.Conditions)
assert.Len(t, obj.Status.Conditions, 5)
for cType, status := range successfulReconcileConditions {
found := assertCondition(obj, cType, status)
if !found {
assert.Fail(t, "expected status condition not found")
}
}
}
func assertCondition(ocs v1.OCSInitialization, conditionType conditionsv1.ConditionType, status corev1.ConditionStatus) bool {
for _, objCondition := range ocs.Status.Conditions {
if objCondition.Type == conditionType {
if objCondition.Status == status {
return true
}
}
}
return false
}
func TestEnsureToolsDeployment(t *testing.T) {
testcases := []struct {
label string
enableCephTools bool
}{
{
label: "Case 1",
enableCephTools: true,
},
{
label: "Case 2",
enableCephTools: false,
},
}
for _, tc := range testcases {
ocs, request, reconciler := getTestParams(false, t)
ocs.Spec.EnableCephTools = tc.enableCephTools
err := reconciler.ensureToolsDeployment(&ocs)
assert.NoErrorf(t, err, "[%s] failed to create ceph tools", tc.label)
if tc.enableCephTools {
cephtoolsDeployment := &appsv1.Deployment{}
err := reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: rookCephToolDeploymentName, Namespace: request.Namespace}, cephtoolsDeployment)
assert.NoErrorf(t, err, "[%s] failed to create ceph tools", tc.label)
}
}
}
func TestEnsureToolsDeploymentUpdate(t *testing.T) {
var replicaTwo int32 = 2
testcases := []struct {
label string
enableCephTools bool
}{
{
label: "Case 1", // existing ceph tools pod should get updated
enableCephTools: true,
},
{
label: "Case 2", // existing ceph tool pod should get deleted
enableCephTools: false,
},
}
for _, tc := range testcases {
ocs, request, reconciler := getTestParams(false, t)
ocs.Spec.EnableCephTools = tc.enableCephTools
cephTools := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: rookCephToolDeploymentName,
Namespace: request.Namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicaTwo,
},
}
err := reconciler.Client.Create(context.TODO(), cephTools)
assert.NoError(t, err)
err = reconciler.ensureToolsDeployment(&ocs)
assert.NoErrorf(t, err, "[%s] failed to create ceph tools deployment", tc.label)
cephtoolsDeployment := &appsv1.Deployment{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: rookCephToolDeploymentName, Namespace: request.Namespace}, cephtoolsDeployment)
if tc.enableCephTools {
assert.NoErrorf(t, err, "[%s] failed to get ceph tools deployment", tc.label)
assert.Equalf(t, int32(1), *cephtoolsDeployment.Spec.Replicas, "[%s] failed to update the ceph tools pod", tc.label)
} else {
assert.Errorf(t, err, "[%s] failed to delete ceph tools deployment when it was disabled in the spec", tc.label)
}
}
}
func TestEnsureRookCephOperatorConfig(t *testing.T) {
ocsinit := &v1.OCSInitialization{
ObjectMeta: metav1.ObjectMeta{
Name: "ocsinit",
Namespace: "openshift-storage",
},
}
ocsinitFalse := ocsinit
ocsinitFalse.Status.RookCephOperatorConfigCreated = false
ocsinitTrue := ocsinit
ocsinitTrue.Status.RookCephOperatorConfigCreated = true
type args struct {
reconciler OCSInitializationReconciler
initialData *v1.OCSInitialization
}
testcases := []struct {
name string
args args
wantErr bool
}{
{
name: "RookCephOperatorConfigCreated not set",
args: args{
initialData: ocsinit,
reconciler: getReconciler(t, ocsinit),
},
wantErr: false,
},
{
name: "RookCephOperatorConfigCreated is false",
args: args{
initialData: ocsinitFalse,
reconciler: getReconciler(t, ocsinitFalse),
},
wantErr: false,
},
{
name: "RookCephOperatorConfigCreated is true",
args: args{
initialData: ocsinitTrue,
reconciler: getReconciler(t, ocsinitTrue),
},
wantErr: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
if err := tc.args.reconciler.ensureRookCephOperatorConfig(tc.args.initialData); (err != nil) != tc.wantErr {
t.Errorf("ReconcileOCSInitialization.ensureRookCephOperatorConfig() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}