Skip to content

Commit

Permalink
add unit tests for tensorflow controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zw0610 committed Dec 18, 2021
1 parent b51bfda commit 0bd176e
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pkg/controller.v1/tensorflow/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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")},
Expand All @@ -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())
})

0 comments on commit 0bd176e

Please sign in to comment.