...

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 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 inject
    17  
    18  import (
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	"github.com/onsi/gomega/gexec"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	ctrl "sigs.k8s.io/controller-runtime"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    32  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    33  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    34  
    35  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    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 k8sClient client.Client
    43  var k8sManager ctrl.Manager
    44  var testEnv *envtest.Environment
    45  
    46  func TestAPIs(t *testing.T) {
    47  	RegisterFailHandler(Fail)
    48  
    49  	RunSpecsWithDefaultAndCustomReporters(t,
    50  		"webhook inject",
    51  		[]Reporter{printer.NewlineReporter{}})
    52  }
    53  
    54  var _ = BeforeSuite(func(done Done) {
    55  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    56  
    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  	cfg, err := testEnv.Start()
    70  	Expect(err).ToNot(HaveOccurred())
    71  	Expect(cfg).ToNot(BeNil())
    72  
    73  	err = scheme.AddToScheme(scheme.Scheme)
    74  	Expect(err).NotTo(HaveOccurred())
    75  
    76  	err = v1alpha1.AddToScheme(scheme.Scheme)
    77  	Expect(err).NotTo(HaveOccurred())
    78  
    79  	// +kubebuilder:scaffold:scheme
    80  
    81  	k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{
    82  		Scheme:             scheme.Scheme,
    83  		MetricsBindAddress: "0",
    84  	})
    85  	Expect(err).ToNot(HaveOccurred())
    86  
    87  	go func() {
    88  		err = k8sManager.Start(ctrl.SetupSignalHandler())
    89  		Expect(err).ToNot(HaveOccurred())
    90  	}()
    91  
    92  	k8sClient = k8sManager.GetClient()
    93  	Expect(k8sClient).ToNot(BeNil())
    94  
    95  	close(done)
    96  }, 60)
    97  
    98  var _ = AfterSuite(func() {
    99  	By("tearing down the test environment")
   100  	gexec.KillAndWait(5 * time.Second)
   101  	err := testEnv.Stop()
   102  	Expect(err).ToNot(HaveOccurred())
   103  })
   104