|
| 1 | +// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1alpha1 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "net/netip" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + "github.com/ironcore-dev/network-operator/api/core/v1alpha1" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("Interface Webhook", func() { |
| 17 | + var ( |
| 18 | + obj *v1alpha1.Interface |
| 19 | + oldObj *v1alpha1.Interface |
| 20 | + validator InterfaceCustomValidator |
| 21 | + ) |
| 22 | + |
| 23 | + BeforeEach(func() { |
| 24 | + obj = &v1alpha1.Interface{ |
| 25 | + Spec: v1alpha1.InterfaceSpec{ |
| 26 | + DeviceRef: v1alpha1.LocalObjectReference{Name: "test-device"}, |
| 27 | + Name: "test-interface", |
| 28 | + AdminState: v1alpha1.AdminStateUp, |
| 29 | + Type: v1alpha1.InterfaceTypeLoopback, |
| 30 | + }, |
| 31 | + } |
| 32 | + oldObj = &v1alpha1.Interface{ |
| 33 | + Spec: v1alpha1.InterfaceSpec{ |
| 34 | + DeviceRef: v1alpha1.LocalObjectReference{Name: "test-device"}, |
| 35 | + Name: "test-interface", |
| 36 | + AdminState: v1alpha1.AdminStateUp, |
| 37 | + Type: v1alpha1.InterfaceTypeLoopback, |
| 38 | + }, |
| 39 | + } |
| 40 | + validator = InterfaceCustomValidator{} |
| 41 | + Expect(validator).NotTo(BeNil(), "Expected validator to be initialized") |
| 42 | + Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized") |
| 43 | + Expect(obj).NotTo(BeNil(), "Expected obj to be initialized") |
| 44 | + }) |
| 45 | + |
| 46 | + Context("When creating or updating Interfaces under Validating Webhook", func() { |
| 47 | + It("Should allow valid IPv4 addresses", func() { |
| 48 | + obj.Spec.IPv4 = &v1alpha1.InterfaceIPv4{ |
| 49 | + Addresses: []v1alpha1.IPPrefix{ |
| 50 | + {Prefix: netip.MustParsePrefix("10.0.0.1/32")}, |
| 51 | + {Prefix: netip.MustParsePrefix("10.0.1.1/32")}, |
| 52 | + }, |
| 53 | + } |
| 54 | + _, err := validator.ValidateCreate(context.Background(), obj) |
| 55 | + Expect(err).NotTo(HaveOccurred()) |
| 56 | + }) |
| 57 | + |
| 58 | + It("Should reject IPv6 addresses in IPv4 field", func() { |
| 59 | + obj.Spec.IPv4 = &v1alpha1.InterfaceIPv4{ |
| 60 | + Addresses: []v1alpha1.IPPrefix{ |
| 61 | + {Prefix: netip.MustParsePrefix("2001:db8::1/128")}, |
| 62 | + }, |
| 63 | + } |
| 64 | + _, err := validator.ValidateCreate(context.Background(), obj) |
| 65 | + Expect(err).To(HaveOccurred()) |
| 66 | + Expect(err.Error()).To(ContainSubstring("invalid IPv4 address")) |
| 67 | + Expect(err.Error()).To(ContainSubstring("address is IPv6")) |
| 68 | + }) |
| 69 | + |
| 70 | + It("Should reject overlapping IPv4 addresses", func() { |
| 71 | + obj.Spec.IPv4 = &v1alpha1.InterfaceIPv4{ |
| 72 | + Addresses: []v1alpha1.IPPrefix{ |
| 73 | + {Prefix: netip.MustParsePrefix("10.0.0.0/24")}, |
| 74 | + {Prefix: netip.MustParsePrefix("10.0.0.128/25")}, |
| 75 | + }, |
| 76 | + } |
| 77 | + _, err := validator.ValidateCreate(context.Background(), obj) |
| 78 | + Expect(err).To(HaveOccurred()) |
| 79 | + Expect(err.Error()).To(ContainSubstring("overlaps with")) |
| 80 | + }) |
| 81 | + |
| 82 | + It("Should reject updates with invalid IPv4 addresses", func() { |
| 83 | + obj.Spec.IPv4 = &v1alpha1.InterfaceIPv4{ |
| 84 | + Addresses: []v1alpha1.IPPrefix{ |
| 85 | + {Prefix: netip.MustParsePrefix("10.0.0.0/24")}, |
| 86 | + {Prefix: netip.MustParsePrefix("10.0.0.128/25")}, |
| 87 | + }, |
| 88 | + } |
| 89 | + _, err := validator.ValidateUpdate(context.Background(), oldObj, obj) |
| 90 | + Expect(err).To(HaveOccurred()) |
| 91 | + Expect(err.Error()).To(ContainSubstring("overlaps with")) |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments