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