|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + |
| 16 | + admissionv1 "k8s.io/api/admission/v1" |
| 17 | + // +kubebuilder:scaffold:imports |
| 18 | + apimachineryruntime "k8s.io/apimachinery/pkg/runtime" |
| 19 | + "k8s.io/client-go/rest" |
| 20 | + ctrl "sigs.k8s.io/controller-runtime" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 23 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 25 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 27 | +) |
| 28 | + |
| 29 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 30 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 31 | + |
| 32 | +var cfg *rest.Config |
| 33 | +var k8sClient client.Client |
| 34 | +var testEnv *envtest.Environment |
| 35 | +var ctx context.Context |
| 36 | +var cancel context.CancelFunc |
| 37 | + |
| 38 | +func TestAPIs(t *testing.T) { |
| 39 | + RegisterFailHandler(Fail) |
| 40 | + |
| 41 | + RunSpecs(t, "Webhook Suite") |
| 42 | +} |
| 43 | + |
| 44 | +var _ = BeforeSuite(func() { |
| 45 | + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) |
| 46 | + |
| 47 | + ctx, cancel = context.WithCancel(context.TODO()) |
| 48 | + |
| 49 | + By("bootstrapping test environment") |
| 50 | + testEnv = &envtest.Environment{ |
| 51 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, |
| 52 | + ErrorIfCRDPathMissing: false, |
| 53 | + |
| 54 | + // The BinaryAssetsDirectory is only required if you want to run the tests directly |
| 55 | + // without call the makefile target test. If not informed it will look for the |
| 56 | + // default path defined in controller-runtime which is /usr/local/kubebuilder/. |
| 57 | + // Note that you must have the required binaries setup under the bin directory to perform |
| 58 | + // the tests directly. When we run make test it will be setup and used automatically. |
| 59 | + BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", |
| 60 | + fmt.Sprintf("1.31.0-%s-%s", runtime.GOOS, runtime.GOARCH)), |
| 61 | + |
| 62 | + WebhookInstallOptions: envtest.WebhookInstallOptions{ |
| 63 | + Paths: []string{filepath.Join("..", "..", "config", "webhook")}, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + var err error |
| 68 | + // cfg is defined in this file globally. |
| 69 | + cfg, err = testEnv.Start() |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + Expect(cfg).NotTo(BeNil()) |
| 72 | + |
| 73 | + scheme := apimachineryruntime.NewScheme() |
| 74 | + err = AddToScheme(scheme) |
| 75 | + Expect(err).NotTo(HaveOccurred()) |
| 76 | + |
| 77 | + err = admissionv1.AddToScheme(scheme) |
| 78 | + Expect(err).NotTo(HaveOccurred()) |
| 79 | + |
| 80 | + // +kubebuilder:scaffold:scheme |
| 81 | + |
| 82 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + Expect(k8sClient).NotTo(BeNil()) |
| 85 | + |
| 86 | + // start webhook server using Manager |
| 87 | + webhookInstallOptions := &testEnv.WebhookInstallOptions |
| 88 | + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 89 | + Scheme: scheme, |
| 90 | + WebhookServer: webhook.NewServer(webhook.Options{ |
| 91 | + Host: webhookInstallOptions.LocalServingHost, |
| 92 | + Port: webhookInstallOptions.LocalServingPort, |
| 93 | + CertDir: webhookInstallOptions.LocalServingCertDir, |
| 94 | + }), |
| 95 | + LeaderElection: false, |
| 96 | + Metrics: metricsserver.Options{BindAddress: "0"}, |
| 97 | + }) |
| 98 | + Expect(err).NotTo(HaveOccurred()) |
| 99 | + |
| 100 | + err = (&ImagePrefetch{}).SetupWebhookWithManager(mgr) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + |
| 103 | + // +kubebuilder:scaffold:webhook |
| 104 | + |
| 105 | + go func() { |
| 106 | + defer GinkgoRecover() |
| 107 | + err = mgr.Start(ctx) |
| 108 | + Expect(err).NotTo(HaveOccurred()) |
| 109 | + }() |
| 110 | + |
| 111 | + // wait for the webhook server to get ready |
| 112 | + dialer := &net.Dialer{Timeout: time.Second} |
| 113 | + addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) |
| 114 | + Eventually(func() error { |
| 115 | + conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return conn.Close() |
| 120 | + }).Should(Succeed()) |
| 121 | + |
| 122 | +}) |
| 123 | + |
| 124 | +var _ = AfterSuite(func() { |
| 125 | + By("tearing down the test environment") |
| 126 | + cancel() |
| 127 | + err := testEnv.Stop() |
| 128 | + Expect(err).NotTo(HaveOccurred()) |
| 129 | +}) |
0 commit comments