delete_context_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2016 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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "k8s.io/client-go/tools/clientcmd"
  22. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  23. )
  24. type deleteContextTest struct {
  25. config clientcmdapi.Config
  26. contextToDelete string
  27. expectedContexts []string
  28. expectedOut string
  29. }
  30. func TestDeleteContext(t *testing.T) {
  31. conf := clientcmdapi.Config{
  32. Contexts: map[string]*clientcmdapi.Context{
  33. "minikube": {Cluster: "minikube"},
  34. "otherkube": {Cluster: "otherkube"},
  35. },
  36. }
  37. test := deleteContextTest{
  38. config: conf,
  39. contextToDelete: "minikube",
  40. expectedContexts: []string{"otherkube"},
  41. expectedOut: "deleted context minikube from %s\n",
  42. }
  43. test.run(t)
  44. }
  45. func (test deleteContextTest) run(t *testing.T) {
  46. fakeKubeFile, err := ioutil.TempFile("", "")
  47. if err != nil {
  48. t.Fatalf("unexpected error: %v", err)
  49. }
  50. defer os.Remove(fakeKubeFile.Name())
  51. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  52. if err != nil {
  53. t.Fatalf("unexpected error: %v", err)
  54. }
  55. pathOptions := clientcmd.NewDefaultPathOptions()
  56. pathOptions.GlobalFile = fakeKubeFile.Name()
  57. pathOptions.EnvVar = ""
  58. buf := bytes.NewBuffer([]byte{})
  59. errBuf := bytes.NewBuffer([]byte{})
  60. cmd := NewCmdConfigDeleteContext(buf, errBuf, pathOptions)
  61. cmd.SetArgs([]string{test.contextToDelete})
  62. if err := cmd.Execute(); err != nil {
  63. t.Fatalf("unexpected error executing command: %v", err)
  64. }
  65. expectedOutWithFile := fmt.Sprintf(test.expectedOut, fakeKubeFile.Name())
  66. if expectedOutWithFile != buf.String() {
  67. t.Errorf("expected output %s, but got %s", expectedOutWithFile, buf.String())
  68. return
  69. }
  70. // Verify context was removed from kubeconfig file
  71. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  72. if err != nil {
  73. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  74. }
  75. contexts := make([]string, 0, len(config.Contexts))
  76. for k := range config.Contexts {
  77. contexts = append(contexts, k)
  78. }
  79. if !reflect.DeepEqual(test.expectedContexts, contexts) {
  80. t.Errorf("expected contexts %v, but found %v in kubeconfig", test.expectedContexts, contexts)
  81. }
  82. }