Skip to content

Commit

Permalink
Added tests + fixed pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matarpeles committed Oct 29, 2023
1 parent bad6f0c commit 4c7f3f8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion app/invokers/gitlab_pipeline_invoker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import urllib.parse

import requests
from core.config import settings
Expand All @@ -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,
)
Expand Down
4 changes: 1 addition & 3 deletions app/processors/kafka/kafka_to_gitlab_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions tests/unit/processors/kafka/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
48 changes: 31 additions & 17 deletions tests/unit/processors/kafka/test_kafka_to_gitlab_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand All @@ -256,4 +270,4 @@ def test_single_stream_with_subgroup_in_project_name_failure(
ANY,
),
]
)
)

0 comments on commit 4c7f3f8

Please sign in to comment.