github.com/oam-dev/cluster-gateway@v1.9.0/pkg/apis/cluster/v1alpha1/virtualcluster_test.go (about) 1 /* 2 Copyright 2023 The KubeVela 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 v1alpha1_test 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 v1 "k8s.io/api/core/v1" 27 apierrors "k8s.io/apimachinery/pkg/api/errors" 28 metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 "k8s.io/apimachinery/pkg/labels" 31 "k8s.io/utils/pointer" 32 ocmclusterv1 "open-cluster-management.io/api/cluster/v1" 33 ctrlClient "sigs.k8s.io/controller-runtime/pkg/client" 34 "sigs.k8s.io/controller-runtime/pkg/envtest" 35 logf "sigs.k8s.io/controller-runtime/pkg/log" 36 "sigs.k8s.io/controller-runtime/pkg/log/zap" 37 38 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1" 39 clustergatewaycommon "github.com/oam-dev/cluster-gateway/pkg/common" 40 "github.com/oam-dev/cluster-gateway/pkg/config" 41 "github.com/oam-dev/cluster-gateway/pkg/util/scheme" 42 "github.com/oam-dev/cluster-gateway/pkg/util/singleton" 43 ) 44 45 func TestVirtualCluster(t *testing.T) { 46 RegisterFailHandler(Fail) 47 RunSpecs(t, "VirtualCluster API Test") 48 } 49 50 var testEnv *envtest.Environment 51 var cli ctrlClient.Client 52 53 var _ = BeforeSuite(func() { 54 logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))) 55 By("Bootstrapping Test Environment") 56 57 testEnv = &envtest.Environment{ 58 ControlPlaneStartTimeout: time.Minute, 59 ControlPlaneStopTimeout: time.Minute, 60 Scheme: scheme.Scheme, 61 UseExistingCluster: pointer.Bool(false), 62 CRDDirectoryPaths: []string{"../../../../hack/crd/registration"}, 63 } 64 cfg, err := testEnv.Start() 65 Ω(err).To(Succeed()) 66 67 cli, err = ctrlClient.New(cfg, ctrlClient.Options{Scheme: scheme.Scheme}) 68 Ω(err).To(Succeed()) 69 singleton.SetCtrlClient(cli) 70 }) 71 72 var _ = AfterSuite(func() { 73 By("Tearing Down the Test Environment") 74 Ω(testEnv.Stop()).To(Succeed()) 75 }) 76 77 var _ = Describe("Test Cluster API", func() { 78 79 It("Test Cluster API", func() { 80 c := &v1alpha1.VirtualCluster{} 81 c.SetName("example") 82 Ω(c.GetFullName()).To(Equal("example")) 83 c.Spec.Alias = "alias" 84 Ω(c.GetFullName()).To(Equal("example (alias)")) 85 86 By("Test meta info") 87 Ω(c.New()).To(Equal(&v1alpha1.VirtualCluster{})) 88 Ω(c.NamespaceScoped()).To(BeFalse()) 89 Ω(c.ShortNames()).To(SatisfyAll( 90 ContainElement("vc"), 91 ContainElement("vcluster"), 92 ContainElement("vclusters"), 93 ContainElement("virtual-cluster"), 94 ContainElement("virtual-clusters"), 95 )) 96 Ω(c.GetGroupVersionResource().GroupVersion()).To(Equal(v1alpha1.SchemeGroupVersion)) 97 Ω(c.GetGroupVersionResource().Resource).To(Equal("virtualclusters")) 98 Ω(c.IsStorageVersion()).To(BeTrue()) 99 Ω(c.NewList()).To(Equal(&v1alpha1.VirtualClusterList{})) 100 101 ctx := context.Background() 102 103 By("Create storage namespace") 104 Ω(cli.Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: config.SecretNamespace}})).To(Succeed()) 105 106 By("Create cluster secret") 107 Ω(cli.Create(ctx, &v1.Secret{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Name: "test-cluster", 110 Namespace: config.SecretNamespace, 111 Labels: map[string]string{ 112 clustergatewaycommon.LabelKeyClusterCredentialType: string(v1alpha1.CredentialTypeX509Certificate), 113 clustergatewaycommon.LabelKeyClusterEndpointType: string(v1alpha1.ClusterEndpointTypeConst), 114 "key": "value", 115 }, 116 Annotations: map[string]string{v1alpha1.AnnotationClusterAlias: "test-cluster-alias"}, 117 }, 118 })).To(Succeed()) 119 Ω(cli.Create(ctx, &v1.Secret{ 120 ObjectMeta: metav1.ObjectMeta{ 121 Name: "cluster-invalid", 122 Namespace: config.SecretNamespace, 123 }, 124 Data: map[string][]byte{"endpoint": []byte("127.0.0.1:6443")}, 125 })).To(Succeed()) 126 Ω(cli.Create(ctx, &v1.Secret{ 127 ObjectMeta: metav1.ObjectMeta{ 128 Name: "ocm-cluster", 129 Namespace: config.SecretNamespace, 130 Labels: map[string]string{ 131 clustergatewaycommon.LabelKeyClusterCredentialType: string(v1alpha1.CredentialTypeX509Certificate), 132 }, 133 }, 134 })).To(Succeed()) 135 136 By("Test get cluster from cluster secret") 137 obj, err := c.Get(ctx, "test-cluster", nil) 138 Ω(err).To(Succeed()) 139 cluster, ok := obj.(*v1alpha1.VirtualCluster) 140 Ω(ok).To(BeTrue()) 141 Ω(cluster.Spec.Alias).To(Equal("test-cluster-alias")) 142 Ω(cluster.Spec.CredentialType).To(Equal(v1alpha1.CredentialTypeX509Certificate)) 143 Ω(cluster.GetLabels()["key"]).To(Equal("value")) 144 145 _, err = c.Get(ctx, "cluster-invalid", nil) 146 Ω(err).To(Satisfy(v1alpha1.IsInvalidClusterSecretError)) 147 148 By("Create OCM ManagedCluster") 149 Ω(cli.Create(ctx, &ocmclusterv1.ManagedCluster{ 150 ObjectMeta: metav1.ObjectMeta{ 151 Name: "ocm-bad-cluster", 152 Namespace: config.SecretNamespace, 153 }, 154 })).To(Succeed()) 155 Ω(cli.Create(ctx, &ocmclusterv1.ManagedCluster{ 156 ObjectMeta: metav1.ObjectMeta{ 157 Name: "ocm-cluster", 158 Namespace: config.SecretNamespace, 159 Labels: map[string]string{"key": "value"}, 160 }, 161 Spec: ocmclusterv1.ManagedClusterSpec{ 162 ManagedClusterClientConfigs: []ocmclusterv1.ClientConfig{{URL: "test-url"}}, 163 }, 164 })).To(Succeed()) 165 Ω(cli.Create(ctx, &ocmclusterv1.ManagedCluster{ 166 ObjectMeta: metav1.ObjectMeta{ 167 Name: "test-cluster", 168 Namespace: config.SecretNamespace, 169 Labels: map[string]string{"key": "value-dup"}, 170 }, 171 Spec: ocmclusterv1.ManagedClusterSpec{ 172 ManagedClusterClientConfigs: []ocmclusterv1.ClientConfig{{URL: "test-url-dup"}}, 173 }, 174 })).To(Succeed()) 175 176 By("Test get cluster from OCM managed cluster") 177 _, err = c.Get(ctx, "ocm-bad-cluster", nil) 178 Ω(err).To(Satisfy(v1alpha1.IsInvalidManagedClusterError)) 179 180 obj, err = c.Get(ctx, "ocm-cluster", nil) 181 Ω(err).To(Succeed()) 182 cluster, ok = obj.(*v1alpha1.VirtualCluster) 183 Ω(ok).To(BeTrue()) 184 Expect(cluster.Spec.CredentialType).To(Equal(v1alpha1.CredentialTypeOCMManagedCluster)) 185 186 By("Test get local cluster") 187 obj, err = c.Get(ctx, "local", nil) 188 Ω(err).To(Succeed()) 189 cluster, ok = obj.(*v1alpha1.VirtualCluster) 190 Ω(ok).To(BeTrue()) 191 Expect(cluster.Spec.CredentialType).To(Equal(v1alpha1.CredentialTypeInternal)) 192 193 _, err = c.Get(ctx, "cluster-not-exist", nil) 194 Ω(err).To(Satisfy(apierrors.IsNotFound)) 195 196 By("Test list clusters") 197 objs, err := c.List(ctx, nil) 198 Ω(err).To(Succeed()) 199 clusters, ok := objs.(*v1alpha1.VirtualClusterList) 200 Ω(ok).To(BeTrue()) 201 Expect(len(clusters.Items)).To(Equal(3)) 202 Expect(clusters.Items[0].Name).To(Equal("local")) 203 Expect(clusters.Items[1].Name).To(Equal("ocm-cluster")) 204 Expect(clusters.Items[2].Name).To(Equal("test-cluster")) 205 206 By("Test list clusters with labels") 207 objs, err = c.List(ctx, &metainternalversion.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{"key": "value"})}) 208 Ω(err).To(Succeed()) 209 clusters, ok = objs.(*v1alpha1.VirtualClusterList) 210 Ω(ok).To(BeTrue()) 211 Expect(len(clusters.Items)).To(Equal(2)) 212 Expect(clusters.Items[0].Name).To(Equal("ocm-cluster")) 213 Expect(clusters.Items[1].Name).To(Equal("test-cluster")) 214 215 By("Test list clusters that are not control plane") 216 objs, err = c.List(ctx, &metainternalversion.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{ 217 v1alpha1.LabelClusterControlPlane: "false", 218 })}) 219 Ω(err).To(Succeed()) 220 clusters, ok = objs.(*v1alpha1.VirtualClusterList) 221 Ω(ok).To(BeTrue()) 222 Expect(len(clusters.Items)).To(Equal(2)) 223 Expect(clusters.Items[0].Name).To(Equal("ocm-cluster")) 224 Expect(clusters.Items[1].Name).To(Equal("test-cluster")) 225 226 By("Test list clusters that is control plane") 227 objs, err = c.List(ctx, &metainternalversion.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{ 228 v1alpha1.LabelClusterControlPlane: "true", 229 })}) 230 Ω(err).To(Succeed()) 231 clusters, ok = objs.(*v1alpha1.VirtualClusterList) 232 Ω(ok).To(BeTrue()) 233 Expect(len(clusters.Items)).To(Equal(1)) 234 Expect(clusters.Items[0].Name).To(Equal("local")) 235 236 By("Test print table") 237 _, err = c.ConvertToTable(ctx, cluster, nil) 238 Ω(err).To(Succeed()) 239 _, err = c.ConvertToTable(ctx, clusters, nil) 240 Ω(err).To(Succeed()) 241 }) 242 243 })