view_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "testing"
  18. "k8s.io/cli-runtime/pkg/genericclioptions"
  19. "k8s.io/client-go/tools/clientcmd"
  20. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  21. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  22. )
  23. type viewClusterTest struct {
  24. description string
  25. config clientcmdapi.Config //initiate kubectl config
  26. flags []string //kubectl config viw flags
  27. expected string //expect out
  28. }
  29. func TestViewCluster(t *testing.T) {
  30. conf := clientcmdapi.Config{
  31. Kind: "Config",
  32. APIVersion: "v1",
  33. Clusters: map[string]*clientcmdapi.Cluster{
  34. "minikube": {Server: "https://192.168.99.100:8443"},
  35. "my-cluster": {Server: "https://192.168.0.1:3434"},
  36. },
  37. Contexts: map[string]*clientcmdapi.Context{
  38. "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
  39. "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
  40. },
  41. CurrentContext: "minikube",
  42. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  43. "minikube": {Token: "minikube-token"},
  44. "mu-cluster": {Token: "minikube-token"},
  45. },
  46. }
  47. test := viewClusterTest{
  48. description: "Testing for kubectl config view",
  49. config: conf,
  50. expected: `apiVersion: v1
  51. clusters:
  52. - cluster:
  53. server: https://192.168.99.100:8443
  54. name: minikube
  55. - cluster:
  56. server: https://192.168.0.1:3434
  57. name: my-cluster
  58. contexts:
  59. - context:
  60. cluster: minikube
  61. user: minikube
  62. name: minikube
  63. - context:
  64. cluster: my-cluster
  65. user: mu-cluster
  66. name: my-cluster
  67. current-context: minikube
  68. kind: Config
  69. preferences: {}
  70. users:
  71. - name: minikube
  72. user:
  73. token: minikube-token
  74. - name: mu-cluster
  75. user:
  76. token: minikube-token` + "\n",
  77. }
  78. test.run(t)
  79. }
  80. func TestViewClusterMinify(t *testing.T) {
  81. conf := clientcmdapi.Config{
  82. Kind: "Config",
  83. APIVersion: "v1",
  84. Clusters: map[string]*clientcmdapi.Cluster{
  85. "minikube": {Server: "https://192.168.99.100:8443"},
  86. "my-cluster": {Server: "https://192.168.0.1:3434"},
  87. },
  88. Contexts: map[string]*clientcmdapi.Context{
  89. "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
  90. "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
  91. },
  92. CurrentContext: "minikube",
  93. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  94. "minikube": {Token: "minikube-token"},
  95. "mu-cluster": {Token: "minikube-token"},
  96. },
  97. }
  98. testCases := []struct {
  99. description string
  100. config clientcmdapi.Config
  101. flags []string
  102. expected string
  103. }{
  104. {
  105. description: "Testing for kubectl config view --minify=true",
  106. config: conf,
  107. flags: []string{"--minify=true"},
  108. expected: `apiVersion: v1
  109. clusters:
  110. - cluster:
  111. server: https://192.168.99.100:8443
  112. name: minikube
  113. contexts:
  114. - context:
  115. cluster: minikube
  116. user: minikube
  117. name: minikube
  118. current-context: minikube
  119. kind: Config
  120. preferences: {}
  121. users:
  122. - name: minikube
  123. user:
  124. token: minikube-token` + "\n",
  125. },
  126. {
  127. description: "Testing for kubectl config view --minify=true --context=my-cluster",
  128. config: conf,
  129. flags: []string{"--minify=true", "--context=my-cluster"},
  130. expected: `apiVersion: v1
  131. clusters:
  132. - cluster:
  133. server: https://192.168.0.1:3434
  134. name: my-cluster
  135. contexts:
  136. - context:
  137. cluster: my-cluster
  138. user: mu-cluster
  139. name: my-cluster
  140. current-context: my-cluster
  141. kind: Config
  142. preferences: {}
  143. users:
  144. - name: mu-cluster
  145. user:
  146. token: minikube-token` + "\n",
  147. },
  148. }
  149. for _, test := range testCases {
  150. cmdTest := viewClusterTest{
  151. description: test.description,
  152. config: test.config,
  153. flags: test.flags,
  154. expected: test.expected,
  155. }
  156. cmdTest.run(t)
  157. }
  158. }
  159. func (test viewClusterTest) run(t *testing.T) {
  160. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  161. if err != nil {
  162. t.Fatalf("unexpected error: %v", err)
  163. }
  164. defer os.Remove(fakeKubeFile.Name())
  165. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  166. if err != nil {
  167. t.Fatalf("unexpected error: %v", err)
  168. }
  169. pathOptions := clientcmd.NewDefaultPathOptions()
  170. pathOptions.GlobalFile = fakeKubeFile.Name()
  171. pathOptions.EnvVar = ""
  172. streams, _, buf, _ := genericclioptions.NewTestIOStreams()
  173. cmd := NewCmdConfigView(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), streams, pathOptions)
  174. // "context" is a global flag, inherited from base kubectl command in the real world
  175. cmd.Flags().String("context", "", "The name of the kubeconfig context to use")
  176. cmd.Flags().Parse(test.flags)
  177. if err := cmd.Execute(); err != nil {
  178. t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags)
  179. }
  180. if len(test.expected) != 0 {
  181. if buf.String() != test.expected {
  182. t.Errorf("Failed in %q\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
  183. }
  184. }
  185. }