|
| 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 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + clusterWithValidKubeConfig = &v1alpha1.Cluster{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Name: "test1", |
| 33 | + Namespace: "test", |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + clusterWithInvalidKubeConfig = &v1alpha1.Cluster{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "test2", |
| 40 | + Namespace: "test", |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + clusterWithNoKubeConfig = &v1alpha1.Cluster{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: "test3", |
| 47 | + Namespace: "test", |
| 48 | + }, |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +func TestNewClusterClient(t *testing.T) { |
| 53 | + t.Run("cluster with valid kubeconfig", func(t *testing.T) { |
| 54 | + client := fake.NewFakeClient(validSecret) |
| 55 | + c, err := NewClusterClient(client, clusterWithValidKubeConfig) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Expected no errors, got %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + if c == nil { |
| 61 | + t.Fatal("Expected actual client, got nil") |
| 62 | + } |
| 63 | + |
| 64 | + restConfig := c.RESTConfig() |
| 65 | + if restConfig.Host != "https://test-cluster-api:6443" { |
| 66 | + t.Fatalf("Unexpected Host value in RESTConfig: %q", restConfig.Host) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("cluster with no kubeconfig", func(t *testing.T) { |
| 71 | + client := fake.NewFakeClient() |
| 72 | + _, err := NewClusterClient(client, clusterWithNoKubeConfig) |
| 73 | + if !strings.Contains(err.Error(), "secret not found") { |
| 74 | + t.Fatalf("Expected not found error, got %v", err) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("cluster with invalid kubeconfig", func(t *testing.T) { |
| 79 | + client := fake.NewFakeClient(invalidSecret) |
| 80 | + _, err := NewClusterClient(client, clusterWithInvalidKubeConfig) |
| 81 | + if err == nil || apierrors.IsNotFound(err) { |
| 82 | + t.Fatalf("Expected error, got %v", err) |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | +} |
0 commit comments