...

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

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

     1  /*
     2  Copyright 2016 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  	"fmt"
    22  	utiltesting "k8s.io/client-go/util/testing"
    23  	"os"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"k8s.io/client-go/tools/clientcmd"
    28  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    29  )
    30  
    31  type deleteContextTest struct {
    32  	config           clientcmdapi.Config
    33  	contextToDelete  string
    34  	expectedContexts []string
    35  	expectedOut      string
    36  }
    37  
    38  func TestDeleteContext(t *testing.T) {
    39  	conf := clientcmdapi.Config{
    40  		Contexts: map[string]*clientcmdapi.Context{
    41  			"minikube":  {Cluster: "minikube"},
    42  			"otherkube": {Cluster: "otherkube"},
    43  		},
    44  	}
    45  	test := deleteContextTest{
    46  		config:           conf,
    47  		contextToDelete:  "minikube",
    48  		expectedContexts: []string{"otherkube"},
    49  		expectedOut:      "deleted context minikube from %s\n",
    50  	}
    51  
    52  	test.run(t)
    53  }
    54  
    55  func (test deleteContextTest) run(t *testing.T) {
    56  	fakeKubeFile, err := os.CreateTemp(os.TempDir(), "")
    57  	if err != nil {
    58  		t.Fatalf("unexpected error: %v", err)
    59  	}
    60  	defer utiltesting.CloseAndRemove(t, fakeKubeFile)
    61  	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
    62  	if err != nil {
    63  		t.Fatalf("unexpected error: %v", err)
    64  	}
    65  
    66  	pathOptions := clientcmd.NewDefaultPathOptions()
    67  	pathOptions.GlobalFile = fakeKubeFile.Name()
    68  	pathOptions.EnvVar = ""
    69  
    70  	buf := bytes.NewBuffer([]byte{})
    71  	errBuf := bytes.NewBuffer([]byte{})
    72  	cmd := NewCmdConfigDeleteContext(buf, errBuf, pathOptions)
    73  	cmd.SetArgs([]string{test.contextToDelete})
    74  	if err := cmd.Execute(); err != nil {
    75  		t.Fatalf("unexpected error executing command: %v", err)
    76  	}
    77  
    78  	expectedOutWithFile := fmt.Sprintf(test.expectedOut, fakeKubeFile.Name())
    79  	if expectedOutWithFile != buf.String() {
    80  		t.Errorf("expected output %s, but got %s", expectedOutWithFile, buf.String())
    81  		return
    82  	}
    83  
    84  	// Verify context was removed from kubeconfig file
    85  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
    86  	if err != nil {
    87  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
    88  	}
    89  
    90  	contexts := make([]string, 0, len(config.Contexts))
    91  	for k := range config.Contexts {
    92  		contexts = append(contexts, k)
    93  	}
    94  
    95  	if !reflect.DeepEqual(test.expectedContexts, contexts) {
    96  		t.Errorf("expected contexts %v, but found %v in kubeconfig", test.expectedContexts, contexts)
    97  	}
    98  }
    99  

View as plain text