/
controller_test.go
374 lines (349 loc) · 11.6 KB
/
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
package defaultstorageclass
import (
"context"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
cfgv1 "github.com/openshift/api/config/v1"
opv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/cluster-storage-operator/assets"
"github.com/openshift/cluster-storage-operator/pkg/csoclients"
"github.com/openshift/cluster-storage-operator/pkg/operatorclient"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceread"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type testContext struct {
controller factory.Controller
clients *csoclients.Clients
}
type testObjects struct {
storage *opv1.Storage
infrastructure *cfgv1.Infrastructure
storageClasses []*storagev1.StorageClass
}
type operatorTest struct {
name string
initialObjects testObjects
expectedObjects testObjects
expectErr bool
}
func newController(test operatorTest) *testContext {
// Convert to []runtime.Object
initialObjects := &csoclients.FakeTestObjects{}
for _, c := range test.initialObjects.storageClasses {
initialObjects.CoreObjects = append(initialObjects.CoreObjects, c)
}
if test.initialObjects.storage != nil {
initialObjects.OperatorObjects = []runtime.Object{test.initialObjects.storage}
}
if test.initialObjects.infrastructure != nil {
initialObjects.ConfigObjects = []runtime.Object{test.initialObjects.infrastructure}
}
clients := csoclients.NewFakeClients(initialObjects)
recorder := events.NewInMemoryRecorder("operator")
ctrl := NewController(clients, recorder)
return &testContext{
controller: ctrl,
clients: clients,
}
}
type storageClassModifier func(class *storagev1.StorageClass) *storagev1.StorageClass
func getPlatformStorageClass(filename string, modifiers ...storageClassModifier) *storagev1.StorageClass {
assetBytes, err := assets.ReadFile(filename)
if err != nil {
panic(err)
}
sc := resourceread.ReadStorageClassV1OrDie(assetBytes)
for _, modifier := range modifiers {
sc = modifier(sc)
}
return sc
}
func withNoDefault(class *storagev1.StorageClass) *storagev1.StorageClass {
class.Annotations = nil
return class
}
func getInfrastructure(platformType cfgv1.PlatformType) *cfgv1.Infrastructure {
return &cfgv1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: infraConfigName,
},
Status: cfgv1.InfrastructureStatus{
PlatformStatus: &cfgv1.PlatformStatus{
Type: platformType,
},
},
}
}
func getAzureStackHubInfrastructure() *cfgv1.Infrastructure {
return &cfgv1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: infraConfigName,
},
Status: cfgv1.InfrastructureStatus{
PlatformStatus: &cfgv1.PlatformStatus{
Type: cfgv1.AzurePlatformType,
Azure: &cfgv1.AzurePlatformStatus{
CloudName: cfgv1.AzureStackCloud,
},
},
},
}
}
type crModifier func(cr *opv1.Storage) *opv1.Storage
func getCR(modifiers ...crModifier) *opv1.Storage {
cr := &opv1.Storage{
ObjectMeta: metav1.ObjectMeta{Name: operatorclient.GlobalConfigName},
Spec: opv1.StorageSpec{
OperatorSpec: opv1.OperatorSpec{
ManagementState: opv1.Managed,
},
},
Status: opv1.StorageStatus{},
}
for _, modifier := range modifiers {
cr = modifier(cr)
}
return cr
}
func withTrueConditions(conditions ...string) crModifier {
return func(i *opv1.Storage) *opv1.Storage {
if i.Status.Conditions == nil {
i.Status.Conditions = []opv1.OperatorCondition{}
}
for _, c := range conditions {
i.Status.Conditions = append(i.Status.Conditions, opv1.OperatorCondition{
Type: c,
Status: opv1.ConditionTrue,
})
}
return i
}
}
func withFalseConditions(conditions ...string) crModifier {
return func(i *opv1.Storage) *opv1.Storage {
if i.Status.Conditions == nil {
i.Status.Conditions = []opv1.OperatorCondition{}
}
for _, c := range conditions {
i.Status.Conditions = append(i.Status.Conditions, opv1.OperatorCondition{
Type: c,
Status: opv1.ConditionFalse,
})
}
return i
}
}
func TestSync(t *testing.T) {
tests := []operatorTest{
{
// Default storage class is deployed if it does not exist
name: "initial AWS deployment",
initialObjects: testObjects{
storage: getCR(),
infrastructure: getInfrastructure(cfgv1.AWSPlatformType),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml")},
},
expectErr: false,
},
{
// The controller reports Disable on unsupported platforms
name: "initial unsupported platform deployment",
initialObjects: testObjects{
storage: getCR(),
infrastructure: getInfrastructure(cfgv1.BareMetalPlatformType),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+"Disabled", conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
},
expectErr: false,
},
{
// Everything is deployed and the controller does nothing
name: "everything deployed",
initialObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml")},
infrastructure: getInfrastructure(cfgv1.AWSPlatformType),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml")},
},
expectErr: false,
},
{
// The controller does not set default storage class when removed by user
name: "default storage class removed by user",
initialObjects: testObjects{
storage: getCR(),
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml", withNoDefault)},
infrastructure: getInfrastructure(cfgv1.AWSPlatformType),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml", withNoDefault)},
},
expectErr: false,
},
{
// The controller returns error - missing Available is added
name: "infrastructure not found",
initialObjects: testObjects{
storage: getCR(),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
),
},
expectErr: true,
},
{
// The controller syncs fine when azurestackhub is used and does not create a storage class
name: "azurestackhub",
initialObjects: testObjects{
storage: getCR(),
infrastructure: getAzureStackHubInfrastructure(),
},
expectedObjects: testObjects{
storage: getCR(
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
),
},
expectErr: false,
},
{
// The controller returns error + Available is True -> not flipped to False
name: "available not false after error",
initialObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
},
expectedObjects: testObjects{
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
},
expectErr: true,
},
{
// The controller flips Avialable=False to True after a successful sync
name: "available=False set to True after OK",
initialObjects: testObjects{
infrastructure: getInfrastructure(cfgv1.AWSPlatformType),
storage: getCR(
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
},
expectedObjects: testObjects{
storageClasses: []*storagev1.StorageClass{getPlatformStorageClass("storageclasses/aws.yaml")},
storage: getCR(
withTrueConditions(conditionsPrefix+opv1.OperatorStatusTypeAvailable),
withFalseConditions(conditionsPrefix+opv1.OperatorStatusTypeProgressing),
),
},
expectErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Initialize
ctx := newController(test)
finish, cancel := context.WithCancel(context.TODO())
defer cancel()
csoclients.StartInformers(ctx.clients, finish.Done())
csoclients.WaitForSync(ctx.clients, finish.Done())
// Act
err := ctx.controller.Sync(context.TODO(), nil)
// Assert
// Check error
if err != nil && !test.expectErr {
t.Errorf("sync() returned unexpected error: %v", err)
}
if err == nil && test.expectErr {
t.Error("sync() unexpectedly succeeded when error was expected")
}
// Check expectedObjects.storage
if test.expectedObjects.storage != nil {
actualStorage, err := ctx.clients.OperatorClientSet.OperatorV1().Storages().Get(context.TODO(), "cluster", metav1.GetOptions{})
if err != nil {
t.Errorf("Failed to get Storage: %v", err)
}
sanitizeStorage(actualStorage)
sanitizeStorage(test.expectedObjects.storage)
if !equality.Semantic.DeepEqual(test.expectedObjects.storage, actualStorage) {
t.Errorf("Unexpected Storage content:\n%s", cmp.Diff(test.expectedObjects.storage, actualStorage))
}
}
// Check expectedObjects.storageClasses
actualSCList, _ := ctx.clients.KubeClient.StorageV1().StorageClasses().List(context.TODO(), metav1.ListOptions{})
actualSCs := map[string]*storagev1.StorageClass{}
for i := range actualSCList.Items {
sc := &actualSCList.Items[i]
actualSCs[sc.Name] = sc
}
expectedSCs := map[string]*storagev1.StorageClass{}
for _, sc := range test.expectedObjects.storageClasses {
expectedSCs[sc.Name] = sc
}
for name, actualSC := range actualSCs {
expectedSC, found := expectedSCs[name]
if !found {
t.Errorf("Unexpected StorageClass found: %s", name)
continue
}
if !equality.Semantic.DeepEqual(expectedSC, actualSC) {
t.Errorf("Unexpected StorageClass %+v content:\n%s", name, cmp.Diff(expectedSC, actualSC))
}
delete(expectedSCs, name)
}
if len(expectedSCs) > 0 {
for _, crd := range expectedSCs {
t.Errorf("StorageClass %s not created by Sync()", crd.Name)
}
}
})
}
}
func sanitizeStorage(instance *opv1.Storage) {
// Remove condition texts
for i := range instance.Status.Conditions {
instance.Status.Conditions[i].LastTransitionTime = metav1.Time{}
instance.Status.Conditions[i].Message = ""
instance.Status.Conditions[i].Reason = ""
}
// Sort the conditions by name to have consistent position in the array
sort.Slice(instance.Status.Conditions, func(i, j int) bool {
return instance.Status.Conditions[i].Type < instance.Status.Conditions[j].Type
})
}