k8s.io/kubernetes@v1.29.3/pkg/registry/admissionregistration/validatingwebhookconfiguration/strategy_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 validatingwebhookconfiguration 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 24 "k8s.io/kubernetes/pkg/apis/admissionregistration" 25 ) 26 27 func TestValidatingWebhookConfigurationStrategy(t *testing.T) { 28 ctx := genericapirequest.NewDefaultContext() 29 if Strategy.NamespaceScoped() { 30 t.Error("ValidatingWebhookConfiguration strategy must be cluster scoped") 31 } 32 if Strategy.AllowCreateOnUpdate() { 33 t.Errorf("ValidatingWebhookConfiguration should not allow create on update") 34 } 35 36 configuration := validValidatingWebhookConfiguration() 37 Strategy.PrepareForCreate(ctx, configuration) 38 errs := Strategy.Validate(ctx, configuration) 39 if len(errs) != 0 { 40 t.Errorf("Unexpected error validating %v", errs) 41 } 42 invalidConfiguration := &admissionregistration.ValidatingWebhookConfiguration{ 43 ObjectMeta: metav1.ObjectMeta{Name: ""}, 44 } 45 Strategy.PrepareForUpdate(ctx, invalidConfiguration, configuration) 46 errs = Strategy.ValidateUpdate(ctx, invalidConfiguration, configuration) 47 if len(errs) == 0 { 48 t.Errorf("Expected a validation error") 49 } 50 } 51 func validValidatingWebhookConfiguration() *admissionregistration.ValidatingWebhookConfiguration { 52 ignore := admissionregistration.Ignore 53 exact := admissionregistration.Exact 54 thirty := int32(30) 55 none := admissionregistration.SideEffectClassNone 56 servicePath := "/" 57 return &admissionregistration.ValidatingWebhookConfiguration{ 58 ObjectMeta: metav1.ObjectMeta{ 59 Name: "foo", 60 }, 61 Webhooks: []admissionregistration.ValidatingWebhook{{ 62 Name: "foo.example.io", 63 ClientConfig: admissionregistration.WebhookClientConfig{ 64 Service: &admissionregistration.ServiceReference{ 65 Name: "foo", 66 Namespace: "bar", 67 Path: &servicePath, 68 Port: 443, 69 }, 70 }, 71 FailurePolicy: &ignore, 72 MatchPolicy: &exact, 73 TimeoutSeconds: &thirty, 74 NamespaceSelector: &metav1.LabelSelector{}, 75 ObjectSelector: &metav1.LabelSelector{}, 76 SideEffects: &none, 77 AdmissionReviewVersions: []string{"v1beta1"}, 78 }}, 79 } 80 }