"""Test for Jenkins related tasks.""" from unittest.mock import call, MagicMock, patch import pytest from feed_ingestion.util.jenkins.tasks import build_jenkins_dbt MOCK_JENKINS_CONFIG = { 'feeds_required_for_jenkins_build': ['spotify_licensorplaceholder_streams', 'spotify_licensorplaceholder_aggregated_streams'], 'jenkins_username': 'jenkinsjobrunner', 'jenkins_secrets_path': 'swf_feed_ingestion', 'jenkins_url': 'https://scheduler.theorchard.io', 'jenkins_job': 'dbt-scheduler-analytics-pipeline', 'jenkins_job_params': {'FULL_REFRESH': False}, } @pytest.fixture(autouse=True) def mock_get_overall_status(): """Yield overall status.""" overall_status_path = ( 'garcon_contrib.dynamo_feed_status.garcon_feed_status.' 'get_overall_status') with patch(overall_status_path) as overall_status: yield overall_status @pytest.fixture(autouse=True) def mock_task_status(): """Yield task status.""" task_status_path = 'feed_ingestion.tasks.task_status' with patch(task_status_path) as task_status: task_status.is_completed_task.return_value = False task_status.mark_completed_task = MagicMock() yield task_status @pytest.fixture(autouse=True) def mock_jenkins(): """Mock jenkins.""" jenkins_path = 'feed_ingestion.util.jenkins.tasks.jenkins' with patch(jenkins_path) as jenkins: jenkins.Jenkins.return_value = MagicMock() yield jenkins @pytest.fixture(autouse=True) def mock_get_secret(): """Yield get_secret.""" path = 'feed_ingestion.util.jenkins.tasks.get_secret' with patch(path) as get_secret: get_secret.return_value = 'pass' yield get_secret def test_build_jenkins_dbt_without_build_dbt( mock_get_secret, mock_jenkins, mock_task_status, mock_get_overall_status): """Test test_build_jenkins_dbt if build_dbt is not 'True'.""" response = build_jenkins_dbt( MagicMock(), '', 'theorchard', '2023-01-01', '', MOCK_JENKINS_CONFIG) mock_get_overall_status.assert_not_called() assert not mock_task_status.mark_completed_task.called mock_jenkins.assert_not_called() assert response == { 'stop': True, 'reason': 'Skipping, as "build_dbt" is not True', } mock_task_status.mark_completed_task.assert_not_called() def test_build_jenkins_dbt_with_not_ingested_reports( mock_get_secret, mock_jenkins, mock_task_status, mock_get_overall_status): """Test test_build_jenkins_dbt if reports are not ingested.""" mock_get_overall_status.return_value = 'NOT_INGESTED' response = build_jenkins_dbt( MagicMock(), '', 'theorchard', '2023-01-01', 'True', MOCK_JENKINS_CONFIG) assert not mock_task_status.mark_completed_task.called mock_get_overall_status.assert_any_call( 'spotify_theorchard_streams', '2023-01-01') mock_jenkins.assert_not_called() assert response == { 'stop': True, 'reason': 'spotify_theorchard_streams at 2023-01-01 ' 'is not ingested yet', } mock_task_status.mark_completed_task.assert_not_called() def test_build_jenkins_dbt_with_ingested_reports( mock_get_secret, mock_jenkins, mock_task_status, mock_get_overall_status): """Test test_build_jenkins_dbt if reports are ingested.""" mock_get_overall_status.return_value = 'INGESTED' mock_jenkins.Jenkins.return_value.build_job.return_value = '27' response = build_jenkins_dbt( MagicMock(), 'some_feed', 'theorchard', '2023-01-01', 'True', MOCK_JENKINS_CONFIG) assert mock_task_status.mark_completed_task.called assert mock_task_status.mark_completed_task.call_args_list == [ call('some_feed', '2023-01-01', 'build_jenkins_dbt') ] mock_get_overall_status.assert_has_calls( [call(report.replace( 'licensorplaceholder', 'theorchard'), '2023-01-01') for report in MOCK_JENKINS_CONFIG.get( 'feeds_required_for_jenkins_build')], any_order=True) mock_jenkins.Jenkins.assert_called_with( MOCK_JENKINS_CONFIG.get('jenkins_url'), username='jenkinsjobrunner', password='pass') mock_jenkins.Jenkins.return_value.build_job.assert_called_with( MOCK_JENKINS_CONFIG.get('jenkins_job'), MOCK_JENKINS_CONFIG.get('jenkins_job_params')) mock_get_secret.assert_called_with( MOCK_JENKINS_CONFIG.get('jenkins_secrets_path'), 'JENKINS_API_TOKEN') assert response == { 'job_url': 'https://scheduler.theorchard.io' '/job/dbt-scheduler-analytics-pipeline/', 'message': 'Triggered job dbt-scheduler-analytics-pipeline' ', queue id: 27' }