"""Test script file.""" import os from unittest.mock import Mock from unittest.mock import patch from alerts import script import pytest JENKINS_USER = os.getenv('JENKINS_USER') JENKINS_TOKEN = os.getenv('JENKINS_TOKEN') GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') LINK_TO_POST = os.getenv('LINK_TO_POST') @pytest.fixture def red_jobs(): """Fixture for the red pipelines.""" return ([{ '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'abacus-deploy-pipeline', 'url': 'https://pipeline.theorchard.io/job/abacus-deploy-pipeline/', 'color': 'red'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'akhq-pipeline', 'url': 'https://pipeline.theorchard.io/job/akhq-pipeline/', 'color': 'red'}]) @pytest.fixture def job(): """Fixture for the pipeline.""" return ({'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'akhq-pipeline', 'url': 'https://pipeline.theorchard.io/job/akhq-pipeline/', 'color': 'red'}) @pytest.fixture def not_wip_jobs(): """Fixture for the filtered pipelines.""" return ([{ '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'abacus-deploy-pipeline', 'url': 'https://pipeline.theorchard.io/job/abacus-deploy-pipeline/', 'color': 'red', 'description': 'smth'}, { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'akhq-pipeline', 'url': 'https://pipeline.theorchard.io/job/akhq-pipeline/', 'color': 'red', 'description': 'smth'}]) @pytest.fixture def pipeline_api(): """Fixture for the pipeline api.""" return { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'color': 'red', 'lastCompletedBuild': { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'number': 59, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/59/' # noqa: E501 }, 'lastFailedBuild': { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'number': 59, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/59/' # noqa: E501 }, 'lastStableBuild': { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'number': 55, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/55/' # noqa: E501 }, 'lastSuccessfulBuild': { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'number': 55, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/55/' # noqa: E501 }, 'lastUnstableBuild': None, 'lastUnsuccessfulBuild': { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'number': 59, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/59/' # noqa: E501 } } @pytest.fixture def last_unsuccessful_build(): """Fixture for the pipeline last unsuccessful build api.""" return { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'actions': [ { '_class': 'hudson.plugins.git.util.BuildData', 'remoteUrls': [ 'git@github.com:theorchard/bff-notifications.git' ], 'scmName': '' }, ], 'nextBuild': None, 'previousBuild': { 'number': 58, 'url': 'https://pipeline.theorchard.io/job/bff-notifications-pipeline/58/' # noqa: E501 }, 'timestamp': 1641244346115, } @pytest.fixture def jobs_to_post(): """Fixture for the format {repo, author, pipeline} links.""" return [{'repo': 'https://github.com/theorchard/auth0-hosted-pages', 'author': 'https://github.com/ivnnv', 'url': 'https://pipeline.theorchard.io/job/auth0-hosted-pages-pipeline/', # noqa: E501 'LastExecution': '2022-03-22', 'days': '10'}, {'repo': 'https://github.com/theorchard/daemon-asset-copy', 'author': 'https://github.com/elinapohrebna', 'url': 'https://pipeline.theorchard.io/job/daemon-asset-copy-pipeline/', # noqa: E501 'LastExecution': '2022-03-22', 'days': '20'}, {'repo': 'https://github.com/theorchard/daemon-video', 'author': 'https://github.com/piannone', 'url': 'https://pipeline.theorchard.io/job/daemon-video-pipeline/', # noqa: E501 'LastExecution': '2022-03-22', 'days': '0'} ] @pytest.fixture def days_over_2_weeks(): """Fixture for the list of pipelines' dates > 14.""" return 20 def test_get_red_jobs(red_jobs): """Check the jobs filtering.""" assert script.get_red_jobs([{ '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', # noqa: E501 'name': 'abacus-deploy-pipeline', 'url': 'https://pipeline.theorchard.io/job/abacus-deploy-pipeline/', # noqa: E501 'color': 'red'}, { '_class': 'hudson.model.FreeStyleProject', 'name': 'abacus-sync-static-data-to-s3', 'url': 'https://pipeline.theorchard.io/job/abacus-sync-static-data-to-s3/', # noqa: E501 'color': 'notbuild'}, { '_class': 'hudson.model.FreeStyleProject', 'name': 'add-users-auth0-orgs', 'url': 'https://pipeline.theorchard.io/job/add-users-auth0-orgs/', # noqa: E501 'color': 'blue'}, { '_class': 'hudson.model.FreeStyleProject', 'name': 'ae-qa-test-android', 'url': 'https://pipeline.theorchard.io/job/ae-qa-test-android/', # noqa: E501 'color': 'disabled'}, { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', # noqa: E501 'name': 'akhq-pipeline', 'url': 'https://pipeline.theorchard.io/job/akhq-pipeline/', # noqa: E501 'color': 'red'}]) == red_jobs @patch('alerts.script.requests.get') def test_filter_pipelines(requests_mock, not_wip_jobs): """Check the wip filtering.""" pipelines = [{ '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'abacus-deploy-pipeline', 'url': 'https://pipeline.theorchard.io/job/abacus-deploy-pipeline/', 'color': 'red', 'description': 'smth'}, { '_class': 'hudson.model.FreeStyleProject', 'name': 'add-users-auth0-orgs', 'url': 'https://pipeline.theorchard.io/job/add-users-auth0-orgs/', # noqa: E501 'color': 'red', 'description': 'wip'}, { '_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'akhq-pipeline', 'url': 'https://pipeline.theorchard.io/job/akhq-pipeline/', 'color': 'red', 'description': 'smth'}] requests_mock.return_value.json.side_effect = [pipelines[0], pipelines[1], pipelines[2]] # noqa: E501 result = script.filter_pipelines(pipelines) assert result == not_wip_jobs def test_format_git_links(): """Check the git links formatting.""" links = [{'GitLink': 'git@github.com:theorchard/bff-notifications.git'}, {'GitLink': 'git@github.com:theorchard/bff-promo-player.git'}, {'GitLink': 'git@github.com:sorry-cypress/sorry-cypress.git'}, {'GitLink': 'git@github.com:theorchard/docker-parent-images.git'}, {'GitLink': 'git@github.com:theorchard/frontend-catalog.git'}] for each_link in links: script.format_git_links(each_link) assert links == [{'GitLink': 'theorchard/bff-notifications'}, {'GitLink': 'theorchard/bff-promo-player'}, {'GitLink': 'sorry-cypress/sorry-cypress'}, {'GitLink': 'theorchard/docker-parent-images'}, {'GitLink': 'theorchard/frontend-catalog'}] @patch('alerts.script.json.dumps') @patch('alerts.script.requests.post') def test_post_to_slack(requests_mock, json_mock, jobs_to_post): """Check posting to slack.""" post_mock = Mock() json_dumps_mock = Mock() json_mock.return_value = json_dumps_mock requests_mock.post.return_value = post_mock script.post_to_slack(jobs_to_post) calls_count = requests_mock.call_count data_count = len(jobs_to_post) requests_mock.assert_called_with(LINK_TO_POST, data=json_dumps_mock, headers={'Content-Type': 'application/json'}) assert calls_count == data_count @patch('alerts.script.json.dumps') @patch('alerts.script.requests.post') def test_post_header_to_slack(requests_mock, json_mock, jobs_to_post): """Check posting header to slack.""" post_mock = Mock() json_dumps_mock = Mock() json_mock.return_value = json_dumps_mock requests_mock.post.return_value = post_mock script.post_header_to_slack(jobs_to_post) requests_mock.assert_called_with(LINK_TO_POST, data=json_dumps_mock, headers={'Content-Type': 'application/json'}) @patch('alerts.script.requests.get') def test_get_pipelines(requests_mock): """Check get_pipeline function.""" get_mock = Mock() requests_mock.get.return_value = get_mock script.get_pipelines() requests_mock.assert_called_with('https://pipeline.theorchard.io/api/json', auth=(JENKINS_USER, JENKINS_TOKEN)) def test_search_for_url_in_last_unsuccessful_build(last_unsuccessful_build, job): # noqa: E501 """Check search_for_url_in_last_unsuccessful_build function.""" script.search_for_url_in_last_unsuccessful_build( last_unsuccessful_build, job) assert job['GitLink'] == 'git@github.com:theorchard/bff-notifications.git' @patch('alerts.script.requests.get') def test_search_last_build_git_link(requests_mock, last_unsuccessful_build, pipeline_api, job): """Check search_last_build_git_link function.""" get_mock = Mock(return_value=last_unsuccessful_build) requests_mock.get.return_value = get_mock script.search_last_build_git_link(pipeline_api, job) requests_mock.assert_called_with('https://pipeline.theorchard.io/job/bff-notifications-pipeline/59/api/json/', # noqa: E501 auth=(JENKINS_USER, JENKINS_TOKEN)) def test_get_pipeline_date(last_unsuccessful_build, job): """Check get_pipeline_date function to properly convert timestamp.""" result = script.get_pipeline_date(last_unsuccessful_build, job) assert result == '2022-01-03' def test_get_all_days_over_2_weeks(jobs_to_post, days_over_2_weeks): """Check get_all_days_over_2_weeks function to properly filter by days > 14.""" # noqa: E501 result = script.get_all_days_over_2_weeks(jobs_to_post, []) assert int(result[0]['days']) == days_over_2_weeks def test_delete_todays_pipelines(jobs_to_post): """Check delete_todays_pipelines func to remove <1 day pipelines.""" result = script.delete_todays_pipelines(jobs_to_post) assert result == jobs_to_post[:-1]