github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/mpi/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 mpi
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    23  	"github.com/kubeflow/training-operator/pkg/config"
    24  	"github.com/kubeflow/training-operator/pkg/controller.v1/common"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	"k8s.io/client-go/kubernetes/scheme"
    29  	ctrl "sigs.k8s.io/controller-runtime"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    32  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    33  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    34  	"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    35  	//+kubebuilder:scaffold:imports
    36  )
    37  
    38  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    39  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    40  
    41  var (
    42  	testK8sClient client.Client
    43  	testEnv       *envtest.Environment
    44  	testCtx       context.Context
    45  	testCancel    context.CancelFunc
    46  	reconciler    *MPIJobReconciler
    47  )
    48  
    49  func TestAPIs(t *testing.T) {
    50  	RegisterFailHandler(Fail)
    51  
    52  	RunSpecs(t, "Controller Suite")
    53  }
    54  
    55  var _ = BeforeSuite(func() {
    56  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    57  
    58  	testCtx, testCancel = context.WithCancel(context.TODO())
    59  
    60  	By("bootstrapping test environment")
    61  	testEnv = &envtest.Environment{
    62  		CRDDirectoryPaths:     []string{filepath.Join("..", "..", "..", "manifests", "base", "crds")},
    63  		ErrorIfCRDPathMissing: true,
    64  	}
    65  
    66  	cfg, err := testEnv.Start()
    67  	Expect(err).NotTo(HaveOccurred())
    68  	Expect(cfg).NotTo(BeNil())
    69  
    70  	err = v1beta1.AddToScheme(scheme.Scheme)
    71  	Expect(err).NotTo(HaveOccurred())
    72  	err = kubeflowv1.AddToScheme(scheme.Scheme)
    73  	Expect(err).NotTo(HaveOccurred())
    74  
    75  	// Set Default kubectl delivery image
    76  	config.Config.MPIKubectlDeliveryImage = config.MPIKubectlDeliveryImageDefault
    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  
   100  var _ = AfterSuite(func() {
   101  	By("tearing down the test environment")
   102  	testCancel()
   103  	err := testEnv.Stop()
   104  	Expect(err).NotTo(HaveOccurred())
   105  })