1/* 2Copyright 2017 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package config 18 19import ( 20 "bytes" 21 "io/ioutil" 22 "os" 23 "testing" 24 25 "k8s.io/client-go/tools/clientcmd" 26 clientcmdapi "k8s.io/client-go/tools/clientcmd/api" 27) 28 29type createClusterTest struct { 30 description string 31 config clientcmdapi.Config 32 args []string 33 flags []string 34 expected string 35 expectedConfig clientcmdapi.Config 36} 37 38func TestCreateCluster(t *testing.T) { 39 conf := clientcmdapi.Config{} 40 test := createClusterTest{ 41 description: "Testing 'kubectl config set-cluster' with a new cluster", 42 config: conf, 43 args: []string{"my-cluster"}, 44 flags: []string{ 45 "--server=http://192.168.0.1", 46 "--tls-server-name=my-cluster-name", 47 }, 48 expected: `Cluster "my-cluster" set.` + "\n", 49 expectedConfig: clientcmdapi.Config{ 50 Clusters: map[string]*clientcmdapi.Cluster{ 51 "my-cluster": {Server: "http://192.168.0.1", TLSServerName: "my-cluster-name"}, 52 }, 53 }, 54 } 55 test.run(t) 56} 57 58func TestModifyCluster(t *testing.T) { 59 conf := clientcmdapi.Config{ 60 Clusters: map[string]*clientcmdapi.Cluster{ 61 "my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"}, 62 }, 63 } 64 test := createClusterTest{ 65 description: "Testing 'kubectl config set-cluster' with an existing cluster", 66 config: conf, 67 args: []string{"my-cluster"}, 68 flags: []string{ 69 "--server=https://192.168.0.99", 70 }, 71 expected: `Cluster "my-cluster" set.` + "\n", 72 expectedConfig: clientcmdapi.Config{ 73 Clusters: map[string]*clientcmdapi.Cluster{ 74 "my-cluster": {Server: "https://192.168.0.99"}, 75 }, 76 }, 77 } 78 test.run(t) 79} 80 81func TestModifyClusterServerAndTLS(t *testing.T) { 82 conf := clientcmdapi.Config{ 83 Clusters: map[string]*clientcmdapi.Cluster{ 84 "my-cluster": {Server: "https://192.168.0.1"}, 85 }, 86 } 87 test := createClusterTest{ 88 description: "Testing 'kubectl config set-cluster' with an existing cluster", 89 config: conf, 90 args: []string{"my-cluster"}, 91 flags: []string{ 92 "--server=https://192.168.0.99", 93 "--tls-server-name=my-cluster-name", 94 }, 95 expected: `Cluster "my-cluster" set.` + "\n", 96 expectedConfig: clientcmdapi.Config{ 97 Clusters: map[string]*clientcmdapi.Cluster{ 98 "my-cluster": {Server: "https://192.168.0.99", TLSServerName: "my-cluster-name"}, 99 }, 100 }, 101 } 102 test.run(t) 103} 104 105func (test createClusterTest) run(t *testing.T) { 106 fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") 107 if err != nil { 108 t.Fatalf("unexpected error: %v", err) 109 } 110 defer os.Remove(fakeKubeFile.Name()) 111 err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name()) 112 if err != nil { 113 t.Fatalf("unexpected error: %v", err) 114 } 115 pathOptions := clientcmd.NewDefaultPathOptions() 116 pathOptions.GlobalFile = fakeKubeFile.Name() 117 pathOptions.EnvVar = "" 118 buf := bytes.NewBuffer([]byte{}) 119 cmd := NewCmdConfigSetCluster(buf, pathOptions) 120 cmd.SetArgs(test.args) 121 cmd.Flags().Parse(test.flags) 122 if err := cmd.Execute(); err != nil { 123 t.Fatalf("unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags) 124 } 125 config, err := clientcmd.LoadFromFile(fakeKubeFile.Name()) 126 if err != nil { 127 t.Fatalf("unexpected error loading kubeconfig file: %v", err) 128 } 129 if len(test.expected) != 0 { 130 if buf.String() != test.expected { 131 t.Errorf("Failed in %q\n expected %v\n but got %v", test.description, test.expected, buf.String()) 132 } 133 } 134 if len(test.args) > 0 { 135 cluster, ok := config.Clusters[test.args[0]] 136 if !ok { 137 t.Errorf("expected cluster %v, but got nil", test.args[0]) 138 return 139 } 140 if cluster.Server != test.expectedConfig.Clusters[test.args[0]].Server { 141 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) 142 } 143 if cluster.TLSServerName != test.expectedConfig.Clusters[test.args[0]].TLSServerName { 144 t.Errorf("Fail in %q\n expected cluster TLS server name %q\n but got %q\n ", test.description, test.expectedConfig.Clusters[test.args[0]].TLSServerName, cluster.TLSServerName) 145 } 146 } 147} 148