Debian Codegraph @

pkg/server/options/server_run_options_test.go

raw
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 options
18
19 import (
20 "fmt"
21 "strings"
22 "testing"
23 "time"
24
25 utilerrors "k8s.io/apimachinery/pkg/util/errors"
26 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
27 "k8s.io/apimachinery/pkg/util/version"
28 utilfeature "k8s.io/apiserver/pkg/util/feature"
29 basecompatibility "k8s.io/component-base/compatibility"
30 netutils "k8s.io/utils/net"
31 )
32
33 func TestServerRunOptionsValidate(t *testing.T) {
34 testComponent := "test"
35 testCases := []struct {
36 name string
37 testOptions *ServerRunOptions
38 emulationVersion string
39 expectErr string
40 }{
41 {
42 name: "Test when MaxRequestsInFlight is negative value",
43 testOptions: &ServerRunOptions{
44 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
45 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
46 MaxRequestsInFlight: -400,
47 MaxMutatingRequestsInFlight: 200,
48 RequestTimeout: time.Duration(2) * time.Minute,
49 MinRequestTimeout: 1800,
50 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
51 MaxRequestBodyBytes: 10 * 1024 * 1024,
52 ComponentGlobalsRegistry: newTestRegistry(testComponent),
53 ComponentName: testComponent,
54 },
55 expectErr: "--max-requests-inflight can not be negative value",
56 },
57 {
58 name: "Test when MaxMutatingRequestsInFlight is negative value",
59 testOptions: &ServerRunOptions{
60 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
61 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
62 MaxRequestsInFlight: 400,
63 MaxMutatingRequestsInFlight: -200,
64 RequestTimeout: time.Duration(2) * time.Minute,
65 MinRequestTimeout: 1800,
66 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
67 MaxRequestBodyBytes: 10 * 1024 * 1024,
68 ComponentGlobalsRegistry: newTestRegistry(testComponent),
69 ComponentName: testComponent,
70 },
71 expectErr: "--max-mutating-requests-inflight can not be negative value",
72 },
73 {
74 name: "Test when RequestTimeout is negative value",
75 testOptions: &ServerRunOptions{
76 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
77 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
78 MaxRequestsInFlight: 400,
79 MaxMutatingRequestsInFlight: 200,
80 RequestTimeout: -time.Duration(2) * time.Minute,
81 MinRequestTimeout: 1800,
82 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
83 MaxRequestBodyBytes: 10 * 1024 * 1024,
84 ComponentGlobalsRegistry: newTestRegistry(testComponent),
85 ComponentName: testComponent,
86 },
87 expectErr: "--request-timeout can not be negative value",
88 },
89 {
90 name: "Test when MinRequestTimeout is negative value",
91 testOptions: &ServerRunOptions{
92 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
93 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
94 MaxRequestsInFlight: 400,
95 MaxMutatingRequestsInFlight: 200,
96 RequestTimeout: time.Duration(2) * time.Minute,
97 MinRequestTimeout: -1800,
98 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
99 MaxRequestBodyBytes: 10 * 1024 * 1024,
100 ComponentGlobalsRegistry: newTestRegistry(testComponent),
101 ComponentName: testComponent,
102 },
103 expectErr: "--min-request-timeout can not be negative value",
104 },
105 {
106 name: "Test when JSONPatchMaxCopyBytes is negative value",
107 testOptions: &ServerRunOptions{
108 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
109 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
110 MaxRequestsInFlight: 400,
111 MaxMutatingRequestsInFlight: 200,
112 RequestTimeout: time.Duration(2) * time.Minute,
113 MinRequestTimeout: 1800,
114 JSONPatchMaxCopyBytes: -10 * 1024 * 1024,
115 MaxRequestBodyBytes: 10 * 1024 * 1024,
116 ComponentGlobalsRegistry: newTestRegistry(testComponent),
117 ComponentName: testComponent,
118 },
119 expectErr: "ServerRunOptions.JSONPatchMaxCopyBytes can not be negative value",
120 },
121 {
122 name: "Test when MaxRequestBodyBytes is negative value",
123 testOptions: &ServerRunOptions{
124 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
125 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
126 MaxRequestsInFlight: 400,
127 MaxMutatingRequestsInFlight: 200,
128 RequestTimeout: time.Duration(2) * time.Minute,
129 MinRequestTimeout: 1800,
130 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
131 MaxRequestBodyBytes: -10 * 1024 * 1024,
132 ComponentGlobalsRegistry: newTestRegistry(testComponent),
133 ComponentName: testComponent,
134 },
135 expectErr: "ServerRunOptions.MaxRequestBodyBytes can not be negative value",
136 },
137 {
138 name: "Test when LivezGracePeriod is negative value",
139 testOptions: &ServerRunOptions{
140 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
141 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
142 MaxRequestsInFlight: 400,
143 MaxMutatingRequestsInFlight: 200,
144 RequestTimeout: time.Duration(2) * time.Minute,
145 MinRequestTimeout: 1800,
146 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
147 MaxRequestBodyBytes: 10 * 1024 * 1024,
148 LivezGracePeriod: -time.Second,
149 ComponentGlobalsRegistry: newTestRegistry(testComponent),
150 ComponentName: testComponent,
151 },
152 expectErr: "--livez-grace-period can not be a negative value",
153 },
154 {
155 name: "Test when MinimalShutdownDuration is negative value",
156 testOptions: &ServerRunOptions{
157 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
158 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
159 MaxRequestsInFlight: 400,
160 MaxMutatingRequestsInFlight: 200,
161 RequestTimeout: time.Duration(2) * time.Minute,
162 MinRequestTimeout: 1800,
163 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
164 MaxRequestBodyBytes: 10 * 1024 * 1024,
165 ShutdownDelayDuration: -time.Second,
166 ComponentGlobalsRegistry: newTestRegistry(testComponent),
167 ComponentName: testComponent,
168 },
169 expectErr: "--shutdown-delay-duration can not be negative value",
170 },
171 {
172 name: "Test when HSTSHeaders is valid",
173 testOptions: &ServerRunOptions{
174 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
175 CorsAllowedOriginList: []string{"10.10.10.100", "10.10.10.200"},
176 HSTSDirectives: []string{"fakevalue", "includeSubDomains", "preload"},
177 MaxRequestsInFlight: 400,
178 MaxMutatingRequestsInFlight: 200,
179 RequestTimeout: time.Duration(2) * time.Minute,
180 MinRequestTimeout: 1800,
181 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
182 MaxRequestBodyBytes: 10 * 1024 * 1024,
183 ComponentGlobalsRegistry: newTestRegistry(testComponent),
184 ComponentName: testComponent,
185 },
186 expectErr: "--strict-transport-security-directives invalid, allowed values: max-age=expireTime, includeSubDomains, preload. see https://tools.ietf.org/html/rfc6797#section-6.1 for more information",
187 },
188 {
189 name: "Test when emulation version is invalid",
190 testOptions: &ServerRunOptions{
191 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
192 CorsAllowedOriginList: []string{"^10.10.10.100$", "^10.10.10.200$"},
193 HSTSDirectives: []string{"max-age=31536000", "includeSubDomains", "preload"},
194 MaxRequestsInFlight: 400,
195 MaxMutatingRequestsInFlight: 200,
196 RequestTimeout: time.Duration(2) * time.Minute,
197 MinRequestTimeout: 1800,
198 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
199 MaxRequestBodyBytes: 10 * 1024 * 1024,
200 ComponentName: testComponent,
201 ComponentGlobalsRegistry: newTestRegistry(testComponent),
202 },
203 emulationVersion: "1.31",
204 expectErr: "emulation version 1.31 is not between [1.32, 1.35.0]",
205 },
206 {
207 name: "Test EmulationForwardCompatible cannot be true if not in emulation mode",
208 testOptions: &ServerRunOptions{
209 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
210 CorsAllowedOriginList: []string{"^10.10.10.100$", "^10.10.10.200$"},
211 HSTSDirectives: []string{"max-age=31536000", "includeSubDomains", "preload"},
212 MaxRequestsInFlight: 400,
213 MaxMutatingRequestsInFlight: 200,
214 RequestTimeout: time.Duration(2) * time.Minute,
215 MinRequestTimeout: 1800,
216 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
217 MaxRequestBodyBytes: 10 * 1024 * 1024,
218 ComponentGlobalsRegistry: newTestRegistry(testComponent),
219 ComponentName: testComponent,
220 EmulationForwardCompatible: true,
221 },
222 expectErr: "ServerRunOptions.EmulationForwardCompatible cannot be set to true if the emulation version is the same as the binary version",
223 },
224 {
225 name: "Test RuntimeConfigEmulationForwardCompatible cannot be true if not in emulation mode",
226 testOptions: &ServerRunOptions{
227 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
228 CorsAllowedOriginList: []string{"^10.10.10.100$", "^10.10.10.200$"},
229 HSTSDirectives: []string{"max-age=31536000", "includeSubDomains", "preload"},
230 MaxRequestsInFlight: 400,
231 MaxMutatingRequestsInFlight: 200,
232 RequestTimeout: time.Duration(2) * time.Minute,
233 MinRequestTimeout: 1800,
234 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
235 MaxRequestBodyBytes: 10 * 1024 * 1024,
236 ComponentGlobalsRegistry: newTestRegistry(testComponent),
237 ComponentName: testComponent,
238 RuntimeConfigEmulationForwardCompatible: true,
239 },
240 expectErr: "ServerRunOptions.RuntimeConfigEmulationForwardCompatible cannot be set to true if the emulation version is the same as the binary version",
241 },
242 {
243 name: "Test EmulationForwardCompatible can be true if in emulation mode",
244 testOptions: &ServerRunOptions{
245 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
246 CorsAllowedOriginList: []string{"^10.10.10.100$", "^10.10.10.200$"},
247 HSTSDirectives: []string{"max-age=31536000", "includeSubDomains", "preload"},
248 MaxRequestsInFlight: 400,
249 MaxMutatingRequestsInFlight: 200,
250 RequestTimeout: time.Duration(2) * time.Minute,
251 MinRequestTimeout: 1800,
252 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
253 MaxRequestBodyBytes: 10 * 1024 * 1024,
254 ComponentGlobalsRegistry: newTestRegistry(testComponent),
255 ComponentName: testComponent,
256 EmulationForwardCompatible: true,
257 RuntimeConfigEmulationForwardCompatible: true,
258 },
259 emulationVersion: "1.34",
260 },
261 {
262 name: "Test when ServerRunOptions is valid",
263 testOptions: &ServerRunOptions{
264 AdvertiseAddress: netutils.ParseIPSloppy("192.168.10.10"),
265 CorsAllowedOriginList: []string{"^10.10.10.100$", "^10.10.10.200$"},
266 HSTSDirectives: []string{"max-age=31536000", "includeSubDomains", "preload"},
267 MaxRequestsInFlight: 400,
268 MaxMutatingRequestsInFlight: 200,
269 RequestTimeout: time.Duration(2) * time.Minute,
270 MinRequestTimeout: 1800,
271 JSONPatchMaxCopyBytes: 10 * 1024 * 1024,
272 MaxRequestBodyBytes: 10 * 1024 * 1024,
273 ComponentGlobalsRegistry: newTestRegistry(testComponent),
274 ComponentName: testComponent,
275 },
276 },
277 }
278
279 for _, testcase := range testCases {
280 t.Run(testcase.name, func(t *testing.T) {
281 if testcase.emulationVersion != "" {
282 effectiveVersion := testcase.testOptions.ComponentGlobalsRegistry.EffectiveVersionFor(testcase.testOptions.ComponentName)
283 effectiveVersion.(basecompatibility.MutableEffectiveVersion).SetEmulationVersion(version.MustParse(testcase.emulationVersion))
284 }
285 errs := testcase.testOptions.Validate()
286 if len(testcase.expectErr) != 0 && !strings.Contains(utilerrors.NewAggregate(errs).Error(), testcase.expectErr) {
287 t.Errorf("got err: %v, expected err: %s", errs, testcase.expectErr)
288 }
289
290 if len(testcase.expectErr) == 0 && len(errs) != 0 {
291 t.Errorf("got err: %s, expected err nil", errs)
292 }
293 })
294 }
295 }
296
297 func newTestRegistry(componentName string) basecompatibility.ComponentGlobalsRegistry {
298 registry := basecompatibility.NewComponentGlobalsRegistry()
299 featureGate := utilfeature.DefaultFeatureGate.DeepCopy()
300 effectiveVersion := basecompatibility.NewEffectiveVersionFromString("1.35", "1.32", "1.32")
301 utilruntime.Must(registry.Register(componentName, effectiveVersion, featureGate))
302 return registry
303 }
304
305 func TestValidateCorsAllowedOriginList(t *testing.T) {
306 tests := []struct {
307 regexp [][]string
308 errShouldContain string
309 }{
310 {
311 regexp: [][]string{
312 {}, // empty list, the cluster operator wants to disable CORS
313 {`^http://foo.com$`},
314 {`^http://foo.com`}, // valid, because we relaxed the validation
315 {`://foo.com$`},
316 {`//foo.com$`},
317 {`^http://foo.com(:|$)`},
318 {`://foo.com(:|$)`},
319 {`//foo.com(:|$)`},
320 {`(^foo.com$)`},
321 {`^http://foo.com$`, `//bar.com(:|$)`},
322 },
323 errShouldContain: "",
324 },
325 {
326 // empty string, indicates that the cluster operator
327 // specified --cors-allowed-origins=""
328 regexp: [][]string{
329 {`^http://foo.com$`, ``},
330 },
331 errShouldContain: "empty value in --cors-allowed-origins",
332 },
333 {
334 regexp: [][]string{
335 {`^foo.com`},
336 {`//foo.com`},
337 {`foo.com$`},
338 {`foo.com(:|$)`},
339 },
340 errShouldContain: "regular expression does not pin to start/end of host in the origin header",
341 },
342 {
343 regexp: [][]string{
344 {`^http://foo.com$`, `^foo.com`}, // one good followed by a bad one
345 },
346 errShouldContain: "regular expression does not pin to start/end of host in the origin header",
347 },
348 }
349
350 for _, test := range tests {
351 for _, regexp := range test.regexp {
352 t.Run(fmt.Sprintf("regexp/%s", regexp), func(t *testing.T) {
353 options := NewServerRunOptions()
354 if errs := options.Validate(); len(errs) != 0 {
355 t.Fatalf("wrong test setup: %#v", errs)
356 }
357
358 options.CorsAllowedOriginList = regexp
359 errsGot := options.Validate()
360 switch {
361 case len(test.errShouldContain) == 0:
362 if len(errsGot) != 0 {
363 t.Errorf("expected no error, but got: %v", errsGot)
364 }
365 default:
366 if len(errsGot) == 0 ||
367 !strings.Contains(utilerrors.NewAggregate(errsGot).Error(), test.errShouldContain) {
368 t.Errorf("expected error to contain: %s, but got: %v", test.errShouldContain, errsGot)
369 }
370 }
371 })
372 }
373 }
374 }
375
376 func TestServerRunOptionsWithShutdownWatchTerminationGracePeriod(t *testing.T) {
377 tests := []struct {
378 name string
379 optionsFn func() *ServerRunOptions
380 errShouldContain string
381 }{
382 {
383 name: "default should be valid",
384 optionsFn: func() *ServerRunOptions {
385 return NewServerRunOptions()
386 },
387 },
388 {
389 name: "negative not allowed",
390 optionsFn: func() *ServerRunOptions {
391 o := NewServerRunOptions()
392 o.ShutdownWatchTerminationGracePeriod = -time.Second
393 return o
394 },
395 errShouldContain: "shutdown-watch-termination-grace-period, if provided, can not be a negative value",
396 },
397 }
398
399 for _, test := range tests {
400 t.Run(test.name, func(t *testing.T) {
401 options := test.optionsFn()
402 errsGot := options.Validate()
403 switch {
404 case len(test.errShouldContain) == 0:
405 if len(errsGot) != 0 {
406 t.Errorf("expected no error, but got: %v", errsGot)
407 }
408 default:
409 if len(errsGot) == 0 ||
410 !strings.Contains(utilerrors.NewAggregate(errsGot).Error(), test.errShouldContain) {
411 t.Errorf("expected error to contain: %s, but got: %v", test.errShouldContain, errsGot)
412 }
413 }
414 })
415 }
416
417 t.Run("default should be zero", func(t *testing.T) {
418 options := NewServerRunOptions()
419 if options.ShutdownWatchTerminationGracePeriod != time.Duration(0) {
420 t.Errorf("expected default of ShutdownWatchTerminationGracePeriod to be zero, but got: %s", options.ShutdownWatchTerminationGracePeriod)
421 }
422 })
423 }