From 4c7f3f8115e5e9d2a5eab2134156ce78fd78b765 Mon Sep 17 00:00:00 2001 From: matarpeles Date: Sun, 29 Oct 2023 14:47:27 +0200 Subject: [PATCH] Added tests + fixed pr comments --- app/invokers/gitlab_pipeline_invoker.py | 3 +- .../kafka/kafka_to_gitlab_processor.py | 4 +- tests/unit/processors/kafka/conftest.py | 1 + .../kafka/test_kafka_to_gitlab_processor.py | 48 ++++++++++++------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app/invokers/gitlab_pipeline_invoker.py b/app/invokers/gitlab_pipeline_invoker.py index 8ba0c8c..eb3b02f 100644 --- a/app/invokers/gitlab_pipeline_invoker.py +++ b/app/invokers/gitlab_pipeline_invoker.py @@ -1,4 +1,5 @@ import logging +import urllib.parse import requests from core.config import settings @@ -13,7 +14,7 @@ def invoke(self, body: dict, project_path: str) -> None: logger.info("GitLabPipelineInvoker - start - project: %s", project_path) res = requests.post( - f"{settings.GITLAB_URL}/api/v4/projects/{project_path}/trigger/pipeline", + f"{settings.GITLAB_URL}/api/v4/projects/{urllib.parse.quote(project_path, safe='')}/trigger/pipeline", json=body, timeout=settings.GITLAB_PIPELINE_INVOKER_TIMEOUT, ) diff --git a/app/processors/kafka/kafka_to_gitlab_processor.py b/app/processors/kafka/kafka_to_gitlab_processor.py index 25ea5cd..0d86f34 100644 --- a/app/processors/kafka/kafka_to_gitlab_processor.py +++ b/app/processors/kafka/kafka_to_gitlab_processor.py @@ -62,9 +62,7 @@ def msg_process(msg: Message, invocation_method: dict, topic: str) -> None: if not invocation_method.get("omitPayload"): body["port_payload"] = msg_value.copy() - gitlab_pipeline_invoker.invoke( - body, f'{gitlab_group}%2F{gitlab_project.replace("/", "%2F")}' - ) + gitlab_pipeline_invoker.invoke(body, f"{gitlab_group}/{gitlab_project}") logger.info( "Successfully processed message from topic %s, partition %d, offset %d", diff --git a/tests/unit/processors/kafka/conftest.py b/tests/unit/processors/kafka/conftest.py index 565ecaa..cfeb2d8 100644 --- a/tests/unit/processors/kafka/conftest.py +++ b/tests/unit/processors/kafka/conftest.py @@ -258,6 +258,7 @@ def get_gitlab_run_message(invocation_method: dict) -> bytes: def mock_gitlab_token(monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("group_project", "token") + @pytest.fixture def mock_gitlab_token_subgroup(monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("group_subgroup_sub2_project", "token") diff --git a/tests/unit/processors/kafka/test_kafka_to_gitlab_processor.py b/tests/unit/processors/kafka/test_kafka_to_gitlab_processor.py index a042106..c118d51 100644 --- a/tests/unit/processors/kafka/test_kafka_to_gitlab_processor.py +++ b/tests/unit/processors/kafka/test_kafka_to_gitlab_processor.py @@ -19,7 +19,9 @@ ], indirect=True, ) -def test_single_stream_success(mock_requests: None, mock_kafka: None, mock_gitlab_token: None) -> None: +def test_single_stream_success( + mock_requests: None, mock_kafka: None, mock_gitlab_token: None +) -> None: Timer(0.01, terminate_consumer).start() with mock.patch.object(consumer_logger, "error") as mock_error: @@ -180,63 +182,75 @@ def test_single_stream_skipped_due_to_wrong_token( ] ) + @pytest.mark.parametrize("mock_requests", [{"status_code": 200}], indirect=True) @pytest.mark.parametrize( "mock_kafka", [ ( "mock_gitlab_run_message", - {"type": "GITLAB", "agent": True, "groupName": "group", "projectName": "subgroup/sub2/project"}, + { + "type": "GITLAB", + "agent": True, + "groupName": "group", + "projectName": "subgroup/sub2/project", + }, settings.KAFKA_RUNS_TOPIC, ), ], indirect=True, ) def test_single_stream_with_subgroup_in_project_name( - mock_requests: None, mock_kafka: None, mock_gitlab_token_subgroup: None + mock_requests: None, mock_kafka: None, mock_gitlab_token_subgroup: None ) -> None: Timer(0.01, terminate_consumer).start() with mock.patch.object(consumer_logger, "error") as mock_error, mock.patch.object( - gitlab_processor_logger, "info" + gitlab_processor_logger, "info" ) as mock_info: streamer = KafkaStreamer(Consumer()) streamer.stream() call_of_missing_token = call( - "Skip process message" - " from topic %s, partition %d, offset %d:" - " no token env variable found for project %s/%s", - ANY, - 0, - 0, - ANY, - ANY, - ) + "Skip process message" + " from topic %s, partition %d, offset %d:" + " no token env variable found for project %s/%s", + ANY, + 0, + 0, + ANY, + ANY, + ) # Check if the expected calls were not made assert call_of_missing_token not in mock_info.call_args_list + @pytest.mark.parametrize("mock_requests", [{"status_code": 200}], indirect=True) @pytest.mark.parametrize( "mock_kafka", [ ( "mock_gitlab_run_message", - {"type": "GITLAB", "agent": True, "groupName": "group", "projectName": "wrong/sub2/project"}, + { + "type": "GITLAB", + "agent": True, + "groupName": "group", + "projectName": "wrong/sub2/project", + }, settings.KAFKA_RUNS_TOPIC, ), ], indirect=True, ) def test_single_stream_with_subgroup_in_project_name_failure( - mock_requests: None, mock_kafka: None, mock_gitlab_token_subgroup: None + mock_requests: None, mock_kafka: None, mock_gitlab_token_subgroup: None ) -> None: Timer(0.01, terminate_consumer).start() with mock.patch.object(consumer_logger, "error") as mock_error, mock.patch.object( - gitlab_processor_logger, "info" + gitlab_processor_logger, "info" ) as mock_info: streamer = KafkaStreamer(Consumer()) @@ -256,4 +270,4 @@ def test_single_stream_with_subgroup_in_project_name_failure( ANY, ), ] - ) \ No newline at end of file + )