-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraits_controller_test.go
More file actions
343 lines (300 loc) · 12.5 KB
/
traits_controller_test.go
File metadata and controls
343 lines (300 loc) · 12.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
SPDX-FileCopyrightText: Copyright 2025 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"net/http"
"github.com/gophercloud/gophercloud/v2/testhelper"
"github.com/gophercloud/gophercloud/v2/testhelper/client"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
)
var _ = Describe("TraitsController", func() {
const (
TraitsBody = `
{
"resource_provider_generation": 1,
"traits": [
"CUSTOM_FOO",
"HW_CPU_X86_VMX"
]
}`
TraitsBodyUpdated = `
{
"resource_provider_generation": 2,
"traits": [
"CUSTOM_FOO",
"CUSTOM_BAR",
"HW_CPU_X86_VMX"
]
}`
)
var (
traitsController *TraitsController
fakeServer testhelper.FakeServer
hypervisorName = types.NamespacedName{Name: "hv-test"}
)
BeforeEach(func(ctx SpecContext) {
By("Setting up the OpenStack http mock server")
fakeServer = testhelper.SetupHTTP()
DeferCleanup(fakeServer.Teardown)
// Install default handler to fail unhandled requests
fakeServer.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Fail("Unhandled request to fake server: " + r.Method + " " + r.URL.Path)
})
By("Creating the TraitsController")
traitsController = &TraitsController{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
serviceClient: client.ServiceClient(fakeServer),
}
By("Creating a Hypervisor resource")
hypervisor := &kvmv1.Hypervisor{
ObjectMeta: metav1.ObjectMeta{
Name: hypervisorName.Name,
},
Spec: kvmv1.HypervisorSpec{
LifecycleEnabled: true,
CustomTraits: []string{"CUSTOM_FOO", "CUSTOM_BAR"},
},
}
Expect(k8sClient.Create(ctx, hypervisor)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, hypervisor)).To(Succeed())
})
})
Context("Reconcile after onboarding before decommissioning", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, TraitsBody)
Expect(err).NotTo(HaveOccurred())
})
// Mock resourceproviders.UpdateTraits
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
// parse request
Expect(r.Method).To(Equal("PUT"))
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
// verify request body
expectedBody := `{
"resource_provider_generation": 1,
"traits": [
"CUSTOM_BAR",
"CUSTOM_FOO",
"HW_CPU_X86_VMX"
]
}`
body := make([]byte, r.ContentLength)
_, err := r.Body.Read(body)
Expect(err == nil || err.Error() == "EOF").To(BeTrue())
Expect(string(body)).To(MatchJSON(expectedBody))
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = fmt.Fprint(w, TraitsBodyUpdated)
Expect(err).NotTo(HaveOccurred())
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: metav1.ConditionFalse,
Reason: "UnitTest",
})
hypervisor.Status.HypervisorID = "1234"
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should update traits and set status condition when traits differ", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
updated := &kvmv1.Hypervisor{}
Expect(traitsController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Traits).To(ContainElements("CUSTOM_FOO", "CUSTOM_BAR", "HW_CPU_X86_VMX"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated)).To(BeTrue())
})
})
Context("Reconcile before onboarding (no condition set)", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
// Mock resourceproviders.UpdateTraits
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
hypervisor.Status.HypervisorID = "1234"
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should not update traits", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
updated := &kvmv1.Hypervisor{}
Expect(traitsController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Traits).NotTo(ContainElements("CUSTOM_FOO", "CUSTOM_BAR", "HW_CPU_X86_VMX"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated)).To(BeFalse())
})
})
Context("Reconcile during onboarding Initial phase", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
// Mock resourceproviders.UpdateTraits
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: metav1.ConditionTrue,
Reason: kvmv1.ConditionReasonInitial,
})
hypervisor.Status.HypervisorID = "1234"
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should not update traits during Initial phase", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
updated := &kvmv1.Hypervisor{}
Expect(traitsController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Traits).NotTo(ContainElements("CUSTOM_FOO", "CUSTOM_BAR", "HW_CPU_X86_VMX"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated)).To(BeFalse())
})
})
Context("Reconcile during onboarding Handover phase", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, TraitsBody)
Expect(err).NotTo(HaveOccurred())
})
// Mock resourceproviders.UpdateTraits
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, TraitsBodyUpdated)
Expect(err).NotTo(HaveOccurred())
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: metav1.ConditionTrue,
Reason: kvmv1.ConditionReasonHandover,
})
hypervisor.Status.HypervisorID = "1234"
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should update traits during Handover phase", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
updated := &kvmv1.Hypervisor{}
Expect(traitsController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Traits).To(ContainElements("CUSTOM_FOO", "CUSTOM_BAR", "HW_CPU_X86_VMX"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated)).To(BeTrue())
})
})
Context("Reconcile when GetTraits returns an error", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits to return a server error
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, `{"error": "Internal Server Error"}`)
})
// UpdateTraits must not be called
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: metav1.ConditionFalse,
Reason: "UnitTest",
})
hypervisor.Status.HypervisorID = "1234"
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should return the GetTraits error", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).To(HaveOccurred())
})
})
Context("Reconcile when terminating", func() {
BeforeEach(func(ctx SpecContext) {
// Mock resourceproviders.GetTraits
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
// Mock resourceproviders.UpdateTraits
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Fail("should not be called")
})
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: metav1.ConditionFalse,
Reason: "UnitTest",
})
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeTerminating,
Status: metav1.ConditionTrue,
Reason: "UnitTest",
})
hypervisor.Status.Traits = []string{"CUSTOM_FOO", "HW_CPU_X86_VMX"}
hypervisor.Status.HypervisorID = "1234"
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})
It("should not update traits", func(ctx SpecContext) {
req := ctrl.Request{NamespacedName: hypervisorName}
_, err := traitsController.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
updated := &kvmv1.Hypervisor{}
Expect(traitsController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Traits).NotTo(ContainElements("CUSTOM_FOO", "CUSTOM_BAR", "HW_CPU_X86_VMX"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated)).To(BeFalse())
})
})
})