set_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "reflect"
  20. "k8s.io/client-go/tools/clientcmd"
  21. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  22. )
  23. type setConfigTest struct {
  24. description string
  25. config clientcmdapi.Config
  26. args []string
  27. expected string
  28. expectedConfig clientcmdapi.Config
  29. }
  30. func TestSetConfigCurrentContext(t *testing.T) {
  31. conf := clientcmdapi.Config{
  32. Kind: "Config",
  33. APIVersion: "v1",
  34. CurrentContext: "minikube",
  35. }
  36. expectedConfig := *clientcmdapi.NewConfig()
  37. expectedConfig.CurrentContext = "my-cluster"
  38. test := setConfigTest{
  39. description: "Testing for kubectl config set current-context",
  40. config: conf,
  41. args: []string{"current-context", "my-cluster"},
  42. expected: `Property "current-context" set.` + "\n",
  43. expectedConfig: expectedConfig,
  44. }
  45. test.run(t)
  46. }
  47. func (test setConfigTest) run(t *testing.T) {
  48. fakeKubeFile, err := ioutil.TempFile("", "")
  49. if err != nil {
  50. t.Fatalf("unexpected error: %v", err)
  51. }
  52. defer os.Remove(fakeKubeFile.Name())
  53. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  54. if err != nil {
  55. t.Fatalf("unexpected error: %v", err)
  56. }
  57. pathOptions := clientcmd.NewDefaultPathOptions()
  58. pathOptions.GlobalFile = fakeKubeFile.Name()
  59. pathOptions.EnvVar = ""
  60. buf := bytes.NewBuffer([]byte{})
  61. cmd := NewCmdConfigSet(buf, pathOptions)
  62. cmd.SetArgs(test.args)
  63. if err := cmd.Execute(); err != nil {
  64. t.Fatalf("unexpected error executing command: %v", err)
  65. }
  66. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  67. if err != nil {
  68. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  69. }
  70. if len(test.expected) != 0 {
  71. if buf.String() != test.expected {
  72. t.Errorf("Failed in:%q\n expected %v\n but got %v", test.description, test.expected, buf.String())
  73. }
  74. }
  75. if !reflect.DeepEqual(*config, test.expectedConfig) {
  76. t.Errorf("Failed in: %q\n expected %v\n but got %v", test.description, *config, test.expectedConfig)
  77. }
  78. }