Merge pull request #83949 from bart0sh/PR0082-kubeadm-use-strings-in-TestTokenOutput

kubeadm: use strings in TestTokenOutput
This commit is contained in:
Kubernetes Prow Robot 2019-10-16 09:36:25 -07:00 committed by GitHub
commit 1086b5e5cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -345,7 +345,7 @@ func TestTokenOutput(t *testing.T) {
usages []string usages []string
extraGroups []string extraGroups []string
outputFormat string outputFormat string
expectedBytes []byte expected string
}{ }{
{ {
name: "JSON output", name: "JSON output",
@ -355,7 +355,7 @@ func TestTokenOutput(t *testing.T) {
usages: []string{"signing", "authentication"}, usages: []string{"signing", "authentication"},
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"}, extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
outputFormat: "json", outputFormat: "json",
expectedBytes: []byte(`{ expected: `{
"kind": "BootstrapToken", "kind": "BootstrapToken",
"apiVersion": "output.kubeadm.k8s.io/v1alpha1", "apiVersion": "output.kubeadm.k8s.io/v1alpha1",
"creationTimestamp": null, "creationTimestamp": null,
@ -369,7 +369,7 @@ func TestTokenOutput(t *testing.T) {
"system:bootstrappers:kubeadm:default-node-token" "system:bootstrappers:kubeadm:default-node-token"
] ]
} }
`), `,
}, },
{ {
name: "YAML output", name: "YAML output",
@ -379,7 +379,7 @@ func TestTokenOutput(t *testing.T) {
usages: []string{"signing", "authentication"}, usages: []string{"signing", "authentication"},
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"}, extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
outputFormat: "yaml", outputFormat: "yaml",
expectedBytes: []byte(`apiVersion: output.kubeadm.k8s.io/v1alpha1 expected: `apiVersion: output.kubeadm.k8s.io/v1alpha1
creationTimestamp: null creationTimestamp: null
description: valid bootstrap tooken description: valid bootstrap tooken
groups: groups:
@ -389,7 +389,7 @@ token: abcdef.1234567890123456
usages: usages:
- signing - signing
- authentication - authentication
`), `,
}, },
{ {
name: "Go template output", name: "Go template output",
@ -399,8 +399,8 @@ usages:
usages: []string{"signing", "authentication"}, usages: []string{"signing", "authentication"},
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"}, extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
outputFormat: "go-template={{println .token .description .usages .groups}}", outputFormat: "go-template={{println .token .description .usages .groups}}",
expectedBytes: []byte(`abcdef.1234567890123456 valid bootstrap tooken [signing authentication] [system:bootstrappers:kubeadm:default-node-token] expected: `abcdef.1234567890123456 valid bootstrap tooken [signing authentication] [system:bootstrappers:kubeadm:default-node-token]
`), `,
}, },
{ {
name: "text output", name: "text output",
@ -410,9 +410,9 @@ usages:
usages: []string{"signing", "authentication"}, usages: []string{"signing", "authentication"},
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"}, extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
outputFormat: "text", outputFormat: "text",
expectedBytes: []byte(`TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS expected: `TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
abcdef.1234567890123456 <forever> <never> signing,authentication valid bootstrap tooken system:bootstrappers:kubeadm:default-node-token abcdef.1234567890123456 <forever> <never> signing,authentication valid bootstrap tooken system:bootstrappers:kubeadm:default-node-token
`), `,
}, },
{ {
name: "jsonpath output", name: "jsonpath output",
@ -422,7 +422,7 @@ abcdef.1234567890123456 <forever> <never> signing,authentication valid b
usages: []string{"signing", "authentication"}, usages: []string{"signing", "authentication"},
extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"}, extraGroups: []string{"system:bootstrappers:kubeadm:default-node-token"},
outputFormat: "jsonpath={.token} {.groups}", outputFormat: "jsonpath={.token} {.groups}",
expectedBytes: []byte(`abcdef.1234567890123456 [system:bootstrappers:kubeadm:default-node-token]`), expected: "abcdef.1234567890123456 [system:bootstrappers:kubeadm:default-node-token]",
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
@ -435,24 +435,21 @@ abcdef.1234567890123456 <forever> <never> signing,authentication valid b
Groups: tc.extraGroups, Groups: tc.extraGroups,
}, },
} }
buf := bytes.NewBufferString("") buf := bytes.Buffer{}
outputFlags := output.NewOutputFlags(&tokenTextPrintFlags{}).WithTypeSetter(outputapischeme.Scheme).WithDefaultOutput(tc.outputFormat) outputFlags := output.NewOutputFlags(&tokenTextPrintFlags{}).WithTypeSetter(outputapischeme.Scheme).WithDefaultOutput(tc.outputFormat)
printer, err := outputFlags.ToPrinter() printer, err := outputFlags.ToPrinter()
if err != nil { if err != nil {
t.Errorf("can't create printer for output format %s: %+v", tc.outputFormat, err) t.Errorf("can't create printer for output format %s: %+v", tc.outputFormat, err)
} }
if err := printer.PrintObj(&token, buf); err != nil { if err := printer.PrintObj(&token, &buf); err != nil {
t.Errorf("unable to print token %s: %+v", token.Token, err) t.Errorf("unable to print token %s: %+v", token.Token, err)
} }
actualBytes := buf.Bytes() actual := buf.String()
if !bytes.Equal(actualBytes, tc.expectedBytes) { if actual != tc.expected {
t.Errorf( t.Errorf(
"failed TestTokenOutput:\n\nexpected:\n%s\n\nactual:\n%s", "failed TestTokenOutput:\n\nexpected:\n%s\n\nactual:\n%s", tc.expected, actual)
string(tc.expectedBytes),
string(actualBytes),
)
} }
}) })
} }