...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/workflow/controllers/suite_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/workflow/controllers

     1  // Copyright 2021 Chaos Mesh 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  
    16  package controllers
    17  
    18  import (
    19  	"context"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	"go.uber.org/fx"
    28  	"k8s.io/client-go/rest"
    29  	"k8s.io/kubectl/pkg/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  
    35  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    36  	"github.com/chaos-mesh/chaos-mesh/controllers/types"
    37  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/test"
    38  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    39  )
    40  
    41  var app *fx.App
    42  var kubeClient client.Client
    43  var restConfig *rest.Config
    44  var testEnv *envtest.Environment
    45  var setupLog = ctrl.Log.WithName("setup")
    46  
    47  // TestWorkflow runs the integration tests of workflow.
    48  // Before run tests, take a look on ENV KUBEBUILDER_ASSETS, it should be set to <repo-root>/output/bin/kubebuilder/bin
    49  func TestWorkflow(t *testing.T) {
    50  	RegisterFailHandler(Fail)
    51  
    52  	RunSpecs(t, "workflow suite")
    53  }
    54  
    55  var _ = BeforeSuite(func(ctx SpecContext) {
    56  	logf.SetLogger(log.NewZapLoggerWithWriter(GinkgoWriter))
    57  	By("bootstrapping test environment")
    58  	t := true
    59  	if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
    60  		testEnv = &envtest.Environment{
    61  			UseExistingCluster: &t,
    62  		}
    63  	} else {
    64  		testEnv = &envtest.Environment{
    65  			CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
    66  		}
    67  	}
    68  
    69  	err := v1alpha1.SchemeBuilder.AddToScheme(scheme.Scheme)
    70  	Expect(err).NotTo(HaveOccurred())
    71  
    72  	restConfig, err = testEnv.Start()
    73  	Expect(err).ToNot(HaveOccurred())
    74  	Expect(restConfig).ToNot(BeNil())
    75  
    76  	kubeClient, err = client.New(restConfig, client.Options{Scheme: scheme.Scheme})
    77  	Expect(err).ToNot(HaveOccurred())
    78  	Expect(kubeClient).ToNot(BeNil())
    79  
    80  	rootLogger, err := log.NewDefaultZapLogger()
    81  	Expect(err).ToNot(HaveOccurred())
    82  
    83  	app = fx.New(
    84  		fx.Options(
    85  			fx.Supply(rootLogger),
    86  			test.Module,
    87  			fx.Supply(restConfig),
    88  			types.ChaosObjects,
    89  		),
    90  		// only startup workflow related
    91  		fx.Invoke(BootstrapWorkflowControllers),
    92  	)
    93  	startCtx, cancel := context.WithTimeout(context.Background(), app.StartTimeout())
    94  	defer cancel()
    95  
    96  	if err := app.Start(startCtx); err != nil {
    97  		setupLog.Error(err, "fail to start manager")
    98  	}
    99  	Expect(err).ToNot(HaveOccurred())
   100  
   101  }, NodeTimeout(60*time.Second))
   102  
   103  var _ = AfterSuite(func() {
   104  	By("tearing down the test environment")
   105  	stopCtx, cancel := context.WithTimeout(context.Background(), app.StopTimeout())
   106  	defer cancel()
   107  
   108  	if err := app.Stop(stopCtx); err != nil {
   109  		setupLog.Error(err, "fail to stop manager")
   110  	}
   111  	err := testEnv.Stop()
   112  	Expect(err).ToNot(HaveOccurred())
   113  })
   114