-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add identical hostnames test for RateLimitPolicy
- Loading branch information
Showing
6 changed files
with
185 additions
and
11 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
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
67 changes: 67 additions & 0 deletions
67
testsuite/tests/kuadrant/identical_hostnames/test_identical_hostnames_rlp_on_gw_and_route.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,67 @@ | ||
""" | ||
Tests behavior of using one HTTPRoute declaring the same hostname as parent Gateway related to RateLimitPolicy. | ||
https://github.com/Kuadrant/kuadrant-operator/blob/main/doc/rate-limiting.md#limitation-multiple-network-resources-with-identical-hostnames | ||
(second topology mentioned there) | ||
""" | ||
|
||
import pytest | ||
|
||
from testsuite.policy.rate_limit_policy import RateLimitPolicy, Limit | ||
|
||
pytestmark = [pytest.mark.kuadrant_only] | ||
|
||
|
||
@pytest.fixture(scope="class") | ||
def rate_limit_policy2(request, gateway, rate_limit_policy_name2, openshift, label): | ||
"""2nd RateLimitPolicy object allowing 1 request per 10 minutes (a.k.a. '1rp10m')""" | ||
rlp = RateLimitPolicy.create_instance(openshift, rate_limit_policy_name2, gateway, labels={"testRun": label}) | ||
request.addfinalizer(rlp.delete) | ||
rlp.add_limit("1rp10m", [Limit(1, 600)]) | ||
rlp.commit() | ||
rlp.wait_for_partially_enforced() | ||
return rlp | ||
|
||
|
||
def test_identical_hostnames_rlp_on_gw_and_route_ignored(client, rate_limit, rate_limit_policy2, hostname): | ||
""" | ||
Tests that Gateway-attached RateLimitPolicy is ignored on 'route2' if both 'route' and 'route2' declare | ||
identical hostname and there is another RateLimitPolicy already successfully enforced on 'route'. | ||
Setup: | ||
- Two HTTPRoutes declaring identical hostnames but different paths ('/nothing/' and '/anything/') | ||
- Empty RateLimitPolicy enforced on the '/nothing/' HTTPRoute | ||
- '1rp10m' RateLimitPolicy (created after Empty RateLimitPolicy) enforced on the Gateway | ||
Test: | ||
- Send a request via 'route' and assert that no 429s (Too Many Requests) are returned | ||
- Send a request via 'route2' and assert that no 429s (Too Many Requests)are returned | ||
- Delete the Empty RateLimitPolicy | ||
- Send a request via both routes | ||
- Assert that on both routes the 429s are returned after single 200 (OK) | ||
""" | ||
|
||
# Verify that the Empty RLP is still enforced despite '1rp10m' RLP being enforced too now | ||
rate_limit.wait_for_ready() | ||
|
||
# Access via 'route' is not limited due to Empty RateLimitPolicy | ||
responses = client.get_many("/nothing/get", 2) | ||
assert all( | ||
r.status_code == 200 for r in responses | ||
), f"Rate Limited resource unexpectedly rejected requests {responses}" | ||
|
||
# Access via 'route2' is limited due to '1rp10m' Gateway RateLimitPolicy ( it is partially enforced) | ||
response = client.get("/anything/get") | ||
assert response.status_code == 200 | ||
responses = client.get_many("/anything/get", 2) | ||
assert all( | ||
r.status_code == 429 for r in responses | ||
), f"Rate Limited resource unexpectedly allowed requests {responses}" | ||
|
||
# Deletion of Empty RateLimitPolicy should make the '1rp10m' Gateway RateLimitPolicy effectively enforced on both | ||
# routes. It might take some time hence the use of retry client. | ||
rate_limit.delete() | ||
rate_limit_policy2.wait_for_ready() | ||
with hostname.client(retry_codes={200}) as retry_client: | ||
response = retry_client.get("/nothing/get") | ||
assert response.status_code == 429 | ||
|
||
response = client.get("/anything/get") | ||
assert response.status_code == 429 |
84 changes: 84 additions & 0 deletions
84
testsuite/tests/kuadrant/identical_hostnames/test_identical_hostnames_rlp_on_routes.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,84 @@ | ||
""" | ||
Tests behavior of using one HTTPRoute declaring the same hostname as parent Gateway related to RateLimitPolicy. | ||
https://github.com/Kuadrant/kuadrant-operator/blob/main/doc/rate-limiting.md#limitation-multiple-network-resources-with-identical-hostnames | ||
(the first topology mentioned there) | ||
""" | ||
|
||
import pytest | ||
|
||
from testsuite.policy.rate_limit_policy import RateLimitPolicy, Limit | ||
|
||
pytestmark = [pytest.mark.kuadrant_only] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rate_limit(rate_limit): | ||
"""Add limit to 1st RateLimitPolicy allowing 1 request per 10 minutes (a.k.a. '1rp10m' RateLimitPolicy)""" | ||
rate_limit.add_limit("1rp10m", [Limit(1, 10)]) | ||
return rate_limit | ||
|
||
|
||
@pytest.fixture(scope="class") | ||
def rate_limit_policy2(request, route2, rate_limit_policy_name2, openshift, label): | ||
"""2nd RateLimitPolicy allowing 2 requests per 10 minutes (a.k.a. '2rp10m' RateLimitPolicy)""" | ||
rlp = RateLimitPolicy.create_instance(openshift, rate_limit_policy_name2, route2, labels={"testRun": label}) | ||
request.addfinalizer(rlp.delete) | ||
rlp.add_limit("2rp10m", [Limit(2, 10)]) | ||
rlp.commit() | ||
rlp.wait_for_ready() | ||
return rlp | ||
|
||
|
||
def test_identical_hostnames_rlp_on_routes_ignored(client, rate_limit, rate_limit_policy2, hostname): | ||
""" | ||
Tests that 1st RateLimitPolicy gets ignored on 'route' declaring identical hostname as 'route2' when another | ||
RateLimitPolicy gets successfully enforced on 'route2'. | ||
Setup: | ||
- Two HTTPRoutes declaring identical hostnames but different paths ('/nothing/' and '/anything/') | ||
- '1rp10m' RateLimitPolicy enforced on the '/nothing/' HTTPRoute | ||
- '2rp10m' RateLimitPolicy (created after '1rp10m' RateLimitPolicy) enforced on the '/anything/' HTTPRoute | ||
Test: | ||
- Send a request via 'route' and assert that no 429s (Too Many Requests) are returned | ||
- Send a request via 'route2' and assert that no 429s (Too Many Requests)are returned | ||
- Delete the Empty RateLimitPolicy | ||
- Send a request via both routes | ||
- Assert that on both routes the 429s are returned after single 200 (OK) | ||
""" | ||
|
||
# Verify that the '1rp10m' RLP is still enforced despite '2rp10m' RLP being enforced too now | ||
rate_limit.wait_for_ready() | ||
|
||
# Access via 'route' is not limited at all because '1rp10m' RateLimitPolicy is ignored | ||
# despite it reporting being successfully enforced | ||
responses = client.get_many("/nothing/get", 3) | ||
assert all( | ||
r.status_code == 200 for r in responses | ||
), f"Rate Limited resource unexpectedly rejected requests {responses}" | ||
|
||
# Access via 'route2' is limited due to '2rp10m' RateLimitPolicy | ||
response = client.get_many("/anything/get", 2) | ||
assert all( | ||
r.status_code == 200 for r in responses | ||
), f"Rate Limited resource unexpectedly rejected requests {responses}" | ||
# There might be more than two 200 OKs responses, might be due to '2rp10m' enforcement still being in progres | ||
with hostname.client(retry_codes={200}) as retry_client: | ||
response = retry_client.get("/anything/get") | ||
assert response.status_code == 429 | ||
|
||
# Deletion of '2rp10m' RateLimitPolicy should make '1rp10m' RateLimitPolicy effectively enforced. | ||
rate_limit_policy2.delete() | ||
rate_limit.wait_for_ready() | ||
|
||
# Access via 'route' should now be limited via '1rp10m' RateLimitPolicy | ||
response = client.get("/nothing/get") | ||
assert response.status_code == 200 | ||
# There might be more than two 200 OKs responses, might be due to '1rp10m' enforcement still being in progres | ||
with hostname.client(retry_codes={200}) as retry_client: | ||
response = retry_client.get("/nothing/get") | ||
assert response.status_code == 429 | ||
|
||
# Access via 'route2' is now not limited at all | ||
responses = client.get_many("/anything/get", 3) | ||
assert all( | ||
r.status_code == 200 for r in responses | ||
), f"Rate Limited resource unexpectedly rejected requests {responses}" |