...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/suite_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2019 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 v1alpha1
    15  
    16  import (
    17  	"os"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/client-go/kubernetes/scheme"
    24  	"k8s.io/client-go/rest"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    27  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    28  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    29  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    30  )
    31  
    32  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    33  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    34  
    35  var cfg *rest.Config
    36  var k8sClient client.Client
    37  var testEnv *envtest.Environment
    38  
    39  func TestAPIs(t *testing.T) {
    40  	RegisterFailHandler(Fail)
    41  
    42  	RunSpecsWithDefaultAndCustomReporters(t,
    43  		"v1 Suite",
    44  		[]Reporter{printer.NewlineReporter{}})
    45  }
    46  
    47  var _ = BeforeSuite(func(done Done) {
    48  	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
    49  
    50  	By("bootstrapping test environment")
    51  	t := true
    52  	if os.Getenv("USE_EXISTING_CLUSTER") == "true" {
    53  		testEnv = &envtest.Environment{
    54  			UseExistingCluster: &t,
    55  		}
    56  	} else {
    57  		testEnv = &envtest.Environment{
    58  			CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
    59  		}
    60  	}
    61  
    62  	err := SchemeBuilder.AddToScheme(scheme.Scheme)
    63  	Expect(err).NotTo(HaveOccurred())
    64  
    65  	cfg, err = testEnv.Start()
    66  	Expect(err).ToNot(HaveOccurred())
    67  	Expect(cfg).ToNot(BeNil())
    68  
    69  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    70  	Expect(err).ToNot(HaveOccurred())
    71  	Expect(k8sClient).ToNot(BeNil())
    72  
    73  	close(done)
    74  }, 60)
    75  
    76  var _ = AfterSuite(func() {
    77  	By("tearing down the test environment")
    78  	err := testEnv.Stop()
    79  	Expect(err).ToNot(HaveOccurred())
    80  })
    81