github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/workloads/v1alpha1/webhook_suite_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package v1alpha1 21 22 import ( 23 "context" 24 "crypto/tls" 25 "fmt" 26 "net" 27 "path/filepath" 28 "testing" 29 "time" 30 31 . "github.com/onsi/ginkgo/v2" 32 . "github.com/onsi/gomega" 33 34 admissionv1beta1 "k8s.io/api/admission/v1beta1" 35 // +kubebuilder:scaffold:imports 36 "k8s.io/apimachinery/pkg/runtime" 37 "k8s.io/client-go/rest" 38 ctrl "sigs.k8s.io/controller-runtime" 39 "sigs.k8s.io/controller-runtime/pkg/client" 40 "sigs.k8s.io/controller-runtime/pkg/envtest" 41 logf "sigs.k8s.io/controller-runtime/pkg/log" 42 "sigs.k8s.io/controller-runtime/pkg/log/zap" 43 44 "github.com/1aal/kubeblocks/pkg/testutil" 45 ) 46 47 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 48 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 49 50 var cfg *rest.Config 51 var k8sClient client.Client 52 var testEnv *envtest.Environment 53 var ctx context.Context 54 var cancel context.CancelFunc 55 var testCtx testutil.TestContext 56 57 func TestAPIs(t *testing.T) { 58 RegisterFailHandler(Fail) 59 60 RunSpecs(t, "Webhook Suite") 61 } 62 63 var _ = BeforeSuite(func() { 64 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 65 66 ctx, cancel = context.WithCancel(context.TODO()) 67 68 By("bootstrapping test environment") 69 testEnv = &envtest.Environment{ 70 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")}, 71 ErrorIfCRDPathMissing: false, 72 WebhookInstallOptions: envtest.WebhookInstallOptions{ 73 Paths: []string{filepath.Join("..", "..", "..", "config", "webhook")}, 74 }, 75 } 76 77 var err error 78 // cfg is defined in this file globally. 79 cfg, err = testEnv.Start() 80 Expect(err).NotTo(HaveOccurred()) 81 Expect(cfg).NotTo(BeNil()) 82 83 scheme := runtime.NewScheme() 84 err = AddToScheme(scheme) 85 Expect(err).NotTo(HaveOccurred()) 86 87 err = admissionv1beta1.AddToScheme(scheme) 88 Expect(err).NotTo(HaveOccurred()) 89 90 // +kubebuilder:scaffold:scheme 91 92 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) 93 Expect(err).NotTo(HaveOccurred()) 94 Expect(k8sClient).NotTo(BeNil()) 95 96 // start webhook server using Manager 97 webhookInstallOptions := &testEnv.WebhookInstallOptions 98 mgr, err := ctrl.NewManager(cfg, ctrl.Options{ 99 Scheme: scheme, 100 Host: webhookInstallOptions.LocalServingHost, 101 Port: webhookInstallOptions.LocalServingPort, 102 CertDir: webhookInstallOptions.LocalServingCertDir, 103 LeaderElection: false, 104 MetricsBindAddress: "0", 105 }) 106 Expect(err).NotTo(HaveOccurred()) 107 108 err = (&ReplicatedStateMachine{}).SetupWebhookWithManager(mgr) 109 Expect(err).NotTo(HaveOccurred()) 110 111 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 112 113 // +kubebuilder:scaffold:webhook 114 115 go func() { 116 defer GinkgoRecover() 117 err = mgr.Start(ctx) 118 Expect(err).NotTo(HaveOccurred()) 119 }() 120 121 // wait for the webhook server to get ready 122 dialer := &net.Dialer{Timeout: time.Second} 123 addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) 124 Eventually(func() error { 125 conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) 126 if err != nil { 127 return err 128 } 129 conn.Close() 130 return nil 131 }).Should(Succeed()) 132 133 }) 134 135 var _ = AfterSuite(func() { 136 cancel() 137 By("tearing down the test environment") 138 err := testEnv.Stop() 139 Expect(err).NotTo(HaveOccurred()) 140 })