|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package controller |
| 19 | + |
| 20 | +import ( |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + |
| 29 | + kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("Hypervisor Controller", func() { |
| 33 | + var hypervisorController *HypervisorController |
| 34 | + |
| 35 | + Context("When reconciling a node", func() { |
| 36 | + const nodeName = "node-test" |
| 37 | + |
| 38 | + BeforeEach(func(ctx SpecContext) { |
| 39 | + hypervisorController = &HypervisorController{ |
| 40 | + Client: k8sClient, |
| 41 | + Scheme: k8sClient.Scheme(), |
| 42 | + } |
| 43 | + |
| 44 | + By("creating the namespace for the reconciler") |
| 45 | + ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "monsoon3"}} |
| 46 | + Expect(client.IgnoreAlreadyExists(k8sClient.Create(ctx, ns))).To(Succeed()) |
| 47 | + |
| 48 | + By("creating the core resource for the Kind Node") |
| 49 | + resource := &corev1.Node{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{ |
| 51 | + Name: nodeName, |
| 52 | + Labels: map[string]string{corev1.LabelTopologyZone: "test-zone"}, //nolint:goconst |
| 53 | + }, |
| 54 | + } |
| 55 | + Expect(k8sClient.Create(ctx, resource)).To(Succeed()) |
| 56 | + }) |
| 57 | + |
| 58 | + AfterEach(func(ctx SpecContext) { |
| 59 | + node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}} |
| 60 | + By("Cleanup the specific node") |
| 61 | + Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed()) |
| 62 | + }) |
| 63 | + |
| 64 | + It("should successfully reconcile the node", func(ctx SpecContext) { |
| 65 | + By("Reconciling the created resource") |
| 66 | + _, err := hypervisorController.Reconcile(ctx, ctrl.Request{ |
| 67 | + NamespacedName: types.NamespacedName{Name: nodeName}, |
| 68 | + }) |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + }) |
| 71 | + |
| 72 | + It("should have created the Hypervisor resource", func(ctx SpecContext) { |
| 73 | + // Get the Hypervisor resource |
| 74 | + hypervisor := &kvmv1.Hypervisor{} |
| 75 | + hypervisorName := types.NamespacedName{Name: nodeName} |
| 76 | + Expect(hypervisorController.Get(ctx, hypervisorName, hypervisor)).To(Succeed()) |
| 77 | + Expect(hypervisor.Name).To(Equal(nodeName)) |
| 78 | + Expect(hypervisor.Labels).ToNot(BeNil()) |
| 79 | + Expect(hypervisor.Labels[corev1.LabelTopologyZone]).To(Equal("test-zone")) |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments