...

Source file src/k8s.io/kubectl/pkg/cmd/config/set_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  	"reflect"
    27  
    28  	"k8s.io/client-go/tools/clientcmd"
    29  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    30  )
    31  
    32  type setConfigTest struct {
    33  	description    string
    34  	config         clientcmdapi.Config
    35  	args           []string
    36  	expected       string
    37  	expectedConfig clientcmdapi.Config
    38  }
    39  
    40  func TestSetConfigCurrentContext(t *testing.T) {
    41  	conf := clientcmdapi.Config{
    42  		Kind:           "Config",
    43  		APIVersion:     "v1",
    44  		CurrentContext: "minikube",
    45  	}
    46  	expectedConfig := *clientcmdapi.NewConfig()
    47  	expectedConfig.CurrentContext = "my-cluster"
    48  	test := setConfigTest{
    49  		description:    "Testing for kubectl config set current-context",
    50  		config:         conf,
    51  		args:           []string{"current-context", "my-cluster"},
    52  		expected:       `Property "current-context" set.` + "\n",
    53  		expectedConfig: expectedConfig,
    54  	}
    55  	test.run(t)
    56  }
    57  
    58  func (test setConfigTest) run(t *testing.T) {
    59  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
    60  	if err != nil {
    61  		t.Fatalf("unexpected error: %v", err)
    62  	}
    63  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
    64  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
    65  	if err != nil {
    66  		t.Fatalf("unexpected error: %v", err)
    67  	}
    68  	pathOptions := clientcmd.NewDefaultPathOptions()
    69  	pathOptions.GlobalFile = fakeKubeFile.Name()
    70  	pathOptions.EnvVar = ""
    71  	buf := bytes.NewBuffer([]byte{})
    72  	cmd := NewCmdConfigSet(buf, pathOptions)
    73  	cmd.SetArgs(test.args)
    74  	if err := cmd.Execute(); err != nil {
    75  		t.Fatalf("unexpected error executing command: %v", err)
    76  	}
    77  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
    78  	if err != nil {
    79  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
    80  	}
    81  	if len(test.expected) != 0 {
    82  		if buf.String() != test.expected {
    83  			t.Errorf("Failed in:%q\n expected %v\n but got %v", test.description, test.expected, buf.String())
    84  		}
    85  	}
    86  	if !reflect.DeepEqual(*config, test.expectedConfig) {
    87  		t.Errorf("Failed in: %q\n expected %v\n but got %v", test.description, *config, test.expectedConfig)
    88  	}
    89  }
    90  

View as plain text