|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes 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 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + . "github.com/onsi/ginkgo/v2" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | + admissionv1beta1 "k8s.io/api/admission/v1beta1" |
| 31 | + "k8s.io/apimachinery/pkg/runtime" |
| 32 | + ctrl "sigs.k8s.io/controller-runtime" |
| 33 | + "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 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 39 | +) |
| 40 | + |
| 41 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 42 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 43 | + |
| 44 | +var ( |
| 45 | + k8sClient client.Client |
| 46 | + testEnv *envtest.Environment |
| 47 | + ctx context.Context |
| 48 | + cancel context.CancelFunc |
| 49 | +) |
| 50 | + |
| 51 | +func TestAPIs(t *testing.T) { |
| 52 | + RegisterFailHandler(Fail) |
| 53 | + |
| 54 | + RunSpecs(t, "Webhook Suite") |
| 55 | +} |
| 56 | + |
| 57 | +var _ = BeforeSuite(func() { |
| 58 | + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) |
| 59 | + |
| 60 | + ctx, cancel = context.WithCancel(context.TODO()) |
| 61 | + |
| 62 | + By("bootstrapping test environment") |
| 63 | + testEnv = &envtest.Environment{ |
| 64 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, |
| 65 | + ErrorIfCRDPathMissing: false, |
| 66 | + WebhookInstallOptions: envtest.WebhookInstallOptions{ |
| 67 | + Paths: []string{filepath.Join("..", "..", "config", "webhook")}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + cfg, err := testEnv.Start() |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + Expect(cfg).NotTo(BeNil()) |
| 74 | + |
| 75 | + scheme := runtime.NewScheme() |
| 76 | + err = AddToScheme(scheme) |
| 77 | + Expect(err).NotTo(HaveOccurred()) |
| 78 | + |
| 79 | + err = admissionv1beta1.AddToScheme(scheme) |
| 80 | + Expect(err).NotTo(HaveOccurred()) |
| 81 | + |
| 82 | + err = admissionv1beta1.AddToScheme(scheme) |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + |
| 85 | + //+kubebuilder:scaffold:scheme |
| 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 | + Scheme: scheme, |
| 95 | + WebhookServer: webhook.NewServer( |
| 96 | + webhook.Options{ |
| 97 | + Host: webhookInstallOptions.LocalServingHost, |
| 98 | + Port: webhookInstallOptions.LocalServingPort, |
| 99 | + CertDir: webhookInstallOptions.LocalServingCertDir, |
| 100 | + }, |
| 101 | + ), |
| 102 | + LeaderElection: false, |
| 103 | + Metrics: metricsserver.Options{BindAddress: "0"}, |
| 104 | + }) |
| 105 | + Expect(err).NotTo(HaveOccurred()) |
| 106 | + |
| 107 | + err = (&Cdk8sAppProxy{}).SetupWebhookWithManager(mgr) |
| 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 | + |
| 127 | + return conn.Close() |
| 128 | + }).Should(Succeed()) |
| 129 | +}) |
| 130 | + |
| 131 | +var _ = AfterSuite(func() { |
| 132 | + cancel() |
| 133 | + By("tearing down the test environment") |
| 134 | + err := testEnv.Stop() |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | +}) |
0 commit comments