-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbiossettings_webhook_test.go
More file actions
222 lines (199 loc) · 8.48 KB
/
biossettings_webhook_test.go
File metadata and controls
222 lines (199 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package v1alpha1
import (
"github.com/ironcore-dev/metal-operator/internal/controller"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
)
var _ = Describe("BIOSSettings Webhook", func() {
var (
biosSettingsV1 *metalv1alpha1.BIOSSettings
validator BIOSSettingsCustomValidator
defaultMockUpServerBiosVersion = "P79 v1.45 (12/06/2017)"
anotherMockUpServerBiosVersion = "P71 v1.45 (12/06/2017)"
)
BeforeEach(func(ctx SpecContext) {
validator = BIOSSettingsCustomValidator{Client: k8sClient}
By("Creating an BIOSSettings")
biosSettingsV1 = &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "foo"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: defaultMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(k8sClient.Create(ctx, biosSettingsV1)).To(Succeed())
})
AfterEach(func(ctx SpecContext) {
By("Deleting the BIOSSettings and Server resources")
Expect(k8sClient.DeleteAllOf(ctx, &metalv1alpha1.BIOSSettings{})).To(Succeed())
Expect(k8sClient.DeleteAllOf(ctx, &metalv1alpha1.Server{})).To(Succeed())
By("Ensuring clean state")
controller.EnsureCleanState()
})
It("Should deny creation if a Server already has a BIOSSettings", func(ctx SpecContext) {
By("Creating another BIOSSettings targeting the same Server")
biosSettingsV2 := &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "foo"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: defaultMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(validator.ValidateCreate(ctx, biosSettingsV2)).Error().To(HaveOccurred())
})
It("Should allow the creation for a Server without a BIOSSettings", func() {
By("Creating a BIOSSettings targeting a new Server")
biosSettingsV2 := &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "bar"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: defaultMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed())
})
It("Should deny update if spec.serverRef is duplicate", func() {
By("Creating a BIOSSettings with different ServerRef")
biosSettingsV2 := &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "bar"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: anotherMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed())
By("Updating an biosSettingsV2 with a conflicting ServerRef")
biosSettingsV2Updated := biosSettingsV2.DeepCopy()
biosSettingsV2Updated.Spec.ServerRef = &v1.LocalObjectReference{Name: "foo"}
Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().To(HaveOccurred())
})
It("Should allow update if a different field is duplicate", func() {
By("Creating an BIOSSetting with different ServerRef")
biosSettingsV2 := &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "bar"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: anotherMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed())
By("Updating an biosSettingsV2 with conflicting Spec.BIOSSettings")
biosSettingsV2Updated := biosSettingsV2.DeepCopy()
biosSettingsV2Updated.Spec.Version = biosSettingsV1.Spec.Version
Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().ToNot(HaveOccurred())
})
It("Should allow update if a ServerRef field is NOT duplicate", func() {
By("Creating an BIOSSetting with different ServerRef")
biosSettingsV2 := &metalv1alpha1.BIOSSettings{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
Spec: metalv1alpha1.BIOSSettingsSpec{
ServerRef: &v1.LocalObjectReference{Name: "bar"},
BIOSSettingsTemplate: metalv1alpha1.BIOSSettingsTemplate{
Version: anotherMockUpServerBiosVersion,
SettingsFlow: []metalv1alpha1.SettingsFlowItem{{
Settings: map[string]string{},
Priority: 1,
Name: "one",
}},
ServerMaintenancePolicy: metalv1alpha1.ServerMaintenancePolicyEnforced,
},
},
}
Expect(k8sClient.Create(ctx, biosSettingsV2)).To(Succeed())
By("Updating an biosSettingsV2 to a non conflicting ServerRef")
biosSettingsV2Updated := biosSettingsV2.DeepCopy()
biosSettingsV2Updated.Spec.ServerRef = &v1.LocalObjectReference{Name: "foobar"}
Expect(validator.ValidateUpdate(ctx, biosSettingsV2, biosSettingsV2Updated)).Error().ToNot(HaveOccurred())
})
It("Should no allow update of BIOSSettings which are in-progress, but should allow to forcefully deleting it", func() {
By("Patching the BIOSSettings V1 to InProgress state")
Eventually(UpdateStatus(biosSettingsV1, func() {
biosSettingsV1.Status.State = metalv1alpha1.BIOSSettingsStateInProgress
})).Should(Succeed())
By("Mocking a corresponding ServerMaintenance for the BIOSSettings V1")
Eventually(Update(biosSettingsV1, func() {
biosSettingsV1.Spec.ServerMaintenanceRef = &metalv1alpha1.ObjectReference{Name: "maintenance"}
})).Should(Succeed())
By("Denying the spec update of an in-progress BIOSSettings")
biosSettingsV1Updated := biosSettingsV1.DeepCopy()
biosSettingsV1Updated.Spec.SettingsFlow = []metalv1alpha1.SettingsFlowItem{{Priority: 1, Settings: map[string]string{"test": "value"}}}
Expect(validator.ValidateUpdate(ctx, biosSettingsV1, biosSettingsV1Updated)).Error().To(HaveOccurred())
By("Allowing the spec update of an in-progress BIOSSettings with force-update annotation")
biosSettingsV1Updated.Annotations = map[string]string{metalv1alpha1.OperationAnnotation: metalv1alpha1.OperationAnnotationForceUpdateInProgress}
Expect(validator.ValidateUpdate(ctx, biosSettingsV1, biosSettingsV1Updated)).Error().ToNot(HaveOccurred())
By("Ensuring the BIOSSettings V1 is back to Applied state")
Eventually(UpdateStatus(biosSettingsV1, func() {
biosSettingsV1.Status.State = metalv1alpha1.BIOSSettingsStateApplied
})).Should(Succeed())
})
It("Should deny deletion of an in-progress BIOSSettings", func() {
By("Patching the BIOSSettings V1 to InProgress state")
Eventually(UpdateStatus(biosSettingsV1, func() {
biosSettingsV1.Status.State = metalv1alpha1.BIOSSettingsStateInProgress
})).Should(Succeed())
By("Denying the deletion of an in-progress BIOSSettings")
Expect(k8sClient.Delete(ctx, biosSettingsV1)).To(Not(Succeed()))
By("Ensuring the BIOSSettings V1 is back to Applied state")
Eventually(UpdateStatus(biosSettingsV1, func() {
biosSettingsV1.Status.State = metalv1alpha1.BIOSSettingsStateApplied
})).Should(Succeed())
})
})