sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/get_kubeconfig_test.go (about)

     1  /*
     2  Copyright 2019 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 client
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  
    25  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    26  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    27  )
    28  
    29  func Test_clusterctlClient_GetKubeconfig(t *testing.T) {
    30  	ctx := context.Background()
    31  
    32  	configClient := newFakeConfig(ctx)
    33  	kubeconfig := cluster.Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"}
    34  	clusterClient := newFakeCluster(cluster.Kubeconfig{Path: "cluster1"}, configClient)
    35  
    36  	// create a clusterctl client where the proxy returns an empty namespace
    37  	clusterClient.fakeProxy = test.NewFakeProxy().WithNamespace("").WithFakeCAPISetup()
    38  	badClient := newFakeClient(ctx, configClient).WithCluster(clusterClient)
    39  
    40  	tests := []struct {
    41  		name      string
    42  		client    *fakeClient
    43  		options   GetKubeconfigOptions
    44  		expectErr bool
    45  	}{
    46  		{
    47  			name:      "returns error if unable to get client for mgmt cluster",
    48  			client:    fakeEmptyCluster(),
    49  			expectErr: true,
    50  		},
    51  		{
    52  			name:      "returns error if unable namespace is empty",
    53  			client:    badClient,
    54  			options:   GetKubeconfigOptions{Kubeconfig: Kubeconfig(kubeconfig)},
    55  			expectErr: true,
    56  		},
    57  	}
    58  
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			g := NewWithT(t)
    62  
    63  			config, err := tt.client.GetKubeconfig(ctx, tt.options)
    64  			if tt.expectErr {
    65  				g.Expect(err).To(HaveOccurred())
    66  				return
    67  			}
    68  			g.Expect(err).ToNot(HaveOccurred())
    69  			g.Expect(config).ToNot(BeEmpty())
    70  		})
    71  	}
    72  }