...

Source file src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_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 deleteClusterTest struct {
    32  	config           clientcmdapi.Config
    33  	clusterToDelete  string
    34  	expectedClusters []string
    35  	expectedOut      string
    36  }
    37  
    38  func TestDeleteCluster(t *testing.T) {
    39  	conf := clientcmdapi.Config{
    40  		Clusters: map[string]*clientcmdapi.Cluster{
    41  			"minikube":  {Server: "https://192.168.0.99"},
    42  			"otherkube": {Server: "https://192.168.0.100"},
    43  		},
    44  	}
    45  	test := deleteClusterTest{
    46  		config:           conf,
    47  		clusterToDelete:  "minikube",
    48  		expectedClusters: []string{"otherkube"},
    49  		expectedOut:      "deleted cluster minikube from %s\n",
    50  	}
    51  
    52  	test.run(t)
    53  }
    54  
    55  func (test deleteClusterTest) 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  	cmd := NewCmdConfigDeleteCluster(buf, pathOptions)
    72  	cmd.SetArgs([]string{test.clusterToDelete})
    73  	if err := cmd.Execute(); err != nil {
    74  		t.Fatalf("unexpected error executing command: %v", err)
    75  	}
    76  
    77  	expectedOutWithFile := fmt.Sprintf(test.expectedOut, fakeKubeFile.Name())
    78  	if expectedOutWithFile != buf.String() {
    79  		t.Errorf("expected output %s, but got %s", expectedOutWithFile, buf.String())
    80  		return
    81  	}
    82  
    83  	// Verify cluster was removed from kubeconfig file
    84  	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
    85  	if err != nil {
    86  		t.Fatalf("unexpected error loading kubeconfig file: %v", err)
    87  	}
    88  
    89  	clusters := make([]string, 0, len(config.Clusters))
    90  	for k := range config.Clusters {
    91  		clusters = append(clusters, k)
    92  	}
    93  
    94  	if !reflect.DeepEqual(test.expectedClusters, clusters) {
    95  		t.Errorf("expected clusters %v, but found %v in kubeconfig", test.expectedClusters, clusters)
    96  	}
    97  }
    98  

View as plain text