From 9730e8804522b410c59876de49c7b12fcde32cc2 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Mon, 28 Nov 2022 13:46:04 +0100 Subject: [PATCH] Issue #78 force/hardcode sentinelhub as secondary service backend for now --- src/openeo_aggregator/backend.py | 12 +++++--- tests/conftest.py | 20 +++++++++++-- tests/test_views.py | 51 ++++++++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/openeo_aggregator/backend.py b/src/openeo_aggregator/backend.py index 5cc752cf..a16c6d60 100644 --- a/src/openeo_aggregator/backend.py +++ b/src/openeo_aggregator/backend.py @@ -749,12 +749,16 @@ def create_service(self, user_id: str, process_graph: dict, service_type: str, a """ # TODO: configuration is not used. What to do with it? - # TODO: Determine backend based on service type? + # TODO: hardcoded/forced "SentinelHub only" support for now. + # Instead, properly determine backend based on service type? # See https://github.com/Open-EO/openeo-aggregator/issues/78#issuecomment-1326180557 # and https://github.com/Open-EO/openeo-aggregator/issues/83 - backend_id = self._processing.get_backend_for_process_graph( - process_graph=process_graph, api_version=api_version - ) + if "sentinelhub" in self._backends._backend_urls: + backend_id = "sentinelhub" + else: + backend_id = self._processing.get_backend_for_process_graph( + process_graph=process_graph, api_version=api_version + ) process_graph = self._processing.preprocess_process_graph(process_graph, backend_id=backend_id) con = self._backends.get_connection(backend_id) diff --git a/tests/conftest.py b/tests/conftest.py index 424c52c4..18e2faba 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -101,12 +101,26 @@ def base_config( @pytest.fixture -def config(base_config, backend1, backend2) -> AggregatorConfig: +def backend1_id() -> str: + """Id of first upstream backend. As a fixture to allow per-test override""" + return "b1" + + +@pytest.fixture +def backend2_id() -> str: + """Id of second upstream backend. As a fixture to allow per-test override""" + return "b2" + + +@pytest.fixture +def config( + base_config, backend1, backend2, backend1_id, backend2_id +) -> AggregatorConfig: """Config for most tests with two backends.""" conf = base_config conf.aggregator_backends = { - "b1": backend1, - "b2": backend2, + backend1_id: backend1, + backend2_id: backend2, } return conf diff --git a/tests/test_views.py b/tests/test_views.py index e73566d6..e4adbdba 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1475,13 +1475,13 @@ def test_create_wmts(self, api100, requests_mock, backend1): api100.set_auth_bearer_token(TEST_USER_BEARER_TOKEN) backend_service_id = 'c63d6c27-c4c2-4160-b7bd-9e32f582daec' - expected_openeo_id = "b1-" + backend_service_id + expected_agg_id = f"b1-{backend_service_id}" # The aggregator MUST NOT point to the backend instance but to its own endpoint. # This is handled by the openeo python driver in openeo_driver.views.services_post. - expected_location = "/openeo/1.0.0/services/" + expected_openeo_id + expected_location = f"/openeo/1.0.0/services/{expected_agg_id}" # However, backend1 must report its OWN location. - location_backend_1 = backend1 + "/services" + backend_service_id + location_backend_1 = f"{backend1}/services/{backend_service_id}" process_graph = {"foo": {"process_id": "foo", "arguments": {}}} post_data = { @@ -1504,8 +1504,49 @@ def test_create_wmts(self, api100, requests_mock, backend1): resp = api100.post('/services', json=post_data).assert_status_code(201) - assert resp.headers['OpenEO-Identifier'] == expected_openeo_id - assert resp.headers['Location'] == expected_location + assert resp.headers["OpenEO-Identifier"] == expected_agg_id + assert resp.headers["Location"] == expected_location + + @pytest.mark.parametrize("backend2_id", ["sentinelhub"]) + def test_create_wmts_forced_sentinelhub( + self, api100, requests_mock, backend1, backend2_id, backend2 + ): + """When the payload is correct the service should be successfully created, + the service ID should be prepended with the backend ID, + and location should point to the aggregator, not to the backend directly. + """ + # TODO this is a temp test for a temp "force sentinelhub" workaround hack (https://github.com/Open-EO/openeo-aggregator/issues/78) + + api100.set_auth_bearer_token(TEST_USER_BEARER_TOKEN) + + backend_service_id = "c63d6c27-c4c2-4160-b7bd-9e32f582daec" + expected_agg_id = "sentinelhub-" + backend_service_id + + # The aggregator MUST NOT point to the backend instance but to its own endpoint. + # This is handled by the openeo python driver in openeo_driver.views.services_post. + expected_location = f"/openeo/1.0.0/services/{expected_agg_id}" + upstream_location = f"{backend2}/services/{backend_service_id}" + + process_graph = {"foo": {"process_id": "foo", "arguments": {}}} + post_data = { + "type": "WMTS", + "process": {"process_graph": process_graph, "id": "filter_temporal_wmts"}, + "title": "My Service", + "description": "Service description", + } + requests_mock.post( + backend2 + "/services", + headers={ + "OpenEO-Identifier": backend_service_id, + "Location": upstream_location, + }, + status_code=201, + ) + + resp = api100.post("/services", json=post_data).assert_status_code(201) + + assert resp.headers["OpenEO-Identifier"] == expected_agg_id + assert resp.headers["Location"] == expected_location # ProcessGraphMissingException and ProcessGraphInvalidException are well known reasons for a bad client request. @pytest.mark.parametrize("exception_class", [ProcessGraphMissingException, ProcessGraphInvalidException])