|
| 1 | +/* |
| 2 | +Copyright 2021 The Karmada 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 app |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + kubefake "k8s.io/client-go/kubernetes/fake" |
| 29 | + |
| 30 | + "github.com/karmada-io/karmada/cmd/agent/app/options" |
| 31 | + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" |
| 32 | + karmadafake "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/fake" |
| 33 | +) |
| 34 | + |
| 35 | +func newTestOpts(clusterName string) *options.Options { |
| 36 | + return &options.Options{ |
| 37 | + ClusterName: clusterName, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func newKubeSystemNamespace(uid string) *corev1.Namespace { |
| 42 | + return &corev1.Namespace{ |
| 43 | + ObjectMeta: metav1.ObjectMeta{ |
| 44 | + Name: metav1.NamespaceSystem, |
| 45 | + UID: types.UID(uid), |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func newCluster(name string, syncMode clusterv1alpha1.ClusterSyncMode, id string) *clusterv1alpha1.Cluster { |
| 51 | + return &clusterv1alpha1.Cluster{ |
| 52 | + ObjectMeta: metav1.ObjectMeta{ |
| 53 | + Name: name, |
| 54 | + }, |
| 55 | + Spec: clusterv1alpha1.ClusterSpec{ |
| 56 | + SyncMode: syncMode, |
| 57 | + ID: id, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestValidateExternallyRegisteredCluster(t *testing.T) { |
| 63 | + const ( |
| 64 | + clusterName = "member1" |
| 65 | + clusterUID = "test-uid-12345" |
| 66 | + ) |
| 67 | + |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + cluster *clusterv1alpha1.Cluster |
| 71 | + nsUID string |
| 72 | + noKubeSystemNS bool |
| 73 | + wantErr bool |
| 74 | + errContains string |
| 75 | + }{ |
| 76 | + { |
| 77 | + name: "happy path with matching ID", |
| 78 | + cluster: newCluster(clusterName, clusterv1alpha1.Pull, clusterUID), |
| 79 | + nsUID: clusterUID, |
| 80 | + wantErr: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "happy path with empty cluster ID", |
| 84 | + cluster: newCluster(clusterName, clusterv1alpha1.Pull, ""), |
| 85 | + nsUID: clusterUID, |
| 86 | + wantErr: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "cluster not found", |
| 90 | + cluster: nil, |
| 91 | + nsUID: clusterUID, |
| 92 | + wantErr: true, |
| 93 | + errContains: "failed to get cluster", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "cluster being deleted", |
| 97 | + cluster: func() *clusterv1alpha1.Cluster { |
| 98 | + c := newCluster(clusterName, clusterv1alpha1.Pull, clusterUID) |
| 99 | + now := metav1.NewTime(time.Now()) |
| 100 | + c.DeletionTimestamp = &now |
| 101 | + c.Finalizers = []string{"test-finalizer"} |
| 102 | + return c |
| 103 | + }(), |
| 104 | + nsUID: clusterUID, |
| 105 | + wantErr: true, |
| 106 | + errContains: "is being deleted", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "wrong sync mode (Push)", |
| 110 | + cluster: newCluster(clusterName, clusterv1alpha1.Push, clusterUID), |
| 111 | + nsUID: clusterUID, |
| 112 | + wantErr: true, |
| 113 | + errContains: "SyncMode", |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "cluster ID mismatch", |
| 117 | + cluster: newCluster(clusterName, clusterv1alpha1.Pull, "different-uid"), |
| 118 | + nsUID: clusterUID, |
| 119 | + wantErr: true, |
| 120 | + errContains: "cluster ID mismatch", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "member cluster missing kube-system namespace", |
| 124 | + cluster: newCluster(clusterName, clusterv1alpha1.Pull, clusterUID), |
| 125 | + noKubeSystemNS: true, |
| 126 | + wantErr: true, |
| 127 | + errContains: "failed to obtain cluster ID", |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tt := range tests { |
| 132 | + t.Run(tt.name, func(t *testing.T) { |
| 133 | + var karmadaClient *karmadafake.Clientset |
| 134 | + if tt.cluster != nil { |
| 135 | + karmadaClient = karmadafake.NewSimpleClientset(tt.cluster) |
| 136 | + } else { |
| 137 | + karmadaClient = karmadafake.NewSimpleClientset() |
| 138 | + } |
| 139 | + |
| 140 | + var memberKubeClient *kubefake.Clientset |
| 141 | + if tt.noKubeSystemNS { |
| 142 | + memberKubeClient = kubefake.NewSimpleClientset() |
| 143 | + } else { |
| 144 | + memberKubeClient = kubefake.NewSimpleClientset(newKubeSystemNamespace(tt.nsUID)) |
| 145 | + } |
| 146 | + opts := newTestOpts(clusterName) |
| 147 | + |
| 148 | + err := validateExternallyRegisteredCluster(context.Background(), opts, karmadaClient, memberKubeClient) |
| 149 | + |
| 150 | + if tt.wantErr { |
| 151 | + if err == nil { |
| 152 | + t.Fatal("expected error, got nil") |
| 153 | + } |
| 154 | + if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) { |
| 155 | + t.Errorf("expected error containing %q, got %q", tt.errContains, err.Error()) |
| 156 | + } |
| 157 | + } else { |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("unexpected error: %v", err) |
| 160 | + } |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments