unset_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 unsetConfigTest struct {
  23. description string
  24. config clientcmdapi.Config
  25. args []string
  26. expected string
  27. expectedErr string
  28. }
  29. func TestUnsetConfigString(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 := unsetConfigTest{
  44. description: "Testing for kubectl config unset a value",
  45. config: conf,
  46. args: []string{"current-context"},
  47. expected: `Property "current-context" unset.` + "\n",
  48. }
  49. test.run(t)
  50. }
  51. func TestUnsetConfigMap(t *testing.T) {
  52. conf := clientcmdapi.Config{
  53. Kind: "Config",
  54. APIVersion: "v1",
  55. Clusters: map[string]*clientcmdapi.Cluster{
  56. "minikube": {Server: "https://192.168.99.100:8443"},
  57. "my-cluster": {Server: "https://192.168.0.1:3434"},
  58. },
  59. Contexts: map[string]*clientcmdapi.Context{
  60. "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
  61. "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
  62. },
  63. CurrentContext: "minikube",
  64. }
  65. test := unsetConfigTest{
  66. description: "Testing for kubectl config unset a map",
  67. config: conf,
  68. args: []string{"clusters"},
  69. expected: `Property "clusters" unset.` + "\n",
  70. }
  71. test.run(t)
  72. }
  73. func TestUnsetUnexistConfig(t *testing.T) {
  74. conf := clientcmdapi.Config{
  75. Kind: "Config",
  76. APIVersion: "v1",
  77. Clusters: map[string]*clientcmdapi.Cluster{
  78. "minikube": {Server: "https://192.168.99.100:8443"},
  79. "my-cluster": {Server: "https://192.168.0.1:3434"},
  80. },
  81. Contexts: map[string]*clientcmdapi.Context{
  82. "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
  83. "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
  84. },
  85. CurrentContext: "minikube",
  86. }
  87. test := unsetConfigTest{
  88. description: "Testing for kubectl config unset a unexist map key",
  89. config: conf,
  90. args: []string{"contexts.foo.namespace"},
  91. expectedErr: "current map key `foo` is invalid",
  92. }
  93. test.run(t)
  94. }
  95. func (test unsetConfigTest) run(t *testing.T) {
  96. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  97. if err != nil {
  98. t.Fatalf("unexpected error: %v", err)
  99. }
  100. defer os.Remove(fakeKubeFile.Name())
  101. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  102. if err != nil {
  103. t.Fatalf("unexpected error: %v", err)
  104. }
  105. pathOptions := clientcmd.NewDefaultPathOptions()
  106. pathOptions.GlobalFile = fakeKubeFile.Name()
  107. pathOptions.EnvVar = ""
  108. buf := bytes.NewBuffer([]byte{})
  109. cmd := NewCmdConfigUnset(buf, pathOptions)
  110. opts := &unsetOptions{configAccess: pathOptions}
  111. err = opts.complete(cmd, test.args)
  112. if err == nil {
  113. err = opts.run(buf)
  114. }
  115. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  116. if err != nil {
  117. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  118. }
  119. if err != nil && err.Error() != test.expectedErr {
  120. t.Fatalf("expected error:\n %v\nbut got error:\n%v", test.expectedErr, err)
  121. }
  122. if len(test.expected) != 0 {
  123. if buf.String() != test.expected {
  124. t.Errorf("Failed in :%q\n expected %v\n but got %v", test.description, test.expected, buf.String())
  125. }
  126. }
  127. if test.args[0] == "current-context" {
  128. if config.CurrentContext != "" {
  129. t.Errorf("Failed in :%q\n expected current-context nil,but got %v", test.description, config.CurrentContext)
  130. }
  131. } else if test.args[0] == "clusters" {
  132. if len(config.Clusters) != 0 {
  133. t.Errorf("Failed in :%q\n expected clusters nil map, but got %v", test.description, config.Clusters)
  134. }
  135. }
  136. }