|
| 1 | +/* |
| 2 | +Copyright 2024 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 webhooks |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "runtime/debug" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/onsi/gomega/format" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/client-go/kubernetes/scheme" |
| 27 | + utilconversion "sigs.k8s.io/cluster-api/util/conversion" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 29 | + |
| 30 | + infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" |
| 31 | +) |
| 32 | + |
| 33 | +type pointerToObject[T any] interface { |
| 34 | + *T |
| 35 | + runtime.Object |
| 36 | +} |
| 37 | + |
| 38 | +// fuzzCustomValidator fuzzes a CustomValidator with objects of the validator's expected type. |
| 39 | +func fuzzCustomValidator[O any, PO pointerToObject[O]](t *testing.T, name string, validator webhook.CustomValidator) { |
| 40 | + t.Helper() |
| 41 | + fuzz := utilconversion.GetFuzzer(scheme.Scheme) |
| 42 | + ctx := context.TODO() |
| 43 | + |
| 44 | + t.Run(name, func(t *testing.T) { |
| 45 | + for i := 0; i < 1000; i++ { |
| 46 | + var previous PO = new(O) |
| 47 | + var dst PO = new(O) |
| 48 | + fuzz.Fuzz(previous) |
| 49 | + fuzz.Fuzz(dst) |
| 50 | + |
| 51 | + checkPanic := func(f func(), name string, args ...runtime.Object) { |
| 52 | + defer func() { |
| 53 | + if r := recover(); r != nil { |
| 54 | + t.Errorf("PANIC in %s", name) |
| 55 | + for i, arg := range args { |
| 56 | + t.Errorf("arg %d:\n%s", i, format.Object(arg, 1)) |
| 57 | + } |
| 58 | + t.Errorf("Stack trace:\n%s", debug.Stack()) |
| 59 | + t.FailNow() |
| 60 | + } |
| 61 | + }() |
| 62 | + f() |
| 63 | + } |
| 64 | + |
| 65 | + checkPanic(func() { |
| 66 | + _, _ = validator.ValidateCreate(ctx, dst) |
| 67 | + }, "ValidateCreate()", dst) |
| 68 | + checkPanic(func() { |
| 69 | + _, _ = validator.ValidateUpdate(ctx, previous, dst) |
| 70 | + }, "ValidateUpdate()", previous, dst) |
| 71 | + checkPanic(func() { |
| 72 | + _, _ = validator.ValidateDelete(ctx, previous) |
| 73 | + }, "ValidateDelete()", previous) |
| 74 | + } |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func Test_FuzzClusterWebhook(t *testing.T) { |
| 79 | + fuzzCustomValidator[infrav1.OpenStackCluster](t, "OpenStackCluster", &openStackClusterWebhook{}) |
| 80 | +} |
| 81 | + |
| 82 | +func Test_FuzzClusterTemplateWebhook(t *testing.T) { |
| 83 | + fuzzCustomValidator[infrav1.OpenStackClusterTemplate](t, "OpenStackClusterTemplate", &openStackClusterTemplateWebhook{}) |
| 84 | +} |
| 85 | + |
| 86 | +func Test_FuzzMachineWebhook(t *testing.T) { |
| 87 | + fuzzCustomValidator[infrav1.OpenStackMachine](t, "OpenStackMachine", &openStackMachineWebhook{}) |
| 88 | +} |
| 89 | + |
| 90 | +func Test_FuzzMachineTemplateWebhook(t *testing.T) { |
| 91 | + fuzzCustomValidator[infrav1.OpenStackMachineTemplate](t, "OpenStackMachineTemplate", &openStackMachineTemplateWebhook{}) |
| 92 | +} |
0 commit comments