Skip to content

Commit

Permalink
add test code for kubectl config set-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjiajin committed Apr 19, 2017
1 parent d264cdf commit 988aadb
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ go_test(
srcs = [
"config_test.go",
"create_authinfo_test.go",
"create_cluster_test.go",
"create_context_test.go",
"current_context_test.go",
"delete_cluster_test.go",
Expand Down
123 changes: 123 additions & 0 deletions pkg/kubectl/cmd/config/create_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"bytes"
"io/ioutil"
"os"
"testing"

"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

type createClusterTest struct {
description string
config clientcmdapi.Config //initiate kubectl config
args []string //kubectl set-context args
flags []string //kubectl set-context flags
expected string //expectd out
expectedConfig clientcmdapi.Config //expect kubectl config
}

func TestCreateCluster(t *testing.T) {
conf := clientcmdapi.Config{}
test := createClusterTest{
description: "Testing kubectl config set-cluster a cluster",
config: conf,
args: []string{"my-cluster"},
flags: []string{
"--api-version=my-version",
"--server=http://192.168.0.1",
},
expected: `Cluster "my-cluster" set.` + "\n",
expectedConfig: clientcmdapi.Config{
APIVersion: "my-version",
Clusters: map[string]*clientcmdapi.Cluster{
"my-cluster": {Server: "http://192.168.0.1"},
},
},
}
test.run(t)
}

func TestModifyCluster(t *testing.T) {
conf := clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
"my-cluster": {Server: "https://192.168.0.1"},
},
}
test := createClusterTest{
description: "Testing kubectl config set-cluster a exist cluster",
config: conf,
args: []string{"my-cluster"},
flags: []string{
"--api-version=my-version",
"--server=https://192.168.0.99",
},
expected: `Cluster "my-cluster" set.` + "\n",
expectedConfig: clientcmdapi.Config{
APIVersion: "my-version",
Clusters: map[string]*clientcmdapi.Cluster{
"my-cluster": {Server: "https://192.168.0.99"},
},
},
}
test.run(t)
}

func (test createClusterTest) run(t *testing.T) {
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer os.Remove(fakeKubeFile.Name())
err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
pathOptions := clientcmd.NewDefaultPathOptions()
pathOptions.GlobalFile = fakeKubeFile.Name()
pathOptions.EnvVar = ""
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigSetCluster(buf, pathOptions)
cmd.SetArgs(test.args)
cmd.Flags().Parse(test.flags)
if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error executing command: %v,kubectl set-cluster args:%v,flags:%v", err)
}
config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
if err != nil {
t.Fatalf("unexpected error loading kubeconfig file: %v", err)
}
if len(test.expected) != 0 {
if buf.String() != test.expected {
t.Errorf("Failded in %q\n expected %v\n but got %v", test.expected, buf.String())
}
}
if len(test.args) > 0 {
cluster, ok := config.Clusters[test.args[0]]
if !ok {
t.Errorf("expected create cluster %v, but got nil", test.args[0])
return
}
if cluster.Server != test.expectedConfig.Clusters[test.args[0]].Server {
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)
}
}
}

0 comments on commit 988aadb

Please sign in to comment.