k8s.io/kubernetes@v1.29.3/pkg/apis/admissionregistration/util_test.go (about)

     1  /*
     2  Copyright 2023 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 admissionregistration
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	genericfeatures "k8s.io/apiserver/pkg/features"
    25  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    26  	featuregatetesting "k8s.io/component-base/featuregate/testing"
    27  )
    28  
    29  func TestDropDisabledMutatingWebhookConfigurationFields(t *testing.T) {
    30  	tests := []struct {
    31  		name               string
    32  		old                *MutatingWebhookConfiguration
    33  		new                *MutatingWebhookConfiguration
    34  		featureGateEnabled bool
    35  		expected           []MatchCondition
    36  	}{
    37  		{
    38  			name: "create with no match conditions, feature gate off",
    39  			old:  nil,
    40  			new: &MutatingWebhookConfiguration{
    41  				Webhooks: []MutatingWebhook{
    42  					{},
    43  				},
    44  			},
    45  			featureGateEnabled: false,
    46  			expected:           nil,
    47  		},
    48  		{
    49  			name: "create with match conditions, feature gate off",
    50  			old:  nil,
    51  			new: &MutatingWebhookConfiguration{
    52  				Webhooks: []MutatingWebhook{
    53  					{
    54  						MatchConditions: []MatchCondition{
    55  							{
    56  								Name: "test1",
    57  							},
    58  						},
    59  					},
    60  					{
    61  						MatchConditions: []MatchCondition{
    62  							{
    63  								Name: "test1",
    64  							},
    65  						},
    66  					},
    67  				},
    68  			},
    69  			featureGateEnabled: false,
    70  			expected:           nil,
    71  		},
    72  		{
    73  			name: "create with no match conditions, feature gate on",
    74  			old:  nil,
    75  			new: &MutatingWebhookConfiguration{
    76  				Webhooks: []MutatingWebhook{
    77  					{},
    78  					{},
    79  				},
    80  			},
    81  			featureGateEnabled: true,
    82  			expected:           nil,
    83  		},
    84  		{
    85  			name: "create with match conditions, feature gate on",
    86  			old:  nil,
    87  			new: &MutatingWebhookConfiguration{
    88  				Webhooks: []MutatingWebhook{
    89  					{
    90  						MatchConditions: []MatchCondition{
    91  							{
    92  								Name: "test1",
    93  							},
    94  						},
    95  					},
    96  					{
    97  						MatchConditions: []MatchCondition{
    98  							{
    99  								Name: "test1",
   100  							},
   101  						},
   102  					},
   103  				},
   104  			},
   105  			featureGateEnabled: true,
   106  			expected: []MatchCondition{
   107  				{
   108  					Name: "test1",
   109  				},
   110  			},
   111  		},
   112  		{
   113  			name: "update with old has match conditions feature gate on",
   114  			old: &MutatingWebhookConfiguration{
   115  				Webhooks: []MutatingWebhook{
   116  					{
   117  						MatchConditions: []MatchCondition{
   118  							{
   119  								Name: "test1",
   120  							},
   121  						},
   122  					},
   123  					{
   124  						MatchConditions: []MatchCondition{
   125  							{
   126  								Name: "test1",
   127  							},
   128  						},
   129  					},
   130  				},
   131  			},
   132  			new: &MutatingWebhookConfiguration{
   133  				Webhooks: []MutatingWebhook{
   134  					{
   135  						MatchConditions: []MatchCondition{
   136  							{
   137  								Name: "test1",
   138  							},
   139  						},
   140  					},
   141  					{
   142  						MatchConditions: []MatchCondition{
   143  							{
   144  								Name: "test1",
   145  							},
   146  						},
   147  					},
   148  				},
   149  			},
   150  			featureGateEnabled: true,
   151  			expected: []MatchCondition{
   152  				{
   153  					Name: "test1",
   154  				},
   155  			},
   156  		},
   157  		{
   158  			name: "update with old has match conditions feature gate off",
   159  			old: &MutatingWebhookConfiguration{
   160  				Webhooks: []MutatingWebhook{
   161  					{
   162  						MatchConditions: []MatchCondition{
   163  							{
   164  								Name: "test1",
   165  							},
   166  						},
   167  					},
   168  					{
   169  						MatchConditions: []MatchCondition{
   170  							{
   171  								Name: "test1",
   172  							},
   173  						},
   174  					},
   175  				},
   176  			},
   177  			new: &MutatingWebhookConfiguration{
   178  				Webhooks: []MutatingWebhook{
   179  					{
   180  						MatchConditions: []MatchCondition{
   181  							{
   182  								Name: "test1",
   183  							},
   184  						},
   185  					},
   186  					{
   187  						MatchConditions: []MatchCondition{
   188  							{
   189  								Name: "test1",
   190  							},
   191  						},
   192  					},
   193  				},
   194  			},
   195  			featureGateEnabled: false,
   196  			expected: []MatchCondition{
   197  				{
   198  					Name: "test1",
   199  				},
   200  			},
   201  		},
   202  	}
   203  	for _, test := range tests {
   204  		t.Run(test.name, func(t *testing.T) {
   205  			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.AdmissionWebhookMatchConditions, test.featureGateEnabled)()
   206  			DropDisabledMutatingWebhookConfigurationFields(test.new, test.old)
   207  
   208  			for _, hook := range test.new.Webhooks {
   209  				if test.expected == nil {
   210  					if hook.MatchConditions != nil {
   211  						t.Error("expected all hooks matchConditions to be nil")
   212  					}
   213  				} else {
   214  					require.Equal(t, len(test.expected), len(hook.MatchConditions))
   215  					for i, matchCondition := range hook.MatchConditions {
   216  						require.Equal(t, test.expected[i], matchCondition)
   217  					}
   218  				}
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  func TestDropDisabledValidatingWebhookConfigurationFields(t *testing.T) {
   225  	tests := []struct {
   226  		name               string
   227  		old                *ValidatingWebhookConfiguration
   228  		new                *ValidatingWebhookConfiguration
   229  		featureGateEnabled bool
   230  		expected           []MatchCondition
   231  	}{
   232  		{
   233  			name: "create with no match conditions, feature gate off",
   234  			old:  nil,
   235  			new: &ValidatingWebhookConfiguration{
   236  				Webhooks: []ValidatingWebhook{
   237  					{},
   238  				},
   239  			},
   240  			featureGateEnabled: false,
   241  			expected:           nil,
   242  		},
   243  		{
   244  			name: "create with match conditions, feature gate off",
   245  			old:  nil,
   246  			new: &ValidatingWebhookConfiguration{
   247  				Webhooks: []ValidatingWebhook{
   248  					{
   249  						MatchConditions: []MatchCondition{
   250  							{
   251  								Name: "test1",
   252  							},
   253  						},
   254  					},
   255  					{
   256  						MatchConditions: []MatchCondition{
   257  							{
   258  								Name: "test1",
   259  							},
   260  						},
   261  					},
   262  				},
   263  			},
   264  			featureGateEnabled: false,
   265  			expected:           nil,
   266  		},
   267  		{
   268  			name: "create with no match conditions, feature gate on",
   269  			old:  nil,
   270  			new: &ValidatingWebhookConfiguration{
   271  				Webhooks: []ValidatingWebhook{
   272  					{},
   273  					{},
   274  				},
   275  			},
   276  			featureGateEnabled: true,
   277  			expected:           nil,
   278  		},
   279  		{
   280  			name: "create with match conditions, feature gate on",
   281  			old:  nil,
   282  			new: &ValidatingWebhookConfiguration{
   283  				Webhooks: []ValidatingWebhook{
   284  					{
   285  						MatchConditions: []MatchCondition{
   286  							{
   287  								Name: "test1",
   288  							},
   289  						},
   290  					},
   291  					{
   292  						MatchConditions: []MatchCondition{
   293  							{
   294  								Name: "test1",
   295  							},
   296  						},
   297  					},
   298  				},
   299  			},
   300  			featureGateEnabled: true,
   301  			expected: []MatchCondition{
   302  				{
   303  					Name: "test1",
   304  				},
   305  			},
   306  		},
   307  		{
   308  			name: "update with old has match conditions feature gate on",
   309  			old: &ValidatingWebhookConfiguration{
   310  				Webhooks: []ValidatingWebhook{
   311  					{
   312  						MatchConditions: []MatchCondition{
   313  							{
   314  								Name: "test1",
   315  							},
   316  						},
   317  					},
   318  					{
   319  						MatchConditions: []MatchCondition{
   320  							{
   321  								Name: "test1",
   322  							},
   323  						},
   324  					},
   325  				},
   326  			},
   327  			new: &ValidatingWebhookConfiguration{
   328  				Webhooks: []ValidatingWebhook{
   329  					{
   330  						MatchConditions: []MatchCondition{
   331  							{
   332  								Name: "test1",
   333  							},
   334  						},
   335  					},
   336  					{
   337  						MatchConditions: []MatchCondition{
   338  							{
   339  								Name: "test1",
   340  							},
   341  						},
   342  					},
   343  				},
   344  			},
   345  			featureGateEnabled: true,
   346  			expected: []MatchCondition{
   347  				{
   348  					Name: "test1",
   349  				},
   350  			},
   351  		},
   352  		{
   353  			name: "update with old has match conditions feature gate off",
   354  			old: &ValidatingWebhookConfiguration{
   355  				Webhooks: []ValidatingWebhook{
   356  					{
   357  						MatchConditions: []MatchCondition{
   358  							{
   359  								Name: "test1",
   360  							},
   361  						},
   362  					},
   363  					{
   364  						MatchConditions: []MatchCondition{
   365  							{
   366  								Name: "test1",
   367  							},
   368  						},
   369  					},
   370  				},
   371  			},
   372  			new: &ValidatingWebhookConfiguration{
   373  				Webhooks: []ValidatingWebhook{
   374  					{
   375  						MatchConditions: []MatchCondition{
   376  							{
   377  								Name: "test1",
   378  							},
   379  						},
   380  					},
   381  					{
   382  						MatchConditions: []MatchCondition{
   383  							{
   384  								Name: "test1",
   385  							},
   386  						},
   387  					},
   388  				},
   389  			},
   390  			featureGateEnabled: false,
   391  			expected: []MatchCondition{
   392  				{
   393  					Name: "test1",
   394  				},
   395  			},
   396  		},
   397  	}
   398  	for _, test := range tests {
   399  		t.Run(test.name, func(t *testing.T) {
   400  			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.AdmissionWebhookMatchConditions, test.featureGateEnabled)()
   401  			DropDisabledValidatingWebhookConfigurationFields(test.new, test.old)
   402  
   403  			for _, hook := range test.new.Webhooks {
   404  				if test.expected == nil {
   405  					if hook.MatchConditions != nil {
   406  						t.Error("expected all hooks matchConditions to be nil")
   407  					}
   408  				} else {
   409  					require.Equal(t, len(test.expected), len(hook.MatchConditions))
   410  					for i, matchCondition := range hook.MatchConditions {
   411  						require.Equal(t, test.expected[i], matchCondition)
   412  					}
   413  				}
   414  			}
   415  		})
   416  	}
   417  }