@@ -58,45 +58,47 @@ func TestCreateServiceAccount(t *testing.T) {
58
58
}
59
59
60
60
for _ , tc := range tests {
61
- client := clientsetfake .NewSimpleClientset ()
62
- if tc .createErr != nil {
63
- client .PrependReactor ("create" , "serviceaccounts" , func (action core.Action ) (bool , runtime.Object , error ) {
64
- return true , nil , tc .createErr
65
- })
66
- }
67
-
68
- err := CreateServiceAccount (client )
69
- if tc .expectErr {
70
- if err == nil {
71
- t .Errorf ("CreateServiceAccounts(%s) wanted err, got nil" , tc .name )
61
+ t .Run (tc .name , func (t * testing.T ) {
62
+ client := clientsetfake .NewSimpleClientset ()
63
+ if tc .createErr != nil {
64
+ client .PrependReactor ("create" , "serviceaccounts" , func (action core.Action ) (bool , runtime.Object , error ) {
65
+ return true , nil , tc .createErr
66
+ })
72
67
}
73
- continue
74
- } else if ! tc .expectErr && err != nil {
75
- t .Errorf ("CreateServiceAccounts(%s) returned unexpected err: %v" , tc .name , err )
76
- }
77
68
78
- wantResourcesCreated := 1
79
- if len (client .Actions ()) != wantResourcesCreated {
80
- t .Errorf ("CreateServiceAccounts(%s) should have made %d actions, but made %d" , tc .name , wantResourcesCreated , len (client .Actions ()))
81
- }
69
+ err := CreateServiceAccount (client )
70
+ if tc .expectErr {
71
+ if err == nil {
72
+ t .Errorf ("CreateServiceAccounts(%s) wanted err, got nil" , tc .name )
73
+ }
74
+ return
75
+ } else if ! tc .expectErr && err != nil {
76
+ t .Errorf ("CreateServiceAccounts(%s) returned unexpected err: %v" , tc .name , err )
77
+ }
82
78
83
- for _ , action := range client .Actions () {
84
- if action .GetVerb () != "create" || action .GetResource ().Resource != "serviceaccounts" {
85
- t .Errorf ("CreateServiceAccounts(%s) called [%v %v], but wanted [create serviceaccounts]" ,
86
- tc .name , action .GetVerb (), action .GetResource ().Resource )
79
+ wantResourcesCreated := 1
80
+ if len (client .Actions ()) != wantResourcesCreated {
81
+ t .Errorf ("CreateServiceAccounts(%s) should have made %d actions, but made %d" , tc .name , wantResourcesCreated , len (client .Actions ()))
87
82
}
88
- }
89
83
84
+ for _ , action := range client .Actions () {
85
+ if action .GetVerb () != "create" || action .GetResource ().Resource != "serviceaccounts" {
86
+ t .Errorf ("CreateServiceAccounts(%s) called [%v %v], but wanted [create serviceaccounts]" ,
87
+ tc .name , action .GetVerb (), action .GetResource ().Resource )
88
+ }
89
+ }
90
+ })
90
91
}
91
92
}
92
93
93
94
func TestCompileManifests (t * testing.T ) {
94
95
var tests = []struct {
96
+ name string
95
97
manifest string
96
98
data interface {}
97
- expected bool
98
99
}{
99
100
{
101
+ name : "KubeDNSDeployment manifest" ,
100
102
manifest : KubeDNSDeployment ,
101
103
data : struct { DeploymentName , KubeDNSImage , DNSMasqImage , SidecarImage , DNSBindAddr , DNSProbeAddr , DNSDomain , ControlPlaneTaintKey string }{
102
104
DeploymentName : "foo" ,
@@ -108,91 +110,87 @@ func TestCompileManifests(t *testing.T) {
108
110
DNSDomain : "foo" ,
109
111
ControlPlaneTaintKey : "foo" ,
110
112
},
111
- expected : true ,
112
113
},
113
114
{
115
+ name : "KubeDNSService manifest" ,
114
116
manifest : KubeDNSService ,
115
117
data : struct { DNSIP string }{
116
118
DNSIP : "foo" ,
117
119
},
118
- expected : true ,
119
120
},
120
121
{
122
+ name : "CoreDNSDeployment manifest" ,
121
123
manifest : CoreDNSDeployment ,
122
124
data : struct { DeploymentName , Image , ControlPlaneTaintKey string }{
123
125
DeploymentName : "foo" ,
124
126
Image : "foo" ,
125
127
ControlPlaneTaintKey : "foo" ,
126
128
},
127
- expected : true ,
128
- },
129
- {
130
- manifest : KubeDNSService ,
131
- data : struct { DNSIP string }{
132
- DNSIP : "foo" ,
133
- },
134
- expected : true ,
135
129
},
136
130
{
131
+ name : "CoreDNSConfigMap manifest" ,
137
132
manifest : CoreDNSConfigMap ,
138
133
data : struct { DNSDomain , Federation , UpstreamNameserver , StubDomain string }{
139
134
DNSDomain : "foo" ,
140
135
Federation : "foo" ,
141
136
UpstreamNameserver : "foo" ,
142
137
StubDomain : "foo" ,
143
138
},
144
- expected : true ,
145
139
},
146
140
}
147
141
for _ , rt := range tests {
148
- _ , actual := kubeadmutil .ParseTemplate (rt .manifest , rt .data )
149
- if (actual == nil ) != rt .expected {
150
- t .Errorf (
151
- "failed CompileManifests:\n \t expected: %t\n \t actual: %t" ,
152
- rt .expected ,
153
- (actual == nil ),
154
- )
155
- }
142
+ t .Run (rt .name , func (t * testing.T ) {
143
+ _ , err := kubeadmutil .ParseTemplate (rt .manifest , rt .data )
144
+ if err != nil {
145
+ t .Errorf ("unexpected ParseTemplate failure: %+v" , err )
146
+ }
147
+ })
156
148
}
157
149
}
158
150
159
151
func TestGetDNSIP (t * testing.T ) {
160
152
var tests = []struct {
161
- svcSubnet , expectedDNSIP string
153
+ name , svcSubnet , expectedDNSIP string
162
154
}{
163
155
{
156
+ name : "subnet mask 12" ,
164
157
svcSubnet : "10.96.0.0/12" ,
165
158
expectedDNSIP : "10.96.0.10" ,
166
159
},
167
160
{
161
+ name : "subnet mask 26" ,
168
162
svcSubnet : "10.87.116.64/26" ,
169
163
expectedDNSIP : "10.87.116.74" ,
170
164
},
171
165
}
172
166
for _ , rt := range tests {
173
- dnsIP , err := kubeadmconstants .GetDNSIP (rt .svcSubnet )
174
- if err != nil {
175
- t .Fatalf ("couldn't get dnsIP : %v" , err )
176
- }
167
+ t .Run (rt .name , func (t * testing.T ) {
168
+ dnsIP , err := kubeadmconstants .GetDNSIP (rt .svcSubnet )
169
+ if err != nil {
170
+ t .Fatalf ("couldn't get dnsIP : %v" , err )
171
+ }
177
172
178
- actualDNSIP := dnsIP .String ()
179
- if actualDNSIP != rt .expectedDNSIP {
180
- t .Errorf (
181
- "failed GetDNSIP\n \t expected: %s\n \t actual: %s" ,
182
- rt .expectedDNSIP ,
183
- actualDNSIP ,
184
- )
185
- }
173
+ actualDNSIP := dnsIP .String ()
174
+ if actualDNSIP != rt .expectedDNSIP {
175
+ t .Errorf (
176
+ "failed GetDNSIP\n \t expected: %s\n \t actual: %s" ,
177
+ rt .expectedDNSIP ,
178
+ actualDNSIP ,
179
+ )
180
+ }
181
+ })
186
182
}
187
183
}
188
184
189
185
func TestTranslateStubDomainKubeDNSToCoreDNS (t * testing.T ) {
190
186
testCases := []struct {
187
+ name string
191
188
configMap * v1.ConfigMap
192
189
expectOne string
193
190
expectTwo string
194
191
}{
195
192
{
193
+ name : "valid call 1" ,
196
194
configMap : & v1.ConfigMap {
197
195
ObjectMeta : metav1.ObjectMeta {
198
196
Name : "kube-dns" ,
@@ -234,6 +232,7 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
234
232
}` ,
235
233
},
236
234
{
235
+ name : "empty call" ,
237
236
configMap : & v1.ConfigMap {
238
237
ObjectMeta : metav1.ObjectMeta {
239
238
Name : "kubedns" ,
@@ -244,6 +243,7 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
244
243
expectOne : "" ,
245
244
},
246
245
{
246
+ name : "valid call 2" ,
247
247
configMap : & v1.ConfigMap {
248
248
ObjectMeta : metav1.ObjectMeta {
249
249
Name : "kube-dns" ,
@@ -285,6 +285,7 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
285
285
}` ,
286
286
},
287
287
{
288
+ name : "missing stubDomains" ,
288
289
configMap : & v1.ConfigMap {
289
290
ObjectMeta : metav1.ObjectMeta {
290
291
Name : "kube-dns" ,
@@ -299,22 +300,26 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
299
300
},
300
301
}
301
302
for _ , testCase := range testCases {
302
- out , err := translateStubDomainOfKubeDNSToForwardCoreDNS (kubeDNSStubDomain , testCase .configMap )
303
- if err != nil {
304
- t .Errorf ("unexpected error: %v" , err )
305
- }
306
- if ! strings .Contains (out , testCase .expectOne ) && ! strings .Contains (out , testCase .expectTwo ) {
307
- t .Errorf ("expected to find %q or %q in output: %q" , testCase .expectOne , testCase .expectTwo , out )
308
- }
303
+ t .Run (testCase .name , func (t * testing.T ) {
304
+ out , err := translateStubDomainOfKubeDNSToForwardCoreDNS (kubeDNSStubDomain , testCase .configMap )
305
+ if err != nil {
306
+ t .Errorf ("unexpected error: %v" , err )
307
+ }
308
+ if ! strings .Contains (out , testCase .expectOne ) && ! strings .Contains (out , testCase .expectTwo ) {
309
+ t .Errorf ("expected to find %q or %q in output: %q" , testCase .expectOne , testCase .expectTwo , out )
310
+ }
311
+ })
309
312
}
310
313
}
311
314
312
315
func TestTranslateUpstreamKubeDNSToCoreDNS (t * testing.T ) {
313
316
testCases := []struct {
317
+ name string
314
318
configMap * v1.ConfigMap
315
319
expect string
316
320
}{
317
321
{
322
+ name : "expect resolv.conf" ,
318
323
configMap : & v1.ConfigMap {
319
324
ObjectMeta : metav1.ObjectMeta {
320
325
Name : "kube-dns" ,
@@ -325,6 +330,7 @@ func TestTranslateUpstreamKubeDNSToCoreDNS(t *testing.T) {
325
330
expect : "/etc/resolv.conf" ,
326
331
},
327
332
{
333
+ name : "expect list of Name Server IP addresses" ,
328
334
configMap : & v1.ConfigMap {
329
335
ObjectMeta : metav1.ObjectMeta {
330
336
Name : "kubedns" ,
@@ -339,6 +345,7 @@ func TestTranslateUpstreamKubeDNSToCoreDNS(t *testing.T) {
339
345
expect : "8.8.8.8 8.8.4.4 4.4.4.4" ,
340
346
},
341
347
{
348
+ name : "no stubDomains: expect list of Name Server IP addresses" ,
342
349
configMap : & v1.ConfigMap {
343
350
ObjectMeta : metav1.ObjectMeta {
344
351
Name : "kubedns" ,
@@ -353,23 +360,27 @@ func TestTranslateUpstreamKubeDNSToCoreDNS(t *testing.T) {
353
360
},
354
361
}
355
362
for _ , testCase := range testCases {
356
- out , err := translateUpstreamNameServerOfKubeDNSToUpstreamProxyCoreDNS (kubeDNSUpstreamNameservers , testCase .configMap )
357
- if err != nil {
358
- t .Errorf ("unexpected error: %v" , err )
359
- }
360
- if ! strings .Contains (out , testCase .expect ) {
361
- t .Errorf ("expected to find %q in output: %q" , testCase .expect , out )
362
- }
363
+ t .Run (testCase .name , func (t * testing.T ) {
364
+ out , err := translateUpstreamNameServerOfKubeDNSToUpstreamProxyCoreDNS (kubeDNSUpstreamNameservers , testCase .configMap )
365
+ if err != nil {
366
+ t .Errorf ("unexpected error: %v" , err )
367
+ }
368
+ if ! strings .Contains (out , testCase .expect ) {
369
+ t .Errorf ("expected to find %q in output: %q" , testCase .expect , out )
370
+ }
371
+ })
363
372
}
364
373
}
365
374
366
375
func TestTranslateFederationKubeDNSToCoreDNS (t * testing.T ) {
367
376
testCases := []struct {
377
+ name string
368
378
configMap * v1.ConfigMap
369
379
expectOne string
370
380
expectTwo string
371
381
}{
372
382
{
383
+ name : "valid call" ,
373
384
configMap : & v1.ConfigMap {
374
385
ObjectMeta : metav1.ObjectMeta {
375
386
Name : "kube-dns" ,
@@ -394,6 +405,7 @@ func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
394
405
}` ,
395
406
},
396
407
{
408
+ name : "empty data" ,
397
409
configMap : & v1.ConfigMap {
398
410
ObjectMeta : metav1.ObjectMeta {
399
411
Name : "kubedns" ,
@@ -404,6 +416,7 @@ func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
404
416
expectOne : "" ,
405
417
},
406
418
{
419
+ name : "missing federations data" ,
407
420
configMap : & v1.ConfigMap {
408
421
ObjectMeta : metav1.ObjectMeta {
409
422
Name : "kube-dns" ,
@@ -419,22 +432,26 @@ func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
419
432
},
420
433
}
421
434
for _ , testCase := range testCases {
422
- out , err := translateFederationsofKubeDNSToCoreDNS (kubeDNSFederation , "cluster.local" , testCase .configMap )
423
- if err != nil {
424
- t .Errorf ("unexpected error: %v" , err )
425
- }
426
- if ! strings .Contains (out , testCase .expectOne ) && ! strings .Contains (out , testCase .expectTwo ) {
427
- t .Errorf ("expected to find %q or %q in output: %q" , testCase .expectOne , testCase .expectTwo , out )
428
- }
435
+ t .Run (testCase .name , func (t * testing.T ) {
436
+ out , err := translateFederationsofKubeDNSToCoreDNS (kubeDNSFederation , "cluster.local" , testCase .configMap )
437
+ if err != nil {
438
+ t .Errorf ("unexpected error: %v" , err )
439
+ }
440
+ if ! strings .Contains (out , testCase .expectOne ) && ! strings .Contains (out , testCase .expectTwo ) {
441
+ t .Errorf ("expected to find %q or %q in output: %q" , testCase .expectOne , testCase .expectTwo , out )
442
+ }
443
+ })
429
444
}
430
445
}
431
446
432
447
func TestDeploymentsHaveSystemClusterCriticalPriorityClassName (t * testing.T ) {
433
448
testCases := []struct {
449
+ name string
434
450
manifest string
435
451
data interface {}
436
452
}{
437
453
{
454
+ name : "KubeDNSDeployment" ,
438
455
manifest : KubeDNSDeployment ,
439
456
data : struct { DeploymentName , KubeDNSImage , DNSMasqImage , SidecarImage , DNSBindAddr , DNSProbeAddr , DNSDomain , ControlPlaneTaintKey string }{
440
457
DeploymentName : "foo" ,
@@ -448,6 +465,7 @@ func TestDeploymentsHaveSystemClusterCriticalPriorityClassName(t *testing.T) {
448
465
},
449
466
},
450
467
{
468
+ name : "CoreDNSDeployment" ,
451
469
manifest : CoreDNSDeployment ,
452
470
data : struct { DeploymentName , Image , ControlPlaneTaintKey string }{
453
471
DeploymentName : "foo" ,
@@ -457,13 +475,15 @@ func TestDeploymentsHaveSystemClusterCriticalPriorityClassName(t *testing.T) {
457
475
},
458
476
}
459
477
for _ , testCase := range testCases {
460
- deploymentBytes , _ := kubeadmutil .ParseTemplate (testCase .manifest , testCase .data )
461
- deployment := & apps.Deployment {}
462
- if err := kuberuntime .DecodeInto (clientsetscheme .Codecs .UniversalDecoder (), deploymentBytes , deployment ); err != nil {
463
- t .Errorf ("unexpected error: %v" , err )
464
- }
465
- if deployment .Spec .Template .Spec .PriorityClassName != "system-cluster-critical" {
466
- t .Errorf ("expected to see system-cluster-critical priority class name. Got %q instead" , deployment .Spec .Template .Spec .PriorityClassName )
467
- }
478
+ t .Run (testCase .name , func (t * testing.T ) {
479
+ deploymentBytes , _ := kubeadmutil .ParseTemplate (testCase .manifest , testCase .data )
480
+ deployment := & apps.Deployment {}
481
+ if err := kuberuntime .DecodeInto (clientsetscheme .Codecs .UniversalDecoder (), deploymentBytes , deployment ); err != nil {
482
+ t .Errorf ("unexpected error: %v" , err )
483
+ }
484
+ if deployment .Spec .Template .Spec .PriorityClassName != "system-cluster-critical" {
485
+ t .Errorf ("expected to see system-cluster-critical priority class name. Got %q instead" , deployment .Spec .Template .Spec .PriorityClassName )
486
+ }
487
+ })
468
488
}
469
489
}
0 commit comments