...

Source file src/k8s.io/kubectl/pkg/cmd/config/set_cluster_test.go

Documentation: k8s.io/kubectl/pkg/cmd/config

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"testing"
    23  
    24  	utiltesting "k8s.io/client-go/util/testing"
    25  
    26  	"k8s.io/client-go/tools/clientcmd"
    27  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    28  )
    29  
    30  type setClusterTest struct {
    31  	description    string
    32  	config         clientcmdapi.Config
    33  	args           []string
    34  	flags          []string
    35  	expected       string
    36  	expectedConfig clientcmdapi.Config
    37  }
    38  
    39  func TestCreateCluster(t *testing.T) {
    40  	conf := clientcmdapi.Config{}
    41  	test := setClusterTest{
    42  		description: "Testing 'kubectl config set-cluster' with a new cluster",
    43  		config:      conf,
    44  		args:        []string{"my-cluster"},
    45  		flags: []string{
    46  			"--server=http://192.168.0.1",
    47  			"--tls-server-name=my-cluster-name",
    48  		},
    49  		expected: `Cluster "my-cluster" set.` + "\n",
    50  		expectedConfig: clientcmdapi.Config{
    51  			Clusters: map[string]*clientcmdapi.Cluster{
    52  				"my-cluster": {Server: "http://192.168.0.1", TLSServerName: "my-cluster-name"},
    53  			},
    54  		},
    55  	}
    56  	test.run(t)
    57  }
    58  
    59  func TestCreateClusterWithProxy(t *testing.T) {
    60  	conf := clientcmdapi.Config{}
    61  	test := setClusterTest{
    62  		description: "Testing 'kubectl config set-cluster' with a new cluster",
    63  		config:      conf,
    64  		args:        []string{"my-cluster"},
    65  		flags: []string{
    66  			"--server=http://192.168.0.1",
    67  			"--tls-server-name=my-cluster-name",
    68  			"--proxy-url=http://192.168.0.2",
    69  		},
    70  		expected: `Cluster "my-cluster" set.` + "\n",
    71  		expectedConfig: clientcmdapi.Config{
    72  			Clusters: map[string]*clientcmdapi.Cluster{
    73  				"my-cluster": {
    74  					Server:        "http://192.168.0.1",
    75  					TLSServerName: "my-cluster-name",
    76  					ProxyURL:      "http://192.168.0.2",
    77  				},
    78  			},
    79  		},
    80  	}
    81  	test.run(t)
    82  }
    83  
    84  func TestModifyCluster(t *testing.T) {
    85  	conf := clientcmdapi.Config{
    86  		Clusters: map[string]*clientcmdapi.Cluster{
    87  			"my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"},
    88  		},
    89  	}
    90  	test := setClusterTest{
    91  		description: "Testing 'kubectl config set-cluster' with an existing cluster",
    92  		config:      conf,
    93  		args:        []string{"my-cluster"},
    94  		flags: []string{
    95  			"--server=https://192.168.0.99",
    96  		},
    97  		expected: `Cluster "my-cluster" set.` + "\n",
    98  		expectedConfig: clientcmdapi.Config{
    99  			Clusters: map[string]*clientcmdapi.Cluster{
   100  				"my-cluster": {Server: "https://192.168.0.99"},
   101  			},
   102  		},
   103  	}
   104  	test.run(t)
   105  }
   106  
   107  // TestModifyClusterWithProxy tests setting proxy-url in kubeconfig
   108  func TestModifyClusterWithProxy(t *testing.T) {
   109  	conf := clientcmdapi.Config{
   110  		Clusters: map[string]*clientcmdapi.Cluster{
   111  			"my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"},
   112  		},
   113  	}
   114  	test := setClusterTest{
   115  		description: "Testing 'kubectl config set-cluster' with an existing cluster",
   116  		config:      conf,
   117  		args:        []string{"my-cluster"},
   118  		flags: []string{
   119  			"--server=https://192.168.0.99",
   120  			"--proxy-url=https://192.168.0.100",
   121  		},
   122  		expected: `Cluster "my-cluster" set.` + "\n",
   123  		expectedConfig: clientcmdapi.Config{
   124  			Clusters: map[string]*clientcmdapi.Cluster{
   125  				"my-cluster": {Server: "https://192.168.0.99", ProxyURL: "https://192.168.0.100"},
   126  			},
   127  		},
   128  	}
   129  	test.run(t)
   130  }
   131  
   132  // TestModifyClusterWithProxyOverride tests updating proxy-url
   133  // in kubeconfig which already exists
   134  func TestModifyClusterWithProxyOverride(t *testing.T) {
   135  	conf := clientcmdapi.Config{
   136  		Clusters: map[string]*clientcmdapi.Cluster{
   137  			"my-cluster": {
   138  				Server:        "https://192.168.0.1",
   139  				TLSServerName: "to-be-cleared",
   140  				ProxyURL:      "https://192.168.0.2",
   141  			},
   142  		},
   143  	}
   144  	test := setClusterTest{
   145  		description: "Testing 'kubectl config set-cluster' with an existing cluster",
   146  		config:      conf,
   147  		args:        []string{"my-cluster"},
   148  		flags: []string{
   149  			"--server=https://192.168.0.99",
   150  			"--proxy-url=https://192.168.0.100",
   151  		},
   152  		expected: `Cluster "my-cluster" set.` + "\n",
   153  		expectedConfig: clientcmdapi.Config{
   154  			Clusters: map[string]*clientcmdapi.Cluster{
   155  				"my-cluster": {Server: "https://192.168.0.99", ProxyURL: "https://192.168.0.100"},
   156  			},
   157  		},
   158  	}
   159  	test.run(t)
   160  }
   161  
   162  func TestModifyClusterServerAndTLS(t *testing.T) {
   163  	conf := clientcmdapi.Config{
   164  		Clusters: map[string]*clientcmdapi.Cluster{
   165  			"my-cluster": {Server: "https://192.168.0.1"},
   166  		},
   167  	}
   168  	test := setClusterTest{
   169  		description: "Testing 'kubectl config set-cluster' with an existing cluster",
   170  		config:      conf,
   171  		args:        []string{"my-cluster"},
   172  		flags: []string{
   173  			"--server=https://192.168.0.99",
   174  			"--tls-server-name=my-cluster-name",
   175  		},
   176  		expected: `Cluster "my-cluster" set.` + "\n",
   177  		expectedConfig: clientcmdapi.Config{
   178  			Clusters: map[string]*clientcmdapi.Cluster{
   179  				"my-cluster": {Server: "https://192.168.0.99", TLSServerName: "my-cluster-name"},
   180  			},
   181  		},
   182  	}
   183  	test.run(t)
   184  }
   185  
   186  func (test setClusterTest) run(t *testing.T) {
   187  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
   188  	if err != nil {
   189  		t.Fatalf("unexpected error: %v", err)
   190  	}
   191  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
   192  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
   193  	if err != nil {
   194  		t.Fatalf("unexpected error: %v", err)
   195  	}
   196  	pathOptions := clientcmd.NewDefaultPathOptions()
   197  	pathOptions.GlobalFile = fakeKubeFile.Name()
   198  	pathOptions.EnvVar = ""
   199  	buf := bytes.NewBuffer([]byte{})
   200  	cmd := NewCmdConfigSetCluster(buf, pathOptions)
   201  	cmd.SetArgs(test.args)
   202  	cmd.Flags().Parse(test.flags)
   203  	if err := cmd.Execute(); err != nil {
   204  		t.Fatalf("unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags)
   205  	}
   206  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
   207  	if err != nil {
   208  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
   209  	}
   210  	if len(test.expected) != 0 {
   211  		if buf.String() != test.expected {
   212  			t.Errorf("Failed in %q\n expected %v\n but got %v", test.description, test.expected, buf.String())
   213  		}
   214  	}
   215  	if len(test.args) > 0 {
   216  		cluster, ok := config.Clusters[test.args[0]]
   217  		if !ok {
   218  			t.Errorf("expected cluster %v, but got nil", test.args[0])
   219  			return
   220  		}
   221  		if cluster.Server != test.expectedConfig.Clusters[test.args[0]].Server {
   222  			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)
   223  		}
   224  		if cluster.TLSServerName != test.expectedConfig.Clusters[test.args[0]].TLSServerName {
   225  			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)
   226  		}
   227  	}
   228  }
   229  

View as plain text