...

Source file src/k8s.io/kubectl/pkg/cmd/config/use_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 useContextTest struct {
    31  	description    string
    32  	config         clientcmdapi.Config //initiate kubectl config
    33  	args           []string            //kubectl config use-context args
    34  	expected       string              //expect out
    35  	expectedConfig clientcmdapi.Config //expect kubectl config
    36  }
    37  
    38  func TestUseContext(t *testing.T) {
    39  	conf := clientcmdapi.Config{
    40  		Kind:       "Config",
    41  		APIVersion: "v1",
    42  		Clusters: map[string]*clientcmdapi.Cluster{
    43  			"minikube":   {Server: "https://192.168.99.100:8443"},
    44  			"my-cluster": {Server: "https://192.168.0.1:3434"},
    45  		},
    46  		Contexts: map[string]*clientcmdapi.Context{
    47  			"minikube":   {AuthInfo: "minikube", Cluster: "minikube"},
    48  			"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
    49  		},
    50  		CurrentContext: "minikube",
    51  	}
    52  	test := useContextTest{
    53  		description: "Testing for kubectl config use-context",
    54  		config:      conf,
    55  		args:        []string{"my-cluster"},
    56  		expected:    `Switched to context "my-cluster".` + "\n",
    57  		expectedConfig: clientcmdapi.Config{
    58  			Kind:       "Config",
    59  			APIVersion: "v1",
    60  			Clusters: map[string]*clientcmdapi.Cluster{
    61  				"minikube":   {Server: "https://192.168.99.100:8443"},
    62  				"my-cluster": {Server: "https://192.168.0.1:3434"},
    63  			},
    64  			Contexts: map[string]*clientcmdapi.Context{
    65  				"minikube":   {AuthInfo: "minikube", Cluster: "minikube"},
    66  				"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
    67  			},
    68  			CurrentContext: "my-cluster",
    69  		},
    70  	}
    71  	test.run(t)
    72  }
    73  
    74  func (test useContextTest) run(t *testing.T) {
    75  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
    76  	if err != nil {
    77  		t.Fatalf("unexpected error: %v", err)
    78  	}
    79  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
    80  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
    81  	if err != nil {
    82  		t.Fatalf("unexpected error: %v", err)
    83  	}
    84  	pathOptions := clientcmd.NewDefaultPathOptions()
    85  	pathOptions.GlobalFile = fakeKubeFile.Name()
    86  	pathOptions.EnvVar = ""
    87  	buf := bytes.NewBuffer([]byte{})
    88  	cmd := NewCmdConfigUseContext(buf, pathOptions)
    89  	cmd.SetArgs(test.args)
    90  	if err := cmd.Execute(); err != nil {
    91  		t.Fatalf("unexpected error executing command: %v,kubectl config use-context args: %v", err, test.args)
    92  	}
    93  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
    94  	if err != nil {
    95  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
    96  	}
    97  	if len(test.expected) != 0 {
    98  		if buf.String() != test.expected {
    99  			t.Errorf("Failed in :%q\n expected %v\n, but got %v\n", test.description, test.expected, buf.String())
   100  		}
   101  	}
   102  	if test.expectedConfig.CurrentContext != config.CurrentContext {
   103  		t.Errorf("Failed in :%q\n expected config %v, but found %v\n in kubeconfig\n", test.description, test.expectedConfig, config)
   104  	}
   105  }
   106  

View as plain text