1
16
17 package config
18
19 import (
20 "os"
21 "testing"
22
23 utiltesting "k8s.io/client-go/util/testing"
24
25 "k8s.io/cli-runtime/pkg/genericiooptions"
26 "k8s.io/client-go/tools/clientcmd"
27 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
28 )
29
30 type viewClusterTest struct {
31 description string
32 config clientcmdapi.Config
33 flags []string
34 expected string
35 }
36
37 func TestViewCluster(t *testing.T) {
38 conf := clientcmdapi.Config{
39 Kind: "Config",
40 APIVersion: "v1",
41 Clusters: map[string]*clientcmdapi.Cluster{
42 "minikube": {Server: "https://192.168.99.100:8443"},
43 "my-cluster": {Server: "https://192.168.0.1:3434"},
44 },
45 Contexts: map[string]*clientcmdapi.Context{
46 "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
47 "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
48 },
49 CurrentContext: "minikube",
50 AuthInfos: map[string]*clientcmdapi.AuthInfo{
51 "minikube": {
52 ClientKeyData: []byte("notredacted"),
53 Token: "notredacted",
54 Username: "foo",
55 Password: "notredacted",
56 },
57 "mu-cluster": {
58 ClientKeyData: []byte("notredacted"),
59 Token: "notredacted",
60 Username: "bar",
61 Password: "notredacted",
62 },
63 },
64 }
65
66 test := viewClusterTest{
67 description: "Testing for kubectl config view",
68 config: conf,
69 expected: `apiVersion: v1
70 clusters:
71 - cluster:
72 server: https://192.168.99.100:8443
73 name: minikube
74 - cluster:
75 server: https://192.168.0.1:3434
76 name: my-cluster
77 contexts:
78 - context:
79 cluster: minikube
80 user: minikube
81 name: minikube
82 - context:
83 cluster: my-cluster
84 user: mu-cluster
85 name: my-cluster
86 current-context: minikube
87 kind: Config
88 preferences: {}
89 users:
90 - name: minikube
91 user:
92 client-key-data: DATA+OMITTED
93 password: REDACTED
94 token: REDACTED
95 username: foo
96 - name: mu-cluster
97 user:
98 client-key-data: DATA+OMITTED
99 password: REDACTED
100 token: REDACTED
101 username: bar` + "\n",
102 }
103
104 test.run(t)
105
106 }
107
108 func TestViewClusterUnredacted(t *testing.T) {
109 conf := clientcmdapi.Config{
110 Kind: "Config",
111 APIVersion: "v1",
112 Clusters: map[string]*clientcmdapi.Cluster{
113 "minikube": {Server: "https://192.168.99.100:8443"},
114 "my-cluster": {Server: "https://192.168.0.1:3434"},
115 },
116 Contexts: map[string]*clientcmdapi.Context{
117 "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
118 "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
119 },
120 CurrentContext: "minikube",
121 AuthInfos: map[string]*clientcmdapi.AuthInfo{
122 "minikube": {
123 ClientKeyData: []byte("notredacted"),
124 ClientCertificateData: []byte("plaintext"),
125 Token: "notredacted",
126 Username: "foo",
127 Password: "notredacted",
128 },
129 "mu-cluster": {
130 ClientKeyData: []byte("notredacted"),
131 ClientCertificateData: []byte("plaintext"),
132 Token: "notredacted",
133 Username: "bar",
134 Password: "notredacted",
135 },
136 },
137 }
138
139 testCases := []struct {
140 description string
141 config clientcmdapi.Config
142 flags []string
143 expected string
144 }{
145 {
146 description: "Testing for kubectl config view --raw=true",
147 config: conf,
148 flags: []string{"--raw=true"},
149 expected: `apiVersion: v1
150 clusters:
151 - cluster:
152 server: https://192.168.99.100:8443
153 name: minikube
154 - cluster:
155 server: https://192.168.0.1:3434
156 name: my-cluster
157 contexts:
158 - context:
159 cluster: minikube
160 user: minikube
161 name: minikube
162 - context:
163 cluster: my-cluster
164 user: mu-cluster
165 name: my-cluster
166 current-context: minikube
167 kind: Config
168 preferences: {}
169 users:
170 - name: minikube
171 user:
172 client-certificate-data: cGxhaW50ZXh0
173 client-key-data: bm90cmVkYWN0ZWQ=
174 password: notredacted
175 token: notredacted
176 username: foo
177 - name: mu-cluster
178 user:
179 client-certificate-data: cGxhaW50ZXh0
180 client-key-data: bm90cmVkYWN0ZWQ=
181 password: notredacted
182 token: notredacted
183 username: bar` + "\n",
184 },
185 }
186
187 for _, test := range testCases {
188 cmdTest := viewClusterTest{
189 description: test.description,
190 config: test.config,
191 flags: test.flags,
192 expected: test.expected,
193 }
194 cmdTest.run(t)
195 }
196
197 }
198
199 func TestViewClusterMinify(t *testing.T) {
200 conf := clientcmdapi.Config{
201 Kind: "Config",
202 APIVersion: "v1",
203 Clusters: map[string]*clientcmdapi.Cluster{
204 "minikube": {Server: "https://192.168.99.100:8443"},
205 "my-cluster": {Server: "https://192.168.0.1:3434"},
206 },
207 Contexts: map[string]*clientcmdapi.Context{
208 "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
209 "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
210 },
211 CurrentContext: "minikube",
212 AuthInfos: map[string]*clientcmdapi.AuthInfo{
213 "minikube": {
214 ClientKeyData: []byte("notredacted"),
215 Token: "notredacted",
216 Username: "foo",
217 Password: "notredacted",
218 },
219 "mu-cluster": {
220 ClientKeyData: []byte("notredacted"),
221 Token: "notredacted",
222 Username: "bar",
223 Password: "notredacted",
224 },
225 },
226 }
227
228 testCases := []struct {
229 description string
230 config clientcmdapi.Config
231 flags []string
232 expected string
233 }{
234 {
235 description: "Testing for kubectl config view --minify=true",
236 config: conf,
237 flags: []string{"--minify=true"},
238 expected: `apiVersion: v1
239 clusters:
240 - cluster:
241 server: https://192.168.99.100:8443
242 name: minikube
243 contexts:
244 - context:
245 cluster: minikube
246 user: minikube
247 name: minikube
248 current-context: minikube
249 kind: Config
250 preferences: {}
251 users:
252 - name: minikube
253 user:
254 client-key-data: DATA+OMITTED
255 password: REDACTED
256 token: REDACTED
257 username: foo` + "\n",
258 },
259 {
260 description: "Testing for kubectl config view --minify=true --context=my-cluster",
261 config: conf,
262 flags: []string{"--minify=true", "--context=my-cluster"},
263 expected: `apiVersion: v1
264 clusters:
265 - cluster:
266 server: https://192.168.0.1:3434
267 name: my-cluster
268 contexts:
269 - context:
270 cluster: my-cluster
271 user: mu-cluster
272 name: my-cluster
273 current-context: my-cluster
274 kind: Config
275 preferences: {}
276 users:
277 - name: mu-cluster
278 user:
279 client-key-data: DATA+OMITTED
280 password: REDACTED
281 token: REDACTED
282 username: bar` + "\n",
283 },
284 }
285
286 for _, test := range testCases {
287 cmdTest := viewClusterTest{
288 description: test.description,
289 config: test.config,
290 flags: test.flags,
291 expected: test.expected,
292 }
293 cmdTest.run(t)
294 }
295 }
296
297 func (test viewClusterTest) run(t *testing.T) {
298 fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
299 if err != nil {
300 t.Fatalf("unexpected error: %v", err)
301 }
302 defer utiltesting.CloseAndRemove(t, fakeKubeFile)
303 err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
304 if err != nil {
305 t.Fatalf("unexpected error: %v", err)
306 }
307 pathOptions := clientcmd.NewDefaultPathOptions()
308 pathOptions.GlobalFile = fakeKubeFile.Name()
309 pathOptions.EnvVar = ""
310 streams, _, buf, _ := genericiooptions.NewTestIOStreams()
311 cmd := NewCmdConfigView(streams, pathOptions)
312
313 cmd.Flags().String("context", "", "The name of the kubeconfig context to use")
314 cmd.Flags().Parse(test.flags)
315
316 if err := cmd.Execute(); err != nil {
317 t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags)
318 }
319 if len(test.expected) != 0 {
320 if buf.String() != test.expected {
321 t.Errorf("Failed in %q\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
322 }
323 }
324 }
325
View as plain text