-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypervisor_controller_test.go
More file actions
391 lines (347 loc) · 13.2 KB
/
hypervisor_controller_test.go
File metadata and controls
391 lines (347 loc) · 13.2 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, LibVirtVersion 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 (
"context"
"errors"
"time"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
"github.com/coreos/go-systemd/v22/dbus"
golibvirt "github.com/digitalocean/go-libvirt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/cobaltcore-dev/kvm-node-agent/internal/kernel"
"github.com/cobaltcore-dev/kvm-node-agent/internal/libvirt"
"github.com/cobaltcore-dev/kvm-node-agent/internal/sys"
"github.com/cobaltcore-dev/kvm-node-agent/internal/systemd"
)
var _ = Describe("Hypervisor Controller", func() {
Context("When testing Start method", func() {
It("should successfully start and subscribe to libvirt events", func() {
ctx := context.Background()
eventCallbackCalled := false
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Libvirt: &libvirt.InterfaceMock{
ConnectFunc: func() error {
return nil
},
WatchDomainChangesFunc: func(eventId golibvirt.DomainEventID, handlerId string, handler func(context.Context, any)) {
eventCallbackCalled = true
Expect(handlerId).To(Equal("reconcile-on-domain-lifecycle"))
},
},
reconcileCh: make(chan event.GenericEvent, 1),
}
err := controllerReconciler.Start(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(eventCallbackCalled).To(BeTrue())
})
It("should fail when libvirt connection fails", func() {
ctx := context.Background()
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Libvirt: &libvirt.InterfaceMock{
ConnectFunc: func() error {
return errors.New("connection failed")
},
},
reconcileCh: make(chan event.GenericEvent, 1),
}
err := controllerReconciler.Start(ctx)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("connection failed"))
})
})
Context("When testing triggerReconcile method", func() {
It("should send an event to reconcile channel", func() {
const testHostname = "test-host"
const testNamespace = "test-namespace"
// Override hostname and namespace for this test
originalHostname := sys.Hostname
originalNamespace := sys.Namespace
sys.Hostname = testHostname
sys.Namespace = testNamespace
defer func() {
sys.Hostname = originalHostname
sys.Namespace = originalNamespace
}()
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
reconcileCh: make(chan event.GenericEvent, 1),
}
// Trigger reconcile in a goroutine to avoid blocking
go controllerReconciler.triggerReconcile()
// Wait for the event with a timeout
select {
case evt := <-controllerReconciler.reconcileCh:
Expect(evt.Object).NotTo(BeNil())
hv, ok := evt.Object.(*kvmv1.Hypervisor)
Expect(ok).To(BeTrue())
Expect(hv.Name).To(Equal(testHostname))
Expect(hv.Namespace).To(Equal(testNamespace))
Expect(hv.Kind).To(Equal("Hypervisor"))
Expect(hv.APIVersion).To(Equal("kvm.cloud.sap/v1"))
case <-time.After(2 * time.Second):
Fail("timeout waiting for reconcile event")
}
})
})
Context("When testing SetupWithManager method", func() {
It("should successfully setup controller with manager and read kernel parameters", func() {
// Create a test manager
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: k8sClient.Scheme(),
})
Expect(err).NotTo(HaveOccurred())
// Use mock kernel reader
mockKernelReader := &kernel.InterfaceMock{
ReadParametersFunc: func() (*kernel.Parameters, error) {
return &kernel.Parameters{
CommandLine: "quiet splash console=ttyS0 intel_iommu=on",
}, nil
},
}
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Systemd: &systemd.InterfaceMock{
DescribeFunc: func(ctx context.Context) (*systemd.Descriptor, error) {
return &systemd.Descriptor{
OperatingSystemReleaseData: []string{
"PRETTY_NAME=\"Garden Linux 1877.8\"",
"GARDENLINUX_VERSION=1877.8",
},
KernelVersion: "6.1.0",
KernelRelease: "6.1.0-gardenlinux",
KernelName: "Linux",
HardwareVendor: "Test Vendor",
HardwareModel: "Test Model",
HardwareSerial: "TEST123",
FirmwareVersion: "1.0",
FirmwareVendor: "Test BIOS",
FirmwareDate: time.Now().UnixMicro(),
}, nil
},
},
KernelReader: mockKernelReader,
}
err = controllerReconciler.SetupWithManager(mgr)
Expect(err).NotTo(HaveOccurred())
Expect(controllerReconciler.reconcileCh).NotTo(BeNil())
Expect(controllerReconciler.osDescriptor).NotTo(BeNil())
Expect(controllerReconciler.osDescriptor.OperatingSystemReleaseData).To(HaveLen(2))
// Verify that kernel reader was called and parameters were stored
Expect(mockKernelReader.ReadParametersCalls()).To(HaveLen(1))
Expect(controllerReconciler.kernelParameters).NotTo(BeNil())
Expect(
controllerReconciler.kernelParameters.CommandLine,
).To(Equal("quiet splash console=ttyS0 intel_iommu=on"))
})
It("should fail when systemd Describe returns error", func() {
// Create a test manager
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: k8sClient.Scheme(),
})
Expect(err).NotTo(HaveOccurred())
mockKernelReader := &kernel.InterfaceMock{
ReadParametersFunc: func() (*kernel.Parameters, error) {
return &kernel.Parameters{CommandLine: "quiet splash"}, nil
},
}
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
KernelReader: mockKernelReader,
Systemd: &systemd.InterfaceMock{
DescribeFunc: func(ctx context.Context) (*systemd.Descriptor, error) {
return nil, errors.New("systemd describe failed")
},
},
}
err = controllerReconciler.SetupWithManager(mgr)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unable to get Systemd hostname describe()"))
Expect(err.Error()).To(ContainSubstring("systemd describe failed"))
})
It("should fail when kernel parameters cannot be read", func() {
// Create a test manager
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: k8sClient.Scheme(),
})
Expect(err).NotTo(HaveOccurred())
mockKernelReader := &kernel.InterfaceMock{
ReadParametersFunc: func() (*kernel.Parameters, error) {
return nil, errors.New("failed to read /proc/cmdline")
},
}
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
KernelReader: mockKernelReader,
Systemd: &systemd.InterfaceMock{
DescribeFunc: func(ctx context.Context) (*systemd.Descriptor, error) {
return &systemd.Descriptor{
KernelVersion: "6.1.0",
}, nil
},
},
}
err = controllerReconciler.SetupWithManager(mgr)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unable to read kernel parameters"))
Expect(err.Error()).To(ContainSubstring("failed to read /proc/cmdline"))
Expect(mockKernelReader.ReadParametersCalls()).To(HaveLen(1))
})
})
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
ctx := context.Background()
typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
hypervisor := &kvmv1.Hypervisor{}
BeforeEach(func() {
By("creating the custom resource for the Kind Hypervisor")
err := k8sClient.Get(ctx, typeNamespacedName, hypervisor)
if err != nil && apierrors.IsNotFound(err) {
resource := &kvmv1.Hypervisor{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
// override the hostname
sys.Hostname = resourceName
})
AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &kvmv1.Hypervisor{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())
By("Cleanup the specific resource instance Hypervisor")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource with kernel parameters", func() {
By("Reconciling the created resource")
controllerReconciler := &HypervisorReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Libvirt: &libvirt.InterfaceMock{
CloseFunc: func() error {
return nil
},
ConnectFunc: func() error {
return nil
},
ProcessFunc: func(hv kvmv1.Hypervisor) (kvmv1.Hypervisor, error) {
hv.Status.Instances = []kvmv1.Instance{
{
ID: "25e2ea06-f6be-4bac-856d-8c2d0bdbcdee",
Name: "test-instance",
Active: false,
},
}
hv.Status.LibVirtVersion = "10.9.0"
hv.Status.NumInstances = 1
hv.Status.Capabilities = kvmv1.Capabilities{
HostCpuArch: "x86_64",
HostCpus: *resource.NewQuantity(4, resource.DecimalSI),
HostMemory: *resource.NewQuantity(8192, resource.DecimalSI),
}
hv.Status.DomainCapabilities = kvmv1.DomainCapabilities{
Arch: "x86_64",
HypervisorType: "kvm",
SupportedCpuModes: []string{"mode/example", "mode/example/1"},
}
return hv, nil
},
},
Systemd: &systemd.InterfaceMock{
CloseFunc: func() {},
IsConnectedFunc: func() bool {
return true
},
ListUnitsByNamesFunc: func(ctx context.Context, units []string) ([]dbus.UnitStatus, error) {
return []dbus.UnitStatus{
{
Name: "test-unit",
Description: "test-unit-description",
ActiveState: "active",
},
}, nil
},
DescribeFunc: func(ctx context.Context) (*systemd.Descriptor, error) {
return nil, nil
},
},
osDescriptor: &systemd.Descriptor{
OperatingSystemReleaseData: []string{
"PRETTY_NAME=\"Garden Linux 1877.8\"",
"GARDENLINUX_VERSION=1877.8",
"GARDENLINUX_COMMIT_ID_LONG=abcdef1234567890",
"GARDENLINUX_FEATURES=_rescue,log,sap",
"VARIANT_ID=metal-sci_usi-amd64",
},
},
kernelParameters: &kernel.Parameters{
CommandLine: "quiet splash console=ttyS0 intel_iommu=on",
},
}
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
By("Checking the status of the reconciled resource")
err = k8sClient.Get(ctx, typeNamespacedName, hypervisor)
Expect(err).NotTo(HaveOccurred())
Expect(hypervisor.Status.Instances).To(HaveLen(1))
Expect(hypervisor.Status.Instances[0].ID).To(Equal("25e2ea06-f6be-4bac-856d-8c2d0bdbcdee"))
Expect(hypervisor.Status.Conditions).To(HaveLen(2))
Expect(hypervisor.Status.Conditions[0].Type).To(Equal("test-unit"))
Expect(hypervisor.Status.Conditions[0].Status).To(Equal(metav1.ConditionTrue))
Expect(hypervisor.Status.Conditions[0].Reason).To(Equal("Running"))
Expect(hypervisor.Status.Conditions[1].Type).To(Equal("LibVirtConnection"))
Expect(hypervisor.Status.Conditions[1].Status).To(Equal(metav1.ConditionTrue))
Expect(hypervisor.Status.Conditions[1].Reason).To(Equal("Connected"))
Expect(hypervisor.Status.Capabilities.HostCpuArch).To(Equal("x86_64"))
Expect(hypervisor.Status.Capabilities.HostCpus.AsDec().UnscaledBig()).
To(Equal(resource.NewQuantity(4, resource.DecimalSI).AsDec().UnscaledBig()))
Expect(hypervisor.Status.Capabilities.HostMemory.AsDec().UnscaledBig()).
To(Equal(resource.NewQuantity(8192, resource.DecimalSI).AsDec().UnscaledBig()))
Expect(hypervisor.Status.OperatingSystem.PrettyVersion).To(Equal("\"Garden Linux 1877.8\""))
Expect(hypervisor.Status.OperatingSystem.Version).To(Equal("1877.8"))
Expect(hypervisor.Status.OperatingSystem.GardenLinuxCommitID).To(Equal("abcdef1234567890"))
Expect(hypervisor.Status.OperatingSystem.GardenLinuxFeatures).To(Equal([]string{"_rescue", "log", "sap"}))
Expect(hypervisor.Status.OperatingSystem.VariantID).To(Equal("metal-sci_usi-amd64"))
Expect(
hypervisor.Status.OperatingSystem.KernelCommandLine,
).To(Equal("quiet splash console=ttyS0 intel_iommu=on"))
})
})
})