sigs.k8s.io/cluster-api@v1.6.3/cmd/clusterctl/client/cluster/workload_cluster_test.go (about) 1 /* 2 Copyright 2020 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 cluster 18 19 import ( 20 "context" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 28 "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" 29 "sigs.k8s.io/cluster-api/util/secret" 30 ) 31 32 func Test_WorkloadCluster_GetKubeconfig(t *testing.T) { 33 var ( 34 validKubeConfig = ` 35 clusters: 36 - cluster: 37 certificate-authority-data: stuff 38 server: https://test-cluster-api:6443 39 name: test1 40 contexts: 41 - context: 42 cluster: test1 43 user: test1-admin 44 name: test1-admin@test1 45 current-context: test1-admin@test1 46 kind: Config 47 preferences: {} 48 users: 49 - name: test1-admin 50 user: 51 client-certificate-data: stuff-cert-data 52 client-key-data: stuff-key-data 53 ` 54 55 validSecret = &corev1.Secret{ 56 ObjectMeta: metav1.ObjectMeta{ 57 Name: "test1-kubeconfig", 58 Namespace: "test", 59 Labels: map[string]string{clusterv1.ClusterNameLabel: "test1"}, 60 }, 61 Data: map[string][]byte{ 62 secret.KubeconfigDataName: []byte(validKubeConfig), 63 }, 64 } 65 ) 66 67 tests := []struct { 68 name string 69 expectErr bool 70 proxy Proxy 71 }{ 72 { 73 name: "return secret data", 74 expectErr: false, 75 proxy: test.NewFakeProxy().WithObjs(validSecret), 76 }, 77 { 78 name: "return error if cannot find secert", 79 expectErr: true, 80 proxy: test.NewFakeProxy(), 81 }, 82 } 83 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 g := NewWithT(t) 87 88 ctx := context.Background() 89 90 wc := newWorkloadCluster(tt.proxy) 91 data, err := wc.GetKubeconfig(ctx, "test1", "test") 92 93 if tt.expectErr { 94 g.Expect(err).To(HaveOccurred()) 95 return 96 } 97 98 g.Expect(err).ToNot(HaveOccurred()) 99 g.Expect(data).To(Equal(string(validSecret.Data[secret.KubeconfigDataName]))) 100 }) 101 } 102 }