...

Source file src/k8s.io/kubectl/pkg/cmd/config/unset_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 unsetConfigTest struct {
    31  	description string
    32  	config      clientcmdapi.Config
    33  	args        []string
    34  	expected    string
    35  	expectedErr string
    36  }
    37  
    38  func TestUnsetConfigString(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 := unsetConfigTest{
    53  		description: "Testing for kubectl config unset a value",
    54  		config:      conf,
    55  		args:        []string{"current-context"},
    56  		expected:    `Property "current-context" unset.` + "\n",
    57  	}
    58  	test.run(t)
    59  }
    60  
    61  func TestUnsetConfigMap(t *testing.T) {
    62  	conf := clientcmdapi.Config{
    63  		Kind:       "Config",
    64  		APIVersion: "v1",
    65  		Clusters: map[string]*clientcmdapi.Cluster{
    66  			"minikube":   {Server: "https://192.168.99.100:8443"},
    67  			"my-cluster": {Server: "https://192.168.0.1:3434"},
    68  		},
    69  		Contexts: map[string]*clientcmdapi.Context{
    70  			"minikube":   {AuthInfo: "minikube", Cluster: "minikube"},
    71  			"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
    72  		},
    73  		CurrentContext: "minikube",
    74  	}
    75  	test := unsetConfigTest{
    76  		description: "Testing for kubectl config unset a map",
    77  		config:      conf,
    78  		args:        []string{"clusters"},
    79  		expected:    `Property "clusters" unset.` + "\n",
    80  	}
    81  	test.run(t)
    82  }
    83  
    84  func TestUnsetUnexistConfig(t *testing.T) {
    85  	conf := clientcmdapi.Config{
    86  		Kind:       "Config",
    87  		APIVersion: "v1",
    88  		Clusters: map[string]*clientcmdapi.Cluster{
    89  			"minikube":   {Server: "https://192.168.99.100:8443"},
    90  			"my-cluster": {Server: "https://192.168.0.1:3434"},
    91  		},
    92  		Contexts: map[string]*clientcmdapi.Context{
    93  			"minikube":   {AuthInfo: "minikube", Cluster: "minikube"},
    94  			"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
    95  		},
    96  		CurrentContext: "minikube",
    97  	}
    98  
    99  	test := unsetConfigTest{
   100  		description: "Testing for kubectl config unset a unexist map key",
   101  		config:      conf,
   102  		args:        []string{"contexts.foo.namespace"},
   103  		expectedErr: "current map key `foo` is invalid",
   104  	}
   105  	test.run(t)
   106  
   107  }
   108  
   109  func (test unsetConfigTest) run(t *testing.T) {
   110  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
   111  	if err != nil {
   112  		t.Fatalf("unexpected error: %v", err)
   113  	}
   114  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
   115  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
   116  	if err != nil {
   117  		t.Fatalf("unexpected error: %v", err)
   118  	}
   119  	pathOptions := clientcmd.NewDefaultPathOptions()
   120  	pathOptions.GlobalFile = fakeKubeFile.Name()
   121  	pathOptions.EnvVar = ""
   122  	buf := bytes.NewBuffer([]byte{})
   123  	cmd := NewCmdConfigUnset(buf, pathOptions)
   124  	opts := &unsetOptions{configAccess: pathOptions}
   125  	err = opts.complete(cmd, test.args)
   126  	if err == nil {
   127  		err = opts.run(buf)
   128  	}
   129  	if test.expectedErr == "" && err != nil {
   130  		t.Fatalf("unexpected error: %v", err)
   131  	}
   132  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
   133  	if err != nil {
   134  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
   135  	}
   136  
   137  	if err != nil && err.Error() != test.expectedErr {
   138  		t.Fatalf("expected error:\n %v\nbut got error:\n%v", test.expectedErr, err)
   139  	}
   140  	if len(test.expected) != 0 {
   141  		if buf.String() != test.expected {
   142  			t.Errorf("Failed in :%q\n expected %v\n but got %v", test.description, test.expected, buf.String())
   143  		}
   144  	}
   145  	if test.args[0] == "current-context" {
   146  		if config.CurrentContext != "" {
   147  			t.Errorf("Failed in :%q\n expected current-context nil,but got %v", test.description, config.CurrentContext)
   148  		}
   149  	} else if test.args[0] == "clusters" {
   150  		if len(config.Clusters) != 0 {
   151  			t.Errorf("Failed in :%q\n expected clusters nil map, but got %v", test.description, config.Clusters)
   152  		}
   153  	}
   154  }
   155  

View as plain text