...

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

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/common

     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 common
    17  
    18  import (
    19  	"context"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/go-logr/logr"
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	"go.uber.org/fx"
    29  	"k8s.io/client-go/rest"
    30  	"k8s.io/kubectl/pkg/scheme"
    31  	ctrl "sigs.k8s.io/controller-runtime"
    32  	"sigs.k8s.io/controller-runtime/pkg/client"
    33  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    34  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    35  
    36  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    37  	"github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl"
    38  	"github.com/chaos-mesh/chaos-mesh/controllers/common/condition"
    39  	"github.com/chaos-mesh/chaos-mesh/controllers/common/desiredphase"
    40  	"github.com/chaos-mesh/chaos-mesh/controllers/common/finalizers"
    41  	"github.com/chaos-mesh/chaos-mesh/controllers/common/pipeline"
    42  	"github.com/chaos-mesh/chaos-mesh/controllers/schedule/utils"
    43  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/chaosdaemon"
    44  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/test"
    45  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    46  	"github.com/chaos-mesh/chaos-mesh/pkg/selector"
    47  )
    48  
    49  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    50  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    51  
    52  var app *fx.App
    53  var k8sClient client.Client
    54  var lister *utils.ActiveLister
    55  var cfg *rest.Config
    56  var testEnv *envtest.Environment
    57  var setupLog = ctrl.Log.WithName("setup")
    58  
    59  func TestCommon(t *testing.T) {
    60  	RegisterFailHandler(Fail)
    61  
    62  	RunSpecs(t, "Common suit")
    63  }
    64  
    65  var _ = BeforeSuite(func(ctx SpecContext) {
    66  	logf.SetLogger(log.NewZapLoggerWithWriter(GinkgoWriter))
    67  	By("bootstrapping test environment")
    68  	t := true
    69  	if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
    70  		testEnv = &envtest.Environment{
    71  			UseExistingCluster: &t,
    72  		}
    73  	} else {
    74  		testEnv = &envtest.Environment{
    75  			CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
    76  		}
    77  	}
    78  
    79  	err := v1alpha1.SchemeBuilder.AddToScheme(scheme.Scheme)
    80  	Expect(err).NotTo(HaveOccurred())
    81  
    82  	cfg, err = testEnv.Start()
    83  	Expect(err).ToNot(HaveOccurred())
    84  	Expect(cfg).ToNot(BeNil())
    85  
    86  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    87  	Expect(err).ToNot(HaveOccurred())
    88  	Expect(k8sClient).ToNot(BeNil())
    89  
    90  	rootLogger, err := log.NewDefaultZapLogger()
    91  	Expect(err).ToNot(HaveOccurred())
    92  	By("start application")
    93  	app = fx.New(
    94  		fx.Options(
    95  			fx.Supply(rootLogger),
    96  			test.Module,
    97  			chaosimpl.AllImpl,
    98  			selector.Module,
    99  			fx.Provide(chaosdaemon.New),
   100  			fx.Provide(func() []pipeline.PipelineStep {
   101  				return []pipeline.PipelineStep{finalizers.InitStep, desiredphase.Step, condition.Step, finalizers.CleanStep}
   102  			}),
   103  			fx.Invoke(Bootstrap),
   104  			fx.Supply(cfg),
   105  		),
   106  		fx.Invoke(Run),
   107  	)
   108  	startCtx, cancel := context.WithTimeout(context.Background(), app.StartTimeout())
   109  	defer cancel()
   110  
   111  	if err := app.Start(startCtx); err != nil {
   112  		setupLog.Error(err, "fail to start manager")
   113  	}
   114  	Expect(err).ToNot(HaveOccurred())
   115  
   116  }, NodeTimeout(60*time.Second))
   117  
   118  var _ = AfterSuite(func() {
   119  	By("tearing down the test environment")
   120  	stopCtx, cancel := context.WithTimeout(context.Background(), app.StopTimeout())
   121  	defer cancel()
   122  
   123  	if err := app.Stop(stopCtx); err != nil {
   124  		setupLog.Error(err, "fail to stop manager")
   125  	}
   126  	err := testEnv.Stop()
   127  	Expect(err).ToNot(HaveOccurred())
   128  })
   129  
   130  type RunParams struct {
   131  	fx.In
   132  
   133  	Mgr    ctrl.Manager
   134  	Logger logr.Logger
   135  }
   136  
   137  func Run(params RunParams) error {
   138  	lister = utils.NewActiveLister(k8sClient, params.Logger)
   139  	return nil
   140  }
   141