| 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 |
| 20 |
|
| 21 |
|
| 22 |
class jenkins: |
| 23 |
class JenkinsException(Exception): |
| 24 |
pass |
| 25 |
|
| 26 |
class NotFoundException(JenkinsException): |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class JenkinsBuildMock: |
| 31 |
def get_build_status(self): |
| 32 |
try: |
| 33 |
instance = JenkinsMock() |
| 34 |
response = JenkinsMock.get_build_info(instance, "host-delete", 1234) |
| 35 |
return response |
| 36 |
except jenkins.JenkinsException: |
| 37 |
response = {} |
| 38 |
response["result"] = "ABSENT" |
| 39 |
return response |
| 40 |
except Exception as e: |
| 41 |
fail_json(msg=f"Unable to fetch build information, {e}") |
| 42 |
|
| 43 |
|
| 44 |
class JenkinsMock: |
| 45 |
def get_job_info(self, name): |
| 46 |
return {"nextBuildNumber": 1234} |
| 47 |
|
| 48 |
def get_build_info(self, name, build_number): |
| 49 |
if name == "host-delete": |
| 50 |
raise jenkins.JenkinsException(f"job {name} number {build_number} does not exist") |
| 51 |
elif name == "create-detached": |
| 52 |
return {"building": True, "result": None} |
| 53 |
return {"building": True, "result": "SUCCESS"} |
| 54 |
|
| 55 |
def build_job(self, *args): |
| 56 |
return None |
| 57 |
|
| 58 |
def delete_build(self, name, build_number): |
| 59 |
return None |
| 60 |
|
| 61 |
def stop_build(self, name, build_number): |
| 62 |
return None |
| 63 |
|
| 64 |
|
| 65 |
class JenkinsMockIdempotent: |
| 66 |
def get_job_info(self, name): |
| 67 |
return {"nextBuildNumber": 1235} |
| 68 |
|
| 69 |
def get_build_info(self, name, build_number): |
| 70 |
return {"building": False, "result": "ABORTED"} |
| 71 |
|
| 72 |
def build_job(self, *args): |
| 73 |
return None |
| 74 |
|
| 75 |
def delete_build(self, name, build_number): |
| 76 |
raise jenkins.NotFoundException(f"job {name} number {build_number} does not exist") |
| 77 |
|
| 78 |
def stop_build(self, name, build_number): |
| 79 |
return None |
| 80 |
|
| 81 |
|
| 82 |
class TestJenkinsBuild(unittest.TestCase): |
| 83 |
def setUp(self): |
| 84 |
self.mock_module_helper = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) |
| 85 |
self.mock_module_helper.start() |
| 86 |
self.addCleanup(self.mock_module_helper.stop) |
| 87 |
|
| 88 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 89 |
def test_module_fail_when_required_args_missing(self, test_deps): |
| 90 |
test_deps.return_value = None |
| 91 |
with self.assertRaises(AnsibleFailJson): |
| 92 |
with set_module_args({}): |
| 93 |
jenkins_build.main() |
| 94 |
|
| 95 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 96 |
def test_module_fail_when_missing_build_number(self, test_deps): |
| 97 |
test_deps.return_value = None |
| 98 |
with self.assertRaises(AnsibleFailJson): |
| 99 |
with set_module_args({"name": "required-if", "state": "stopped"}): |
| 100 |
jenkins_build.main() |
| 101 |
|
| 102 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 103 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 104 |
def test_module_create_build(self, jenkins_connection, test_deps): |
| 105 |
test_deps.return_value = None |
| 106 |
jenkins_connection.return_value = JenkinsMock() |
| 107 |
|
| 108 |
with self.assertRaises(AnsibleExitJson): |
| 109 |
with set_module_args({"name": "host-check", "user": "abc", "token": "xyz"}): |
| 110 |
jenkins_build.main() |
| 111 |
|
| 112 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 113 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 114 |
def test_module_stop_build(self, jenkins_connection, test_deps): |
| 115 |
test_deps.return_value = None |
| 116 |
jenkins_connection.return_value = JenkinsMock() |
| 117 |
|
| 118 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 119 |
with set_module_args( |
| 120 |
{"name": "host-check", "build_number": "1234", "state": "stopped", "user": "abc", "token": "xyz"} |
| 121 |
): |
| 122 |
jenkins_build.main() |
| 123 |
|
| 124 |
self.assertTrue(return_json.exception.args[0]["changed"]) |
| 125 |
|
| 126 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 127 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 128 |
def test_module_stop_build_again(self, jenkins_connection, test_deps): |
| 129 |
test_deps.return_value = None |
| 130 |
jenkins_connection.return_value = JenkinsMockIdempotent() |
| 131 |
|
| 132 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 133 |
with set_module_args( |
| 134 |
{"name": "host-check", "build_number": "1234", "state": "stopped", "user": "abc", "password": "xyz"} |
| 135 |
): |
| 136 |
jenkins_build.main() |
| 137 |
|
| 138 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 139 |
|
| 140 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 141 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 142 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_build_status") |
| 143 |
def test_module_delete_build(self, build_status, jenkins_connection, test_deps): |
| 144 |
test_deps.return_value = None |
| 145 |
jenkins_connection.return_value = JenkinsMock() |
| 146 |
build_status.return_value = JenkinsBuildMock().get_build_status() |
| 147 |
|
| 148 |
with self.assertRaises(AnsibleExitJson): |
| 149 |
with set_module_args( |
| 150 |
{"name": "host-delete", "build_number": "1234", "state": "absent", "user": "abc", "token": "xyz"} |
| 151 |
): |
| 152 |
jenkins_build.main() |
| 153 |
|
| 154 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 155 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 156 |
def test_module_delete_build_again(self, jenkins_connection, test_deps): |
| 157 |
test_deps.return_value = None |
| 158 |
jenkins_connection.return_value = JenkinsMockIdempotent() |
| 159 |
|
| 160 |
with self.assertRaises(AnsibleFailJson): |
| 161 |
with set_module_args( |
| 162 |
{"name": "host-delete", "build_number": "1234", "state": "absent", "user": "abc", "token": "xyz"} |
| 163 |
): |
| 164 |
jenkins_build.main() |
| 165 |
|
| 166 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 167 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 168 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_build_status") |
| 169 |
def test_module_create_build_without_detach(self, build_status, jenkins_connection, test_deps): |
| 170 |
test_deps.return_value = None |
| 171 |
jenkins_connection.return_value = JenkinsMock() |
| 172 |
build_status.return_value = JenkinsBuildMock().get_build_status() |
| 173 |
|
| 174 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 175 |
with set_module_args({"name": "create-detached", "user": "abc", "token": "xyz"}): |
| 176 |
jenkins_build.main() |
| 177 |
|
| 178 |
self.assertFalse(return_json.exception.args[0]["changed"]) |
| 179 |
|
| 180 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.test_dependencies") |
| 181 |
@patch("ansible_collections.community.general.plugins.modules.jenkins_build.JenkinsBuild.get_jenkins_connection") |
| 182 |
def test_module_create_build_detached(self, jenkins_connection, test_deps): |
| 183 |
test_deps.return_value = None |
| 184 |
jenkins_connection.return_value = JenkinsMock() |
| 185 |
|
| 186 |
with self.assertRaises(AnsibleExitJson) as return_json: |
| 187 |
with set_module_args({"name": "create-detached", "user": "abc", "token": "xyz", "detach": True}): |
| 188 |
jenkins_build.main() |
| 189 |
|
| 190 |
self.assertTrue(return_json.exception.args[0]["changed"]) |