...

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