...

Source file src/k8s.io/kubernetes/pkg/apis/admissionregistration/v1/defaults_test.go

Documentation: k8s.io/kubernetes/pkg/apis/admissionregistration/v1

     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  }
   135  
   136  func TestDefaultAdmissionPolicy(t *testing.T) {
   137  	fail := v1.Fail
   138  	equivalent := v1.Equivalent
   139  	allScopes := v1.AllScopes
   140  
   141  	tests := []struct {
   142  		name     string
   143  		original runtime.Object
   144  		expected runtime.Object
   145  	}{
   146  		{
   147  			name: "ValidatingAdmissionPolicy",
   148  			original: &v1.ValidatingAdmissionPolicy{
   149  				Spec: v1.ValidatingAdmissionPolicySpec{
   150  					MatchConstraints: &v1.MatchResources{},
   151  				},
   152  			},
   153  			expected: &v1.ValidatingAdmissionPolicy{
   154  				Spec: v1.ValidatingAdmissionPolicySpec{
   155  					MatchConstraints: &v1.MatchResources{
   156  						MatchPolicy:       &equivalent,
   157  						NamespaceSelector: &metav1.LabelSelector{},
   158  						ObjectSelector:    &metav1.LabelSelector{},
   159  					},
   160  					FailurePolicy: &fail,
   161  				},
   162  			},
   163  		},
   164  		{
   165  			name: "ValidatingAdmissionPolicyBinding",
   166  			original: &v1.ValidatingAdmissionPolicyBinding{
   167  				Spec: v1.ValidatingAdmissionPolicyBindingSpec{
   168  					MatchResources: &v1.MatchResources{},
   169  				},
   170  			},
   171  			expected: &v1.ValidatingAdmissionPolicyBinding{
   172  				Spec: v1.ValidatingAdmissionPolicyBindingSpec{
   173  					MatchResources: &v1.MatchResources{
   174  						MatchPolicy:       &equivalent,
   175  						NamespaceSelector: &metav1.LabelSelector{},
   176  						ObjectSelector:    &metav1.LabelSelector{},
   177  					},
   178  				},
   179  			},
   180  		},
   181  		{
   182  			name: "scope=*",
   183  			original: &v1.ValidatingAdmissionPolicy{
   184  				Spec: v1.ValidatingAdmissionPolicySpec{
   185  					MatchConstraints: &v1.MatchResources{
   186  						ResourceRules: []v1.NamedRuleWithOperations{{}},
   187  					},
   188  				},
   189  			},
   190  			expected: &v1.ValidatingAdmissionPolicy{
   191  				Spec: v1.ValidatingAdmissionPolicySpec{
   192  					MatchConstraints: &v1.MatchResources{
   193  						MatchPolicy:       &equivalent,
   194  						NamespaceSelector: &metav1.LabelSelector{},
   195  						ObjectSelector:    &metav1.LabelSelector{},
   196  						ResourceRules: []v1.NamedRuleWithOperations{
   197  							{
   198  								RuleWithOperations: v1.RuleWithOperations{
   199  									Rule: v1.Rule{
   200  										Scope: &allScopes, // defaulted
   201  									},
   202  								},
   203  							},
   204  						},
   205  					},
   206  					FailurePolicy: &fail,
   207  				},
   208  			},
   209  		},
   210  	}
   211  
   212  	for _, test := range tests {
   213  		t.Run(test.name, func(t *testing.T) {
   214  			original := test.original
   215  			expected := test.expected
   216  			legacyscheme.Scheme.Default(original)
   217  			if !apiequality.Semantic.DeepEqual(original, expected) {
   218  				t.Error(cmp.Diff(expected, original))
   219  			}
   220  		})
   221  	}
   222  }
   223  

View as plain text