rename_context_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "testing"
  21. "k8s.io/client-go/tools/clientcmd"
  22. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  23. )
  24. const (
  25. currentContext = "current-context"
  26. newContext = "new-context"
  27. nonexistentCurrentContext = "nonexistent-current-context"
  28. existentNewContext = "existent-new-context"
  29. )
  30. var (
  31. contextData = clientcmdapi.NewContext()
  32. )
  33. type renameContextTest struct {
  34. description string
  35. initialConfig clientcmdapi.Config // initial config
  36. expectedConfig clientcmdapi.Config // expected config
  37. args []string // kubectl rename-context args
  38. expectedOut string // expected out message
  39. expectedErr string // expected error message
  40. }
  41. func TestRenameContext(t *testing.T) {
  42. initialConfig := clientcmdapi.Config{
  43. CurrentContext: currentContext,
  44. Contexts: map[string]*clientcmdapi.Context{currentContext: contextData}}
  45. expectedConfig := clientcmdapi.Config{
  46. CurrentContext: newContext,
  47. Contexts: map[string]*clientcmdapi.Context{newContext: contextData}}
  48. test := renameContextTest{
  49. description: "Testing for kubectl config rename-context whose context to be renamed is the CurrentContext",
  50. initialConfig: initialConfig,
  51. expectedConfig: expectedConfig,
  52. args: []string{currentContext, newContext},
  53. expectedOut: fmt.Sprintf("Context %q renamed to %q.\n", currentContext, newContext),
  54. expectedErr: "",
  55. }
  56. test.run(t)
  57. }
  58. func TestRenameNonexistentContext(t *testing.T) {
  59. initialConfig := clientcmdapi.Config{
  60. CurrentContext: currentContext,
  61. Contexts: map[string]*clientcmdapi.Context{currentContext: contextData}}
  62. test := renameContextTest{
  63. description: "Testing for kubectl config rename-context whose context to be renamed no exists",
  64. initialConfig: initialConfig,
  65. expectedConfig: initialConfig,
  66. args: []string{nonexistentCurrentContext, newContext},
  67. expectedOut: "",
  68. expectedErr: fmt.Sprintf("cannot rename the context %q, it's not in", nonexistentCurrentContext),
  69. }
  70. test.run(t)
  71. }
  72. func TestRenameToAlreadyExistingContext(t *testing.T) {
  73. initialConfig := clientcmdapi.Config{
  74. CurrentContext: currentContext,
  75. Contexts: map[string]*clientcmdapi.Context{
  76. currentContext: contextData,
  77. existentNewContext: contextData}}
  78. test := renameContextTest{
  79. description: "Testing for kubectl config rename-context whose the new name is already in another context.",
  80. initialConfig: initialConfig,
  81. expectedConfig: initialConfig,
  82. args: []string{currentContext, existentNewContext},
  83. expectedOut: "",
  84. expectedErr: fmt.Sprintf("cannot rename the context %q, the context %q already exists", currentContext, existentNewContext),
  85. }
  86. test.run(t)
  87. }
  88. func (test renameContextTest) run(t *testing.T) {
  89. fakeKubeFile, _ := ioutil.TempFile("", "")
  90. defer os.Remove(fakeKubeFile.Name())
  91. err := clientcmd.WriteToFile(test.initialConfig, fakeKubeFile.Name())
  92. if err != nil {
  93. t.Fatalf("unexpected error: %v", err)
  94. }
  95. pathOptions := clientcmd.NewDefaultPathOptions()
  96. pathOptions.GlobalFile = fakeKubeFile.Name()
  97. pathOptions.EnvVar = ""
  98. options := RenameContextOptions{
  99. configAccess: pathOptions,
  100. contextName: test.args[0],
  101. newName: test.args[1],
  102. }
  103. buf := bytes.NewBuffer([]byte{})
  104. cmd := NewCmdConfigRenameContext(buf, options.configAccess)
  105. options.Complete(cmd, test.args, buf)
  106. options.Validate()
  107. err = options.RunRenameContext(buf)
  108. if len(test.expectedErr) != 0 {
  109. if err == nil {
  110. t.Errorf("Did not get %v", test.expectedErr)
  111. } else {
  112. if !strings.Contains(err.Error(), test.expectedErr) {
  113. t.Errorf("Expected error %v, but got %v", test.expectedErr, err)
  114. }
  115. }
  116. return
  117. }
  118. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  119. if err != nil {
  120. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  121. }
  122. _, oldExists := config.Contexts[currentContext]
  123. _, newExists := config.Contexts[newContext]
  124. if (!newExists) || (oldExists) || (config.CurrentContext != newContext) {
  125. t.Errorf("Failed in: %q\n expected %v\n but got %v", test.description, test.expectedConfig, *config)
  126. }
  127. if len(test.expectedOut) != 0 {
  128. if buf.String() != test.expectedOut {
  129. t.Errorf("Failed in:%q\n expected out %v\n but got %v", test.description, test.expectedOut, buf.String())
  130. }
  131. }
  132. }