Skip to content

Commit 97e9908

Browse files
Check that failing libvirt connection is reflected
1 parent e338118 commit 97e9908

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

internal/controller/hypervisor_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,14 @@ func (r *HypervisorReconciler) Start(ctx context.Context) error {
355355
log.Error(err, "unable to connect to libvirt")
356356
// Set the hypervisor's LibVirtType condition to false with the
357357
// error message, so that it's visible in the status.
358+
base := hypervisor.DeepCopy()
358359
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
359360
Type: LibVirtType, // TODO: This should be a kvmv1 condition.
360361
Status: metav1.ConditionFalse,
361362
Message: fmt.Sprintf("unable to connect to libvirt: %v", err),
362363
Reason: "ConnectFailed",
363364
})
364-
patch := client.MergeFromWithOptions(hypervisor.DeepCopy(), client.MergeFromWithOptimisticLock{})
365+
patch := client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})
365366
if err := r.Status().Patch(ctx, &hypervisor, patch); err != nil {
366367
log.Error(err, "unable to update hypervisor status after failed libvirt connection")
367368
}

internal/controller/hypervisor_controller_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package controller
2020
import (
2121
"context"
2222
"errors"
23+
"strings"
2324
"time"
2425

2526
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
@@ -139,6 +140,23 @@ var _ = Describe("Hypervisor Controller", func() {
139140
Expect(err).To(HaveOccurred())
140141
Expect(err.Error()).To(ContainSubstring("context done while trying to connect to libvirt"))
141142
}
143+
144+
// Verify that the hypervisor status reflects the failed libvirt connection
145+
var updatedHypervisor kvmv1.Hypervisor
146+
Eventually(func() bool {
147+
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: hypervisorName}, &updatedHypervisor)
148+
if err != nil {
149+
return false
150+
}
151+
for _, condition := range updatedHypervisor.Status.Conditions {
152+
if condition.Type == "LibVirtConnection" {
153+
return condition.Status == metav1.ConditionFalse &&
154+
condition.Reason == "ConnectFailed" &&
155+
strings.Contains(condition.Message, "connection failed")
156+
}
157+
}
158+
return false
159+
}, 2*time.Second, 100*time.Millisecond).Should(BeTrue(), "hypervisor status should reflect failed libvirt connection")
142160
})
143161

144162
It("should fail when hypervisor resource does not exist", func() {

0 commit comments

Comments
 (0)