k8s.io/kubernetes@v1.29.3/pkg/apis/admissionregistration/v1/defaults_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 v1_test 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 24 v1 "k8s.io/api/admissionregistration/v1" 25 apiequality "k8s.io/apimachinery/pkg/api/equality" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/kubernetes/pkg/api/legacyscheme" 29 _ "k8s.io/kubernetes/pkg/apis/admissionregistration/install" 30 utilpointer "k8s.io/utils/pointer" 31 ) 32 33 func TestDefaultAdmissionWebhook(t *testing.T) { 34 fail := v1.Fail 35 equivalent := v1.Equivalent 36 never := v1.NeverReinvocationPolicy 37 ten := int32(10) 38 allScopes := v1.AllScopes 39 40 tests := []struct { 41 name string 42 original runtime.Object 43 expected runtime.Object 44 }{ 45 { 46 name: "ValidatingWebhookConfiguration", 47 original: &v1.ValidatingWebhookConfiguration{ 48 Webhooks: []v1.ValidatingWebhook{{}}, 49 }, 50 expected: &v1.ValidatingWebhookConfiguration{ 51 Webhooks: []v1.ValidatingWebhook{{ 52 FailurePolicy: &fail, 53 MatchPolicy: &equivalent, 54 TimeoutSeconds: &ten, 55 NamespaceSelector: &metav1.LabelSelector{}, 56 ObjectSelector: &metav1.LabelSelector{}, 57 }}, 58 }, 59 }, 60 { 61 name: "MutatingWebhookConfiguration", 62 original: &v1.MutatingWebhookConfiguration{ 63 Webhooks: []v1.MutatingWebhook{{}}, 64 }, 65 expected: &v1.MutatingWebhookConfiguration{ 66 Webhooks: []v1.MutatingWebhook{{ 67 FailurePolicy: &fail, 68 MatchPolicy: &equivalent, 69 ReinvocationPolicy: &never, 70 TimeoutSeconds: &ten, 71 NamespaceSelector: &metav1.LabelSelector{}, 72 ObjectSelector: &metav1.LabelSelector{}, 73 }}, 74 }, 75 }, 76 { 77 name: "scope=*", 78 original: &v1.MutatingWebhookConfiguration{ 79 Webhooks: []v1.MutatingWebhook{{ 80 Rules: []v1.RuleWithOperations{{}}, 81 }}, 82 }, 83 expected: &v1.MutatingWebhookConfiguration{ 84 Webhooks: []v1.MutatingWebhook{{ 85 Rules: []v1.RuleWithOperations{{Rule: v1.Rule{ 86 Scope: &allScopes, // defaulted 87 }}}, 88 FailurePolicy: &fail, 89 MatchPolicy: &equivalent, 90 ReinvocationPolicy: &never, 91 TimeoutSeconds: &ten, 92 NamespaceSelector: &metav1.LabelSelector{}, 93 ObjectSelector: &metav1.LabelSelector{}, 94 }}, 95 }, 96 }, 97 { 98 name: "port=443", 99 original: &v1.MutatingWebhookConfiguration{ 100 Webhooks: []v1.MutatingWebhook{{ 101 ClientConfig: v1.WebhookClientConfig{ 102 Service: &v1.ServiceReference{}, 103 }, 104 }}, 105 }, 106 expected: &v1.MutatingWebhookConfiguration{ 107 Webhooks: []v1.MutatingWebhook{{ 108 ClientConfig: v1.WebhookClientConfig{ 109 Service: &v1.ServiceReference{ 110 Port: utilpointer.Int32(443), // defaulted 111 }, 112 }, 113 FailurePolicy: &fail, 114 MatchPolicy: &equivalent, 115 ReinvocationPolicy: &never, 116 TimeoutSeconds: &ten, 117 NamespaceSelector: &metav1.LabelSelector{}, 118 ObjectSelector: &metav1.LabelSelector{}, 119 }}, 120 }, 121 }, 122 } 123 124 for _, test := range tests { 125 t.Run(test.name, func(t *testing.T) { 126 original := test.original 127 expected := test.expected 128 legacyscheme.Scheme.Default(original) 129 if !apiequality.Semantic.DeepEqual(original, expected) { 130 t.Error(cmp.Diff(expected, original)) 131 } 132 }) 133 } 134 }