sigs.k8s.io/cluster-api@v1.7.1/controllers/remote/cluster_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 remote 18 19 import ( 20 "testing" 21 "time" 22 23 . "github.com/onsi/gomega" 24 corev1 "k8s.io/api/core/v1" 25 apierrors "k8s.io/apimachinery/pkg/api/errors" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 "sigs.k8s.io/controller-runtime/pkg/client/fake" 29 30 "sigs.k8s.io/cluster-api/util/secret" 31 ) 32 33 var ( 34 clusterWithValidKubeConfig = client.ObjectKey{ 35 Name: "test1", 36 Namespace: metav1.NamespaceDefault, 37 } 38 39 clusterWithInvalidKubeConfig = client.ObjectKey{ 40 Name: "test2", 41 Namespace: metav1.NamespaceDefault, 42 } 43 44 clusterWithNoKubeConfig = client.ObjectKey{ 45 Name: "test3", 46 Namespace: metav1.NamespaceDefault, 47 } 48 49 validKubeConfig = ` 50 clusters: 51 - cluster: 52 server: https://test-cluster-api.nodomain.example.com:6443 53 name: test-cluster-api 54 contexts: 55 - context: 56 cluster: test-cluster-api 57 user: kubernetes-admin 58 name: kubernetes-admin@test-cluster-api 59 current-context: kubernetes-admin@test-cluster-api 60 kind: Config 61 preferences: {} 62 users: 63 - name: kubernetes-admin 64 ` 65 66 validSecret = &corev1.Secret{ 67 ObjectMeta: metav1.ObjectMeta{ 68 Name: "test1-kubeconfig", 69 Namespace: metav1.NamespaceDefault, 70 }, 71 Data: map[string][]byte{ 72 secret.KubeconfigDataName: []byte(validKubeConfig), 73 }, 74 } 75 76 invalidSecret = &corev1.Secret{ 77 ObjectMeta: metav1.ObjectMeta{ 78 Name: "test2-kubeconfig", 79 Namespace: metav1.NamespaceDefault, 80 }, 81 Data: map[string][]byte{ 82 secret.KubeconfigDataName: []byte("Not valid!!1"), 83 }, 84 } 85 ) 86 87 func TestNewClusterClient(t *testing.T) { 88 t.Run("cluster with valid kubeconfig", func(t *testing.T) { 89 gs := NewWithT(t) 90 91 client := fake.NewClientBuilder().WithObjects(validSecret).Build() 92 c, err := NewClusterClient(ctx, "test-source", client, clusterWithValidKubeConfig) 93 gs.Expect(err).ToNot(HaveOccurred()) 94 95 // Since we do not have a remote server to connect to, we should expect to get 96 // an error to that effect for the purpose of this test. 97 // Note: The error occurs here and not in `NewClusterClient` as with the lazy 98 // restmapper only the List call actually communicates with the server. 99 err = c.List(ctx, &corev1.NodeList{}) 100 gs.Expect(err).To(MatchError(ContainSubstring("no such host"))) 101 102 restConfig, err := RESTConfig(ctx, "test-source", client, clusterWithValidKubeConfig) 103 gs.Expect(err).ToNot(HaveOccurred()) 104 gs.Expect(restConfig.Host).To(Equal("https://test-cluster-api.nodomain.example.com:6443")) 105 gs.Expect(restConfig.UserAgent).To(MatchRegexp("remote.test/unknown test-source (.*) cluster.x-k8s.io/unknown")) 106 gs.Expect(restConfig.Timeout).To(Equal(10 * time.Second)) 107 }) 108 109 t.Run("cluster with no kubeconfig", func(t *testing.T) { 110 gs := NewWithT(t) 111 112 client := fake.NewClientBuilder().Build() 113 _, err := NewClusterClient(ctx, "test-source", client, clusterWithNoKubeConfig) 114 gs.Expect(err).To(MatchError(ContainSubstring("not found"))) 115 }) 116 117 t.Run("cluster with invalid kubeconfig", func(t *testing.T) { 118 gs := NewWithT(t) 119 120 client := fake.NewClientBuilder().WithObjects(invalidSecret).Build() 121 _, err := NewClusterClient(ctx, "test-source", client, clusterWithInvalidKubeConfig) 122 gs.Expect(err).To(HaveOccurred()) 123 gs.Expect(apierrors.IsNotFound(err)).To(BeFalse()) 124 }) 125 }