github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/tensorflow/suite_test.go (about) 1 // Copyright 2021 The Kubeflow Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tensorflow 16 17 import ( 18 "context" 19 "fmt" 20 "path/filepath" 21 "testing" 22 "time" 23 24 kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1" 25 "github.com/kubeflow/training-operator/pkg/controller.v1/common" 26 "github.com/kubeflow/training-operator/pkg/util/testutil" 27 28 . "github.com/onsi/ginkgo/v2" 29 . "github.com/onsi/gomega" 30 corev1 "k8s.io/api/core/v1" 31 "k8s.io/client-go/kubernetes/scheme" 32 ctrl "sigs.k8s.io/controller-runtime" 33 "sigs.k8s.io/controller-runtime/pkg/client" 34 "sigs.k8s.io/controller-runtime/pkg/envtest" 35 logf "sigs.k8s.io/controller-runtime/pkg/log" 36 "sigs.k8s.io/controller-runtime/pkg/log/zap" 37 "volcano.sh/apis/pkg/apis/scheduling/v1beta1" 38 //+kubebuilder:scaffold:imports 39 ) 40 41 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 42 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 43 44 var ( 45 testK8sClient client.Client 46 testEnv *envtest.Environment 47 testCtx context.Context 48 testCancel context.CancelFunc 49 reconciler *TFJobReconciler 50 ) 51 52 func TestAPIs(t *testing.T) { 53 RegisterFailHandler(Fail) 54 55 RunSpecs(t, "Controller Suite") 56 } 57 58 var _ = BeforeSuite(func() { 59 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 60 61 testCtx, testCancel = context.WithCancel(context.TODO()) 62 63 By("bootstrapping test environment") 64 testEnv = &envtest.Environment{ 65 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "manifests", "base", "crds")}, 66 ErrorIfCRDPathMissing: true, 67 } 68 69 cfg, err := testEnv.Start() 70 Expect(err).NotTo(HaveOccurred()) 71 Expect(cfg).NotTo(BeNil()) 72 73 err = v1beta1.AddToScheme(scheme.Scheme) 74 Expect(err).NotTo(HaveOccurred()) 75 err = kubeflowv1.AddToScheme(scheme.Scheme) 76 Expect(err).NotTo(HaveOccurred()) 77 78 //+kubebuilder:scaffold:scheme 79 80 testK8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 81 Expect(err).NotTo(HaveOccurred()) 82 Expect(testK8sClient).NotTo(BeNil()) 83 84 mgr, err := ctrl.NewManager(cfg, ctrl.Options{ 85 MetricsBindAddress: "0", 86 }) 87 Expect(err).NotTo(HaveOccurred()) 88 89 gangSchedulingSetupFunc := common.GenNonGangSchedulerSetupFunc() 90 reconciler = NewReconciler(mgr, gangSchedulingSetupFunc) 91 Expect(reconciler.SetupWithManager(mgr, 1)).NotTo(HaveOccurred()) 92 93 go func() { 94 defer GinkgoRecover() 95 err = mgr.Start(testCtx) 96 Expect(err).ToNot(HaveOccurred(), "failed to run manager") 97 }() 98 99 // This step is introduced to make sure cache starts before running any tests 100 Eventually(func() error { 101 nsList := &corev1.NamespaceList{} 102 if err := testK8sClient.List(context.Background(), nsList); err != nil { 103 return err 104 } else if len(nsList.Items) < 1 { 105 return fmt.Errorf("cannot get at lease one namespace, got %d", len(nsList.Items)) 106 } 107 return nil 108 }, testutil.Timeout, testutil.Interval).Should(BeNil()) 109 }) 110 111 var _ = AfterSuite(func() { 112 By("tearing down the test environment") 113 testCancel() 114 // Give 5 seconds to stop all tests 115 time.Sleep(5 * time.Second) 116 err := testEnv.Stop() 117 Expect(err).NotTo(HaveOccurred()) 118 })