|
| 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 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + v1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + |
| 12 | + metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1" |
| 13 | + // TODO (user): Add any additional imports if needed |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("BIOSSettings Webhook", func() { |
| 17 | + var ( |
| 18 | + obj *metalv1alpha1.BIOSSettings |
| 19 | + oldObj *metalv1alpha1.BIOSSettings |
| 20 | + validator BIOSSettingsCustomValidator |
| 21 | + ) |
| 22 | + |
| 23 | + BeforeEach(func() { |
| 24 | + obj = &metalv1alpha1.BIOSSettings{} |
| 25 | + oldObj = &metalv1alpha1.BIOSSettings{} |
| 26 | + validator = BIOSSettingsCustomValidator{Client: k8sClient} |
| 27 | + Expect(validator).NotTo(BeNil(), "Expected validator to be initialized") |
| 28 | + Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized") |
| 29 | + Expect(obj).NotTo(BeNil(), "Expected obj to be initialized") |
| 30 | + }) |
| 31 | + |
| 32 | + AfterEach(func() { |
| 33 | + // TODO (user): Add any teardown logic common to all tests |
| 34 | + }) |
| 35 | + |
| 36 | + Context("When creating or updating BIOSSettings under Validating Webhook", func() { |
| 37 | + |
| 38 | + It("Should deny creation if a Spec.ServerRef field is duplicate", func(ctx SpecContext) { |
| 39 | + By("Creating an BIOSSetttings") |
| 40 | + biosSettingsV1 := &metalv1alpha1.BIOSSettings{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Namespace: "ns.Name", |
| 43 | + GenerateName: "test-", |
| 44 | + }, |
| 45 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 46 | + BIOSSettings: metalv1alpha1.Settings{Version: "P70 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 47 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 48 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 49 | + }, |
| 50 | + } |
| 51 | + Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed()) |
| 52 | + DeferCleanup(k8sClient.Delete, biosSettingsV1) |
| 53 | + |
| 54 | + By("Creating another BIOSSettings with existing ServerRef") |
| 55 | + biosSettingsV2 := &metalv1alpha1.BIOSSettings{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Namespace: "ns.Name", |
| 58 | + GenerateName: "test-", |
| 59 | + }, |
| 60 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 61 | + BIOSSettings: metalv1alpha1.Settings{Version: "P71 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 62 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 63 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 64 | + }, |
| 65 | + } |
| 66 | + Expect(validator.ValidateCreate(ctx, biosSettingsV2)).Error().To(HaveOccurred()) |
| 67 | + }) |
| 68 | + |
| 69 | + It("Should create if a Spec.ServerRef field is NOT duplicate", func() { |
| 70 | + By("Creating an BIOSSetttings") |
| 71 | + biosSettingsV1 := &metalv1alpha1.BIOSSettings{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Namespace: "ns.Name", |
| 74 | + GenerateName: "test-", |
| 75 | + }, |
| 76 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 77 | + BIOSSettings: metalv1alpha1.Settings{Version: "P70 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 78 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 79 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 80 | + }, |
| 81 | + } |
| 82 | + Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed()) |
| 83 | + DeferCleanup(k8sClient.Delete, biosSettingsV1) |
| 84 | + |
| 85 | + By("Creating another BIOSSetting with different ServerRef") |
| 86 | + biosSettingsV2 := &metalv1alpha1.BIOSSettings{ |
| 87 | + ObjectMeta: metav1.ObjectMeta{ |
| 88 | + Namespace: "ns.Name", |
| 89 | + GenerateName: "test-", |
| 90 | + }, |
| 91 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 92 | + BIOSSettings: metalv1alpha1.Settings{Version: "P71 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 93 | + ServerRef: &v1.LocalObjectReference{Name: "bar"}, |
| 94 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 95 | + }, |
| 96 | + } |
| 97 | + Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed()) |
| 98 | + DeferCleanup(k8sClient.Delete, biosSettingsV2) |
| 99 | + }) |
| 100 | + |
| 101 | + It("Should deny update if a Spec.ServerRef field is duplicate", func() { |
| 102 | + By("Creating an BIOSSetttings") |
| 103 | + biosSettingsV1 := &metalv1alpha1.BIOSSettings{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Namespace: "ns.Name", |
| 106 | + GenerateName: "test-", |
| 107 | + }, |
| 108 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 109 | + BIOSSettings: metalv1alpha1.Settings{Version: "P70 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 110 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 111 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 112 | + }, |
| 113 | + } |
| 114 | + Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed()) |
| 115 | + DeferCleanup(k8sClient.Delete, biosSettingsV1) |
| 116 | + |
| 117 | + By("Creating an BIOSSetting with different ServerRef") |
| 118 | + biosSettingsV2 := &metalv1alpha1.BIOSSettings{ |
| 119 | + ObjectMeta: metav1.ObjectMeta{ |
| 120 | + Namespace: "ns.Name", |
| 121 | + GenerateName: "test-", |
| 122 | + }, |
| 123 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 124 | + BIOSSettings: metalv1alpha1.Settings{Version: "P71 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 125 | + ServerRef: &v1.LocalObjectReference{Name: "bar"}, |
| 126 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 127 | + }, |
| 128 | + } |
| 129 | + Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed()) |
| 130 | + DeferCleanup(k8sClient.Delete, biosSettingsV2) |
| 131 | + |
| 132 | + By("Updating an biosSettingsV2 to conflicting Spec.ServerRef") |
| 133 | + biosSettingsV2Updated := biosSettingsV2.DeepCopy() |
| 134 | + biosSettingsV2Updated.Spec.ServerRef = &v1.LocalObjectReference{Name: "foo"} |
| 135 | + Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().To(HaveOccurred()) |
| 136 | + }) |
| 137 | + |
| 138 | + It("Should allow update if a different field is duplicate", func() { |
| 139 | + By("Creating an BIOSSetttings") |
| 140 | + biosSettingsV1 := &metalv1alpha1.BIOSSettings{ |
| 141 | + ObjectMeta: metav1.ObjectMeta{ |
| 142 | + Namespace: "ns.Name", |
| 143 | + GenerateName: "test-", |
| 144 | + }, |
| 145 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 146 | + BIOSSettings: metalv1alpha1.Settings{Version: "P70 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 147 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 148 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 149 | + }, |
| 150 | + } |
| 151 | + Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed()) |
| 152 | + DeferCleanup(k8sClient.Delete, biosSettingsV1) |
| 153 | + |
| 154 | + By("Creating an BIOSSetting with different ServerRef") |
| 155 | + biosSettingsV2 := &metalv1alpha1.BIOSSettings{ |
| 156 | + ObjectMeta: metav1.ObjectMeta{ |
| 157 | + Namespace: "ns.Name", |
| 158 | + GenerateName: "test-", |
| 159 | + }, |
| 160 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 161 | + BIOSSettings: metalv1alpha1.Settings{Version: "P71 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 162 | + ServerRef: &v1.LocalObjectReference{Name: "bar"}, |
| 163 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 164 | + }, |
| 165 | + } |
| 166 | + Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed()) |
| 167 | + DeferCleanup(k8sClient.Delete, biosSettingsV2) |
| 168 | + |
| 169 | + By("Updating an biosSettingsV2 to conflicting Spec.BIOSSettings") |
| 170 | + biosSettingsV2Updated := biosSettingsV2.DeepCopy() |
| 171 | + biosSettingsV2Updated.Spec.BIOSSettings = biosSettingsV1.Spec.BIOSSettings |
| 172 | + Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().ToNot(HaveOccurred()) |
| 173 | + }) |
| 174 | + |
| 175 | + It("Should allow update if a ServerRef field is NOT duplicate", func() { |
| 176 | + By("Creating an BIOSSetttings") |
| 177 | + biosSettingsV1 := &metalv1alpha1.BIOSSettings{ |
| 178 | + ObjectMeta: metav1.ObjectMeta{ |
| 179 | + Namespace: "ns.Name", |
| 180 | + GenerateName: "test-", |
| 181 | + }, |
| 182 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 183 | + BIOSSettings: metalv1alpha1.Settings{Version: "P70 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 184 | + ServerRef: &v1.LocalObjectReference{Name: "foo"}, |
| 185 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 186 | + }, |
| 187 | + } |
| 188 | + Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed()) |
| 189 | + DeferCleanup(k8sClient.Delete, biosSettingsV1) |
| 190 | + |
| 191 | + By("Creating an BIOSSetting with different ServerRef") |
| 192 | + biosSettingsV2 := &metalv1alpha1.BIOSSettings{ |
| 193 | + ObjectMeta: metav1.ObjectMeta{ |
| 194 | + Namespace: "ns.Name", |
| 195 | + GenerateName: "test-", |
| 196 | + }, |
| 197 | + Spec: metalv1alpha1.BIOSSettingsSpec{ |
| 198 | + BIOSSettings: metalv1alpha1.Settings{Version: "P71 v1.45 (12/06/2017)", SettingsMap: map[string]string{}}, |
| 199 | + ServerRef: &v1.LocalObjectReference{Name: "bar"}, |
| 200 | + ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced, |
| 201 | + }, |
| 202 | + } |
| 203 | + Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed()) |
| 204 | + DeferCleanup(k8sClient.Delete, biosSettingsV2) |
| 205 | + |
| 206 | + By("Updating an biosSettingsV2 to NON conflicting Spec.ServerRef ") |
| 207 | + biosSettingsV2Updated := biosSettingsV2.DeepCopy() |
| 208 | + biosSettingsV2Updated.Spec.ServerRef = &v1.LocalObjectReference{Name: "foobar"} |
| 209 | + Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().ToNot(HaveOccurred()) |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | +}) |
0 commit comments