...

Source file src/k8s.io/kubectl/pkg/cmd/config/set_context_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 setContextTest struct {
    31  	description    string
    32  	testContext    string              // name of the context being modified
    33  	config         clientcmdapi.Config //initiate kubectl config
    34  	args           []string            //kubectl set-context args
    35  	flags          []string            //kubectl set-context flags
    36  	expected       string              //expectd out
    37  	expectedConfig clientcmdapi.Config //expect kubectl config
    38  }
    39  
    40  func TestCreateContext(t *testing.T) {
    41  	conf := clientcmdapi.Config{}
    42  	test := setContextTest{
    43  		testContext: "shaker-context",
    44  		description: "Testing for create a new context",
    45  		config:      conf,
    46  		args:        []string{"shaker-context"},
    47  		flags: []string{
    48  			"--cluster=cluster_nickname",
    49  			"--user=user_nickname",
    50  			"--namespace=namespace",
    51  		},
    52  		expected: `Context "shaker-context" created.` + "\n",
    53  		expectedConfig: clientcmdapi.Config{
    54  			Contexts: map[string]*clientcmdapi.Context{
    55  				"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"}},
    56  		},
    57  	}
    58  	test.run(t)
    59  }
    60  func TestModifyContext(t *testing.T) {
    61  	conf := clientcmdapi.Config{
    62  		Contexts: map[string]*clientcmdapi.Context{
    63  			"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
    64  			"not-this":       {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
    65  	test := setContextTest{
    66  		testContext: "shaker-context",
    67  		description: "Testing for modify a already exist context",
    68  		config:      conf,
    69  		args:        []string{"shaker-context"},
    70  		flags: []string{
    71  			"--cluster=cluster_nickname",
    72  			"--user=user_nickname",
    73  			"--namespace=namespace",
    74  		},
    75  		expected: `Context "shaker-context" modified.` + "\n",
    76  		expectedConfig: clientcmdapi.Config{
    77  			Contexts: map[string]*clientcmdapi.Context{
    78  				"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
    79  				"not-this":       {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
    80  	}
    81  	test.run(t)
    82  }
    83  
    84  func TestModifyCurrentContext(t *testing.T) {
    85  	conf := clientcmdapi.Config{
    86  		CurrentContext: "shaker-context",
    87  		Contexts: map[string]*clientcmdapi.Context{
    88  			"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
    89  			"not-this":       {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
    90  	test := setContextTest{
    91  		testContext: "shaker-context",
    92  		description: "Testing for modify a current context",
    93  		config:      conf,
    94  		args:        []string{},
    95  		flags: []string{
    96  			"--current",
    97  			"--cluster=cluster_nickname",
    98  			"--user=user_nickname",
    99  			"--namespace=namespace",
   100  		},
   101  		expected: `Context "shaker-context" modified.` + "\n",
   102  		expectedConfig: clientcmdapi.Config{
   103  			Contexts: map[string]*clientcmdapi.Context{
   104  				"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
   105  				"not-this":       {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
   106  	}
   107  	test.run(t)
   108  }
   109  
   110  func (test setContextTest) run(t *testing.T) {
   111  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
   112  	if err != nil {
   113  		t.Fatalf("unexpected error: %v", err)
   114  	}
   115  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
   116  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
   117  	if err != nil {
   118  		t.Fatalf("unexpected error: %v", err)
   119  	}
   120  
   121  	pathOptions := clientcmd.NewDefaultPathOptions()
   122  	pathOptions.GlobalFile = fakeKubeFile.Name()
   123  	pathOptions.EnvVar = ""
   124  	buf := bytes.NewBuffer([]byte{})
   125  	cmd := NewCmdConfigSetContext(buf, pathOptions)
   126  	cmd.SetArgs(test.args)
   127  	cmd.Flags().Parse(test.flags)
   128  	if err := cmd.Execute(); err != nil {
   129  		t.Fatalf("unexpected error executing command: %v,kubectl set-context args: %v,flags: %v", err, test.args, test.flags)
   130  	}
   131  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
   132  	if err != nil {
   133  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
   134  	}
   135  	if len(test.expected) != 0 {
   136  		if buf.String() != test.expected {
   137  			t.Errorf("Fail in %q:\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
   138  		}
   139  	}
   140  	if test.expectedConfig.Contexts != nil {
   141  		expectContext := test.expectedConfig.Contexts[test.testContext]
   142  		actualContext := config.Contexts[test.testContext]
   143  		if expectContext.AuthInfo != actualContext.AuthInfo || expectContext.Cluster != actualContext.Cluster ||
   144  			expectContext.Namespace != actualContext.Namespace {
   145  			t.Errorf("Fail in %q:\n expected Context %v\n but found %v in kubeconfig\n", test.description, expectContext, actualContext)
   146  		}
   147  	}
   148  }
   149  

View as plain text