create_cluster_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 createClusterTest struct {
  23. description string
  24. config clientcmdapi.Config
  25. args []string
  26. flags []string
  27. expected string
  28. expectedConfig clientcmdapi.Config
  29. }
  30. func TestCreateCluster(t *testing.T) {
  31. conf := clientcmdapi.Config{}
  32. test := createClusterTest{
  33. description: "Testing 'kubectl config set-cluster' with a new cluster",
  34. config: conf,
  35. args: []string{"my-cluster"},
  36. flags: []string{
  37. "--server=http://192.168.0.1",
  38. },
  39. expected: `Cluster "my-cluster" set.` + "\n",
  40. expectedConfig: clientcmdapi.Config{
  41. Clusters: map[string]*clientcmdapi.Cluster{
  42. "my-cluster": {Server: "http://192.168.0.1"},
  43. },
  44. },
  45. }
  46. test.run(t)
  47. }
  48. func TestModifyCluster(t *testing.T) {
  49. conf := clientcmdapi.Config{
  50. Clusters: map[string]*clientcmdapi.Cluster{
  51. "my-cluster": {Server: "https://192.168.0.1"},
  52. },
  53. }
  54. test := createClusterTest{
  55. description: "Testing 'kubectl config set-cluster' with an existing cluster",
  56. config: conf,
  57. args: []string{"my-cluster"},
  58. flags: []string{
  59. "--server=https://192.168.0.99",
  60. },
  61. expected: `Cluster "my-cluster" set.` + "\n",
  62. expectedConfig: clientcmdapi.Config{
  63. Clusters: map[string]*clientcmdapi.Cluster{
  64. "my-cluster": {Server: "https://192.168.0.99"},
  65. },
  66. },
  67. }
  68. test.run(t)
  69. }
  70. func (test createClusterTest) run(t *testing.T) {
  71. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  72. if err != nil {
  73. t.Fatalf("unexpected error: %v", err)
  74. }
  75. defer os.Remove(fakeKubeFile.Name())
  76. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  77. if err != nil {
  78. t.Fatalf("unexpected error: %v", err)
  79. }
  80. pathOptions := clientcmd.NewDefaultPathOptions()
  81. pathOptions.GlobalFile = fakeKubeFile.Name()
  82. pathOptions.EnvVar = ""
  83. buf := bytes.NewBuffer([]byte{})
  84. cmd := NewCmdConfigSetCluster(buf, pathOptions)
  85. cmd.SetArgs(test.args)
  86. cmd.Flags().Parse(test.flags)
  87. if err := cmd.Execute(); err != nil {
  88. t.Fatalf("unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags)
  89. }
  90. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  91. if err != nil {
  92. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  93. }
  94. if len(test.expected) != 0 {
  95. if buf.String() != test.expected {
  96. t.Errorf("Failed in %q\n expected %v\n but got %v", test.description, test.expected, buf.String())
  97. }
  98. }
  99. if len(test.args) > 0 {
  100. cluster, ok := config.Clusters[test.args[0]]
  101. if !ok {
  102. t.Errorf("expected cluster %v, but got nil", test.args[0])
  103. return
  104. }
  105. if cluster.Server != test.expectedConfig.Clusters[test.args[0]].Server {
  106. t.Errorf("Fail in %q\n expected cluster server %v\n but got %v\n ", test.description, test.expectedConfig.Clusters[test.args[0]].Server, cluster.Server)
  107. }
  108. }
  109. }