github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/xgboost/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 xgboost
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/client-go/kubernetes/scheme"
    24  	"path/filepath"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    28  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    29  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    30  	"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    31  
    32  	kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    33  	"github.com/kubeflow/training-operator/pkg/controller.v1/common"
    34  	//+kubebuilder:scaffold:imports
    35  )
    36  
    37  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    38  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    39  
    40  var (
    41  	testK8sClient client.Client
    42  	testEnv       *envtest.Environment
    43  	testCtx       context.Context
    44  	testCancel    context.CancelFunc
    45  )
    46  
    47  func TestAPIs(t *testing.T) {
    48  	RegisterFailHandler(Fail)
    49  
    50  	RunSpecs(t, "Controller Suite")
    51  }
    52  
    53  var _ = BeforeSuite(func() {
    54  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    55  
    56  	testCtx, testCancel = context.WithCancel(context.TODO())
    57  
    58  	By("bootstrapping test environment")
    59  	testEnv = &envtest.Environment{
    60  		CRDDirectoryPaths:     []string{filepath.Join("..", "..", "..", "manifests", "base", "crds")},
    61  		ErrorIfCRDPathMissing: true,
    62  	}
    63  
    64  	cfg, err := testEnv.Start()
    65  	Expect(err).NotTo(HaveOccurred())
    66  	Expect(cfg).NotTo(BeNil())
    67  
    68  	err = v1beta1.AddToScheme(scheme.Scheme)
    69  	Expect(err).NotTo(HaveOccurred())
    70  	err = kubeflowv1.AddToScheme(scheme.Scheme)
    71  	Expect(err).NotTo(HaveOccurred())
    72  
    73  	//+kubebuilder:scaffold:scheme
    74  
    75  	testK8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    76  	Expect(err).NotTo(HaveOccurred())
    77  	Expect(testK8sClient).NotTo(BeNil())
    78  
    79  	mgr, err := ctrl.NewManager(cfg, ctrl.Options{
    80  		MetricsBindAddress: "0",
    81  	})
    82  	Expect(err).NotTo(HaveOccurred())
    83  
    84  	gangSchedulingSetupFunc := common.GenNonGangSchedulerSetupFunc()
    85  	r := NewReconciler(mgr, gangSchedulingSetupFunc)
    86  
    87  	Expect(r.SetupWithManager(mgr, 1)).NotTo(HaveOccurred())
    88  
    89  	go func() {
    90  		defer GinkgoRecover()
    91  		err = mgr.Start(testCtx)
    92  		Expect(err).ToNot(HaveOccurred(), "failed to run manager")
    93  	}()
    94  })
    95  
    96  var _ = AfterSuite(func() {
    97  	By("tearing down the test environment")
    98  	testCancel()
    99  	err := testEnv.Stop()
   100  	Expect(err).NotTo(HaveOccurred())
   101  })