github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/api/v1alpha1/webhooks/releaseplan/suite_test.go (about) 1 /* 2 Copyright 2022. 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 releaseplan 18 19 import ( 20 "context" 21 "crypto/tls" 22 "fmt" 23 "net" 24 "path/filepath" 25 "testing" 26 "time" 27 28 toolkit "github.com/konflux-ci/operator-toolkit/webhook" 29 "github.com/redhat-appstudio/release-service/api/v1alpha1" 30 "github.com/redhat-appstudio/release-service/api/v1alpha1/webhooks/author" 31 "sigs.k8s.io/controller-runtime/pkg/metrics/server" 32 crwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" 33 34 . "github.com/onsi/ginkgo/v2" 35 . "github.com/onsi/gomega" 36 37 admissionv1beta1 "k8s.io/api/admission/v1beta1" 38 //+kubebuilder:scaffold:imports 39 "k8s.io/apimachinery/pkg/runtime" 40 ctrl "sigs.k8s.io/controller-runtime" 41 "sigs.k8s.io/controller-runtime/pkg/client" 42 "sigs.k8s.io/controller-runtime/pkg/envtest" 43 logf "sigs.k8s.io/controller-runtime/pkg/log" 44 "sigs.k8s.io/controller-runtime/pkg/log/zap" 45 "sigs.k8s.io/controller-runtime/pkg/manager" 46 ) 47 48 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 49 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 50 51 const ( 52 // Timeout for Eventually blocks 53 timeout = time.Second * 10 54 ) 55 56 var ( 57 cancel context.CancelFunc 58 ctx context.Context 59 k8sClient client.Client 60 mgr manager.Manager 61 testEnv *envtest.Environment 62 webhook *Webhook 63 ) 64 65 func TestReleasePlanWebhook(t *testing.T) { 66 RegisterFailHandler(Fail) 67 RunSpecs(t, "ReleasePlan Webhook Suite") 68 } 69 70 var _ = BeforeSuite(func() { 71 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 72 ctx, cancel = context.WithCancel(context.TODO()) 73 74 By("bootstrapping test environment") 75 testEnv = &envtest.Environment{ 76 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "config", "crd", "bases")}, 77 ErrorIfCRDPathMissing: false, 78 WebhookInstallOptions: envtest.WebhookInstallOptions{ 79 Paths: []string{filepath.Join("..", "..", "..", "..", "config", "webhook")}, 80 }, 81 } 82 83 cfg, err := testEnv.Start() 84 Expect(err).NotTo(HaveOccurred()) 85 Expect(cfg).NotTo(BeNil()) 86 87 scheme := runtime.NewScheme() 88 Expect(v1alpha1.AddToScheme(scheme)).To(Succeed()) 89 Expect(admissionv1beta1.AddToScheme(scheme)).To(Succeed()) 90 91 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(k8sClient).NotTo(BeNil()) 94 95 // start webhook server using Manager 96 webhookInstallOptions := &testEnv.WebhookInstallOptions 97 mgr, err = ctrl.NewManager(cfg, ctrl.Options{ 98 LeaderElection: false, 99 Metrics: server.Options{ 100 BindAddress: "0", 101 }, 102 WebhookServer: crwebhook.NewServer(crwebhook.Options{ 103 CertDir: webhookInstallOptions.LocalServingCertDir, 104 Host: webhookInstallOptions.LocalServingHost, 105 Port: webhookInstallOptions.LocalServingPort, 106 }), 107 Scheme: scheme, 108 }) 109 Expect(err).NotTo(HaveOccurred()) 110 111 err = toolkit.SetupWebhooks(mgr, &Webhook{}, &author.Webhook{}) 112 Expect(err).NotTo(HaveOccurred()) 113 114 //+kubebuilder:scaffold:webhook 115 116 go func() { 117 defer GinkgoRecover() 118 err = mgr.Start(ctx) 119 Expect(err).NotTo(HaveOccurred()) 120 }() 121 122 // wait for the webhook server to get ready 123 dialer := &net.Dialer{Timeout: time.Second} 124 addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) 125 Eventually(func() error { 126 conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) 127 if err != nil { 128 return err 129 } 130 _ = conn.Close() 131 return nil 132 }).Should(Succeed()) 133 134 }) 135 136 var _ = AfterSuite(func() { 137 cancel() 138 By("tearing down the test environment") 139 err := testEnv.Stop() 140 Expect(err).NotTo(HaveOccurred()) 141 })