forked from Kuadrant/testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Kuadrant#167 from pehala/ratelimit
Add basic Limitador tests
- Loading branch information
Showing
10 changed files
with
199 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""RateLimitPolicy related objects""" | ||
from time import sleep | ||
|
||
import openshift as oc | ||
from testsuite.openshift.client import OpenShiftClient | ||
from testsuite.openshift.objects import OpenShiftObject, modify | ||
from testsuite.openshift.objects.gateway_api import Referencable | ||
|
||
|
||
class RateLimitPolicy(OpenShiftObject): | ||
"""RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to an Gateway/HTTPRoute""" | ||
|
||
@classmethod | ||
def create_instance(cls, openshift: OpenShiftClient, name, route: Referencable, labels: dict[str, str] = None): | ||
"""Creates new instance of RateLimitPolicy""" | ||
model = { | ||
"apiVersion": "kuadrant.io/v1beta1", | ||
"kind": "RateLimitPolicy", | ||
"metadata": {"name": name, "namespace": openshift.project, "labels": labels}, | ||
"spec": { | ||
"targetRef": route.reference, | ||
"rateLimits": [ | ||
{ | ||
"configurations": [ | ||
{"actions": [{"generic_key": {"descriptor_key": "limited", "descriptor_value": "1"}}]} | ||
] | ||
} | ||
], | ||
}, | ||
} | ||
|
||
return cls(model, context=openshift.context) | ||
|
||
@modify | ||
def add_limit(self, max_value, seconds, conditions: list[str] = None): | ||
"""Add another limit""" | ||
conditions = conditions or [] | ||
limits = self.model.spec.rateLimits[0].setdefault("limits", []) | ||
limit = {"maxValue": max_value, "seconds": seconds, "conditions": conditions, "variables": []} | ||
limits.append(limit) | ||
|
||
def commit(self): | ||
result = super().commit() | ||
|
||
# wait for RLP to be actually applied, conditions itself is not enough, sleep is needed | ||
def _policy_is_ready(obj): | ||
return "conditions" in obj.model.status and obj.model.status.conditions[0].status == "True" | ||
|
||
with oc.timeout(60): | ||
success, _, _ = self.self_selector().until_all(success_func=_policy_is_ready, tolerate_failures=5) | ||
assert success | ||
|
||
# https://github.com/Kuadrant/kuadrant-operator/issues/140 | ||
sleep(60) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Conftest for all limitador tests""" | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def kuadrant(kuadrant): | ||
"""Skip if not running on Kuadrant""" | ||
if not kuadrant: | ||
pytest.skip("Limitador test can only run on Kuadrant for now") | ||
return kuadrant | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def commit(request, rate_limit): | ||
"""Commits all important stuff before tests""" | ||
request.addfinalizer(rate_limit.delete) | ||
rate_limit.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
Tests that a single limit is enforced as expected over one iteration | ||
""" | ||
import pytest | ||
|
||
from testsuite.utils import fire_requests | ||
|
||
|
||
@pytest.fixture( | ||
scope="module", | ||
params=[ | ||
pytest.param((2, 20), id="2 requests every 20 sec"), | ||
pytest.param((5, 15), id="5 requests every 15 sec"), | ||
pytest.param((3, 10), id="3 request every 10 sec"), | ||
], | ||
) | ||
def limit_time(request): | ||
"""Combination of max requests and time period""" | ||
return request.param | ||
|
||
|
||
# pylint: disable=unused-argument | ||
@pytest.fixture(scope="module") | ||
def rate_limit_name(blame, limit_time): | ||
"""Generate name for each combination of limit_time""" | ||
return blame("limit") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rate_limit(rate_limit, limit_time): | ||
"""Add limit to the policy""" | ||
limit, time = limit_time | ||
rate_limit.add_limit(limit, time) | ||
return rate_limit | ||
|
||
|
||
def test_limit(client, limit_time): | ||
"""Tests that simple limit is applied successfully""" | ||
limit, time = limit_time | ||
|
||
fire_requests(client, limit, time, grace_requests=1) |
18 changes: 18 additions & 0 deletions
18
testsuite/tests/kuadrant/limitador/test_multiple_iterations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Tests that a single limit is enforced as expected over multiple iterations | ||
""" | ||
import pytest | ||
|
||
from testsuite.utils import fire_requests | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rate_limit(rate_limit): | ||
"""Add limit to the policy""" | ||
rate_limit.add_limit(5, 10) | ||
return rate_limit | ||
|
||
|
||
def test_multiple_iterations(client): | ||
"""Tests that simple limit is applied successfully and works for multiple iterations""" | ||
fire_requests(client, 5, 10, iterations=10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters