sigs.k8s.io/cluster-api/bootstrap/kubeadm@v0.0.0-20191016155141-23a891785b60/controllers/suite_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes 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 controllers
    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/klog"
    27  	"k8s.io/klog/klogr"
    28  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2"
    29  	clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    32  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    33  	// +kubebuilder:scaffold:imports
    34  )
    35  
    36  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    37  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    38  
    39  var k8sClient client.Client
    40  var testEnv *envtest.Environment
    41  
    42  func init() {
    43  	klog.InitFlags(nil)
    44  	klog.SetOutput(GinkgoWriter)
    45  	logf.SetLogger(klogr.New())
    46  }
    47  
    48  func TestAPIs(t *testing.T) {
    49  	RegisterFailHandler(Fail)
    50  
    51  	RunSpecsWithDefaultAndCustomReporters(t,
    52  		"Controller Suite",
    53  		[]Reporter{envtest.NewlineReporter{}})
    54  }
    55  
    56  var _ = BeforeSuite(func(done Done) {
    57  	By("bootstrapping test environment")
    58  	testEnv = &envtest.Environment{
    59  		CRDDirectoryPaths: []string{
    60  			filepath.Join("..", "config", "crd", "bases"),
    61  			// This is copied in from cluster-api using ./hack/update-capi-crds.sh
    62  			filepath.Join("..", "config-capi", "crds"),
    63  		},
    64  	}
    65  
    66  	cfg, err := testEnv.Start()
    67  	Expect(err).ToNot(HaveOccurred())
    68  	Expect(cfg).ToNot(BeNil())
    69  
    70  	Expect(bootstrapv1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
    71  	Expect(clusterv1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
    72  	// +kubebuilder:scaffold:scheme
    73  
    74  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    75  	Expect(err).ToNot(HaveOccurred())
    76  	Expect(k8sClient).ToNot(BeNil())
    77  
    78  	close(done)
    79  }, 60)
    80  
    81  var _ = AfterSuite(func() {
    82  	By("tearing down the test environment")
    83  	err := testEnv.Stop()
    84  	Expect(err).ToNot(HaveOccurred())
    85  })