diff --git a/testsuite/httpx/__init__.py b/testsuite/httpx/__init__.py index 2ba4a9f5a..3a555112a 100644 --- a/testsuite/httpx/__init__.py +++ b/testsuite/httpx/__init__.py @@ -103,3 +103,11 @@ def request( if len(e.args) > 0 and any("Name or service not known" in arg for arg in e.args): raise UnexpectedResponse("Didn't expect 'Name or service not known' error", None) from e raise + + def get_many(self, url, count, *, params=None, headers=None, auth=None) -> list[Response]: + """Send multiple `GET` requests.""" + responses = [] + for _ in range(count): + responses.append(self.get(url, params=params, headers=headers, auth=auth)) + + return responses diff --git a/testsuite/openshift/objects/gateway_api/gateway.py b/testsuite/openshift/objects/gateway_api/gateway.py index e8f75e13a..b673a68f6 100644 --- a/testsuite/openshift/objects/gateway_api/gateway.py +++ b/testsuite/openshift/objects/gateway_api/gateway.py @@ -124,8 +124,8 @@ def get_tls_cert(self) -> Certificate: def delete_tls_secret(self): """Deletes secret with TLS certificate used by the gateway""" - tls_secret = self.openshift.get_secret(self.cert_secret_name) - tls_secret.delete(ignore_not_found=True) + with self.openshift.context: + selector(f"secret/{self.cert_secret_name}").delete(ignore_not_found=True) def get_spoke_gateway(self, spokes: dict[str, OpenShiftClient]) -> "MGCGateway": """ diff --git a/testsuite/openshift/objects/rate_limit.py b/testsuite/openshift/objects/rate_limit.py index 7074c72a1..ded279cd4 100644 --- a/testsuite/openshift/objects/rate_limit.py +++ b/testsuite/openshift/objects/rate_limit.py @@ -60,6 +60,6 @@ def _policy_is_ready(obj): assert success # https://github.com/Kuadrant/kuadrant-operator/issues/140 - sleep(60) + sleep(90) return result diff --git a/testsuite/tests/kuadrant/limitador/conftest.py b/testsuite/tests/kuadrant/limitador/conftest.py index 2df44e6e5..b4a712375 100644 --- a/testsuite/tests/kuadrant/limitador/conftest.py +++ b/testsuite/tests/kuadrant/limitador/conftest.py @@ -1,4 +1,5 @@ -"""Conftest for all limitador tests""" +"""Conftest for rate limit tests""" + import pytest diff --git a/testsuite/tests/kuadrant/limitador/test_basic_limit.py b/testsuite/tests/kuadrant/limitador/test_basic_limit.py index 6a0009817..d00e504c9 100644 --- a/testsuite/tests/kuadrant/limitador/test_basic_limit.py +++ b/testsuite/tests/kuadrant/limitador/test_basic_limit.py @@ -1,10 +1,10 @@ """ Tests that a single limit is enforced as expected over one iteration """ + import pytest from testsuite.openshift.objects.rate_limit import Limit -from testsuite.utils import fire_requests @pytest.fixture( @@ -36,4 +36,8 @@ def rate_limit(rate_limit, limit): def test_limit(client, limit): """Tests that simple limit is applied successfully""" - fire_requests(client, limit, grace_requests=1) + responses = client.get_many("/get", limit.limit) + assert all( + r.status_code == 200 for r in responses + ), f"Rate Limited resource unexpectedly rejected requests {responses}" + assert client.get("/get").status_code == 429 diff --git a/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py b/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py index 7cf411647..92de415e7 100644 --- a/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py +++ b/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py @@ -1,10 +1,11 @@ """ Tests that a single limit is enforced as expected over multiple iterations """ +from time import sleep + import pytest from testsuite.openshift.objects.rate_limit import Limit -from testsuite.utils import fire_requests @pytest.fixture(scope="module") @@ -16,4 +17,10 @@ def rate_limit(rate_limit): def test_multiple_iterations(client): """Tests that simple limit is applied successfully and works for multiple iterations""" - fire_requests(client, Limit(5, 10), iterations=10) + for _ in range(10): + responses = client.get_many("/get", 5) + assert all( + r.status_code == 200 for r in responses + ), f"Rate Limited resource unexpectedly rejected requests {responses}" + assert client.get("/get").status_code == 429 + sleep(10) diff --git a/testsuite/utils.py b/testsuite/utils.py index c066b9fd5..1e703eb48 100644 --- a/testsuite/utils.py +++ b/testsuite/utils.py @@ -4,23 +4,17 @@ import json import os import secrets -import typing from collections.abc import Collection from importlib import resources from io import StringIO -from time import sleep from typing import Dict, Union from urllib.parse import urlparse, ParseResult -import httpx from weakget import weakget from testsuite.certificates import Certificate, CFSSLClient, CertInfo from testsuite.config import settings -if typing.TYPE_CHECKING: - from testsuite.openshift.objects.rate_limit import Limit - MESSAGE_1KB = resources.files("testsuite.resources.performance.files").joinpath("message_1kb.txt") @@ -116,35 +110,6 @@ def create_csv_file(rows: list) -> StringIO: return file -def fire_requests(client, limit: "Limit", grace_requests=0, iterations=1, path="/get"): - """ - Fires requests meant to test if it is correctly rate-limited - :param client: Client instance - :param limit: Expected rate limit - :param grace_requests: Number of requests on top of max_requests - which will be made but not checked, improves stability - :param iterations: Number of periods to tests - :param path: URL path of the request - :return: - """ - period = limit.duration - max_requests = limit.limit - url = f"{client.base_url}/{path}" - for iteration in range(iterations): - sleep(period) - for i in range(max_requests): - assert ( - httpx.get(url).status_code == 200 - ), f"{i + 1}/{max_requests} request from {iteration + 1} iteration failed" - - for i in range(grace_requests): - httpx.get(url) - - assert httpx.get(url).status_code == 429, f"Iteration {iteration + 1} failed to start limiting" - sleep(period) - assert httpx.get(url).status_code == 200, f"Iteration {iteration + 1} failed to reset limits" - - def extract_response(response, header="Simple", key="data"): """ Extracts response added by Authorino from header