sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1alpha3/webhook_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 v1alpha3 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 // +kubebuilder:scaffold:imports 32 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 33 "sigs.k8s.io/cluster-api-provider-aws/test/helpers" 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 ( 40 testEnv *helpers.TestEnvironment 41 ctx = ctrl.SetupSignalHandler() 42 ) 43 44 func TestAPIs(t *testing.T) { 45 RegisterFailHandler(Fail) 46 47 RunSpecsWithDefaultAndCustomReporters(t, 48 "Controller Suite", 49 []Reporter{printer.NewlineReporter{}}) 50 } 51 52 func TestMain(m *testing.M) { 53 setup() 54 defer teardown() 55 m.Run() 56 } 57 58 func setup() { 59 utilruntime.Must(AddToScheme(scheme.Scheme)) 60 utilruntime.Must(infrav1.AddToScheme(scheme.Scheme)) 61 62 testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{ 63 path.Join("config", "crd", "bases"), 64 }, 65 ).WithWebhookConfiguration("unmanaged", path.Join("config", "webhook", "manifests.yaml")) 66 var err error 67 testEnv, err = testEnvConfig.Build() 68 if err != nil { 69 panic(err) 70 } 71 if err := (&infrav1.AWSCluster{}).SetupWebhookWithManager(testEnv); err != nil { 72 panic(fmt.Sprintf("Unable to setup AWSCluster webhook: %v", err)) 73 } 74 if err := (&infrav1.AWSMachine{}).SetupWebhookWithManager(testEnv); err != nil { 75 panic(fmt.Sprintf("Unable to setup AWSMachine webhook: %v", err)) 76 } 77 if err := (&infrav1.AWSMachineTemplate{}).SetupWebhookWithManager(testEnv); err != nil { 78 panic(fmt.Sprintf("Unable to setup AWSMachineTemplate webhook: %v", err)) 79 } 80 go func() { 81 fmt.Println("Starting the manager") 82 if err := testEnv.StartManager(ctx); err != nil { 83 panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) 84 } 85 }() 86 testEnv.WaitForWebhooks() 87 } 88 89 func teardown() { 90 if err := testEnv.Stop(); err != nil { 91 panic(fmt.Sprintf("Failed to stop envtest: %v", err)) 92 } 93 }