use_context_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 useContextTest struct {
  23. description string
  24. config clientcmdapi.Config //initiate kubectl config
  25. args []string //kubectl config use-context args
  26. expected string //expect out
  27. expectedConfig clientcmdapi.Config //expect kubectl config
  28. }
  29. func TestUseContext(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. }
  43. test := useContextTest{
  44. description: "Testing for kubectl config use-context",
  45. config: conf,
  46. args: []string{"my-cluster"},
  47. expected: `Switched to context "my-cluster".` + "\n",
  48. expectedConfig: clientcmdapi.Config{
  49. Kind: "Config",
  50. APIVersion: "v1",
  51. Clusters: map[string]*clientcmdapi.Cluster{
  52. "minikube": {Server: "https://192.168.99.100:8443"},
  53. "my-cluster": {Server: "https://192.168.0.1:3434"},
  54. },
  55. Contexts: map[string]*clientcmdapi.Context{
  56. "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
  57. "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
  58. },
  59. CurrentContext: "my-cluster",
  60. },
  61. }
  62. test.run(t)
  63. }
  64. func (test useContextTest) run(t *testing.T) {
  65. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  66. if err != nil {
  67. t.Fatalf("unexpected error: %v", err)
  68. }
  69. defer os.Remove(fakeKubeFile.Name())
  70. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  71. if err != nil {
  72. t.Fatalf("unexpected error: %v", err)
  73. }
  74. pathOptions := clientcmd.NewDefaultPathOptions()
  75. pathOptions.GlobalFile = fakeKubeFile.Name()
  76. pathOptions.EnvVar = ""
  77. buf := bytes.NewBuffer([]byte{})
  78. cmd := NewCmdConfigUseContext(buf, pathOptions)
  79. cmd.SetArgs(test.args)
  80. if err := cmd.Execute(); err != nil {
  81. t.Fatalf("unexpected error executing command: %v,kubectl config use-context args: %v", err, test.args)
  82. }
  83. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  84. if err != nil {
  85. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  86. }
  87. if len(test.expected) != 0 {
  88. if buf.String() != test.expected {
  89. t.Errorf("Failed in :%q\n expected %v\n, but got %v\n", test.description, test.expected, buf.String())
  90. }
  91. }
  92. if test.expectedConfig.CurrentContext != config.CurrentContext {
  93. t.Errorf("Failed in :%q\n expected config %v, but found %v\n in kubeconfig\n", test.description, test.expectedConfig, config)
  94. }
  95. }