...

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