// Copyright © 2023 Ory Corp // SPDX-License-Identifier: Apache-2.0 package controllers_test import ( "context" "path/filepath" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" // +kubebuilder:scaffold:imports ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. var cfg *rest.Config var k8sClient client.Client var testEnv *envtest.Environment func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) RunSpecsWithDefaultAndCustomReporters(t, "Controller Suite", []Reporter{}) } var _ = BeforeSuite(func(done Done) { logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))) By("bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) Expect(cfg).ToNot(BeNil()) // +kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) Expect(err).ToNot(HaveOccurred()) Expect(k8sClient).ToNot(BeNil()) close(done) }, 60) var _ = AfterSuite(func() { By("tearing down the test environment") // Need to retry a bit if the first stop fails due to a bug: // https://github.com/kubernetes-sigs/controller-runtime/issues/1571 err := retry.OnError(retry.DefaultBackoff, func(err error) bool { return true }, func() error { return testEnv.Stop() }) Expect(err).NotTo(HaveOccurred()) }) // SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and // writes the request to requests after Reconcile is finished. func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) { requests := make(chan reconcile.Request) fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { result, err := inner.Reconcile(ctx, req) requests <- req return result, err }) return fn, requests } // StartTestManager adds recFn func StartTestManager(mgr manager.Manager) context.Context { ctx := context.Background() go func() { defer GinkgoRecover() defer ctx.Done() Expect(mgr.Start(ctx)).NotTo(HaveOccurred()) }() return ctx }