...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/common/suit_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  
    24  	"github.com/go-logr/logr"
    25  	. "github.com/onsi/ginkgo"
    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  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    34  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    35  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    36  
    37  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    38  	"github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl"
    39  	"github.com/chaos-mesh/chaos-mesh/controllers/common/condition"
    40  	"github.com/chaos-mesh/chaos-mesh/controllers/common/desiredphase"
    41  	"github.com/chaos-mesh/chaos-mesh/controllers/common/finalizers"
    42  	"github.com/chaos-mesh/chaos-mesh/controllers/common/pipeline"
    43  	"github.com/chaos-mesh/chaos-mesh/controllers/schedule/utils"
    44  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/chaosdaemon"
    45  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/test"
    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  	RunSpecsWithDefaultAndCustomReporters(t,
    63  		"Common suit",
    64  		[]Reporter{printer.NewlineReporter{}})
    65  }
    66  
    67  var _ = BeforeSuite(func() {
    68  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    69  	By("bootstrapping test environment")
    70  	t := true
    71  	if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
    72  		testEnv = &envtest.Environment{
    73  			UseExistingCluster: &t,
    74  		}
    75  	} else {
    76  		testEnv = &envtest.Environment{
    77  			CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
    78  		}
    79  	}
    80  
    81  	err := v1alpha1.SchemeBuilder.AddToScheme(scheme.Scheme)
    82  	Expect(err).NotTo(HaveOccurred())
    83  
    84  	cfg, err = testEnv.Start()
    85  	Expect(err).ToNot(HaveOccurred())
    86  	Expect(cfg).ToNot(BeNil())
    87  
    88  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    89  	Expect(err).ToNot(HaveOccurred())
    90  	Expect(k8sClient).ToNot(BeNil())
    91  
    92  	By("start application")
    93  	app = fx.New(
    94  		fx.Options(
    95  			test.Module,
    96  			chaosimpl.AllImpl,
    97  			selector.Module,
    98  			fx.Provide(chaosdaemon.New),
    99  			fx.Provide(func() []pipeline.PipelineStep {
   100  				return []pipeline.PipelineStep{finalizers.Step, desiredphase.Step, condition.Step}
   101  			}),
   102  			fx.Invoke(Bootstrap),
   103  			fx.Supply(cfg),
   104  		),
   105  		fx.Invoke(Run),
   106  	)
   107  	startCtx, cancel := context.WithTimeout(context.Background(), app.StartTimeout())
   108  	defer cancel()
   109  
   110  	if err := app.Start(startCtx); err != nil {
   111  		setupLog.Error(err, "fail to start manager")
   112  	}
   113  	Expect(err).ToNot(HaveOccurred())
   114  
   115  }, 60)
   116  
   117  var _ = AfterSuite(func() {
   118  	By("tearing down the test environment")
   119  	stopCtx, cancel := context.WithTimeout(context.Background(), app.StopTimeout())
   120  	defer cancel()
   121  
   122  	if err := app.Stop(stopCtx); err != nil {
   123  		setupLog.Error(err, "fail to stop manager")
   124  	}
   125  	err := testEnv.Stop()
   126  	Expect(err).ToNot(HaveOccurred())
   127  })
   128  
   129  type RunParams struct {
   130  	fx.In
   131  
   132  	Mgr    ctrl.Manager
   133  	Logger logr.Logger
   134  }
   135  
   136  func Run(params RunParams) error {
   137  	lister = utils.NewActiveLister(k8sClient, params.Logger)
   138  	return nil
   139  }
   140