1
16
17 package config
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23
24 "k8s.io/cli-runtime/pkg/genericiooptions"
25 "k8s.io/client-go/tools/clientcmd"
26 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
27 cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
28 )
29
30 func TestDeleteUserComplete(t *testing.T) {
31 var tests = []struct {
32 name string
33 args []string
34 err string
35 }{
36 {
37 name: "no args",
38 args: []string{},
39 err: "user to delete is required",
40 },
41 {
42 name: "user provided",
43 args: []string{"minikube"},
44 err: "",
45 },
46 }
47
48 for i := range tests {
49 test := tests[i]
50 t.Run(test.name, func(t *testing.T) {
51 tf := cmdtesting.NewTestFactory()
52 defer tf.Cleanup()
53
54 ioStreams, _, out, _ := genericiooptions.NewTestIOStreams()
55 pathOptions, err := tf.PathOptionsWithConfig(clientcmdapi.Config{})
56 if err != nil {
57 t.Fatalf("unexpected error executing command: %v", err)
58 }
59
60 cmd := NewCmdConfigDeleteUser(ioStreams, pathOptions)
61 cmd.SetOut(out)
62 options := NewDeleteUserOptions(ioStreams, pathOptions)
63
64 if err := options.Complete(cmd, test.args); err != nil {
65 if test.err == "" {
66 t.Fatalf("unexpected error executing command: %v", err)
67 }
68
69 if !strings.Contains(err.Error(), test.err) {
70 t.Fatalf("expected error to contain %v, got %v", test.err, err.Error())
71 }
72
73 return
74 }
75
76 if options.configFile != pathOptions.GlobalFile {
77 t.Fatalf("expected configFile to be %v, got %v", pathOptions.GlobalFile, options.configFile)
78 }
79 })
80 }
81 }
82
83 func TestDeleteUserValidate(t *testing.T) {
84 var tests = []struct {
85 name string
86 user string
87 config clientcmdapi.Config
88 err string
89 }{
90 {
91 name: "user not in config",
92 user: "kube",
93 config: clientcmdapi.Config{
94 AuthInfos: map[string]*clientcmdapi.AuthInfo{
95 "minikube": {Username: "minikube"},
96 },
97 },
98 err: "cannot delete user kube",
99 },
100 {
101 name: "user in config",
102 user: "kube",
103 config: clientcmdapi.Config{
104 AuthInfos: map[string]*clientcmdapi.AuthInfo{
105 "minikube": {Username: "minikube"},
106 "kube": {Username: "kube"},
107 },
108 },
109 err: "",
110 },
111 }
112
113 for i := range tests {
114 test := tests[i]
115 t.Run(test.name, func(t *testing.T) {
116 tf := cmdtesting.NewTestFactory()
117 defer tf.Cleanup()
118
119 ioStreams, _, _, _ := genericiooptions.NewTestIOStreams()
120 pathOptions, err := tf.PathOptionsWithConfig(test.config)
121 if err != nil {
122 t.Fatalf("unexpected error executing command: %v", err)
123 }
124
125 options := NewDeleteUserOptions(ioStreams, pathOptions)
126 options.config = &test.config
127 options.user = test.user
128
129 if err := options.Validate(); err != nil {
130 if !strings.Contains(err.Error(), test.err) {
131 t.Fatalf("expected: %s but got %s", test.err, err.Error())
132 }
133
134 return
135 }
136 })
137 }
138 }
139
140 func TestDeleteUserRun(t *testing.T) {
141 var tests = []struct {
142 name string
143 user string
144 config clientcmdapi.Config
145 expectedUsers []string
146 out string
147 }{
148 {
149 name: "delete user",
150 user: "kube",
151 config: clientcmdapi.Config{
152 AuthInfos: map[string]*clientcmdapi.AuthInfo{
153 "minikube": {Username: "minikube"},
154 "kube": {Username: "kube"},
155 },
156 },
157 expectedUsers: []string{"minikube"},
158 out: "deleted user kube from",
159 },
160 }
161
162 for i := range tests {
163 test := tests[i]
164 t.Run(test.name, func(t *testing.T) {
165 tf := cmdtesting.NewTestFactory()
166 defer tf.Cleanup()
167
168 ioStreams, _, out, _ := genericiooptions.NewTestIOStreams()
169 pathOptions, err := tf.PathOptionsWithConfig(test.config)
170 if err != nil {
171 t.Fatalf("unexpected error executing command: %v", err)
172 }
173
174 options := NewDeleteUserOptions(ioStreams, pathOptions)
175 options.config = &test.config
176 options.configFile = pathOptions.GlobalFile
177 options.user = test.user
178
179 if err := options.Run(); err != nil {
180 t.Fatalf("unexpected error executing command: %v", err)
181 }
182
183 if got := out.String(); !strings.Contains(got, test.out) {
184 t.Fatalf("expected: %s but got %s", test.out, got)
185 }
186
187 config, err := clientcmd.LoadFromFile(options.configFile)
188 if err != nil {
189 t.Fatalf("unexpected error executing command: %v", err)
190 }
191
192 users := make([]string, 0, len(config.AuthInfos))
193 for user := range config.AuthInfos {
194 users = append(users, user)
195 }
196
197 if !reflect.DeepEqual(test.expectedUsers, users) {
198 t.Fatalf("expected %v, got %v", test.expectedUsers, users)
199 }
200 })
201 }
202 }
203
View as plain text