...

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  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"k8s.io/client-go/kubernetes/scheme"
    27  	"k8s.io/client-go/rest"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    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  	RunSpecs(t, "v1 Suite")
    45  }
    46  
    47  var _ = BeforeSuite(func(ctx SpecContext) {
    48  
    49  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    50  
    51  	By("bootstrapping test environment")
    52  	t := true
    53  	if os.Getenv("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  }, NodeTimeout(60*time.Second))
    75  
    76  var _ = AfterSuite(func() {
    77  	By("tearing down the test environment")
    78  	err := testEnv.Stop()
    79  	Expect(err).ToNot(HaveOccurred())
    80  })
    81