From aba5ef0f7316143972d3283a6bce6d8e361d35a4 Mon Sep 17 00:00:00 2001 From: Jakub Smolar Date: Thu, 19 Oct 2023 13:59:12 +0200 Subject: [PATCH] Refactor of fire_requests method --- testsuite/httpx/__init__.py | 8 +++++ testsuite/openshift/objects/rate_limit.py | 2 +- .../tests/kuadrant/limitador/conftest.py | 3 +- .../kuadrant/limitador/test_basic_limit.py | 8 +++-- .../limitador/test_multiple_iterations.py | 11 ++++-- testsuite/utils.py | 35 ------------------- 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/testsuite/httpx/__init__.py b/testsuite/httpx/__init__.py index 2ba4a9f5..3a555112 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/rate_limit.py b/testsuite/openshift/objects/rate_limit.py index 7074c72a..ded279cd 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 2df44e6e..b4a71237 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 6a000981..d00e504c 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 7cf41164..92de415e 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 c066b9fd..1e703eb4 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