Skip to content

Commit

Permalink
tests added to kuadrant_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed May 3, 2024
1 parent d9ba611 commit cc8b936
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
20 changes: 17 additions & 3 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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())

Expand Down Expand Up @@ -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())
}
}
99 changes: 99 additions & 0 deletions controllers/kuadrant_controller_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})

0 comments on commit cc8b936

Please sign in to comment.