github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/pytorch/pytorchjob_controller_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 pytorch
    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  	. "github.com/onsi/gomega"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  	ctrl "sigs.k8s.io/controller-runtime"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    33  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    34  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    35  	"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    36  	//+kubebuilder:scaffold:imports
    37  )
    38  
    39  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    40  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    41  
    42  var (
    43  	testK8sClient client.Client
    44  	testEnv       *envtest.Environment
    45  	testCtx       context.Context
    46  	testCancel    context.CancelFunc
    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 config.
    76  	config.Config.PyTorchInitContainerImage = config.PyTorchInitContainerImageDefault
    77  	config.Config.PyTorchInitContainerTemplateFile = config.PyTorchInitContainerTemplateFileDefault
    78  
    79  	//+kubebuilder:scaffold:scheme
    80  
    81  	testK8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    82  	Expect(err).NotTo(HaveOccurred())
    83  	Expect(testK8sClient).NotTo(BeNil())
    84  
    85  	mgr, err := ctrl.NewManager(cfg, ctrl.Options{
    86  		MetricsBindAddress: "0",
    87  	})
    88  	Expect(err).NotTo(gomega.HaveOccurred())
    89  
    90  	gangSchedulingSetupFunc := common.GenNonGangSchedulerSetupFunc()
    91  	r := NewReconciler(mgr, gangSchedulingSetupFunc)
    92  
    93  	Expect(r.SetupWithManager(mgr, 1)).NotTo(gomega.HaveOccurred())
    94  
    95  	go func() {
    96  		defer GinkgoRecover()
    97  		err = mgr.Start(testCtx)
    98  		Expect(err).ToNot(HaveOccurred(), "failed to run manager")
    99  	}()
   100  })
   101  
   102  var _ = AfterSuite(func() {
   103  	By("tearing down the test environment")
   104  	testCancel()
   105  	err := testEnv.Stop()
   106  	Expect(err).NotTo(HaveOccurred())
   107  })