...

Source file src/sigs.k8s.io/controller-runtime/pkg/webhook/webhook_suite_test.go

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

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

View as plain text