create_context_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "bytes"
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. "k8s.io/client-go/tools/clientcmd"
  20. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  21. )
  22. type createContextTest struct {
  23. description string
  24. testContext string // name of the context being modified
  25. config clientcmdapi.Config //initiate kubectl config
  26. args []string //kubectl set-context args
  27. flags []string //kubectl set-context flags
  28. expected string //expectd out
  29. expectedConfig clientcmdapi.Config //expect kubectl config
  30. }
  31. func TestCreateContext(t *testing.T) {
  32. conf := clientcmdapi.Config{}
  33. test := createContextTest{
  34. testContext: "shaker-context",
  35. description: "Testing for create a new context",
  36. config: conf,
  37. args: []string{"shaker-context"},
  38. flags: []string{
  39. "--cluster=cluster_nickname",
  40. "--user=user_nickname",
  41. "--namespace=namespace",
  42. },
  43. expected: `Context "shaker-context" created.` + "\n",
  44. expectedConfig: clientcmdapi.Config{
  45. Contexts: map[string]*clientcmdapi.Context{
  46. "shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"}},
  47. },
  48. }
  49. test.run(t)
  50. }
  51. func TestModifyContext(t *testing.T) {
  52. conf := clientcmdapi.Config{
  53. Contexts: map[string]*clientcmdapi.Context{
  54. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
  55. "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  56. test := createContextTest{
  57. testContext: "shaker-context",
  58. description: "Testing for modify a already exist context",
  59. config: conf,
  60. args: []string{"shaker-context"},
  61. flags: []string{
  62. "--cluster=cluster_nickname",
  63. "--user=user_nickname",
  64. "--namespace=namespace",
  65. },
  66. expected: `Context "shaker-context" modified.` + "\n",
  67. expectedConfig: clientcmdapi.Config{
  68. Contexts: map[string]*clientcmdapi.Context{
  69. "shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
  70. "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
  71. }
  72. test.run(t)
  73. }
  74. func TestModifyCurrentContext(t *testing.T) {
  75. conf := clientcmdapi.Config{
  76. CurrentContext: "shaker-context",
  77. Contexts: map[string]*clientcmdapi.Context{
  78. "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
  79. "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
  80. test := createContextTest{
  81. testContext: "shaker-context",
  82. description: "Testing for modify a current context",
  83. config: conf,
  84. args: []string{},
  85. flags: []string{
  86. "--current",
  87. "--cluster=cluster_nickname",
  88. "--user=user_nickname",
  89. "--namespace=namespace",
  90. },
  91. expected: `Context "shaker-context" modified.` + "\n",
  92. expectedConfig: clientcmdapi.Config{
  93. Contexts: map[string]*clientcmdapi.Context{
  94. "shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
  95. "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
  96. }
  97. test.run(t)
  98. }
  99. func (test createContextTest) run(t *testing.T) {
  100. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  101. if err != nil {
  102. t.Fatalf("unexpected error: %v", err)
  103. }
  104. defer os.Remove(fakeKubeFile.Name())
  105. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  106. if err != nil {
  107. t.Fatalf("unexpected error: %v", err)
  108. }
  109. pathOptions := clientcmd.NewDefaultPathOptions()
  110. pathOptions.GlobalFile = fakeKubeFile.Name()
  111. pathOptions.EnvVar = ""
  112. buf := bytes.NewBuffer([]byte{})
  113. cmd := NewCmdConfigSetContext(buf, pathOptions)
  114. cmd.SetArgs(test.args)
  115. cmd.Flags().Parse(test.flags)
  116. if err := cmd.Execute(); err != nil {
  117. t.Fatalf("unexpected error executing command: %v,kubectl set-context args: %v,flags: %v", err, test.args, test.flags)
  118. }
  119. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  120. if err != nil {
  121. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  122. }
  123. if len(test.expected) != 0 {
  124. if buf.String() != test.expected {
  125. t.Errorf("Fail in %q:\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
  126. }
  127. }
  128. if test.expectedConfig.Contexts != nil {
  129. expectContext := test.expectedConfig.Contexts[test.testContext]
  130. actualContext := config.Contexts[test.testContext]
  131. if expectContext.AuthInfo != actualContext.AuthInfo || expectContext.Cluster != actualContext.Cluster ||
  132. expectContext.Namespace != actualContext.Namespace {
  133. t.Errorf("Fail in %q:\n expected Context %v\n but found %v in kubeconfig\n", test.description, expectContext, actualContext)
  134. }
  135. }
  136. }