From cc8b936cb3ad2c14b5dc16f69ee4b60ff355f75f Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 3 May 2024 14:04:20 +0200 Subject: [PATCH] tests added to kuadrant_controller --- controllers/helper_test.go | 20 ++++- controllers/kuadrant_controller_test.go | 99 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 controllers/kuadrant_controller_test.go diff --git a/controllers/helper_test.go b/controllers/helper_test.go index 223065bfa..681db6c43 100644 --- a/controllers/helper_test.go +++ b/controllers/helper_test.go @@ -32,6 +32,7 @@ import ( gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" kuadrantdnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1" + limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" @@ -57,11 +58,11 @@ const ( TestIPAddressTwo = "172.0.0.2" ) -func ApplyKuadrantCR(namespace string) { - ApplyKuadrantCRWithName(namespace, "kuadrant-sample") +func ApplyKuadrantCR(namespace string, mutateFns ...func(kCR *kuadrantv1beta1.Kuadrant)) { + ApplyKuadrantCRWithName(namespace, "kuadrant-sample", mutateFns...) } -func ApplyKuadrantCRWithName(namespace, name string) { +func ApplyKuadrantCRWithName(namespace, name string, mutateFns ...func(kCR *kuadrantv1beta1.Kuadrant)) { kuadrantCR := &kuadrantv1beta1.Kuadrant{ TypeMeta: metav1.TypeMeta{ Kind: "Kuadrant", @@ -72,6 +73,11 @@ func ApplyKuadrantCRWithName(namespace, name string) { Namespace: namespace, }, } + + for _, mutateFn := range mutateFns { + mutateFn(kuadrantCR) + } + err := k8sClient.Create(context.Background(), kuadrantCR) Expect(err).ToNot(HaveOccurred()) @@ -661,3 +667,11 @@ func createSelfSignedIssuerSpec() certmanv1.IssuerSpec { }, } } + +func testLimitadorIsReady(ctx context.Context, lKey client.ObjectKey) func(g Gomega) { + return func(g Gomega) { + existing := &limitadorv1alpha1.Limitador{} + g.Expect(k8sClient.Get(ctx, lKey, existing)).To(Succeed()) + g.Expect(meta.IsStatusConditionTrue(existing.Status.Conditions, limitadorv1alpha1.StatusConditionReady)).To(BeTrue()) + } +} diff --git a/controllers/kuadrant_controller_test.go b/controllers/kuadrant_controller_test.go new file mode 100644 index 000000000..489e619b2 --- /dev/null +++ b/controllers/kuadrant_controller_test.go @@ -0,0 +1,99 @@ +//go:build integration + +package controllers + +import ( + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + "sigs.k8s.io/controller-runtime/pkg/client" + + limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" + + kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" + "github.com/kuadrant/kuadrant-operator/pkg/common" +) + +var _ = FDescribe("Kuadrant controller deploys limitador", func() { + var ( + testNamespace string + afterEachTimeOut = NodeTimeout(3 * time.Minute) + ) + + BeforeEach(func(ctx SpecContext) { + testNamespace = CreateNamespaceWithContext(ctx) + }) + AfterEach(func(ctx SpecContext) { + DeleteNamespaceCallbackWithContext(ctx, testNamespace) + }, afterEachTimeOut) + + Context("when config is empty", func() { + It("creates basic Limitador CR", func(ctx SpecContext) { + ApplyKuadrantCR(testNamespace, func(kCR *kuadrantv1beta1.Kuadrant) { + kCR.Spec.Limitador = nil + }) + + limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace} + + Eventually(testLimitadorIsReady(ctx, limitadorKey)).WithContext(ctx).Should(Succeed()) + }) + }) + + Context("when config has fields set", func() { + It("Limitador CR has the same fields set", func(ctx SpecContext) { + + var ( + affinity *corev1.Affinity = &corev1.Affinity{ + PodAffinity: &corev1.PodAffinity{ + PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ + { + Weight: 100, + PodAffinityTerm: corev1.PodAffinityTerm{ + TopologyKey: "limitador", + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "limitador", + }, + }, + }, + }, + }, + }, + } + + podDisruptionBudget *limitadorv1alpha1.PodDisruptionBudgetType = &limitadorv1alpha1.PodDisruptionBudgetType{ + MinAvailable: &intstr.IntOrString{ + IntVal: 1, + }, + } + + storage *limitadorv1alpha1.Storage = &limitadorv1alpha1.Storage{ + Disk: &limitadorv1alpha1.DiskSpec{}, + } + ) + + ApplyKuadrantCR(testNamespace, func(kCR *kuadrantv1beta1.Kuadrant) { + kCR.Spec.Limitador = &kuadrantv1beta1.LimitadorSpec{} + kCR.Spec.Limitador.Affinity = affinity + kCR.Spec.Limitador.PodDisruptionBudget = podDisruptionBudget + kCR.Spec.Limitador.Storage = storage + }) + + limitadorKey := client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace} + + Eventually(testLimitadorIsReady(ctx, limitadorKey)).WithContext(ctx).Should(Succeed()) + + limitador := &limitadorv1alpha1.Limitador{} + err := k8sClient.Get(ctx, limitadorKey, limitador) + // must exist + Expect(err).ToNot(HaveOccurred()) + Expect(limitador.Spec.Affinity).To(Equal(affinity)) + Expect(limitador.Spec.PodDisruptionBudget).To(Equal(podDisruptionBudget)) + Expect(limitador.Spec.Storage).To(Equal(storage)) + }) + }) +})