|
| 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 v1beta1_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "k8s.io/client-go/rest" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + |
| 30 | + admissionv1beta1 "k8s.io/api/admission/v1beta1" |
| 31 | + //+kubebuilder:scaffold:imports |
| 32 | + "k8s.io/apimachinery/pkg/runtime" |
| 33 | + infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1" |
| 34 | + ctrl "sigs.k8s.io/controller-runtime" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 37 | +) |
| 38 | + |
| 39 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 40 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 41 | + |
| 42 | +var k8sClient client.Client |
| 43 | +var testEnv *envtest.Environment |
| 44 | +var ctx context.Context |
| 45 | +var cancel context.CancelFunc |
| 46 | + |
| 47 | +func TestAPIs(t *testing.T) { |
| 48 | + RegisterFailHandler(Fail) |
| 49 | + |
| 50 | + RunSpecs(t, "Webhook Suite") |
| 51 | +} |
| 52 | + |
| 53 | +var _ = BeforeSuite(func() { |
| 54 | + ctx, cancel = context.WithCancel(context.TODO()) |
| 55 | + |
| 56 | + By("bootstrapping test environment") |
| 57 | + testEnv = &envtest.Environment{ |
| 58 | + CRDDirectoryPaths: []string{filepath.Join("../../", "config", "crd", "bases")}, |
| 59 | + ErrorIfCRDPathMissing: false, |
| 60 | + WebhookInstallOptions: envtest.WebhookInstallOptions{ |
| 61 | + Paths: []string{filepath.Join("../../", "config", "webhook")}, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + var cfg *rest.Config |
| 66 | + var err error |
| 67 | + done := make(chan interface{}) |
| 68 | + go func() { |
| 69 | + defer GinkgoRecover() |
| 70 | + cfg, err = testEnv.Start() |
| 71 | + close(done) |
| 72 | + }() |
| 73 | + Eventually(done).WithTimeout(time.Minute).Should(BeClosed()) |
| 74 | + Expect(err).NotTo(HaveOccurred()) |
| 75 | + Expect(cfg).NotTo(BeNil()) |
| 76 | + |
| 77 | + scheme := runtime.NewScheme() |
| 78 | + err = infrav1.AddToScheme(scheme) |
| 79 | + Expect(err).NotTo(HaveOccurred()) |
| 80 | + |
| 81 | + err = admissionv1beta1.AddToScheme(scheme) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + |
| 84 | + //+kubebuilder:scaffold:scheme |
| 85 | + |
| 86 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) |
| 87 | + Expect(err).NotTo(HaveOccurred()) |
| 88 | + Expect(k8sClient).NotTo(BeNil()) |
| 89 | + |
| 90 | + // start webhook server using Manager |
| 91 | + webhookInstallOptions := &testEnv.WebhookInstallOptions |
| 92 | + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 93 | + Scheme: scheme, |
| 94 | + Host: webhookInstallOptions.LocalServingHost, |
| 95 | + Port: webhookInstallOptions.LocalServingPort, |
| 96 | + CertDir: webhookInstallOptions.LocalServingCertDir, |
| 97 | + LeaderElection: false, |
| 98 | + MetricsBindAddress: "0", |
| 99 | + }) |
| 100 | + Expect(err).NotTo(HaveOccurred()) |
| 101 | + |
| 102 | + //+kubebuilder:scaffold:webhook |
| 103 | + |
| 104 | + go func() { |
| 105 | + defer GinkgoRecover() |
| 106 | + err = mgr.Start(ctrl.SetupSignalHandler()) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + }() |
| 109 | +}) |
| 110 | + |
| 111 | +var _ = AfterSuite(func() { |
| 112 | + cancel() |
| 113 | + By("tearing down the test environment") |
| 114 | + err := testEnv.Stop() |
| 115 | + Expect(err).NotTo(HaveOccurred()) |
| 116 | +}) |
0 commit comments