Skip to content

Commit 1086b5e

Browse files
authored
Merge pull request kubernetes#83949 from bart0sh/PR0082-kubeadm-use-strings-in-TestTokenOutput
kubeadm: use strings in TestTokenOutput
2 parents d1188a6 + 3dedaf4 commit 1086b5e

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

cmd/kubeadm/app/cmd/token_test.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ func TestRunDeleteTokens(t *testing.T) {
338338

339339
func TestTokenOutput(t *testing.T) {
340340
testCases := []struct {
341-
name string
342-
id string
343-
secret string
344-
description string
345-
usages []string
346-
extraGroups []string
347-
outputFormat string
348-
expectedBytes []byte
341+
name string
342+
id string
343+
secret string
344+
description string
345+
usages []string
346+
extraGroups []string
347+
outputFormat string
348+
expected string
349349
}{
350350
{
351351
name: "JSON output",
@@ -355,7 +355,7 @@ func TestTokenOutput(t *testing.T) {
355355
usages: []string{"signing", "authentication"},
356356
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
357357
outputFormat: "json",
358-
expectedBytes: []byte(`{
358+
expected: `{
359359
"kind": "BootstrapToken",
360360
"apiVersion": "output.kubeadm.k8s.io/v1alpha1",
361361
"creationTimestamp": null,
@@ -369,7 +369,7 @@ func TestTokenOutput(t *testing.T) {
369369
"system:bootstrappers:kubeadm:default-node-token"
370370
]
371371
}
372-
`),
372+
`,
373373
},
374374
{
375375
name: "YAML output",
@@ -379,7 +379,7 @@ func TestTokenOutput(t *testing.T) {
379379
usages: []string{"signing", "authentication"},
380380
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
381381
outputFormat: "yaml",
382-
expectedBytes: []byte(`apiVersion: output.kubeadm.k8s.io/v1alpha1
382+
expected: `apiVersion: output.kubeadm.k8s.io/v1alpha1
383383
creationTimestamp: null
384384
description: valid bootstrap tooken
385385
groups:
@@ -389,7 +389,7 @@ token: abcdef.1234567890123456
389389
usages:
390390
- signing
391391
- authentication
392-
`),
392+
`,
393393
},
394394
{
395395
name: "Go template output",
@@ -399,8 +399,8 @@ usages:
399399
usages: []string{"signing", "authentication"},
400400
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
401401
outputFormat: "go-template={{println .token .description .usages .groups}}",
402-
expectedBytes: []byte(`abcdef.1234567890123456 valid bootstrap tooken [signing authentication] [system:bootstrappers:kubeadm:default-node-token]
403-
`),
402+
expected: `abcdef.1234567890123456 valid bootstrap tooken [signing authentication] [system:bootstrappers:kubeadm:default-node-token]
403+
`,
404404
},
405405
{
406406
name: "text output",
@@ -410,19 +410,19 @@ usages:
410410
usages: []string{"signing", "authentication"},
411411
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
412412
outputFormat: "text",
413-
expectedBytes: []byte(`TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
413+
expected: `TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
414414
abcdef.1234567890123456 <forever> <never> signing,authentication valid bootstrap tooken system:bootstrappers:kubeadm:default-node-token
415-
`),
415+
`,
416416
},
417417
{
418-
name: "jsonpath output",
419-
id: "abcdef",
420-
secret: "1234567890123456",
421-
description: "valid bootstrap tooken",
422-
usages: []string{"signing", "authentication"},
423-
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
424-
outputFormat: "jsonpath={.token} {.groups}",
425-
expectedBytes: []byte(`abcdef.1234567890123456 [system:bootstrappers:kubeadm:default-node-token]`),
418+
name: "jsonpath output",
419+
id: "abcdef",
420+
secret: "1234567890123456",
421+
description: "valid bootstrap tooken",
422+
usages: []string{"signing", "authentication"},
423+
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
424+
outputFormat: "jsonpath={.token} {.groups}",
425+
expected: "abcdef.1234567890123456 [system:bootstrappers:kubeadm:default-node-token]",
426426
},
427427
}
428428
for _, tc := range testCases {
@@ -435,24 +435,21 @@ abcdef.1234567890123456 <forever> <never> signing,authentication valid b
435435
Groups: tc.extraGroups,
436436
},
437437
}
438-
buf := bytes.NewBufferString("")
438+
buf := bytes.Buffer{}
439439
outputFlags := output.NewOutputFlags(&tokenTextPrintFlags{}).WithTypeSetter(outputapischeme.Scheme).WithDefaultOutput(tc.outputFormat)
440440
printer, err := outputFlags.ToPrinter()
441441
if err != nil {
442442
t.Errorf("can't create printer for output format %s: %+v", tc.outputFormat, err)
443443
}
444444

445-
if err := printer.PrintObj(&token, buf); err != nil {
445+
if err := printer.PrintObj(&token, &buf); err != nil {
446446
t.Errorf("unable to print token %s: %+v", token.Token, err)
447447
}
448448

449-
actualBytes := buf.Bytes()
450-
if !bytes.Equal(actualBytes, tc.expectedBytes) {
449+
actual := buf.String()
450+
if actual != tc.expected {
451451
t.Errorf(
452-
"failed TestTokenOutput:\n\nexpected:\n%s\n\nactual:\n%s",
453-
string(tc.expectedBytes),
454-
string(actualBytes),
455-
)
452+
"failed TestTokenOutput:\n\nexpected:\n%s\n\nactual:\n%s", tc.expected, actual)
456453
}
457454
})
458455
}

0 commit comments

Comments
 (0)