sigs.k8s.io/cluster-api-provider-aws@v1.5.5/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 "fmt" 21 "path" 22 "testing" 23 24 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 25 "k8s.io/client-go/kubernetes/scheme" 26 ctrl "sigs.k8s.io/controller-runtime" 27 28 // +kubebuilder:scaffold:imports 29 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 30 "sigs.k8s.io/cluster-api-provider-aws/test/helpers" 31 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 32 ) 33 34 var ( 35 testEnv *helpers.TestEnvironment 36 ctx = ctrl.SetupSignalHandler() 37 ) 38 39 func TestMain(m *testing.M) { 40 setup() 41 defer teardown() 42 m.Run() 43 } 44 45 func setup() { 46 utilruntime.Must(infrav1.AddToScheme(scheme.Scheme)) 47 utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme)) 48 testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{ 49 path.Join("config", "crd", "bases"), 50 }, 51 ).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml")) 52 var err error 53 testEnv, err = testEnvConfig.Build() 54 if err != nil { 55 panic(err) 56 } 57 if err := (&infrav1.AWSCluster{}).SetupWebhookWithManager(testEnv); err != nil { 58 panic(fmt.Sprintf("Unable to setup AWSCluster webhook: %v", err)) 59 } 60 if err := (&infrav1.AWSMachine{}).SetupWebhookWithManager(testEnv); err != nil { 61 panic(fmt.Sprintf("Unable to setup AWSMachine webhook: %v", err)) 62 } 63 if err := (&infrav1.AWSMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil { 64 panic(fmt.Sprintf("Unable to setup AWSMachineTemplate webhook: %v", err)) 65 } 66 if err := (&infrav1.AWSClusterControllerIdentity{}).SetupWebhookWithManager(testEnv); err != nil { 67 panic(fmt.Sprintf("Unable to setup AWSClusterControllerIdentity webhook: %v", err)) 68 } 69 go func() { 70 fmt.Println("Starting the manager") 71 if err := testEnv.StartManager(ctx); err != nil { 72 panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) 73 } 74 }() 75 testEnv.WaitForWebhooks() 76 } 77 78 func teardown() { 79 if err := testEnv.Stop(); err != nil { 80 panic(fmt.Sprintf("Failed to stop envtest: %v", err)) 81 } 82 }