Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chouetz committed Mar 5, 2025
1 parent f07858b commit 0f26a51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tasks/libs/notify/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_pipeline_type():
"""
if os.environ.get('DEPLOY_AGENT', '') == 'true':
return 'deploy'
elif os.environ.get('TRIGGERED_PIPELINE', 'false') == 'true':
elif os.environ.get('TRIGGERED_PIPELINE', 'false') == 'true' or 'DDR_WORKFLOW_ID' in os.environ:
return 'trigger'
else:
return 'merge'
26 changes: 24 additions & 2 deletions tasks/unit_tests/notify_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ def test_merge(self, api_mock, print_mock):
self.assertTrue("merge" in print_mock.mock_calls[0].args[0])
repo_mock.jobs.get.assert_called()

@patch('tasks.libs.pipeline.notifications.get_pr_from_commit', new=MagicMock(return_value=""))
@patch('tasks.libs.notify.pipeline_status.get_jobs_skipped_on_pr', new=MagicMock(return_value=([], "")))
@patch('slack_sdk.WebClient', new=MagicMock())
@patch('builtins.print')
@patch('tasks.libs.ciproviders.gitlab_api.get_gitlab_api')
def test_merge_ddci(self, api_mock, print_mock):
repo_mock = api_mock.return_value.projects.get.return_value
repo_mock.jobs.get.return_value.artifact.return_value = b"{}"
repo_mock.jobs.get.return_value.trace.return_value = b"Log trace"
repo_mock.pipelines.get.return_value.ref = "test"
repo_mock.pipelines.get.return_value.source = "api"
repo_mock.pipelines.get.return_value.started_at = "2025-02-07T05:59:48.396Z"
repo_mock.pipelines.get.return_value.finished_at = "2025-02-07T06:59:48.396Z"
list_mock = repo_mock.pipelines.get.return_value.jobs.list
list_mock.side_effect = [get_fake_jobs(), []]
with patch.dict('os.environ', {'SLACK_DATADOG_AGENT_BOT_TOKEN': 'coin'}, clear=True):
notify.send_message(MockContext(), "42", dry_run=True)
list_mock.assert_called()
repo_mock.pipelines.get.assert_called_with("42")
self.assertTrue("merge" in print_mock.mock_calls[0].args[0])
repo_mock.jobs.get.assert_called()

@patch('tasks.libs.pipeline.notifications.get_pr_from_commit', new=MagicMock(return_value=""))
@patch('tasks.libs.notify.pipeline_status.get_jobs_skipped_on_pr', new=MagicMock(return_value=([], "")))
@patch('slack_sdk.WebClient', new=MagicMock())
Expand Down Expand Up @@ -264,6 +286,7 @@ def test_deploy_with_get_failed_call(self, print_mock, api_mock):
@patch('tasks.libs.pipeline.notifications.get_pr_from_commit', new=MagicMock(return_value=""))
@patch('tasks.libs.notify.pipeline_status.get_jobs_skipped_on_pr', new=MagicMock(return_value=([], "")))
@patch('slack_sdk.WebClient', new=MagicMock())
@patch.dict('os.environ', {'SLACK_DATADOG_AGENT_BOT_TOKEN': 'miaou', 'TRIGGERED_PIPELINE': 'true'}, clear=True)
@patch('tasks.libs.ciproviders.gitlab_api.get_gitlab_api')
@patch('builtins.print')
def test_trigger_with_get_failed_call(self, print_mock, api_mock):
Expand All @@ -279,8 +302,7 @@ def test_trigger_with_get_failed_call(self, print_mock, api_mock):
repo_mock.pipelines.get.return_value.started_at = "2025-02-07T05:59:48.396Z"
repo_mock.pipelines.get.return_value.finished_at = "2025-02-07T06:59:48.396Z"

with patch.dict('os.environ', {'SLACK_DATADOG_AGENT_BOT_TOKEN': 'miaou'}, clear=True):
notify.send_message(MockContext(), "42", dry_run=True)
notify.send_message(MockContext(), "42", dry_run=True)
self.assertTrue("arrow_forward" in print_mock.mock_calls[0].args[0])
self.assertTrue("[:hourglass: 60 min]" in print_mock.mock_calls[0].args[0])
trace_mock.assert_called()
Expand Down

0 comments on commit 0f26a51

Please sign in to comment.