...
1
16
17 package config
18
19 import (
20 "testing"
21
22 "k8s.io/cli-runtime/pkg/genericiooptions"
23 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
24 cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
25 )
26
27 func TestGetUsersRun(t *testing.T) {
28 var tests = []struct {
29 name string
30 config clientcmdapi.Config
31 expected string
32 }{
33 {
34 name: "no users",
35 config: clientcmdapi.Config{},
36 expected: "NAME\n",
37 },
38 {
39 name: "some users",
40 config: clientcmdapi.Config{
41 AuthInfos: map[string]*clientcmdapi.AuthInfo{
42 "minikube": {Username: "minikube"},
43 "admin": {Username: "admin"},
44 },
45 },
46 expected: `NAME
47 admin
48 minikube
49 `,
50 },
51 }
52
53 for i := range tests {
54 test := tests[i]
55 t.Run(test.name, func(t *testing.T) {
56 tf := cmdtesting.NewTestFactory()
57 defer tf.Cleanup()
58
59 ioStreams, _, out, _ := genericiooptions.NewTestIOStreams()
60 pathOptions, err := tf.PathOptionsWithConfig(test.config)
61 if err != nil {
62 t.Fatalf("unexpected error executing command: %v", err)
63 }
64 options := NewGetUsersOptions(ioStreams, pathOptions)
65
66 if err = options.Run(); err != nil {
67 t.Fatalf("unexpected error executing command: %v", err)
68 }
69
70 if got := out.String(); got != test.expected {
71 t.Fatalf("expected: %s but got %s", test.expected, got)
72 }
73 })
74 }
75 }
76
View as plain text