@@ -16,6 +16,7 @@ package storageclassrequest
16
16
import (
17
17
"context"
18
18
"fmt"
19
+ "strings"
19
20
"testing"
20
21
21
22
v1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
@@ -33,6 +34,9 @@ import (
33
34
)
34
35
35
36
const (
37
+ pgAutoscaleMode = "pg_autoscale_mode"
38
+ pgNum = "pg_num"
39
+ pgpNum = "pgp_num"
36
40
namespaceName = "test-ns"
37
41
deviceClass = "ssd"
38
42
storageProfileKind = "StorageProfile"
@@ -49,6 +53,46 @@ var fakeStorageProfile = &v1.StorageProfile{
49
53
},
50
54
}
51
55
56
+ var validStorageProfile = & v1.StorageProfile {
57
+ TypeMeta : metav1.TypeMeta {Kind : storageProfileKind },
58
+ ObjectMeta : metav1.ObjectMeta {
59
+ Name : "valid" ,
60
+ Namespace : namespaceName ,
61
+ },
62
+ Spec : v1.StorageProfileSpec {
63
+ DeviceClass : deviceClass ,
64
+ BlockPoolConfiguration : v1.BlockPoolConfigurationSpec {
65
+ Parameters : map [string ]string {
66
+ pgAutoscaleMode : "on" ,
67
+ pgNum : "128" ,
68
+ pgpNum : "128" ,
69
+ },
70
+ },
71
+ },
72
+ Status : v1.StorageProfileStatus {Phase : "" },
73
+ }
74
+
75
+ // A rejected StorageProfile is one that is invalid due to having a blank device class field and is set to
76
+ // Rejected in its phase.
77
+ var rejectedStorageProfile = & v1.StorageProfile {
78
+ TypeMeta : metav1.TypeMeta {Kind : storageProfileKind },
79
+ ObjectMeta : metav1.ObjectMeta {
80
+ Name : "rejected" ,
81
+ Namespace : namespaceName ,
82
+ },
83
+ Spec : v1.StorageProfileSpec {
84
+ DeviceClass : "" ,
85
+ BlockPoolConfiguration : v1.BlockPoolConfigurationSpec {
86
+ Parameters : map [string ]string {
87
+ pgAutoscaleMode : "on" ,
88
+ pgNum : "128" ,
89
+ pgpNum : "128" ,
90
+ },
91
+ },
92
+ },
93
+ Status : v1.StorageProfileStatus {Phase : "" },
94
+ }
95
+
52
96
var fakeStorageCluster = & v1.StorageCluster {
53
97
ObjectMeta : metav1.ObjectMeta {
54
98
Name : "test-storagecluster" ,
@@ -225,6 +269,200 @@ func TestProfileReconcile(t *testing.T) {
225
269
assert .NoError (t , err , caseLabel )
226
270
}
227
271
272
+ func TestStorageProfileCephBlockPool (t * testing.T ) {
273
+ var err error
274
+ var caseCounter int
275
+
276
+ var primaryTestCases = []struct {
277
+ label string
278
+ expectedPoolName string
279
+ failureExpected bool
280
+ createObjects []runtime.Object
281
+ storageProfile * v1.StorageProfile
282
+ }{
283
+ {
284
+ label : "valid profile" ,
285
+ expectedPoolName : "test-valid-blockpool" ,
286
+ failureExpected : false ,
287
+ storageProfile : validStorageProfile ,
288
+ createObjects : []runtime.Object {
289
+ & rookCephv1.CephBlockPool {
290
+ ObjectMeta : metav1.ObjectMeta {
291
+ Name : "test-valid-blockpool" ,
292
+ Namespace : namespaceName ,
293
+ Labels : map [string ]string {
294
+ controllers .StorageConsumerNameLabel : fakeStorageConsumer .Name ,
295
+ controllers .StorageProfileSpecLabel : validStorageProfile .GetSpecHash (),
296
+ },
297
+ }, Spec : rookCephv1.NamedBlockPoolSpec {
298
+ Name : "spec" ,
299
+ PoolSpec : rookCephv1.PoolSpec {
300
+ FailureDomain : "zone" ,
301
+ DeviceClass : deviceClass ,
302
+ Parameters : map [string ]string {},
303
+ },
304
+ },
305
+ },
306
+ },
307
+ },
308
+ {
309
+ label : "rejected profile" ,
310
+ expectedPoolName : "test-rejected-blockpool" ,
311
+ failureExpected : true ,
312
+ storageProfile : rejectedStorageProfile ,
313
+ createObjects : []runtime.Object {
314
+ & rookCephv1.CephBlockPool {
315
+ ObjectMeta : metav1.ObjectMeta {
316
+ Name : "test-rejected-blockpool" ,
317
+ Namespace : namespaceName ,
318
+ Labels : map [string ]string {
319
+ controllers .StorageConsumerNameLabel : fakeStorageConsumer .Name ,
320
+ controllers .StorageProfileSpecLabel : rejectedStorageProfile .GetSpecHash (),
321
+ },
322
+ }, Spec : rookCephv1.NamedBlockPoolSpec {
323
+ Name : "spec" ,
324
+ PoolSpec : rookCephv1.PoolSpec {
325
+ FailureDomain : "zone" ,
326
+ DeviceClass : deviceClass ,
327
+ Parameters : map [string ]string {},
328
+ },
329
+ },
330
+ },
331
+ },
332
+ },
333
+ }
334
+
335
+ for _ , c := range primaryTestCases {
336
+ caseCounter ++
337
+ caseLabel := fmt .Sprintf ("Case %d: %s" , caseCounter , c .label )
338
+ fmt .Println (caseLabel )
339
+
340
+ r := createFakeReconciler (t )
341
+ r .storageCluster .Spec .DefaultStorageProfile = c .storageProfile .Name
342
+ r .StorageClassRequest .Spec .Type = "blockpool"
343
+
344
+ r .StorageClassRequest .Spec .StorageProfile = c .storageProfile .Name
345
+
346
+ c .createObjects = append (c .createObjects , c .storageProfile )
347
+ c .createObjects = append (c .createObjects , fakeStorageConsumer )
348
+
349
+ fakeClient := fake .NewClientBuilder ().WithScheme (r .Scheme ).WithRuntimeObjects (c .createObjects ... )
350
+ r .Client = fakeClient .Build ()
351
+
352
+ _ , err = r .reconcilePhases ()
353
+ if c .failureExpected {
354
+ assert .Error (t , err , caseLabel )
355
+ continue
356
+ }
357
+ assert .NoError (t , err , caseLabel )
358
+
359
+ assert .Equal (t , c .expectedPoolName , r .cephBlockPool .Name , caseLabel )
360
+
361
+ if strings .Contains (c .expectedPoolName , "valid" ) {
362
+ expectedStorageProfileParameters := validStorageProfile .Spec .BlockPoolConfiguration .Parameters
363
+ actualBlockPoolParameters := r .cephBlockPool .Spec .Parameters
364
+ assert .Equal (t , expectedStorageProfileParameters , actualBlockPoolParameters , caseLabel )
365
+ assert .NotEqual (t , v1 .StorageProfilePhaseRejected , c .storageProfile .Status .Phase )
366
+ } else {
367
+ actualBlockPoolParameters := r .cephBlockPool .Spec .Parameters
368
+ assert .Equal (t , v1 .StorageProfilePhaseRejected , c .storageProfile .Status .Phase )
369
+ assert .Nil (t , actualBlockPoolParameters , caseLabel )
370
+ }
371
+ }
372
+
373
+ }
374
+
375
+ func TestStorageProfileCephFsSubVolGroup (t * testing.T ) {
376
+ var err error
377
+ var caseCounter int
378
+
379
+ var primaryTestCases = []struct {
380
+ label string
381
+ expectedGroupName string
382
+ failureExpected bool
383
+ createObjects []runtime.Object
384
+ cephResources []* v1alpha1.CephResourcesSpec
385
+ storageProfile * v1.StorageProfile
386
+ cephFs * rookCephv1.CephFilesystem
387
+ }{
388
+ {
389
+ label : "valid profile" ,
390
+ expectedGroupName : "test-subvolgroup" ,
391
+ storageProfile : fakeStorageProfile ,
392
+ cephFs : fakeCephFs ,
393
+ failureExpected : false ,
394
+ cephResources : []* v1alpha1.CephResourcesSpec {
395
+ {
396
+ Name : "test-subvolgroup" ,
397
+ Kind : "CephFilesystemSubVolumeGroup" ,
398
+ },
399
+ },
400
+ createObjects : []runtime.Object {
401
+ & rookCephv1.CephFilesystemSubVolumeGroup {
402
+ ObjectMeta : metav1.ObjectMeta {
403
+ Name : "test-subvolgroup" ,
404
+ Namespace : namespaceName ,
405
+ },
406
+ Status : & rookCephv1.CephFilesystemSubVolumeGroupStatus {},
407
+ },
408
+ },
409
+ },
410
+ {
411
+ label : "rejected profile" ,
412
+ expectedGroupName : "test-subvolgroup" ,
413
+ storageProfile : rejectedStorageProfile ,
414
+ cephFs : fakeCephFs ,
415
+ failureExpected : true ,
416
+ cephResources : []* v1alpha1.CephResourcesSpec {
417
+ {
418
+ Name : "test-subvolgroup" ,
419
+ Kind : "CephFilesystemSubVolumeGroup" ,
420
+ },
421
+ },
422
+ createObjects : []runtime.Object {
423
+ & rookCephv1.CephFilesystemSubVolumeGroup {
424
+ ObjectMeta : metav1.ObjectMeta {
425
+ Name : "test-subvolgroup" ,
426
+ Namespace : namespaceName ,
427
+ },
428
+ Status : & rookCephv1.CephFilesystemSubVolumeGroupStatus {},
429
+ },
430
+ },
431
+ },
432
+ }
433
+
434
+ for _ , c := range primaryTestCases {
435
+ caseCounter ++
436
+ caseLabel := fmt .Sprintf ("Case %d: %s" , caseCounter , c .label )
437
+ fmt .Println (caseLabel )
438
+
439
+ r := createFakeReconciler (t )
440
+ if strings .Contains (c .label , "rejected" ) {
441
+ r .storageCluster .Spec .DefaultStorageProfile = rejectedStorageProfile .Name
442
+ }
443
+
444
+ r .StorageClassRequest .Status .CephResources = c .cephResources
445
+ r .StorageClassRequest .Spec .Type = "sharedfilesystem"
446
+ r .StorageClassRequest .Spec .StorageProfile = c .storageProfile .Name
447
+
448
+ c .createObjects = append (c .createObjects , c .cephFs )
449
+ c .createObjects = append (c .createObjects , c .storageProfile )
450
+ c .createObjects = append (c .createObjects , fakeStorageConsumer )
451
+ fakeClient := fake .NewClientBuilder ().WithScheme (r .Scheme ).WithRuntimeObjects (c .createObjects ... )
452
+
453
+ r .Client = fakeClient .Build ()
454
+ r .StorageClassRequest .Status .CephResources = c .cephResources
455
+
456
+ _ , err = r .reconcilePhases ()
457
+ if c .failureExpected {
458
+ assert .Error (t , err , caseLabel )
459
+ continue
460
+ }
461
+ assert .NoError (t , err , caseLabel )
462
+ assert .Equal (t , c .expectedGroupName , r .cephFilesystemSubVolumeGroup .Name , caseLabel )
463
+ }
464
+ }
465
+
228
466
func TestCephBlockPool (t * testing.T ) {
229
467
var err error
230
468
var caseCounter int
0 commit comments