github.com/ferryproxy/api@v0.4.2/controllers/traffic/suite_test.go (about)

     1  /*
     2  Copyright 2022 FerryProxy Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package traffic
    18  
    19  import (
    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  	trafficv1alpha2 "github.com/ferryproxy/api/apis/traffic/v1alpha2"
    34  	//+kubebuilder:scaffold:imports
    35  )
    36  
    37  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    38  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    39  
    40  var cfg *rest.Config
    41  var k8sClient client.Client
    42  var testEnv *envtest.Environment
    43  
    44  func TestAPIs(t *testing.T) {
    45  	RegisterFailHandler(Fail)
    46  
    47  	RunSpecsWithDefaultAndCustomReporters(t,
    48  		"Controller Suite",
    49  		[]Reporter{printer.NewlineReporter{}})
    50  }
    51  
    52  var _ = BeforeSuite(func() {
    53  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    54  
    55  	By("bootstrapping test environment")
    56  	testEnv = &envtest.Environment{
    57  		CRDDirectoryPaths:     []string{filepath.Join("..", "..", "config", "crd", "bases")},
    58  		ErrorIfCRDPathMissing: true,
    59  	}
    60  
    61  	var err error
    62  	// cfg is defined in this file globally.
    63  	cfg, err = testEnv.Start()
    64  	Expect(err).NotTo(HaveOccurred())
    65  	Expect(cfg).NotTo(BeNil())
    66  
    67  	err = trafficv1alpha2.AddToScheme(scheme.Scheme)
    68  	Expect(err).NotTo(HaveOccurred())
    69  
    70  	//+kubebuilder:scaffold:scheme
    71  
    72  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    73  	Expect(err).NotTo(HaveOccurred())
    74  	Expect(k8sClient).NotTo(BeNil())
    75  
    76  }, 60)
    77  
    78  var _ = AfterSuite(func() {
    79  	By("tearing down the test environment")
    80  	err := testEnv.Stop()
    81  	Expect(err).NotTo(HaveOccurred())
    82  })