Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions internal/controller/aggregates_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"

Expand Down Expand Up @@ -98,49 +98,53 @@ var _ = Describe("AggregatesController", func() {
)

var (
tc *AggregatesController
fakeServer testhelper.FakeServer
hypervisorName = types.NamespacedName{Name: "hv-test"}
aggregatesController *AggregatesController
fakeServer testhelper.FakeServer
hypervisorName = types.NamespacedName{Name: "hv-test"}
)
// Setup and teardown

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)
})

hypervisor := &kvmv1.Hypervisor{
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: hypervisorName.Name,
},
Spec: kvmv1.HypervisorSpec{
LifecycleEnabled: true,
},
}
Expect(k8sClient.Create(ctx, hypervisor)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, hypervisor)).To(Succeed())
})

Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
meta.SetStatusCondition(&hypervisor.Status.Conditions, v1.Condition{
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeOnboarding,
Status: v1.ConditionFalse,
Status: metav1.ConditionFalse,
Reason: "dontcare",
Message: "dontcare",
})
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())

By("Creating the AggregatesController")
tc = &AggregatesController{
aggregatesController = &AggregatesController{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
computeClient: client.ServiceClient(fakeServer),
}

DeferCleanup(func(ctx SpecContext) {
Expect(tc.Client.Delete(ctx, hypervisor)).To(Succeed())
})
})

JustBeforeEach(func(ctx SpecContext) {
_, err := tc.Reconcile(ctx, ctrl.Request{NamespacedName: hypervisorName})
_, err := aggregatesController.Reconcile(ctx, ctrl.Request{NamespacedName: hypervisorName})
Expect(err).NotTo(HaveOccurred())
})

Expand Down Expand Up @@ -190,7 +194,7 @@ var _ = Describe("AggregatesController", func() {

It("should update Aggregates and set status condition as Aggregates differ", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(aggregatesController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(ContainElements("test-aggregate1"))
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeAggregatesUpdated)).To(BeTrue())
})
Expand Down Expand Up @@ -234,7 +238,7 @@ var _ = Describe("AggregatesController", func() {

It("should update Aggregates and set status condition when Aggregates differ", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(aggregatesController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeAggregatesUpdated)).To(BeTrue())
})
Expand All @@ -245,13 +249,13 @@ var _ = Describe("AggregatesController", func() {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
// Remove the onboarding condition
hypervisor.Status.Conditions = []v1.Condition{}
hypervisor.Status.Conditions = []metav1.Condition{}
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
})

It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(aggregatesController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeAggregatesUpdated)).To(BeFalse())
})
Expand All @@ -262,9 +266,9 @@ var _ = Describe("AggregatesController", func() {
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
// Remove the onboarding condition
meta.SetStatusCondition(&hypervisor.Status.Conditions, v1.Condition{
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: kvmv1.ConditionTypeTerminating,
Status: v1.ConditionTrue,
Status: metav1.ConditionTrue,
Reason: "dontcare",
Message: "dontcare",
})
Expand All @@ -273,7 +277,7 @@ var _ = Describe("AggregatesController", func() {

It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
updated := &kvmv1.Hypervisor{}
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(aggregatesController.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
Expect(updated.Status.Aggregates).To(BeEmpty())
Expect(meta.IsStatusConditionTrue(updated.Status.Conditions, kvmv1.ConditionTypeAggregatesUpdated)).To(BeFalse())
})
Expand Down
48 changes: 28 additions & 20 deletions internal/controller/decomission_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ import (
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
)

const (
EOF = "EOF"
serviceId = "service-1234"
hypervisorName = "node-test"
namespaceName = "namespace-test"
AggregateListWithHv = `
var _ = Describe("Decommission Controller", func() {
const (
EOF = "EOF"
serviceId = "service-1234"
hypervisorName = "node-test"
namespaceName = "namespace-test"
AggregateListWithHv = `
{
"aggregates": [
{
Expand All @@ -54,7 +55,7 @@ const (
]
}
`
AggregateRemoveHostBody = `
AggregateRemoveHostBody = `
{
"aggregate": {
"name": "test-aggregate2",
Expand All @@ -63,28 +64,32 @@ const (
"id": 100001
}
}`
)
)

var _ = Describe("Decommission Controller", func() {
var (
r *NodeDecommissionReconciler
resourceName = types.NamespacedName{Name: hypervisorName}
reconcileReq = ctrl.Request{
decommissionReconciler *NodeDecommissionReconciler
resourceName = types.NamespacedName{Name: hypervisorName}
reconcileReq = ctrl.Request{
NamespacedName: resourceName,
}
fakeServer testhelper.FakeServer
)

BeforeEach(func(ctx SpecContext) {
fakeServer = testhelper.SetupHTTP()
os.Setenv("KVM_HA_SERVICE_URL", fakeServer.Endpoint()+"instance-ha")
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)
})

os.Setenv("KVM_HA_SERVICE_URL", fakeServer.Endpoint()+"instance-ha")
DeferCleanup(func() {
os.Unsetenv("KVM_HA_SERVICE_URL")
fakeServer.Teardown()
})

r = &NodeDecommissionReconciler{
decommissionReconciler = &NodeDecommissionReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
computeClient: client.ServiceClient(fakeServer),
Expand All @@ -94,7 +99,6 @@ var _ = Describe("Decommission Controller", func() {
By("creating the namespace for the reconciler")
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}}
Expect(k8sclient.IgnoreAlreadyExists(k8sClient.Create(ctx, ns))).To(Succeed())

DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, ns)).To(Succeed())
})
Expand All @@ -116,8 +120,8 @@ var _ = Describe("Decommission Controller", func() {

Context("When marking the hypervisor terminating", func() {
JustBeforeEach(func(ctx SpecContext) {
By("reconciling first reconciling the to add the finalizer")
_, err := r.Reconcile(ctx, reconcileReq)
By("reconciling first to add the finalizer")
_, err := decommissionReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())

hypervisor := &kvmv1.Hypervisor{}
Expand Down Expand Up @@ -208,11 +212,15 @@ var _ = Describe("Decommission Controller", func() {
fakeServer.Mux.HandleFunc("DELETE /resource_providers/rp-uuid", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
})

fakeServer.Mux.HandleFunc("DELETE /os-services/service-1234", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
})

It("should set the hypervisor ready condition", func(ctx SpecContext) {
By("reconciling the created resource")
_, err := r.Reconcile(ctx, reconcileReq)
_, err := decommissionReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
hypervisor := &kvmv1.Hypervisor{}
Expect(k8sClient.Get(ctx, resourceName, hypervisor)).To(Succeed())
Expand All @@ -228,7 +236,7 @@ var _ = Describe("Decommission Controller", func() {
It("should set the hypervisor offboarded condition", func(ctx SpecContext) {
By("reconciling the created resource")
for range 3 {
_, err := r.Reconcile(ctx, reconcileReq)
_, err := decommissionReconciler.Reconcile(ctx, reconcileReq)
Expect(err).NotTo(HaveOccurred())
}
Expect(getHypervisorsCalled).To(BeNumerically(">", 0))
Expand Down
63 changes: 28 additions & 35 deletions internal/controller/eviction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ var _ = Describe("Eviction Controller", func() {
}
}`
)

var (
evictionReconciler *EvictionReconciler
typeNamespacedName = types.NamespacedName{
Name: resourceName,
Namespace: namespaceName,
Expand All @@ -70,11 +72,28 @@ var _ = Describe("Eviction Controller", func() {
Name: resourceName,
Namespace: namespaceName,
}
reconcileRequest = ctrl.Request{NamespacedName: typeNamespacedName}
controllerReconciler *EvictionReconciler
fakeServer testhelper.FakeServer
reconcileRequest = ctrl.Request{NamespacedName: typeNamespacedName}
fakeServer testhelper.FakeServer
)

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 EvictionReconciler")
evictionReconciler = &EvictionReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
computeClient: client.ServiceClient(fakeServer),
}
})

AfterEach(func(ctx SpecContext) {
resource := &kvmv1.Eviction{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expand All @@ -84,9 +103,9 @@ var _ = Describe("Eviction Controller", func() {
}
} else {
By("Cleanup the specific resource instance Eviction")
Expect(controllerReconciler).NotTo(BeNil())
Expect(evictionReconciler).NotTo(BeNil())
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
_, err := evictionReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Get(ctx, typeNamespacedName, resource)).Should(HaveOccurred())
}
Expand Down Expand Up @@ -147,32 +166,6 @@ var _ = Describe("Eviction Controller", func() {
})

Describe("Reconciliation", func() {
BeforeEach(func(ctx SpecContext) {
By("Setting up the OpenStack http mock server")
fakeServer = testhelper.SetupHTTP()

DeferCleanup(func(ctx SpecContext) {
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 EvictionReconciler")

controllerReconciler = &EvictionReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
computeClient: client.ServiceClient(fakeServer),
}

DeferCleanup(func() {
controllerReconciler = nil
})
})

Describe("an eviction for an onboarded 'test-hypervisor'", func() {
BeforeEach(func(ctx SpecContext) {
By("creating the hypervisor resource")
Expand Down Expand Up @@ -222,7 +215,7 @@ var _ = Describe("Eviction Controller", func() {

It("should fail reconciliation", func(ctx SpecContext) {
for range 3 {
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
_, err := evictionReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
}

Expand Down Expand Up @@ -294,7 +287,7 @@ var _ = Describe("Eviction Controller", func() {
for i, expectation := range expectations {
By(fmt.Sprintf("Reconciliation step %d", i+1))
// Reconcile the resource
result, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
result, err := evictionReconciler.Reconcile(ctx, reconcileRequest)
Expect(result).To(Equal(ctrl.Result{}))
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -327,7 +320,7 @@ var _ = Describe("Eviction Controller", func() {

It("should succeed the reconciliation", func(ctx SpecContext) {
for range 1 {
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
_, err := evictionReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
}

Expand All @@ -345,7 +338,7 @@ var _ = Describe("Eviction Controller", func() {
))

for range 3 {
_, err = controllerReconciler.Reconcile(ctx, reconcileRequest)
_, err = evictionReconciler.Reconcile(ctx, reconcileRequest)
Expect(err).NotTo(HaveOccurred())
}
err = k8sClient.Get(ctx, typeNamespacedName, resource)
Expand Down
Loading