...
1
16
17 package validatingwebhookconfiguration
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
24 "k8s.io/kubernetes/pkg/apis/admissionregistration"
25 )
26
27 func TestValidatingWebhookConfigurationStrategy(t *testing.T) {
28 ctx := genericapirequest.NewDefaultContext()
29 if Strategy.NamespaceScoped() {
30 t.Error("ValidatingWebhookConfiguration strategy must be cluster scoped")
31 }
32 if Strategy.AllowCreateOnUpdate() {
33 t.Errorf("ValidatingWebhookConfiguration should not allow create on update")
34 }
35
36 configuration := validValidatingWebhookConfiguration()
37 Strategy.PrepareForCreate(ctx, configuration)
38 errs := Strategy.Validate(ctx, configuration)
39 if len(errs) != 0 {
40 t.Errorf("Unexpected error validating %v", errs)
41 }
42 invalidConfiguration := &admissionregistration.ValidatingWebhookConfiguration{
43 ObjectMeta: metav1.ObjectMeta{Name: ""},
44 }
45 Strategy.PrepareForUpdate(ctx, invalidConfiguration, configuration)
46 errs = Strategy.ValidateUpdate(ctx, invalidConfiguration, configuration)
47 if len(errs) == 0 {
48 t.Errorf("Expected a validation error")
49 }
50 }
51 func validValidatingWebhookConfiguration() *admissionregistration.ValidatingWebhookConfiguration {
52 ignore := admissionregistration.Ignore
53 exact := admissionregistration.Exact
54 thirty := int32(30)
55 none := admissionregistration.SideEffectClassNone
56 servicePath := "/"
57 return &admissionregistration.ValidatingWebhookConfiguration{
58 ObjectMeta: metav1.ObjectMeta{
59 Name: "foo",
60 },
61 Webhooks: []admissionregistration.ValidatingWebhook{{
62 Name: "foo.example.io",
63 ClientConfig: admissionregistration.WebhookClientConfig{
64 Service: &admissionregistration.ServiceReference{
65 Name: "foo",
66 Namespace: "bar",
67 Path: &servicePath,
68 Port: 443,
69 },
70 },
71 FailurePolicy: &ignore,
72 MatchPolicy: &exact,
73 TimeoutSeconds: &thirty,
74 NamespaceSelector: &metav1.LabelSelector{},
75 ObjectSelector: &metav1.LabelSelector{},
76 SideEffects: &none,
77 AdmissionReviewVersions: []string{"v1beta1"},
78 }},
79 }
80 }
81
View as plain text