...

Source file src/sigs.k8s.io/controller-runtime/pkg/envtest/envtest_suite_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/envtest

     1  /*
     2  Copyright 2018 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 envtest
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	admissionv1 "k8s.io/api/admissionregistration/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    28  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    29  )
    30  
    31  func TestSource(t *testing.T) {
    32  	RegisterFailHandler(Fail)
    33  	RunSpecs(t, "Envtest Suite")
    34  }
    35  
    36  var env *Environment
    37  
    38  var _ = BeforeSuite(func() {
    39  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    40  	env = &Environment{}
    41  	// we're initializing webhook here and not in webhook.go to also test the envtest install code via WebhookOptions
    42  	initializeWebhookInEnvironment()
    43  	_, err := env.Start()
    44  	Expect(err).NotTo(HaveOccurred())
    45  })
    46  
    47  func initializeWebhookInEnvironment() {
    48  	namespacedScopeV1 := admissionv1.NamespacedScope
    49  	failedTypeV1 := admissionv1.Fail
    50  	equivalentTypeV1 := admissionv1.Equivalent
    51  	noSideEffectsV1 := admissionv1.SideEffectClassNone
    52  	webhookPathV1 := "/failing"
    53  
    54  	env.WebhookInstallOptions = WebhookInstallOptions{
    55  		ValidatingWebhooks: []*admissionv1.ValidatingWebhookConfiguration{
    56  			{
    57  				ObjectMeta: metav1.ObjectMeta{
    58  					Name: "deployment-validation-webhook-config",
    59  				},
    60  				TypeMeta: metav1.TypeMeta{
    61  					Kind:       "ValidatingWebhookConfiguration",
    62  					APIVersion: "admissionregistration.k8s.io/v1",
    63  				},
    64  				Webhooks: []admissionv1.ValidatingWebhook{
    65  					{
    66  						Name: "deployment-validation.kubebuilder.io",
    67  						Rules: []admissionv1.RuleWithOperations{
    68  							{
    69  								Operations: []admissionv1.OperationType{"CREATE", "UPDATE"},
    70  								Rule: admissionv1.Rule{
    71  									APIGroups:   []string{"apps"},
    72  									APIVersions: []string{"v1"},
    73  									Resources:   []string{"deployments"},
    74  									Scope:       &namespacedScopeV1,
    75  								},
    76  							},
    77  						},
    78  						FailurePolicy: &failedTypeV1,
    79  						MatchPolicy:   &equivalentTypeV1,
    80  						SideEffects:   &noSideEffectsV1,
    81  						ClientConfig: admissionv1.WebhookClientConfig{
    82  							Service: &admissionv1.ServiceReference{
    83  								Name:      "deployment-validation-service",
    84  								Namespace: "default",
    85  								Path:      &webhookPathV1,
    86  							},
    87  						},
    88  						AdmissionReviewVersions: []string{"v1"},
    89  					},
    90  				},
    91  			},
    92  			{
    93  				ObjectMeta: metav1.ObjectMeta{
    94  					Name: "deployment-validation-webhook-config",
    95  				},
    96  				TypeMeta: metav1.TypeMeta{
    97  					Kind:       "ValidatingWebhookConfiguration",
    98  					APIVersion: "admissionregistration.k8s.io/v1",
    99  				},
   100  				Webhooks: []admissionv1.ValidatingWebhook{
   101  					{
   102  						Name: "deployment-validation.kubebuilder.io",
   103  						Rules: []admissionv1.RuleWithOperations{
   104  							{
   105  								Operations: []admissionv1.OperationType{"CREATE", "UPDATE"},
   106  								Rule: admissionv1.Rule{
   107  									APIGroups:   []string{"apps"},
   108  									APIVersions: []string{"v1"},
   109  									Resources:   []string{"deployments"},
   110  									Scope:       &namespacedScopeV1,
   111  								},
   112  							},
   113  						},
   114  						FailurePolicy: &failedTypeV1,
   115  						MatchPolicy:   &equivalentTypeV1,
   116  						SideEffects:   &noSideEffectsV1,
   117  						ClientConfig: admissionv1.WebhookClientConfig{
   118  							Service: &admissionv1.ServiceReference{
   119  								Name:      "deployment-validation-service",
   120  								Namespace: "default",
   121  								Path:      &webhookPathV1,
   122  							},
   123  						},
   124  						AdmissionReviewVersions: []string{"v1"},
   125  					},
   126  				},
   127  			},
   128  		},
   129  	}
   130  }
   131  
   132  var _ = AfterSuite(func() {
   133  	Expect(env.Stop()).NotTo(HaveOccurred())
   134  })
   135  

View as plain text