| 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 webhook |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"reflect" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
v1 "k8s.io/api/admissionregistration/v1" |
| 26 |
"sigs.k8s.io/randfill" |
| 27 |
) |
| 28 |
|
| 29 |
func TestMutatingWebhookAccessor(t *testing.T) { |
| 30 |
f := randfill.New() |
| 31 |
for i := 0; i < 100; i++ { |
| 32 |
t.Run(fmt.Sprintf("Run %d/100", i), func(t *testing.T) { |
| 33 |
orig := &v1.MutatingWebhook{} |
| 34 |
f.Fill(orig) |
| 35 |
|
| 36 |
// zero out any accessor type specific fields not included in the accessor |
| 37 |
orig.ReinvocationPolicy = nil |
| 38 |
|
| 39 |
uid := fmt.Sprintf("test.configuration.admission/%s/0", orig.Name) |
| 40 |
accessor := NewMutatingWebhookAccessor(uid, "test.configuration.admission", orig) |
| 41 |
if uid != accessor.GetUID() { |
| 42 |
t.Errorf("expected GetUID to return %s, but got %s", accessor.GetUID(), uid) |
| 43 |
} |
| 44 |
m, ok := accessor.GetMutatingWebhook() |
| 45 |
if !ok { |
| 46 |
t.Errorf("expected GetMutatingWebhook to return ok for mutating webhook accessor") |
| 47 |
} |
| 48 |
if !reflect.DeepEqual(orig, m) { |
| 49 |
t.Errorf("expected GetMutatingWebhook to return original webhook, diff:\n%s", cmp.Diff(orig, m)) |
| 50 |
} |
| 51 |
if _, ok := accessor.GetValidatingWebhook(); ok { |
| 52 |
t.Errorf("expected GetValidatingWebhook to be nil for mutating webhook accessor") |
| 53 |
} |
| 54 |
copy := &v1.MutatingWebhook{ |
| 55 |
Name: accessor.GetName(), |
| 56 |
ClientConfig: accessor.GetClientConfig(), |
| 57 |
Rules: accessor.GetRules(), |
| 58 |
FailurePolicy: accessor.GetFailurePolicy(), |
| 59 |
MatchPolicy: accessor.GetMatchPolicy(), |
| 60 |
NamespaceSelector: accessor.GetNamespaceSelector(), |
| 61 |
ObjectSelector: accessor.GetObjectSelector(), |
| 62 |
SideEffects: accessor.GetSideEffects(), |
| 63 |
TimeoutSeconds: accessor.GetTimeoutSeconds(), |
| 64 |
AdmissionReviewVersions: accessor.GetAdmissionReviewVersions(), |
| 65 |
MatchConditions: accessor.GetMatchConditions(), |
| 66 |
} |
| 67 |
if !reflect.DeepEqual(orig, copy) { |
| 68 |
t.Errorf("expected mutatingWebhook to round trip through WebhookAccessor, diff:\n%s", cmp.Diff(orig, copy)) |
| 69 |
} |
| 70 |
}) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestValidatingWebhookAccessor(t *testing.T) { |
| 75 |
f := randfill.New() |
| 76 |
for i := 0; i < 100; i++ { |
| 77 |
t.Run(fmt.Sprintf("Run %d/100", i), func(t *testing.T) { |
| 78 |
orig := &v1.ValidatingWebhook{} |
| 79 |
f.Fill(orig) |
| 80 |
uid := fmt.Sprintf("test.configuration.admission/%s/0", orig.Name) |
| 81 |
accessor := NewValidatingWebhookAccessor(uid, "test.configuration.admission", orig) |
| 82 |
if uid != accessor.GetUID() { |
| 83 |
t.Errorf("expected GetUID to return %s, but got %s", accessor.GetUID(), uid) |
| 84 |
} |
| 85 |
m, ok := accessor.GetValidatingWebhook() |
| 86 |
if !ok { |
| 87 |
t.Errorf("expected GetValidatingWebhook to return ok for validating webhook accessor") |
| 88 |
} |
| 89 |
if !reflect.DeepEqual(orig, m) { |
| 90 |
t.Errorf("expected GetValidatingWebhook to return original webhook, diff:\n%s", cmp.Diff(orig, m)) |
| 91 |
} |
| 92 |
if _, ok := accessor.GetMutatingWebhook(); ok { |
| 93 |
t.Errorf("expected GetMutatingWebhook to be nil for validating webhook accessor") |
| 94 |
} |
| 95 |
copy := &v1.ValidatingWebhook{ |
| 96 |
Name: accessor.GetName(), |
| 97 |
ClientConfig: accessor.GetClientConfig(), |
| 98 |
Rules: accessor.GetRules(), |
| 99 |
FailurePolicy: accessor.GetFailurePolicy(), |
| 100 |
MatchPolicy: accessor.GetMatchPolicy(), |
| 101 |
NamespaceSelector: accessor.GetNamespaceSelector(), |
| 102 |
ObjectSelector: accessor.GetObjectSelector(), |
| 103 |
SideEffects: accessor.GetSideEffects(), |
| 104 |
TimeoutSeconds: accessor.GetTimeoutSeconds(), |
| 105 |
AdmissionReviewVersions: accessor.GetAdmissionReviewVersions(), |
| 106 |
MatchConditions: accessor.GetMatchConditions(), |
| 107 |
} |
| 108 |
if !reflect.DeepEqual(orig, copy) { |
| 109 |
t.Errorf("expected validatingWebhook to round trip through WebhookAccessor, diff:\n%s", cmp.Diff(orig, copy)) |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
} |