diff --git a/config/settings.local.yaml.tpl b/config/settings.local.yaml.tpl index cc74921d..9db645b1 100644 --- a/config/settings.local.yaml.tpl +++ b/config/settings.local.yaml.tpl @@ -20,6 +20,7 @@ # client_secret: "CLIENT_SECRET" # url: "AUTH0_URL" # mockserver: +# service_name: "mockserver" # url: "MOCKSERVER_URL" # cfssl: "cfssl" # Path to the CFSSL library for TLS tests # hyperfoil: diff --git a/testsuite/config/__init__.py b/testsuite/config/__init__.py index 3c13c89a..7e230079 100644 --- a/testsuite/config/__init__.py +++ b/testsuite/config/__init__.py @@ -41,6 +41,7 @@ def __init__(self, name, default, **kwargs) -> None: DefaultValueValidator("rhsso.url", default=fetch_route("no-ssl-sso")), DefaultValueValidator("rhsso.password", default=fetch_secret("credential-sso", "ADMIN_PASSWORD")), DefaultValueValidator("mockserver.url", default=fetch_route("mockserver", force_http=True)), + Validator("mockserver.service_name", default="mockserver"), Validator("gateway_api", must_exist=False, eq=False) | Validator("service_protection.gateway.name", must_exist=True), ], diff --git a/testsuite/mockserver.py b/testsuite/mockserver.py index 67e4598c..1bbf9cae 100644 --- a/testsuite/mockserver.py +++ b/testsuite/mockserver.py @@ -4,30 +4,60 @@ import httpx from apyproxy import ApyProxy +from testsuite.openshift.client import OpenShiftClient from testsuite.utils import ContentType +from testsuite.gateway import Referencable -class Mockserver: +class Mockserver(Referencable): """ Mockserver deployed in Openshift (located in Tools or self-managed) """ - def __init__(self, url): + def __init__(self, url, service_name: str = "mockserver", tools: OpenShiftClient = None): self.client = ApyProxy(url, session=httpx.Client(verify=False, timeout=5)) + self.service_name = service_name + self.tools = tools - def _expectation(self, expectation_id, response_data): + @property + def reference(self): + return { + "group": "", + "kind": "Service", + "port": 1080, + "name": self.service_name, + "namespace": self.tools.project, + } + + def _expectation(self, expectation_id, json_data): """ - Creates an Expectation with given response_data. + Creates an Expectation from given expectation json. Returns the absolute URL of the expectation """ - json_data = {"id": expectation_id, "httpRequest": {"path": f"/{expectation_id}"}} - json_data.update(response_data) + json_data["id"] = expectation_id + json_data.setdefault("httpRequest", {})["path"] = f"/{expectation_id}" self.client.mockserver.expectation.put(json=json_data) # pylint: disable=protected-access return f"{self.client._url}/{expectation_id}" - def create_expectation( + def create_request_expectation( + self, + expectation_id, + headers: dict[str, list[str]], + ): + """Creates an Expectation - request with given headers""" + json_data = { + "httpRequest": { + "headers": headers, + }, + "httpResponse": { + "body": "", + }, + } + return self._expectation(expectation_id, json_data) + + def create_response_expectation( self, expectation_id, body, diff --git a/testsuite/tests/conftest.py b/testsuite/tests/conftest.py index 3306707d..86f940f5 100644 --- a/testsuite/tests/conftest.py +++ b/testsuite/tests/conftest.py @@ -164,7 +164,9 @@ def mockserver(testconfig): """Returns mockserver""" try: testconfig.validators.validate(only=["mockserver"]) - return Mockserver(testconfig["mockserver"]["url"]) + return Mockserver( + testconfig["mockserver"]["url"], testconfig["mockserver"]["service_name"], testconfig["tools"] + ) except (KeyError, ValidationError) as exc: return pytest.skip(f"Mockserver configuration item is missing: {exc}") diff --git a/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/conftest.py b/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/conftest.py index 3d544c8b..b6030c20 100644 --- a/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/conftest.py +++ b/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/conftest.py @@ -14,7 +14,7 @@ def header(): def opa_policy_expectation(request, mockserver, module_label, header): """Creates Mockserver Expectation that returns Rego query and returns its endpoint""" request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) - return mockserver.create_expectation(module_label, rego_allow_header(*header)) + return mockserver.create_response_expectation(module_label, rego_allow_header(*header)) @pytest.fixture(scope="module") diff --git a/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/test_auto_refresh_policy.py b/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/test_auto_refresh_policy.py index e7368dbe..841b1921 100644 --- a/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/test_auto_refresh_policy.py +++ b/testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/test_auto_refresh_policy.py @@ -18,7 +18,7 @@ def updated_header(): @pytest.fixture(scope="module", autouse=True) def update_external_opa(mockserver, module_label, updated_header): """Updates Expectation with updated header""" - mockserver.create_expectation(module_label, rego_allow_header(*updated_header)) + mockserver.create_response_expectation(module_label, rego_allow_header(*updated_header)) # Sleeps for 1 second to compensate auto-refresh cycle `authorization.opa.externalRegistry.ttl = 1` time.sleep(1) diff --git a/testsuite/tests/kuadrant/authorino/conditions/section_conditions/test_metadata_condition.py b/testsuite/tests/kuadrant/authorino/conditions/section_conditions/test_metadata_condition.py index 38251425..e777c2f5 100644 --- a/testsuite/tests/kuadrant/authorino/conditions/section_conditions/test_metadata_condition.py +++ b/testsuite/tests/kuadrant/authorino/conditions/section_conditions/test_metadata_condition.py @@ -8,7 +8,7 @@ def mockserver_expectation(request, mockserver, module_label): """Creates Mockserver Expectation which returns non-empty response on hit""" request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) - return mockserver.create_expectation(module_label, "response") + return mockserver.create_response_expectation(module_label, "response") @pytest.fixture(scope="module") diff --git a/testsuite/tests/kuadrant/authorino/dinosaur/conftest.py b/testsuite/tests/kuadrant/authorino/dinosaur/conftest.py index 1bcc8ead..4add9944 100644 --- a/testsuite/tests/kuadrant/authorino/dinosaur/conftest.py +++ b/testsuite/tests/kuadrant/authorino/dinosaur/conftest.py @@ -48,7 +48,7 @@ def terms_and_conditions(request, mockserver, module_label): """Creates Mockserver Expectation that returns whether terms are required and returns its endpoint""" def _terms_and_conditions(value): - return mockserver.create_expectation( + return mockserver.create_response_expectation( f"{module_label}-terms", {"terms_required": value}, ContentType.APPLICATION_JSON, @@ -63,7 +63,7 @@ def cluster_info(request, mockserver, module_label): """Creates Mockserver Expectation that returns client ID and returns its endpoint""" def _cluster_info(value): - return mockserver.create_expectation( + return mockserver.create_response_expectation( f"{module_label}-cluster", {"client_id": value}, ContentType.APPLICATION_JSON ) @@ -76,7 +76,7 @@ def resource_info(request, mockserver, module_label): """Creates Mockserver Expectation that returns info about resource and returns its endpoint""" def _resource_info(org_id, owner): - return mockserver.create_expectation( + return mockserver.create_response_expectation( f"{module_label}-resource", {"org_id": org_id, "owner": owner}, ContentType.APPLICATION_JSON, diff --git a/testsuite/tests/kuadrant/authorino/metadata/test_http.py b/testsuite/tests/kuadrant/authorino/metadata/test_http.py index 75f3468a..d3497178 100644 --- a/testsuite/tests/kuadrant/authorino/metadata/test_http.py +++ b/testsuite/tests/kuadrant/authorino/metadata/test_http.py @@ -22,7 +22,7 @@ def country_mock_expectation(request, mockserver, module_label): """Creates Mockserver Expectation which returns simple JSON that contains `allowed_countries`""" request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) - return mockserver.create_expectation(module_label, ALLOWED_COUNTRY, ContentType.APPLICATION_JSON) + return mockserver.create_response_expectation(module_label, ALLOWED_COUNTRY, ContentType.APPLICATION_JSON) @pytest.fixture(scope="module") diff --git a/testsuite/tests/kuadrant/authorino/metadata/test_multi_element_json.py b/testsuite/tests/kuadrant/authorino/metadata/test_multi_element_json.py index 0391d65f..4a9e1a41 100644 --- a/testsuite/tests/kuadrant/authorino/metadata/test_multi_element_json.py +++ b/testsuite/tests/kuadrant/authorino/metadata/test_multi_element_json.py @@ -15,7 +15,7 @@ def json_mock_expectation(request, mockserver, module_label): """Creates Mockserver Expectation which returns multi-element JSON.""" request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) - return mockserver.create_expectation(module_label, MULTI_ELEMENT_JSON, ContentType.APPLICATION_JSON) + return mockserver.create_response_expectation(module_label, MULTI_ELEMENT_JSON, ContentType.APPLICATION_JSON) @pytest.fixture(scope="module") diff --git a/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py b/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py index 459085e7..e4e42d84 100644 --- a/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py +++ b/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py @@ -8,7 +8,7 @@ def mockserver_expectation(request, mockserver, module_label): """Creates Mockserver Expectation which returns non-empty response on hit""" request.addfinalizer(lambda: mockserver.clear_expectation(module_label)) - return mockserver.create_expectation(module_label, "response") + return mockserver.create_response_expectation(module_label, "response") @pytest.fixture(scope="module")