Skip to content

Commit

Permalink
more tests for the new features
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Nov 21, 2023
1 parent 540520d commit 6b87c15
Show file tree
Hide file tree
Showing 10 changed files with 1,219 additions and 122 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/limitador_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ type LimitadorStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

// Represents the observations of a foo's current state.
// Known .status.conditions.type are: "Available"
// Known .status.conditions.type are: "Ready"
// +patchMergeKey=type
// +patchStrategy=merge
// +listType=map
Expand Down
146 changes: 146 additions & 0 deletions controllers/limitador_controller_affinity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package controllers

import (
"context"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/limitador"
)

var _ = Describe("Limitador controller manages affinity", func() {

var testNamespace string

BeforeEach(func() {
CreateNamespace(&testNamespace)
})

AfterEach(DeleteNamespaceCallback(&testNamespace))

Context("Creating a new Limitador object with specific affinity", func() {
var limitadorObj *limitadorv1alpha1.Limitador

affinity := &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"pod": "label",
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Affinity = affinity
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should create a new deployment with the custom affinity", func() {
deployment := appsv1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(
context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
},
&deployment)

return err == nil
}, timeout, interval).Should(BeTrue())

Expect(deployment.Spec.Template.Spec.Affinity).To(Equal(affinity))
})
})

Context("Updating limitador object with new affinity settings", func() {
var limitadorObj *limitadorv1alpha1.Limitador

affinity := &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"pod": "label",
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should modify the deployment with the affinity custom settings", func() {
deployment := appsv1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(), types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
}, &deployment)

return err == nil
}, timeout, interval).Should(BeTrue())

Expect(deployment.Spec.Template.Spec.Affinity).To(BeNil())

updatedLimitador := limitadorv1alpha1.Limitador{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(), types.NamespacedName{
Namespace: testNamespace,
Name: limitadorObj.Name,
}, &updatedLimitador)

if err != nil {
return false
}

updatedLimitador.Spec.Affinity = affinity.DeepCopy()

return k8sClient.Update(context.TODO(), &updatedLimitador) == nil
}, timeout, interval).Should(BeTrue())

Eventually(func() bool {
newDeployment := appsv1.Deployment{}
err := k8sClient.Get(context.TODO(), types.NamespacedName{
Namespace: testNamespace,
Name: limitador.DeploymentName(limitadorObj),
}, &newDeployment)

if err != nil {
return false
}

return reflect.DeepEqual(newDeployment.Spec.Template.Spec.Affinity, affinity)
}, timeout, interval).Should(BeTrue())
})
})
})
157 changes: 157 additions & 0 deletions controllers/limitador_controller_limits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package controllers

import (
"context"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1"
"github.com/kuadrant/limitador-operator/pkg/limitador"
)

var _ = Describe("Limitador controller manages limits", func() {

var testNamespace string

BeforeEach(func() {
CreateNamespace(&testNamespace)
})

AfterEach(DeleteNamespaceCallback(&testNamespace))

Context("Creating a new Limitador object with specific limits", func() {
var limitadorObj *limitadorv1alpha1.Limitador

limits := []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"req.method == 'GET'"},
MaxValue: 10,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
Name: "useless",
},
{
Conditions: []string{"req.method == 'POST'"},
MaxValue: 5,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
limitadorObj.Spec.Limits = limits
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should create configmap with the custom limits", func() {
cm := &v1.ConfigMap{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, cm)

return err == nil
}, timeout, interval).Should(BeTrue())

var cmLimits []limitadorv1alpha1.RateLimit
err := yaml.Unmarshal([]byte(cm.Data[limitador.LimitadorConfigFileName]), &cmLimits)
Expect(err).To(BeNil())
Expect(cmLimits).To(Equal(limits))
})
})

Context("Updating limitador object with new limits", func() {
var limitadorObj *limitadorv1alpha1.Limitador

limits := []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"req.method == 'GET'"},
MaxValue: 10,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
Name: "useless",
},
{
Conditions: []string{"req.method == 'POST'"},
MaxValue: 5,
Namespace: "test-namespace",
Seconds: 60,
Variables: []string{"user_id"},
},
}

BeforeEach(func() {
limitadorObj = basicLimitador(testNamespace)
Expect(k8sClient.Create(context.TODO(), limitadorObj)).Should(Succeed())
Eventually(testLimitadorIsReady(limitadorObj), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Should modify configmap with the new limits", func() {
cm := &v1.ConfigMap{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, cm)

return err == nil
}, timeout, interval).Should(BeTrue())

var cmLimits []limitadorv1alpha1.RateLimit
err := yaml.Unmarshal([]byte(cm.Data[limitador.LimitadorConfigFileName]), &cmLimits)
Expect(err).To(BeNil())
Expect(cmLimits).To(BeEmpty())

updatedLimitador := limitadorv1alpha1.Limitador{}
Eventually(func() bool {
err := k8sClient.Get(context.TODO(), types.NamespacedName{
Namespace: testNamespace,
Name: limitadorObj.Name,
}, &updatedLimitador)

if err != nil {
return false
}

updatedLimitador.Spec.Limits = limits

return k8sClient.Update(context.TODO(), &updatedLimitador) == nil
}, timeout, interval).Should(BeTrue())

Eventually(func() bool {
newCM := &v1.ConfigMap{}
err := k8sClient.Get(context.TODO(),
types.NamespacedName{
Namespace: testNamespace,
Name: limitador.LimitsConfigMapName(limitadorObj),
}, newCM)

if err != nil {
return false
}

var cmLimits []limitadorv1alpha1.RateLimit
err = yaml.Unmarshal([]byte(newCM.Data[limitador.LimitadorConfigFileName]), &cmLimits)
if err != nil {
return false
}

return reflect.DeepEqual(cmLimits, limits)
}, timeout, interval).Should(BeTrue())
})
})
})
Loading

0 comments on commit 6b87c15

Please sign in to comment.