|
| 1 | + |
1 | 2 | /*
|
2 | 3 | Copyright 2019 The Kubernetes Authors.
|
3 | 4 |
|
@@ -30,6 +31,13 @@ import (
|
30 | 31 | "k8s.io/apiserver/pkg/admission"
|
31 | 32 | "k8s.io/apiserver/pkg/authentication/user"
|
32 | 33 | "k8s.io/kubernetes/pkg/apis/core"
|
| 34 | + "k8s.io/client-go/kubernetes/fake" |
| 35 | + api "k8s.io/kubernetes/pkg/apis/core" |
| 36 | + "k8s.io/client-go/informers" |
| 37 | + "k8s.io/client-go/kubernetes" |
| 38 | + "k8s.io/component-base/featuregate" |
| 39 | + "k8s.io/kubernetes/pkg/controller" |
| 40 | + "k8s.io/kubernetes/pkg/features" |
33 | 41 |
|
34 | 42 | "github.com/stretchr/testify/assert"
|
35 | 43 | )
|
@@ -315,6 +323,151 @@ func NewObjectInterfacesForTest() admission.ObjectInterfaces {
|
315 | 323 | return admission.NewObjectInterfacesFromScheme(scheme)
|
316 | 324 | }
|
317 | 325 |
|
| 326 | +func newRuntimeClassForTest(runtimeClassEnabled bool, |
| 327 | + featureInspection bool, |
| 328 | + addLister bool, |
| 329 | + listerObject *v1beta1.RuntimeClass, |
| 330 | + addClient bool, |
| 331 | + clientObject *v1beta1.RuntimeClass) *RuntimeClass { |
| 332 | + runtimeClass := NewRuntimeClass() |
| 333 | + |
| 334 | + if featureInspection { |
| 335 | + relevantFeatures := map[featuregate.Feature]featuregate.FeatureSpec{ |
| 336 | + features.RuntimeClass: {Default: runtimeClassEnabled}, |
| 337 | + features.PodOverhead: {Default: false}, |
| 338 | + } |
| 339 | + fg := featuregate.NewFeatureGate() |
| 340 | + fg.Add(relevantFeatures) |
| 341 | + runtimeClass.InspectFeatureGates(fg) |
| 342 | + } |
| 343 | + |
| 344 | + if addLister { |
| 345 | + informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc()) |
| 346 | + runtimeClass.SetExternalKubeInformerFactory(informerFactory) |
| 347 | + if listerObject != nil { |
| 348 | + informerFactory.Node().V1beta1().RuntimeClasses().Informer().GetStore().Add(listerObject) |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + if addClient { |
| 353 | + var client kubernetes.Interface |
| 354 | + if clientObject != nil { |
| 355 | + client = fake.NewSimpleClientset(clientObject) |
| 356 | + } else { |
| 357 | + client = fake.NewSimpleClientset() |
| 358 | + } |
| 359 | + runtimeClass.SetExternalKubeClientSet(client) |
| 360 | + } |
| 361 | + |
| 362 | + return runtimeClass |
| 363 | +} |
| 364 | + |
| 365 | +func TestValidateInitialization(t *testing.T) { |
| 366 | + tests := []struct { |
| 367 | + name string |
| 368 | + expectError bool |
| 369 | + runtimeClass *RuntimeClass |
| 370 | + }{ |
| 371 | + { |
| 372 | + name: "runtimeClass disabled, success", |
| 373 | + expectError: false, |
| 374 | + runtimeClass: newRuntimeClassForTest(false, true, true, nil, true, nil), |
| 375 | + }, |
| 376 | + { |
| 377 | + name: "runtimeClass enabled, success", |
| 378 | + expectError: false, |
| 379 | + runtimeClass: newRuntimeClassForTest(true, true, true, nil, true, nil), |
| 380 | + }, |
| 381 | + { |
| 382 | + name: "runtimeClass enabled, no feature inspection", |
| 383 | + expectError: true, |
| 384 | + runtimeClass: newRuntimeClassForTest(true, false, true, nil, true, nil), |
| 385 | + }, |
| 386 | + { |
| 387 | + name: "runtimeClass enabled, no lister", |
| 388 | + expectError: true, |
| 389 | + runtimeClass: newRuntimeClassForTest(true, true, false, nil, true, nil,), |
| 390 | + }, |
| 391 | + { |
| 392 | + name: "runtimeClass enabled, no client", |
| 393 | + expectError: true, |
| 394 | + runtimeClass: newRuntimeClassForTest(true, true, true, nil, false, nil), |
| 395 | + }, |
| 396 | + } |
| 397 | + |
| 398 | + for _, tc := range tests { |
| 399 | + t.Run(tc.name, func(t *testing.T) { |
| 400 | + err := tc.runtimeClass.ValidateInitialization() |
| 401 | + if tc.expectError { |
| 402 | + assert.NotEmpty(t, err) |
| 403 | + } else { |
| 404 | + assert.Empty(t, err) |
| 405 | + } |
| 406 | + }) |
| 407 | + } |
| 408 | +} |
| 409 | + |
| 410 | +func TestAdmit(t *testing.T) { |
| 411 | + runtimeClassName := "runtimeClassName" |
| 412 | + |
| 413 | + rc := &v1beta1.RuntimeClass{ |
| 414 | + ObjectMeta: metav1.ObjectMeta{Name: runtimeClassName}, |
| 415 | + } |
| 416 | + |
| 417 | + pod := api.Pod{ |
| 418 | + ObjectMeta: metav1.ObjectMeta{Name: "podname"}, |
| 419 | + Spec: api.PodSpec{ |
| 420 | + RuntimeClassName: &runtimeClassName, |
| 421 | + }, |
| 422 | + } |
| 423 | + |
| 424 | + attributes := admission.NewAttributesRecord(&pod, |
| 425 | + nil, |
| 426 | + api.Kind("kind").WithVersion("version"), |
| 427 | + "", |
| 428 | + "", |
| 429 | + api.Resource("pods").WithVersion("version"), |
| 430 | + "", |
| 431 | + admission.Create, |
| 432 | + nil, |
| 433 | + false, |
| 434 | + nil) |
| 435 | + |
| 436 | + |
| 437 | + tests := []struct { |
| 438 | + name string |
| 439 | + expectError bool |
| 440 | + runtimeClass *RuntimeClass |
| 441 | + }{ |
| 442 | + { |
| 443 | + name: "runtimeClass found by lister", |
| 444 | + expectError: false, |
| 445 | + runtimeClass: newRuntimeClassForTest(true, true, true, rc, true, nil), |
| 446 | + }, |
| 447 | + { |
| 448 | + name: "runtimeClass found by client", |
| 449 | + expectError: false, |
| 450 | + runtimeClass: newRuntimeClassForTest(true, true, true, nil, true, rc), |
| 451 | + }, |
| 452 | + { |
| 453 | + name: "runtimeClass not found by lister nor client", |
| 454 | + expectError: true, |
| 455 | + runtimeClass: newRuntimeClassForTest(true, true, true, nil, true, nil), |
| 456 | + }, |
| 457 | + } |
| 458 | + |
| 459 | + for _, tc := range tests { |
| 460 | + t.Run(tc.name, func(t *testing.T) { |
| 461 | + err := tc.runtimeClass.Admit(context.TODO(), attributes, nil) |
| 462 | + if tc.expectError { |
| 463 | + assert.NotEmpty(t, err) |
| 464 | + } else { |
| 465 | + assert.Empty(t, err) |
| 466 | + } |
| 467 | + }) |
| 468 | + } |
| 469 | +} |
| 470 | + |
318 | 471 | func TestValidate(t *testing.T) {
|
319 | 472 | tests := []struct {
|
320 | 473 | name string
|
|
0 commit comments