@@ -45,14 +45,15 @@ var _ = Describe("Cfg", func() {
45
45
)
46
46
47
47
var (
48
- c Cfg
48
+ c Cfg
49
+ pluginChain []string
50
+ otherPluginChain []string
51
+ )
49
52
53
+ BeforeEach (func () {
50
54
pluginChain = []string {"go.kubebuilder.io/v2" }
51
-
52
55
otherPluginChain = []string {"go.kubebuilder.io/v3" }
53
- )
54
56
55
- BeforeEach (func () {
56
57
c = Cfg {
57
58
Version : Version ,
58
59
Domain : domain ,
@@ -136,6 +137,12 @@ var _ = Describe("Cfg", func() {
136
137
137
138
Context ("Resources" , func () {
138
139
var (
140
+ res resource.Resource
141
+ resWithoutPlural resource.Resource
142
+ checkResource func (result , expected resource.Resource )
143
+ )
144
+
145
+ BeforeEach (func () {
139
146
res = resource.Resource {
140
147
GVK : resource.GVK {
141
148
Group : "group" ,
@@ -157,35 +164,35 @@ var _ = Describe("Cfg", func() {
157
164
},
158
165
}
159
166
resWithoutPlural = res .Copy ()
160
- )
161
167
162
- // As some of the tests insert directly into the slice without using the interface methods,
163
- // regular plural forms should not be present in here. rsWithoutPlural is used for this purpose.
164
- resWithoutPlural .Plural = ""
165
-
166
- // Auxiliary function for GetResource, AddResource and UpdateResource tests
167
- checkResource := func (result , expected resource.Resource ) {
168
- Expect (result .GVK .IsEqualTo (expected .GVK )).To (BeTrue ())
169
- Expect (result .Plural ).To (Equal (expected .Plural ))
170
- Expect (result .Path ).To (Equal (expected .Path ))
171
- if expected .API == nil {
172
- Expect (result .API ).To (BeNil ())
173
- } else {
174
- Expect (result .API ).NotTo (BeNil ())
175
- Expect (result .API .CRDVersion ).To (Equal (expected .API .CRDVersion ))
176
- Expect (result .API .Namespaced ).To (Equal (expected .API .Namespaced ))
177
- }
178
- Expect (result .Controller ).To (Equal (expected .Controller ))
179
- if expected .Webhooks == nil {
180
- Expect (result .Webhooks ).To (BeNil ())
181
- } else {
182
- Expect (result .Webhooks ).NotTo (BeNil ())
183
- Expect (result .Webhooks .WebhookVersion ).To (Equal (expected .Webhooks .WebhookVersion ))
184
- Expect (result .Webhooks .Defaulting ).To (Equal (expected .Webhooks .Defaulting ))
185
- Expect (result .Webhooks .Validation ).To (Equal (expected .Webhooks .Validation ))
186
- Expect (result .Webhooks .Conversion ).To (Equal (expected .Webhooks .Conversion ))
168
+ // As some of the tests insert directly into the slice without using the interface methods,
169
+ // regular plural forms should not be present in here. rsWithoutPlural is used for this purpose.
170
+ resWithoutPlural .Plural = ""
171
+
172
+ // Auxiliary function for GetResource, AddResource and UpdateResource tests
173
+ checkResource = func (result , expected resource.Resource ) {
174
+ Expect (result .GVK .IsEqualTo (expected .GVK )).To (BeTrue ())
175
+ Expect (result .Plural ).To (Equal (expected .Plural ))
176
+ Expect (result .Path ).To (Equal (expected .Path ))
177
+ if expected .API == nil {
178
+ Expect (result .API ).To (BeNil ())
179
+ } else {
180
+ Expect (result .API ).NotTo (BeNil ())
181
+ Expect (result .API .CRDVersion ).To (Equal (expected .API .CRDVersion ))
182
+ Expect (result .API .Namespaced ).To (Equal (expected .API .Namespaced ))
183
+ }
184
+ Expect (result .Controller ).To (Equal (expected .Controller ))
185
+ if expected .Webhooks == nil {
186
+ Expect (result .Webhooks ).To (BeNil ())
187
+ } else {
188
+ Expect (result .Webhooks ).NotTo (BeNil ())
189
+ Expect (result .Webhooks .WebhookVersion ).To (Equal (expected .Webhooks .WebhookVersion ))
190
+ Expect (result .Webhooks .Defaulting ).To (Equal (expected .Webhooks .Defaulting ))
191
+ Expect (result .Webhooks .Validation ).To (Equal (expected .Webhooks .Validation ))
192
+ Expect (result .Webhooks .Conversion ).To (Equal (expected .Webhooks .Conversion ))
193
+ }
187
194
}
188
- }
195
+ })
189
196
190
197
DescribeTable ("ResourcesLength should return the number of resources" ,
191
198
func (n int ) {
@@ -354,6 +361,11 @@ var _ = Describe("Cfg", func() {
354
361
)
355
362
356
363
var (
364
+ c0 , c1 , c2 Cfg
365
+ pluginCfg PluginConfig
366
+ )
367
+
368
+ BeforeEach (func () {
357
369
c0 = Cfg {
358
370
Version : Version ,
359
371
Domain : domain ,
@@ -386,11 +398,11 @@ var _ = Describe("Cfg", func() {
386
398
},
387
399
},
388
400
}
389
- pluginConfig = PluginConfig {
401
+ pluginCfg = PluginConfig {
390
402
Data1 : "plugin value 1" ,
391
403
Data2 : "plugin value 2" ,
392
404
}
393
- )
405
+ } )
394
406
395
407
It ("DecodePluginConfig should fail for no plugin config object" , func () {
396
408
var pluginCfg PluginConfig
@@ -407,32 +419,36 @@ var _ = Describe("Cfg", func() {
407
419
})
408
420
409
421
DescribeTable ("DecodePluginConfig should retrieve the plugin data correctly" ,
410
- func (inputConfig Cfg , expectedPluginConfig PluginConfig ) {
422
+ func (getCfg func () Cfg , expected func () PluginConfig ) {
411
423
var pluginCfg PluginConfig
412
- Expect (inputConfig .DecodePluginConfig (key , & pluginCfg )).To (Succeed ())
413
- Expect (pluginCfg ).To (Equal (expectedPluginConfig ))
424
+ Expect (getCfg () .DecodePluginConfig (key , & pluginCfg )).To (Succeed ())
425
+ Expect (pluginCfg ).To (Equal (expected () ))
414
426
},
415
- Entry ("for an empty plugin config object" , c1 , PluginConfig { }),
416
- Entry ("for a full plugin config object" , c2 , pluginConfig ),
427
+ Entry ("for an empty plugin config object" , func () Cfg { return c1 }, func () PluginConfig { return PluginConfig {} }),
428
+ Entry ("for a full plugin config object" , func () Cfg { return c2 }, func () PluginConfig { return pluginCfg } ),
417
429
// TODO (coverage): add cases where yaml.Marshal returns an error
418
430
// TODO (coverage): add cases where yaml.Unmarshal returns an error
419
431
)
420
432
421
433
DescribeTable ("EncodePluginConfig should encode the plugin data correctly" ,
422
- func (pluginConfig PluginConfig , expectedConfig Cfg ) {
423
- Expect (c .EncodePluginConfig (key , pluginConfig )).To (Succeed ())
424
- Expect (c ).To (Equal (expectedConfig ))
434
+ func (getPluginCfg func () PluginConfig , expectedCfg func () Cfg ) {
435
+ Expect (c .EncodePluginConfig (key , getPluginCfg () )).To (Succeed ())
436
+ Expect (c ).To (Equal (expectedCfg () ))
425
437
},
426
- Entry ("for an empty plugin config object" , PluginConfig {}, c1 ),
427
- Entry ("for a full plugin config object" , pluginConfig , c2 ),
438
+ Entry ("for an empty plugin config object" , func () PluginConfig { return PluginConfig {} }, func () Cfg { return c1 } ),
439
+ Entry ("for a full plugin config object" , func () PluginConfig { return pluginCfg }, func () Cfg { return c2 } ),
428
440
// TODO (coverage): add cases where yaml.Marshal returns an error
429
441
// TODO (coverage): add cases where yaml.Unmarshal returns an error
430
442
)
431
443
})
432
444
433
445
Context ("Persistence" , func () {
434
446
var (
435
- // BeforeEach is called after the entries are evaluated, and therefore, c is not available
447
+ c1 , c2 Cfg
448
+ s1 , s1bis , s2 string
449
+ )
450
+
451
+ BeforeEach (func () {
436
452
c1 = Cfg {
437
453
Version : Version ,
438
454
Domain : domain ,
@@ -472,8 +488,8 @@ var _ = Describe("Cfg", func() {
472
488
Kind : "Kind" ,
473
489
},
474
490
Plural : "kindes" ,
475
- API : & resource. API {} ,
476
- Webhooks : & resource. Webhooks {} ,
491
+ API : nil ,
492
+ Webhooks : nil ,
477
493
},
478
494
{
479
495
GVK : resource.GVK {
@@ -564,22 +580,23 @@ resources:
564
580
webhookVersion: v1
565
581
version: "3"
566
582
`
567
- )
583
+ } )
568
584
569
585
DescribeTable ("MarshalYAML should succeed" ,
570
- func (c Cfg , content string ) {
571
- b , err := c .MarshalYAML ()
586
+ func (getCfg func () Cfg , getContent func () string ) {
587
+ b , err := getCfg () .MarshalYAML ()
572
588
Expect (err ).NotTo (HaveOccurred ())
573
- Expect (string (b )).To (Equal (content ))
589
+ Expect (string (b )).To (Equal (getContent () ))
574
590
},
575
- Entry ("for a basic configuration" , c1 , s1 ),
576
- Entry ("for a full configuration" , c2 , s2 ),
591
+ Entry ("for a basic configuration" , func () Cfg { return c1 }, func () string { return s1 } ),
592
+ Entry ("for a full configuration" , func () Cfg { return c2 }, func () string { return s2 } ),
577
593
)
578
594
579
595
DescribeTable ("UnmarshalYAML should succeed" ,
580
- func (content string , c Cfg ) {
596
+ func (getContent func () string , getCfg func () Cfg ) {
581
597
var unmarshalled Cfg
582
- Expect (unmarshalled .UnmarshalYAML ([]byte (content ))).To (Succeed ())
598
+ Expect (unmarshalled .UnmarshalYAML ([]byte (getContent ()))).To (Succeed ())
599
+ c := getCfg ()
583
600
Expect (unmarshalled .Version .Compare (c .Version )).To (Equal (0 ))
584
601
Expect (unmarshalled .Domain ).To (Equal (c .Domain ))
585
602
Expect (unmarshalled .Repository ).To (Equal (c .Repository ))
@@ -590,9 +607,9 @@ version: "3"
590
607
Expect (unmarshalled .Plugins ).To (HaveLen (len (c .Plugins )))
591
608
// TODO: fully test Plugins field and not on its length
592
609
},
593
- Entry ("basic" , s1 , c1 ),
594
- Entry ("full" , s2 , c2 ),
595
- Entry ("string layout" , s1bis , c1 ),
610
+ Entry ("basic" , func () string { return s1 }, func () Cfg { return c1 } ),
611
+ Entry ("full" , func () string { return s2 }, func () Cfg { return c2 } ),
612
+ Entry ("string layout" , func () string { return s1bis }, func () Cfg { return c1 } ),
596
613
)
597
614
598
615
DescribeTable ("UnmarshalYAML should fail" ,
0 commit comments