get_contexts_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2014 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. )
  22. type getContextsTest struct {
  23. startingConfig clientcmdapi.Config
  24. names []string
  25. noHeader bool
  26. nameOnly bool
  27. expectedOut string
  28. }
  29. func TestGetContextsAll(t *testing.T) {
  30. tconf := clientcmdapi.Config{
  31. CurrentContext: "shaker-context",
  32. Contexts: map[string]*clientcmdapi.Context{
  33. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  34. test := getContextsTest{
  35. startingConfig: tconf,
  36. names: []string{},
  37. noHeader: false,
  38. nameOnly: false,
  39. expectedOut: `CURRENT NAME CLUSTER AUTHINFO NAMESPACE
  40. * shaker-context big-cluster blue-user saw-ns
  41. `,
  42. }
  43. test.run(t)
  44. }
  45. func TestGetContextsAllNoHeader(t *testing.T) {
  46. tconf := clientcmdapi.Config{
  47. CurrentContext: "shaker-context",
  48. Contexts: map[string]*clientcmdapi.Context{
  49. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  50. test := getContextsTest{
  51. startingConfig: tconf,
  52. names: []string{},
  53. noHeader: true,
  54. nameOnly: false,
  55. expectedOut: "* shaker-context big-cluster blue-user saw-ns\n",
  56. }
  57. test.run(t)
  58. }
  59. func TestGetContextsAllSorted(t *testing.T) {
  60. tconf := clientcmdapi.Config{
  61. CurrentContext: "shaker-context",
  62. Contexts: map[string]*clientcmdapi.Context{
  63. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
  64. "abc": {AuthInfo: "blue-user", Cluster: "abc-cluster", Namespace: "kube-system"},
  65. "xyz": {AuthInfo: "blue-user", Cluster: "xyz-cluster", Namespace: "default"}}}
  66. test := getContextsTest{
  67. startingConfig: tconf,
  68. names: []string{},
  69. noHeader: false,
  70. nameOnly: false,
  71. expectedOut: `CURRENT NAME CLUSTER AUTHINFO NAMESPACE
  72. abc abc-cluster blue-user kube-system
  73. * shaker-context big-cluster blue-user saw-ns
  74. xyz xyz-cluster blue-user default
  75. `,
  76. }
  77. test.run(t)
  78. }
  79. func TestGetContextsAllName(t *testing.T) {
  80. tconf := clientcmdapi.Config{
  81. Contexts: map[string]*clientcmdapi.Context{
  82. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  83. test := getContextsTest{
  84. startingConfig: tconf,
  85. names: []string{},
  86. noHeader: false,
  87. nameOnly: true,
  88. expectedOut: "shaker-context\n",
  89. }
  90. test.run(t)
  91. }
  92. func TestGetContextsAllNameNoHeader(t *testing.T) {
  93. tconf := clientcmdapi.Config{
  94. CurrentContext: "shaker-context",
  95. Contexts: map[string]*clientcmdapi.Context{
  96. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  97. test := getContextsTest{
  98. startingConfig: tconf,
  99. names: []string{},
  100. noHeader: true,
  101. nameOnly: true,
  102. expectedOut: "shaker-context\n",
  103. }
  104. test.run(t)
  105. }
  106. func TestGetContextsAllNone(t *testing.T) {
  107. test := getContextsTest{
  108. startingConfig: *clientcmdapi.NewConfig(),
  109. names: []string{},
  110. noHeader: true,
  111. nameOnly: false,
  112. expectedOut: "",
  113. }
  114. test.run(t)
  115. }
  116. func TestGetContextsSelectOneOfTwo(t *testing.T) {
  117. tconf := clientcmdapi.Config{
  118. CurrentContext: "shaker-context",
  119. Contexts: map[string]*clientcmdapi.Context{
  120. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
  121. "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  122. test := getContextsTest{
  123. startingConfig: tconf,
  124. names: []string{"shaker-context"},
  125. noHeader: true,
  126. nameOnly: true,
  127. expectedOut: "shaker-context\n",
  128. }
  129. test.run(t)
  130. }
  131. func (test getContextsTest) run(t *testing.T) {
  132. fakeKubeFile, err := ioutil.TempFile("", "")
  133. if err != nil {
  134. t.Fatalf("unexpected error: %v", err)
  135. }
  136. defer os.Remove(fakeKubeFile.Name())
  137. err = clientcmd.WriteToFile(test.startingConfig, fakeKubeFile.Name())
  138. if err != nil {
  139. t.Fatalf("unexpected error: %v", err)
  140. }
  141. pathOptions := clientcmd.NewDefaultPathOptions()
  142. pathOptions.GlobalFile = fakeKubeFile.Name()
  143. pathOptions.EnvVar = ""
  144. streams, _, buf, _ := genericclioptions.NewTestIOStreams()
  145. options := GetContextsOptions{
  146. configAccess: pathOptions,
  147. }
  148. cmd := NewCmdConfigGetContexts(streams, options.configAccess)
  149. if test.nameOnly {
  150. cmd.Flags().Set("output", "name")
  151. }
  152. if test.noHeader {
  153. cmd.Flags().Set("no-headers", "true")
  154. }
  155. cmd.Run(cmd, test.names)
  156. if len(test.expectedOut) != 0 {
  157. if buf.String() != test.expectedOut {
  158. t.Errorf("Expected\n%s\ngot\n%s", test.expectedOut, buf.String())
  159. }
  160. return
  161. }
  162. }