| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import unittest |
| 8 |
from unittest.mock import patch |
| 9 |
|
| 10 |
from ansible.module_utils import basic |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 12 |
AnsibleExitJson, |
| 13 |
AnsibleFailJson, |
| 14 |
exit_json, |
| 15 |
fail_json, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
from ansible_collections.community.general.plugins.modules import jenkins_build_info |
| 20 |
|
| 21 |
|
| 22 |
class jenkins: |
| 23 |
class JenkinsException(Exception): |
| 24 |
pass |
| 25 |
|
| 26 |
|
| 27 |
class JenkinsBuildMock: |
| 28 |
def __init__(self, name, build_number=None): |
| 29 |
self.name = name |
| 30 |
self.build_number = build_number |
| 31 |
|
| 32 |
def get_build_status(self): |
| 33 |
try: |
| 34 |
instance = JenkinsMock() |
| 35 |
response = JenkinsMock.get_build_info(instance, self.name, self.build_number) |
| 36 |
return response |
| 37 |
except jenkins.JenkinsException: |
| 38 |
response = {} |
| 39 |
response["result"] = "ABSENT" |
| 40 |
return response |
| 41 |
except Exception as e: |
| 42 |
fail_json(msg=f"Unable to fetch build information, {e}") |
| 43 |
|
| 44 |
|
| 45 |
class JenkinsMock: |
| 46 |
def get_build_info(self, name, build_number): |
| 47 |
if name == "job-absent": |
| 48 |
raise jenkins.JenkinsException() |
| 49 |
|
| 50 |
return {"result": "SUCCESS", "build_info": {}} |
| 51 |
|
| 52 |
def get_job_info(self, name): |
| 53 |
if name == "job-absent": |
| 54 |
raise jenkins.JenkinsException() |
| 55 |
|
| 56 |
return {"lastBuild": {"number": 123}} |
| 57 |
|
| 58 |
|
| 59 |
class TestJenkinsBuildInfo(unittest.TestCase): |
| 60 |
def setUp(self): |
| 61 |
self.mock_module_helper = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) |
| 62 |
self.mock_module_helper.start() |
| 63 |
self.addCleanup(self.mock_module_helper.stop) |
| 64 |
|
| 65 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies") |
| 66 |
def test_module_fail_when_required_args_missing(self, test_deps): |
| 67 |
test_deps.return_value = None |
| 68 |
with self.assertRaises(AnsibleFailJson): |
| 69 |
with set_module_args({}): |
| 70 |
jenkins_build_info.main() |
| 71 |
|
| 72 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies") |
| 73 |
@patch( |
| 74 |
"ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection" |
| 75 |
) |
| 76 |
def test_module_get_build_info(self, jenkins_connection, test_deps): |
| 77 |
test_deps.return_value = None |
| 78 |
jenkins_connection.return_value = JenkinsMock() |
| 79 |
|
| 80 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 81 |
with set_module_args({"name": "job-present", "user": "abc", "token": "xyz", "build_number": 30}): |
| 82 |
jenkins_build_info.main() |
| 83 |
|
| 84 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 85 |
|
| 86 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies") |
| 87 |
@patch( |
| 88 |
"ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection" |
| 89 |
) |
| 90 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_build_status") |
| 91 |
def test_module_get_build_info_if_build_does_not_exist(self, build_status, jenkins_connection, test_deps): |
| 92 |
test_deps.return_value = None |
| 93 |
jenkins_connection.return_value = JenkinsMock() |
| 94 |
build_status.return_value = JenkinsBuildMock("job-absent", 30).get_build_status() |
| 95 |
|
| 96 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 97 |
with set_module_args({"name": "job-absent", "user": "abc", "token": "xyz", "build_number": 30}): |
| 98 |
jenkins_build_info.main() |
| 99 |
|
| 100 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 101 |
self.assertTrue(return_json.exception.args[0]["failed"]) |
| 102 |
self.assertEqual("ABSENT", return_json.exception.args[0]["build_info"]["result"]) |
| 103 |
|
| 104 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies") |
| 105 |
@patch( |
| 106 |
"ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection" |
| 107 |
) |
| 108 |
def test_module_get_build_info_get_last_build(self, jenkins_connection, test_deps): |
| 109 |
test_deps.return_value = None |
| 110 |
jenkins_connection.return_value = JenkinsMock() |
| 111 |
|
| 112 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 113 |
with set_module_args({"name": "job-present", "user": "abc", "token": "xyz"}): |
| 114 |
jenkins_build_info.main() |
| 115 |
|
| 116 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 117 |
self.assertEqual("SUCCESS", return_json.exception.args[0]["build_info"]["result"]) |
| 118 |
|
| 119 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.test_dependencies") |
| 120 |
@patch( |
| 121 |
"ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_jenkins_connection" |
| 122 |
) |
| 123 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build_info.JenkinsBuildInfo.get_build_status") |
| 124 |
def test_module_get_build_info_if_job_does_not_exist(self, build_status, jenkins_connection, test_deps): |
| 125 |
test_deps.return_value = None |
| 126 |
jenkins_connection.return_value = JenkinsMock() |
| 127 |
build_status.return_value = JenkinsBuildMock("job-absent").get_build_status() |
| 128 |
|
| 129 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 130 |
with set_module_args({"name": "job-absent", "user": "abc", "token": "xyz"}): |
| 131 |
jenkins_build_info.main() |
| 132 |
|
| 133 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 134 |
self.assertTrue(return_json.exception.args[0]["failed"]) |
| 135 |
self.assertEqual("ABSENT", return_json.exception.args[0]["build_info"]["result"]) |