Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated RouteSelector tests to use SectionNames #600

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions testsuite/kuadrant/policy/authorization/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create_instance(
name,
target: Referencable,
labels: Dict[str, str] = None,
section_name: str = None,
):
"""Creates base instance"""
model: Dict = {
Expand All @@ -37,6 +38,8 @@ def create_instance(
"targetRef": target.reference,
},
}
if section_name:
model["spec"]["targetRef"]["sectionName"] = section_name

return cls(model, context=cluster.context)

Expand Down
3 changes: 3 additions & 0 deletions testsuite/kuadrant/policy/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def create_instance(
name: str,
parent: Referencable,
provider_secret_name: str,
section_name: str = None,
load_balancing: LoadBalancing = None,
labels: dict[str, str] = None,
):
Expand All @@ -86,6 +87,8 @@ def create_instance(

if load_balancing:
model["spec"]["loadBalancing"] = asdict(load_balancing)
if section_name:
model["spec"]["targetRef"]["sectionName"] = section_name

return cls(model, context=cluster.context)

Expand Down
13 changes: 11 additions & 2 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ def __init__(self, *args, **kwargs):
self.spec_section = None

@classmethod
def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None):
def create_instance(
cls,
cluster: KubernetesClient,
name,
target: Referencable,
section_name: str = None,
labels: dict[str, str] = None,
):
"""Creates new instance of RateLimitPolicy"""
model = {
model: dict = {
"apiVersion": "kuadrant.io/v1",
"kind": "RateLimitPolicy",
"metadata": {"name": name, "labels": labels},
"spec": {
"targetRef": target.reference,
},
}
if section_name:
model["spec"]["targetRef"]["sectionName"] = section_name

return cls(model, context=cluster.context)

Expand Down
5 changes: 4 additions & 1 deletion testsuite/kuadrant/policy/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def create_instance(
name: str,
parent: Referencable,
issuer: Referencable,
section_name: str = None,
labels: dict[str, str] = None,
commonName: str = None,
duration: str = None,
Expand All @@ -24,7 +25,7 @@ def create_instance(
): # pylint: disable=invalid-name
"""Creates new instance of TLSPolicy"""

model = {
model: dict = {
"apiVersion": "kuadrant.io/v1",
"kind": "TLSPolicy",
"metadata": {"name": name, "labels": labels},
Expand All @@ -40,6 +41,8 @@ def create_instance(
},
},
}
if section_name:
model["spec"]["targetRef"]["sectionName"] = section_name

return cls(model, context=cluster.context)

Expand Down

This file was deleted.

27 changes: 0 additions & 27 deletions testsuite/tests/singlecluster/limitador/route/test_route_rule.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Conftest for RLP targeting route tests """
"""Conftest for RLP section_name targeting tests"""

import pytest

Expand All @@ -18,3 +18,11 @@ def route(route, backend):
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
return route


@pytest.fixture(scope="module", autouse=True)
def commit(request, route, rate_limit): # pylint: disable=unused-argument
"""Commits RateLimitPolicy after the HTTPRoute is created"""
request.addfinalizer(rate_limit.delete)
rate_limit.commit()
rate_limit.wait_for_ready()
24 changes: 24 additions & 0 deletions testsuite/tests/singlecluster/limitador/section/test_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests that the RLP is correctly applies to the Gateway Listener."""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, gateway):
"""Add a RateLimitPolicy targeting the Gateway Listener."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, "api", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(5, "10s")])
return rlp


def test_listener_match(client):
"""Tests that RLP correctly applies to the given Gateway Listener"""
responses = client.get_many("/get", 5)
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429
assert client.get("/anything").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Test multiple RLP's targeting different HTTPRouteRules do not interfere with each other."""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the first HTTPRouteRule."""
rate_limit = RateLimitPolicy.create_instance(
cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}
)
rate_limit.add_limit("basic", [Limit(3, "5s")])
return rate_limit


@pytest.fixture(scope="module")
def rate_limit2(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the second HTTPRouteRule."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-2", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(2, "5s")])
return rlp


@pytest.fixture(scope="module", autouse=True)
def commit(request, rate_limit, rate_limit2):
"""Commit and wait for RateLimitPolicies to be fully enforced"""
for policy in [rate_limit, rate_limit2]:
request.addfinalizer(policy.delete)
policy.commit()
policy.wait_for_ready()


def test_multiple_limits_targeting_rules(client):
"""Test targeting separate HTTPRouteRules with different limits"""
responses = client.get_many("/get", 3)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"

responses = client.get_many("/anything", 2)
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
assert client.get("/anything").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test that multiple limits targeting same Gateway Listener are correctly applied"""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, gateway):
"""Add a RateLimitPolicy targeting the Gateway Listener with multiple limits."""
rate_limit = RateLimitPolicy.create_instance(
cluster, blame("limit"), gateway, "api", labels={"testRun": module_label}
)
rate_limit.add_limit("test1", [Limit(8, "10s")])
rate_limit.add_limit("test2", [Limit(3, "5s")])
return rate_limit


def test_two_limits_targeting_one_listener(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import pytest

from testsuite.kuadrant.policy import CelPredicate
from testsuite.kuadrant.policy.rate_limit import Limit
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
when = CelPredicate("request.path == '/get'")
rate_limit.add_limit("test1", [Limit(8, "10s")], when=[when])
rate_limit.add_limit("test2", [Limit(3, "5s")], when=[when])
def rate_limit(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the first HTTPRouteRule with two limits."""
rate_limit = RateLimitPolicy.create_instance(
cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}
)
rate_limit.add_limit("test1", [Limit(8, "10s")])
rate_limit.add_limit("test2", [Limit(3, "5s")])
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561")
def test_two_rules_targeting_one_limit(client):
def test_two_limits_targeting_one_rule(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
assert all(
Expand Down
26 changes: 26 additions & 0 deletions testsuite/tests/singlecluster/limitador/section/test_route_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests that the RLP is correctly applied to the route rule"""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the first HTTPRouteRule."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(5, "10s")])
return rlp


def test_rule_match(client):
"""Tests that RLP correctly applies to the given HTTPRouteRule"""
responses = client.get_many("/get", 5)
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429

response = client.get("/anything")
assert response.status_code == 200
Loading