sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/suite_test.go (about)

     1  /*
     2  Copyright 2021 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 v1beta1
    18  
    19  import (
    20  	"fmt"
    21  	"path"
    22  	"testing"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	ctrl "sigs.k8s.io/controller-runtime"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    30  
    31  	"sigs.k8s.io/cluster-api-provider-aws/test/helpers"
    32  )
    33  
    34  var (
    35  	testEnv *helpers.TestEnvironment
    36  	ctx     = ctrl.SetupSignalHandler()
    37  )
    38  
    39  func TestAPIs(t *testing.T) {
    40  	RegisterFailHandler(Fail)
    41  
    42  	RunSpecsWithDefaultAndCustomReporters(t,
    43  		"Controller Suite",
    44  		[]Reporter{printer.NewlineReporter{}})
    45  }
    46  
    47  func TestMain(m *testing.M) {
    48  	setup()
    49  	defer teardown()
    50  	m.Run()
    51  }
    52  
    53  func setup() {
    54  	utilruntime.Must(AddToScheme(scheme.Scheme))
    55  	testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{
    56  		path.Join("config", "crd", "bases"),
    57  	},
    58  	).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml"))
    59  	var err error
    60  	testEnv, err = testEnvConfig.Build()
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	if err := (&AWSCluster{}).SetupWebhookWithManager(testEnv); err != nil {
    65  		panic(fmt.Sprintf("Unable to setup AWSCluster webhook: %v", err))
    66  	}
    67  	if err := (&AWSMachine{}).SetupWebhookWithManager(testEnv); err != nil {
    68  		panic(fmt.Sprintf("Unable to setup AWSMachine webhook: %v", err))
    69  	}
    70  	if err := (&AWSMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil {
    71  		panic(fmt.Sprintf("Unable to setup AWSMachineTemplate webhook: %v", err))
    72  	}
    73  	if err := (&AWSClusterControllerIdentity{}).SetupWebhookWithManager(testEnv); err != nil {
    74  		panic(fmt.Sprintf("Unable to setup AWSClusterControllerIdentity webhook: %v", err))
    75  	}
    76  	if err := (&AWSClusterRoleIdentity{}).SetupWebhookWithManager(testEnv); err != nil {
    77  		panic(fmt.Sprintf("Unable to setup AWSClusterRoleIdentity webhook: %v", err))
    78  	}
    79  	if err := (&AWSClusterStaticIdentity{}).SetupWebhookWithManager(testEnv); err != nil {
    80  		panic(fmt.Sprintf("Unable to setup AWSClusterStaticIdentity webhook: %v", err))
    81  	}
    82  
    83  	go func() {
    84  		fmt.Println("Starting the manager")
    85  		if err := testEnv.StartManager(ctx); err != nil {
    86  			panic(fmt.Sprintf("Failed to start the envtest manager: %v", err))
    87  		}
    88  	}()
    89  	testEnv.WaitForWebhooks()
    90  }
    91  
    92  func teardown() {
    93  	if err := testEnv.Stop(); err != nil {
    94  		panic(fmt.Sprintf("Failed to stop envtest: %v", err))
    95  	}
    96  }