github.com/oam-dev/kubevela@v1.9.11/test/e2e-test/suite_test.go (about) 1 /* 2 Copyright 2021 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 controllers_test 18 19 import ( 20 "context" 21 "fmt" 22 "math/rand" 23 "strconv" 24 "testing" 25 "time" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 kruise "github.com/openkruise/kruise-api/apps/v1alpha1" 31 rbac "k8s.io/api/rbac/v1" 32 crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 33 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 34 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 35 "k8s.io/apimachinery/pkg/runtime" 36 "k8s.io/apimachinery/pkg/runtime/schema" 37 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 38 "sigs.k8s.io/controller-runtime/pkg/client" 39 "sigs.k8s.io/controller-runtime/pkg/client/config" 40 logf "sigs.k8s.io/controller-runtime/pkg/log" 41 "sigs.k8s.io/controller-runtime/pkg/log/zap" 42 controllerscheme "sigs.k8s.io/controller-runtime/pkg/scheme" 43 44 core "github.com/oam-dev/kubevela/apis/core.oam.dev" 45 commontypes "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 46 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 47 "github.com/oam-dev/kubevela/pkg/oam/util" 48 // +kubebuilder:scaffold:imports 49 ) 50 51 var k8sClient client.Client 52 var scheme = runtime.NewScheme() 53 var roleName = "oam-example-com" 54 var roleBindingName = "oam-role-binding" 55 56 func TestAPIs(t *testing.T) { 57 RegisterFailHandler(Fail) 58 59 RunSpecs(t, "OAM Core Resource Controller Suite") 60 } 61 62 var _ = BeforeSuite(func() { 63 By("Bootstrapping test environment") 64 rand.Seed(time.Now().UnixNano()) 65 logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))) 66 err := clientgoscheme.AddToScheme(scheme) 67 Expect(err).Should(BeNil()) 68 err = core.AddToScheme(scheme) 69 Expect(err).Should(BeNil()) 70 err = crdv1.AddToScheme(scheme) 71 Expect(err).Should(BeNil()) 72 err = kruise.AddToScheme(scheme) 73 Expect(err).Should(BeNil()) 74 depExample := &unstructured.Unstructured{} 75 depExample.SetGroupVersionKind(schema.GroupVersionKind{ 76 Group: "example.com", 77 Version: "v1", 78 Kind: "Foo", 79 }) 80 depSchemeGroupVersion := schema.GroupVersion{Group: "example.com", Version: "v1"} 81 depSchemeBuilder := &controllerscheme.Builder{GroupVersion: depSchemeGroupVersion} 82 depSchemeBuilder.Register(depExample.DeepCopyObject()) 83 err = depSchemeBuilder.AddToScheme(scheme) 84 Expect(err).Should(BeNil()) 85 By("Setting up kubernetes client") 86 k8sClient, err = client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme}) 87 if err != nil { 88 logf.Log.Error(err, "failed to create k8sClient") 89 Fail("setup failed") 90 } 91 By("Finished setting up test environment") 92 93 // create workload definition for 'deployments' 94 wdDeploy := v1beta1.WorkloadDefinition{ 95 ObjectMeta: metav1.ObjectMeta{ 96 Name: "deployments.apps", 97 Namespace: "vela-system", 98 }, 99 Spec: v1beta1.WorkloadDefinitionSpec{ 100 Reference: commontypes.DefinitionReference{ 101 Name: "deployments.apps", 102 }, 103 }, 104 } 105 Expect(k8sClient.Create(context.Background(), &wdDeploy)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 106 By("Created deployments.apps") 107 108 exampleClusterRole := rbac.ClusterRole{ 109 ObjectMeta: metav1.ObjectMeta{ 110 Name: roleName, 111 Labels: map[string]string{ 112 "oam": "clusterrole", 113 "rbac.oam.dev/aggregate-to-controller": "true", 114 }, 115 }, 116 Rules: []rbac.PolicyRule{{ 117 APIGroups: []string{"example.com"}, 118 Resources: []string{rbac.ResourceAll}, 119 Verbs: []string{rbac.VerbAll}, 120 }}, 121 } 122 Expect(k8sClient.Create(context.Background(), &exampleClusterRole)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 123 By("Created example.com cluster role for the test service account") 124 125 adminRoleBinding := rbac.ClusterRoleBinding{ 126 ObjectMeta: metav1.ObjectMeta{ 127 Name: roleBindingName, 128 Labels: map[string]string{"oam": "clusterrole"}, 129 }, 130 Subjects: []rbac.Subject{ 131 { 132 Kind: "User", 133 Name: "system:serviceaccount:oam-system:oam-kubernetes-runtime-e2e", 134 }, 135 }, 136 RoleRef: rbac.RoleRef{ 137 APIGroup: "rbac.authorization.k8s.io", 138 Kind: "ClusterRole", 139 Name: "cluster-admin", 140 }, 141 } 142 Expect(k8sClient.Create(context.Background(), &adminRoleBinding)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) 143 By("Created cluster role binding for the test service account") 144 }) 145 146 var _ = AfterSuite(func() { 147 By("Tearing down the test environment") 148 adminRoleBinding := rbac.ClusterRoleBinding{ 149 ObjectMeta: metav1.ObjectMeta{ 150 Name: roleBindingName, 151 Labels: map[string]string{"oam": "clusterrole"}, 152 }, 153 } 154 Expect(k8sClient.Delete(context.Background(), &adminRoleBinding)).Should(BeNil()) 155 By("Deleted the cluster role binding") 156 }) 157 158 // RequestReconcileNow will trigger an immediate reconciliation on K8s object. 159 // Some test cases may fail for timeout to wait a scheduled reconciliation. 160 // This is a workaround to avoid long-time wait before next scheduled 161 // reconciliation. 162 func RequestReconcileNow(ctx context.Context, o client.Object) { 163 oCopy := o.DeepCopyObject() 164 oMeta, ok := oCopy.(metav1.Object) 165 Expect(ok).Should(BeTrue()) 166 oMeta.SetAnnotations(map[string]string{ 167 "app.oam.dev/requestreconcile": time.Now().String(), 168 }) 169 oMeta.SetResourceVersion("") 170 By(fmt.Sprintf("Requset reconcile %q now", oMeta.GetName())) 171 Expect(k8sClient.Patch(ctx, oCopy.(client.Object), client.Merge)).Should(Succeed()) 172 } 173 174 // randomNamespaceName generates a random name based on the basic name. 175 // Running each ginkgo case in a new namespace with a random name can avoid 176 // waiting a long time to GC namespace. 177 func randomNamespaceName(basic string) string { 178 return fmt.Sprintf("%s-%s", basic, strconv.FormatInt(rand.Int63(), 16)) 179 }