...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/webhook/inject/setup_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/webhook/inject

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package inject
    15  
    16  import (
    17  	"os"
    18  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/onsi/gomega/gexec"
    25  	"k8s.io/client-go/kubernetes/scheme"
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    30  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    31  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    32  
    33  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    34  	// +kubebuilder:scaffold:imports
    35  )
    36  
    37  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    38  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    39  
    40  var k8sClient client.Client
    41  var k8sManager ctrl.Manager
    42  var testEnv *envtest.Environment
    43  
    44  func TestAPIs(t *testing.T) {
    45  	RegisterFailHandler(Fail)
    46  
    47  	RunSpecsWithDefaultAndCustomReporters(t,
    48  		"webhook inject",
    49  		[]Reporter{printer.NewlineReporter{}})
    50  }
    51  
    52  var _ = BeforeSuite(func(done Done) {
    53  	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
    54  
    55  	By("bootstrapping test environment")
    56  	t := true
    57  	if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
    58  		testEnv = &envtest.Environment{
    59  			UseExistingCluster: &t,
    60  		}
    61  	} else {
    62  		testEnv = &envtest.Environment{
    63  			CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
    64  		}
    65  	}
    66  
    67  	cfg, err := testEnv.Start()
    68  	Expect(err).ToNot(HaveOccurred())
    69  	Expect(cfg).ToNot(BeNil())
    70  
    71  	err = scheme.AddToScheme(scheme.Scheme)
    72  	Expect(err).NotTo(HaveOccurred())
    73  
    74  	err = v1alpha1.AddToScheme(scheme.Scheme)
    75  	Expect(err).NotTo(HaveOccurred())
    76  
    77  	// +kubebuilder:scaffold:scheme
    78  
    79  	k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{
    80  		Scheme:             scheme.Scheme,
    81  		MetricsBindAddress: "0",
    82  	})
    83  	Expect(err).ToNot(HaveOccurred())
    84  
    85  	go func() {
    86  		err = k8sManager.Start(ctrl.SetupSignalHandler())
    87  		Expect(err).ToNot(HaveOccurred())
    88  	}()
    89  
    90  	k8sClient = k8sManager.GetClient()
    91  	Expect(k8sClient).ToNot(BeNil())
    92  
    93  	close(done)
    94  }, 60)
    95  
    96  var _ = AfterSuite(func() {
    97  	By("tearing down the test environment")
    98  	gexec.KillAndWait(5 * time.Second)
    99  	err := testEnv.Stop()
   100  	Expect(err).ToNot(HaveOccurred())
   101  })
   102