From 0bd176ef614196b33095fbd0e039e2a70e30bf18 Mon Sep 17 00:00:00 2001 From: lucaswzhang Date: Sat, 18 Dec 2021 11:13:08 +0800 Subject: [PATCH] add unit tests for tensorflow controller --- pkg/controller.v1/tensorflow/suite_test.go | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/controller.v1/tensorflow/suite_test.go b/pkg/controller.v1/tensorflow/suite_test.go index 640c6284c5..516110a3ab 100644 --- a/pkg/controller.v1/tensorflow/suite_test.go +++ b/pkg/controller.v1/tensorflow/suite_test.go @@ -15,9 +15,12 @@ package tensorflow import ( + "context" "path/filepath" "testing" + ctrl "sigs.k8s.io/controller-runtime" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "k8s.io/client-go/kubernetes/scheme" @@ -34,8 +37,13 @@ import ( // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. -var k8sClient client.Client -var testEnv *envtest.Environment +var ( + testK8sClient client.Client + testEnv *envtest.Environment + testCtx context.Context + testCancel context.CancelFunc + reconciler *TFJobReconciler +) func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -48,6 +56,8 @@ func TestAPIs(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + testCtx, testCancel = context.WithCancel(context.TODO()) + By("bootstrapping test environment") testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "manifests", "base", "crds")}, @@ -63,14 +73,28 @@ var _ = BeforeSuite(func() { //+kubebuilder:scaffold:scheme - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + testK8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) - Expect(k8sClient).NotTo(BeNil()) + Expect(testK8sClient).NotTo(BeNil()) + + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ + MetricsBindAddress: "0", + }) + Expect(err).NotTo(HaveOccurred()) + + reconciler = NewReconciler(mgr, false) + Expect(reconciler.SetupWithManager(mgr)).NotTo(HaveOccurred()) + go func() { + defer GinkgoRecover() + err = mgr.Start(testCtx) + Expect(err).ToNot(HaveOccurred(), "failed to run manager") + }() }, 60) var _ = AfterSuite(func() { By("tearing down the test environment") + testCancel() err := testEnv.Stop() Expect(err).NotTo(HaveOccurred()) })