| 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 |
from unittest.mock import call, patch |
| 8 |
from xml.etree import ElementTree as et |
| 9 |
|
| 10 |
import jenkins |
| 11 |
import pytest |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
AnsibleExitJson, |
| 14 |
AnsibleFailJson, |
| 15 |
exit_json, |
| 16 |
fail_json, |
| 17 |
set_module_args, |
| 18 |
) |
| 19 |
from pytest import fixture, mark, param, raises |
| 20 |
|
| 21 |
from ansible_collections.community.general.plugins.modules import jenkins_node |
| 22 |
|
| 23 |
|
| 24 |
def xml_equal(x, y): |
| 25 |
# type: (et.Element | str, et.Element | str) -> bool |
| 26 |
if isinstance(x, str): |
| 27 |
x = et.fromstring(x) |
| 28 |
|
| 29 |
if isinstance(y, str): |
| 30 |
y = et.fromstring(y) |
| 31 |
|
| 32 |
if x.tag != y.tag: |
| 33 |
return False |
| 34 |
|
| 35 |
if x.attrib != y.attrib: |
| 36 |
return False |
| 37 |
|
| 38 |
if (x.text or "").strip() != (y.text or "").strip(): |
| 39 |
return False |
| 40 |
|
| 41 |
x_children = list(x) |
| 42 |
y_children = list(y) |
| 43 |
|
| 44 |
if len(x_children) != len(y_children): |
| 45 |
return False |
| 46 |
|
| 47 |
return all(xml_equal(x, y) for x, y in zip(x_children, y_children)) |
| 48 |
|
| 49 |
|
| 50 |
def assert_xml_equal(x, y): |
| 51 |
if xml_equal(x, y): |
| 52 |
return True |
| 53 |
|
| 54 |
if not isinstance(x, str): |
| 55 |
x = et.tostring(x) |
| 56 |
|
| 57 |
if not isinstance(y, str): |
| 58 |
y = et.tostring(y) |
| 59 |
|
| 60 |
raise AssertionError(f"{x} != {y}") |
| 61 |
|
| 62 |
|
| 63 |
@fixture(autouse=True) |
| 64 |
def module(): |
| 65 |
with patch.multiple( |
| 66 |
"ansible.module_utils.basic.AnsibleModule", |
| 67 |
exit_json=exit_json, |
| 68 |
fail_json=fail_json, |
| 69 |
): |
| 70 |
yield |
| 71 |
|
| 72 |
|
| 73 |
@fixture |
| 74 |
def instance(): |
| 75 |
with patch("jenkins.Jenkins", autospec=True) as instance: |
| 76 |
yield instance |
| 77 |
|
| 78 |
|
| 79 |
@fixture |
| 80 |
def get_instance(instance): |
| 81 |
with patch( |
| 82 |
"ansible_collections.community.general.plugins.modules.jenkins_node.JenkinsNode.get_jenkins_instance", |
| 83 |
autospec=True, |
| 84 |
) as mock: |
| 85 |
mock.return_value = instance |
| 86 |
yield mock |
| 87 |
|
| 88 |
|
| 89 |
def test_get_jenkins_instance_with_user_and_token(instance): |
| 90 |
instance.node_exists.return_value = False |
| 91 |
|
| 92 |
with set_module_args( |
| 93 |
{ |
| 94 |
"name": "my-node", |
| 95 |
"state": "absent", |
| 96 |
"url": "https://localhost:8080", |
| 97 |
"user": "admin", |
| 98 |
"token": "password", |
| 99 |
} |
| 100 |
): |
| 101 |
with pytest.raises(AnsibleExitJson): |
| 102 |
jenkins_node.main() |
| 103 |
|
| 104 |
assert instance.call_args == call("https://localhost:8080", "admin", "password") |
| 105 |
|
| 106 |
|
| 107 |
def test_get_jenkins_instance_with_user(instance): |
| 108 |
instance.node_exists.return_value = False |
| 109 |
|
| 110 |
with set_module_args( |
| 111 |
{ |
| 112 |
"name": "my-node", |
| 113 |
"state": "absent", |
| 114 |
"url": "https://localhost:8080", |
| 115 |
"user": "admin", |
| 116 |
} |
| 117 |
): |
| 118 |
with pytest.raises(AnsibleExitJson): |
| 119 |
jenkins_node.main() |
| 120 |
|
| 121 |
assert instance.call_args == call("https://localhost:8080", "admin") |
| 122 |
|
| 123 |
|
| 124 |
def test_get_jenkins_instance_with_no_credential(instance): |
| 125 |
instance.node_exists.return_value = False |
| 126 |
|
| 127 |
with set_module_args( |
| 128 |
{ |
| 129 |
"name": "my-node", |
| 130 |
"state": "absent", |
| 131 |
"url": "https://localhost:8080", |
| 132 |
} |
| 133 |
): |
| 134 |
with pytest.raises(AnsibleExitJson): |
| 135 |
jenkins_node.main() |
| 136 |
|
| 137 |
assert instance.call_args == call("https://localhost:8080") |
| 138 |
|
| 139 |
|
| 140 |
PRESENT_STATES = ["present", "enabled", "disabled"] |
| 141 |
|
| 142 |
|
| 143 |
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES]) |
| 144 |
def test_state_present_when_absent(get_instance, instance, state): |
| 145 |
instance.node_exists.return_value = False |
| 146 |
instance.get_node_config.return_value = "<slave />" |
| 147 |
|
| 148 |
with set_module_args( |
| 149 |
{ |
| 150 |
"name": "my-node", |
| 151 |
"state": state, |
| 152 |
} |
| 153 |
): |
| 154 |
with raises(AnsibleExitJson) as result: |
| 155 |
jenkins_node.main() |
| 156 |
|
| 157 |
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH) |
| 158 |
|
| 159 |
assert result.value.args[0]["created"] is True |
| 160 |
assert result.value.args[0]["changed"] is True |
| 161 |
|
| 162 |
|
| 163 |
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES]) |
| 164 |
def test_state_present_when_absent_check_mode(get_instance, instance, state): |
| 165 |
instance.node_exists.return_value = False |
| 166 |
instance.get_node_config.return_value = "<slave />" |
| 167 |
|
| 168 |
with set_module_args( |
| 169 |
{ |
| 170 |
"name": "my-node", |
| 171 |
"state": state, |
| 172 |
"_ansible_check_mode": True, |
| 173 |
} |
| 174 |
): |
| 175 |
with raises(AnsibleExitJson) as result: |
| 176 |
jenkins_node.main() |
| 177 |
|
| 178 |
assert not instance.create_node.called |
| 179 |
|
| 180 |
assert result.value.args[0]["created"] is True |
| 181 |
assert result.value.args[0]["changed"] is True |
| 182 |
|
| 183 |
|
| 184 |
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES]) |
| 185 |
def test_state_present_when_absent_redirect_auth_error_handled(get_instance, instance, state): |
| 186 |
instance.node_exists.side_effect = [False, True] |
| 187 |
instance.get_node_config.return_value = "<slave />" |
| 188 |
instance.create_node.side_effect = jenkins.JenkinsException |
| 189 |
|
| 190 |
with set_module_args( |
| 191 |
{ |
| 192 |
"name": "my-node", |
| 193 |
"state": state, |
| 194 |
} |
| 195 |
): |
| 196 |
with raises(AnsibleExitJson) as result: |
| 197 |
jenkins_node.main() |
| 198 |
|
| 199 |
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH) |
| 200 |
|
| 201 |
assert result.value.args[0]["created"] is True |
| 202 |
assert result.value.args[0]["changed"] is True |
| 203 |
|
| 204 |
|
| 205 |
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES]) |
| 206 |
def test_state_present_when_absent_other_error_raised(get_instance, instance, state): |
| 207 |
instance.node_exists.side_effect = [False, False] |
| 208 |
instance.get_node_config.return_value = "<slave />" |
| 209 |
instance.create_node.side_effect = jenkins.JenkinsException |
| 210 |
|
| 211 |
with set_module_args( |
| 212 |
{ |
| 213 |
"name": "my-node", |
| 214 |
"state": state, |
| 215 |
} |
| 216 |
): |
| 217 |
with raises(AnsibleFailJson) as result: |
| 218 |
jenkins_node.main() |
| 219 |
|
| 220 |
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH) |
| 221 |
|
| 222 |
assert "Create node failed" in str(result.value.args[0]) |
| 223 |
|
| 224 |
|
| 225 |
def test_state_present_when_present(get_instance, instance): |
| 226 |
instance.node_exists.return_value = True |
| 227 |
instance.get_node_config.return_value = "<slave />" |
| 228 |
|
| 229 |
with set_module_args( |
| 230 |
{ |
| 231 |
"name": "my-node", |
| 232 |
"state": "present", |
| 233 |
} |
| 234 |
): |
| 235 |
with raises(AnsibleExitJson) as result: |
| 236 |
jenkins_node.main() |
| 237 |
|
| 238 |
assert not instance.create_node.called |
| 239 |
|
| 240 |
assert result.value.args[0]["created"] is False |
| 241 |
assert result.value.args[0]["changed"] is False |
| 242 |
|
| 243 |
|
| 244 |
def test_state_absent_when_present(get_instance, instance): |
| 245 |
instance.node_exists.return_value = True |
| 246 |
instance.get_node_config.return_value = "<slave />" |
| 247 |
|
| 248 |
with set_module_args( |
| 249 |
{ |
| 250 |
"name": "my-node", |
| 251 |
"state": "absent", |
| 252 |
} |
| 253 |
): |
| 254 |
with raises(AnsibleExitJson) as result: |
| 255 |
jenkins_node.main() |
| 256 |
|
| 257 |
assert instance.delete_node.call_args == call("my-node") |
| 258 |
|
| 259 |
assert result.value.args[0]["deleted"] is True |
| 260 |
assert result.value.args[0]["changed"] is True |
| 261 |
|
| 262 |
|
| 263 |
def test_state_absent_when_present_check_mode(get_instance, instance): |
| 264 |
instance.node_exists.return_value = True |
| 265 |
instance.get_node_config.return_value = "<slave />" |
| 266 |
|
| 267 |
with set_module_args( |
| 268 |
{ |
| 269 |
"name": "my-node", |
| 270 |
"state": "absent", |
| 271 |
"_ansible_check_mode": True, |
| 272 |
} |
| 273 |
): |
| 274 |
with raises(AnsibleExitJson) as result: |
| 275 |
jenkins_node.main() |
| 276 |
|
| 277 |
assert not instance.delete_node.called |
| 278 |
|
| 279 |
assert result.value.args[0]["deleted"] is True |
| 280 |
assert result.value.args[0]["changed"] is True |
| 281 |
|
| 282 |
|
| 283 |
def test_state_absent_when_present_redirect_auth_error_handled(get_instance, instance): |
| 284 |
instance.node_exists.side_effect = [True, False] |
| 285 |
instance.get_node_config.return_value = "<slave />" |
| 286 |
instance.delete_node.side_effect = jenkins.JenkinsException |
| 287 |
|
| 288 |
with set_module_args( |
| 289 |
{ |
| 290 |
"name": "my-node", |
| 291 |
"state": "absent", |
| 292 |
} |
| 293 |
): |
| 294 |
with raises(AnsibleExitJson) as result: |
| 295 |
jenkins_node.main() |
| 296 |
|
| 297 |
assert instance.delete_node.call_args == call("my-node") |
| 298 |
|
| 299 |
assert result.value.args[0]["deleted"] is True |
| 300 |
assert result.value.args[0]["changed"] is True |
| 301 |
|
| 302 |
|
| 303 |
def test_state_absent_when_present_other_error_raised(get_instance, instance): |
| 304 |
instance.node_exists.side_effect = [True, True] |
| 305 |
instance.get_node_config.return_value = "<slave />" |
| 306 |
instance.delete_node.side_effect = jenkins.JenkinsException |
| 307 |
|
| 308 |
with set_module_args( |
| 309 |
{ |
| 310 |
"name": "my-node", |
| 311 |
"state": "absent", |
| 312 |
} |
| 313 |
): |
| 314 |
with raises(AnsibleFailJson) as result: |
| 315 |
jenkins_node.main() |
| 316 |
|
| 317 |
assert instance.delete_node.call_args == call("my-node") |
| 318 |
|
| 319 |
assert "Delete node failed" in str(result.value.args[0]) |
| 320 |
|
| 321 |
|
| 322 |
def test_state_absent_when_absent(get_instance, instance): |
| 323 |
instance.node_exists.return_value = False |
| 324 |
instance.get_node_config.return_value = "<slave />" |
| 325 |
|
| 326 |
with set_module_args( |
| 327 |
{ |
| 328 |
"name": "my-node", |
| 329 |
"state": "absent", |
| 330 |
} |
| 331 |
): |
| 332 |
with raises(AnsibleExitJson) as result: |
| 333 |
jenkins_node.main() |
| 334 |
|
| 335 |
assert not instance.delete_node.called |
| 336 |
|
| 337 |
assert result.value.args[0]["deleted"] is False |
| 338 |
assert result.value.args[0]["changed"] is False |
| 339 |
|
| 340 |
|
| 341 |
def test_state_enabled_when_offline(get_instance, instance): |
| 342 |
instance.node_exists.return_value = True |
| 343 |
instance.get_node_config.return_value = "<slave />" |
| 344 |
instance.get_node_info.return_value = {"offline": True} |
| 345 |
|
| 346 |
with set_module_args( |
| 347 |
{ |
| 348 |
"name": "my-node", |
| 349 |
"state": "enabled", |
| 350 |
} |
| 351 |
): |
| 352 |
with raises(AnsibleExitJson) as result: |
| 353 |
jenkins_node.main() |
| 354 |
|
| 355 |
assert instance.enable_node.call_args == call("my-node") |
| 356 |
|
| 357 |
assert result.value.args[0]["enabled"] is True |
| 358 |
assert result.value.args[0]["changed"] is True |
| 359 |
|
| 360 |
|
| 361 |
def test_state_enabled_when_offline_check_mode(get_instance, instance): |
| 362 |
instance.node_exists.return_value = True |
| 363 |
instance.get_node_config.return_value = "<slave />" |
| 364 |
instance.get_node_info.return_value = {"offline": True} |
| 365 |
|
| 366 |
with set_module_args( |
| 367 |
{ |
| 368 |
"name": "my-node", |
| 369 |
"state": "enabled", |
| 370 |
"_ansible_check_mode": True, |
| 371 |
} |
| 372 |
): |
| 373 |
with raises(AnsibleExitJson) as result: |
| 374 |
jenkins_node.main() |
| 375 |
|
| 376 |
assert not instance.enable_node.called |
| 377 |
|
| 378 |
assert result.value.args[0]["enabled"] is True |
| 379 |
assert result.value.args[0]["changed"] is True |
| 380 |
|
| 381 |
|
| 382 |
def test_state_enabled_when_offline_redirect_auth_error_handled(get_instance, instance): |
| 383 |
instance.node_exists.return_value = True |
| 384 |
instance.get_node_config.return_value = "<slave />" |
| 385 |
instance.get_node_info.side_effect = [{"offline": True}, {"offline": False}] |
| 386 |
instance.enable_node.side_effect = jenkins.JenkinsException |
| 387 |
|
| 388 |
with set_module_args( |
| 389 |
{ |
| 390 |
"name": "my-node", |
| 391 |
"state": "enabled", |
| 392 |
} |
| 393 |
): |
| 394 |
with raises(AnsibleExitJson) as result: |
| 395 |
jenkins_node.main() |
| 396 |
|
| 397 |
assert instance.enable_node.call_args == call("my-node") |
| 398 |
|
| 399 |
assert result.value.args[0]["enabled"] is True |
| 400 |
assert result.value.args[0]["changed"] is True |
| 401 |
|
| 402 |
|
| 403 |
def test_state_enabled_when_offline_other_error_raised(get_instance, instance): |
| 404 |
instance.node_exists.return_value = True |
| 405 |
instance.get_node_config.return_value = "<slave />" |
| 406 |
instance.get_node_info.side_effect = [{"offline": True}, {"offline": True}] |
| 407 |
instance.enable_node.side_effect = jenkins.JenkinsException |
| 408 |
|
| 409 |
with set_module_args( |
| 410 |
{ |
| 411 |
"name": "my-node", |
| 412 |
"state": "enabled", |
| 413 |
} |
| 414 |
): |
| 415 |
with raises(AnsibleFailJson) as result: |
| 416 |
jenkins_node.main() |
| 417 |
|
| 418 |
assert instance.enable_node.call_args == call("my-node") |
| 419 |
|
| 420 |
assert "Enable node failed" in str(result.value.args[0]) |
| 421 |
|
| 422 |
|
| 423 |
def test_state_enabled_when_not_offline(get_instance, instance): |
| 424 |
instance.node_exists.return_value = True |
| 425 |
instance.get_node_config.return_value = "<slave />" |
| 426 |
instance.get_node_info.return_value = {"offline": False} |
| 427 |
|
| 428 |
with set_module_args( |
| 429 |
{ |
| 430 |
"name": "my-node", |
| 431 |
"state": "enabled", |
| 432 |
} |
| 433 |
): |
| 434 |
with raises(AnsibleExitJson) as result: |
| 435 |
jenkins_node.main() |
| 436 |
|
| 437 |
assert not instance.enable_node.called |
| 438 |
|
| 439 |
assert result.value.args[0]["enabled"] is False |
| 440 |
assert result.value.args[0]["changed"] is False |
| 441 |
|
| 442 |
|
| 443 |
def test_state_disabled_when_not_offline(get_instance, instance): |
| 444 |
instance.node_exists.return_value = True |
| 445 |
instance.get_node_config.return_value = "<slave />" |
| 446 |
instance.get_node_info.return_value = { |
| 447 |
"offline": False, |
| 448 |
"offlineCauseReason": "", |
| 449 |
} |
| 450 |
|
| 451 |
with set_module_args( |
| 452 |
{ |
| 453 |
"name": "my-node", |
| 454 |
"state": "disabled", |
| 455 |
} |
| 456 |
): |
| 457 |
with raises(AnsibleExitJson) as result: |
| 458 |
jenkins_node.main() |
| 459 |
|
| 460 |
assert instance.disable_node.call_args == call("my-node", "") |
| 461 |
|
| 462 |
assert result.value.args[0]["disabled"] is True |
| 463 |
assert result.value.args[0]["changed"] is True |
| 464 |
|
| 465 |
|
| 466 |
def test_state_disabled_when_not_offline_redirect_auth_error_handled(get_instance, instance): |
| 467 |
instance.node_exists.return_value = True |
| 468 |
instance.get_node_config.return_value = "<slave />" |
| 469 |
instance.get_node_info.side_effect = [ |
| 470 |
{ |
| 471 |
"offline": False, |
| 472 |
"offlineCauseReason": "", |
| 473 |
}, |
| 474 |
{ |
| 475 |
"offline": True, |
| 476 |
"offlineCauseReason": "", |
| 477 |
}, |
| 478 |
] |
| 479 |
instance.disable_node.side_effect = jenkins.JenkinsException |
| 480 |
|
| 481 |
with set_module_args( |
| 482 |
{ |
| 483 |
"name": "my-node", |
| 484 |
"state": "disabled", |
| 485 |
} |
| 486 |
): |
| 487 |
with raises(AnsibleExitJson) as result: |
| 488 |
jenkins_node.main() |
| 489 |
|
| 490 |
assert instance.disable_node.call_args == call("my-node", "") |
| 491 |
|
| 492 |
assert result.value.args[0]["disabled"] is True |
| 493 |
assert result.value.args[0]["changed"] is True |
| 494 |
|
| 495 |
|
| 496 |
def test_state_disabled_when_not_offline_other_error_raised(get_instance, instance): |
| 497 |
instance.node_exists.return_value = True |
| 498 |
instance.get_node_config.return_value = "<slave />" |
| 499 |
instance.get_node_info.side_effect = [ |
| 500 |
{ |
| 501 |
"offline": False, |
| 502 |
"offlineCauseReason": "", |
| 503 |
}, |
| 504 |
{ |
| 505 |
"offline": False, |
| 506 |
"offlineCauseReason": "", |
| 507 |
}, |
| 508 |
] |
| 509 |
instance.disable_node.side_effect = jenkins.JenkinsException |
| 510 |
|
| 511 |
with set_module_args( |
| 512 |
{ |
| 513 |
"name": "my-node", |
| 514 |
"state": "disabled", |
| 515 |
} |
| 516 |
): |
| 517 |
with raises(AnsibleFailJson) as result: |
| 518 |
jenkins_node.main() |
| 519 |
|
| 520 |
assert instance.disable_node.call_args == call("my-node", "") |
| 521 |
|
| 522 |
assert "Disable node failed" in str(result.value.args[0]) |
| 523 |
|
| 524 |
|
| 525 |
def test_state_disabled_when_not_offline_check_mode(get_instance, instance): |
| 526 |
instance.node_exists.return_value = True |
| 527 |
instance.get_node_config.return_value = "<slave />" |
| 528 |
instance.get_node_info.return_value = { |
| 529 |
"offline": False, |
| 530 |
"offlineCauseReason": "", |
| 531 |
} |
| 532 |
|
| 533 |
with set_module_args( |
| 534 |
{ |
| 535 |
"name": "my-node", |
| 536 |
"state": "disabled", |
| 537 |
"_ansible_check_mode": True, |
| 538 |
} |
| 539 |
): |
| 540 |
with raises(AnsibleExitJson) as result: |
| 541 |
jenkins_node.main() |
| 542 |
|
| 543 |
assert not instance.disable_node.called |
| 544 |
|
| 545 |
assert result.value.args[0]["disabled"] is True |
| 546 |
assert result.value.args[0]["changed"] is True |
| 547 |
|
| 548 |
|
| 549 |
def test_state_disabled_when_offline(get_instance, instance): |
| 550 |
instance.node_exists.return_value = True |
| 551 |
instance.get_node_config.return_value = "<slave />" |
| 552 |
instance.get_node_info.return_value = { |
| 553 |
"offline": True, |
| 554 |
"offlineCauseReason": "", |
| 555 |
} |
| 556 |
|
| 557 |
with set_module_args( |
| 558 |
{ |
| 559 |
"name": "my-node", |
| 560 |
"state": "disabled", |
| 561 |
} |
| 562 |
): |
| 563 |
with raises(AnsibleExitJson) as result: |
| 564 |
jenkins_node.main() |
| 565 |
|
| 566 |
assert not instance.disable_node.called |
| 567 |
|
| 568 |
assert result.value.args[0]["disabled"] is False |
| 569 |
assert result.value.args[0]["changed"] is False |
| 570 |
|
| 571 |
|
| 572 |
def test_configure_num_executors_when_not_configured(get_instance, instance): |
| 573 |
instance.node_exists.return_value = True |
| 574 |
instance.get_node_config.return_value = "<slave />" |
| 575 |
|
| 576 |
with set_module_args( |
| 577 |
{ |
| 578 |
"name": "my-node", |
| 579 |
"state": "present", |
| 580 |
"num_executors": 3, |
| 581 |
} |
| 582 |
): |
| 583 |
with raises(AnsibleExitJson) as result: |
| 584 |
jenkins_node.main() |
| 585 |
|
| 586 |
assert instance.reconfig_node.call_args[0][0] == "my-node" |
| 587 |
assert_xml_equal( |
| 588 |
instance.reconfig_node.call_args[0][1], |
| 589 |
""" |
| 590 |
<slave> |
| 591 |
<numExecutors>3</numExecutors> |
| 592 |
</slave> |
| 593 |
""", |
| 594 |
) |
| 595 |
|
| 596 |
assert result.value.args[0]["configured"] is True |
| 597 |
assert result.value.args[0]["changed"] is True |
| 598 |
|
| 599 |
|
| 600 |
def test_configure_num_executors_when_not_equal(get_instance, instance): |
| 601 |
instance.node_exists.return_value = True |
| 602 |
instance.get_node_config.return_value = """ |
| 603 |
<slave> |
| 604 |
<numExecutors>3</numExecutors> |
| 605 |
</slave> |
| 606 |
""" |
| 607 |
|
| 608 |
with set_module_args( |
| 609 |
{ |
| 610 |
"name": "my-node", |
| 611 |
"state": "present", |
| 612 |
"num_executors": 2, |
| 613 |
} |
| 614 |
): |
| 615 |
with raises(AnsibleExitJson) as result: |
| 616 |
jenkins_node.main() |
| 617 |
|
| 618 |
assert_xml_equal( |
| 619 |
instance.reconfig_node.call_args[0][1], |
| 620 |
""" |
| 621 |
<slave> |
| 622 |
<numExecutors>2</numExecutors> |
| 623 |
</slave> |
| 624 |
""", |
| 625 |
) |
| 626 |
|
| 627 |
assert result.value.args[0]["configured"] is True |
| 628 |
assert result.value.args[0]["changed"] is True |
| 629 |
|
| 630 |
|
| 631 |
def test_configure_num_executors_when_equal(get_instance, instance): |
| 632 |
instance.node_exists.return_value = True |
| 633 |
instance.get_node_config.return_value = """ |
| 634 |
<slave> |
| 635 |
<numExecutors>2</numExecutors> |
| 636 |
</slave> |
| 637 |
""" |
| 638 |
|
| 639 |
with set_module_args( |
| 640 |
{ |
| 641 |
"name": "my-node", |
| 642 |
"state": "present", |
| 643 |
"num_executors": 2, |
| 644 |
} |
| 645 |
): |
| 646 |
with raises(AnsibleExitJson) as result: |
| 647 |
jenkins_node.main() |
| 648 |
|
| 649 |
assert not instance.reconfig_node.called |
| 650 |
|
| 651 |
assert result.value.args[0]["configured"] is False |
| 652 |
assert result.value.args[0]["changed"] is False |
| 653 |
|
| 654 |
|
| 655 |
def test_configure_labels_when_not_configured(get_instance, instance): |
| 656 |
instance.node_exists.return_value = True |
| 657 |
instance.get_node_config.return_value = "<slave />" |
| 658 |
|
| 659 |
with set_module_args( |
| 660 |
{ |
| 661 |
"name": "my-node", |
| 662 |
"state": "present", |
| 663 |
"labels": [ |
| 664 |
"a", |
| 665 |
"b", |
| 666 |
"c", |
| 667 |
], |
| 668 |
} |
| 669 |
): |
| 670 |
with raises(AnsibleExitJson) as result: |
| 671 |
jenkins_node.main() |
| 672 |
|
| 673 |
assert instance.reconfig_node.call_args[0][0] == "my-node" |
| 674 |
assert_xml_equal( |
| 675 |
instance.reconfig_node.call_args[0][1], |
| 676 |
""" |
| 677 |
<slave> |
| 678 |
<label>a b c</label> |
| 679 |
</slave> |
| 680 |
""", |
| 681 |
) |
| 682 |
|
| 683 |
assert result.value.args[0]["configured"] is True |
| 684 |
assert result.value.args[0]["changed"] is True |
| 685 |
|
| 686 |
|
| 687 |
def test_configure_labels_when_not_equal(get_instance, instance): |
| 688 |
instance.node_exists.return_value = True |
| 689 |
instance.get_node_config.return_value = """ |
| 690 |
<slave> |
| 691 |
<label>a b c</label> |
| 692 |
</slave> |
| 693 |
""" |
| 694 |
|
| 695 |
with set_module_args( |
| 696 |
{ |
| 697 |
"name": "my-node", |
| 698 |
"state": "present", |
| 699 |
"labels": [ |
| 700 |
"a", |
| 701 |
"z", |
| 702 |
"c", |
| 703 |
], |
| 704 |
} |
| 705 |
): |
| 706 |
with raises(AnsibleExitJson) as result: |
| 707 |
jenkins_node.main() |
| 708 |
|
| 709 |
assert instance.reconfig_node.call_args[0][0] == "my-node" |
| 710 |
assert_xml_equal( |
| 711 |
instance.reconfig_node.call_args[0][1], |
| 712 |
""" |
| 713 |
<slave> |
| 714 |
<label>a z c</label> |
| 715 |
</slave> |
| 716 |
""", |
| 717 |
) |
| 718 |
|
| 719 |
assert result.value.args[0]["configured"] is True |
| 720 |
assert result.value.args[0]["changed"] is True |
| 721 |
|
| 722 |
|
| 723 |
def test_configure_labels_when_equal(get_instance, instance): |
| 724 |
instance.node_exists.return_value = True |
| 725 |
instance.get_node_config.return_value = """ |
| 726 |
<slave> |
| 727 |
<label>a b c</label> |
| 728 |
</slave> |
| 729 |
""" |
| 730 |
|
| 731 |
with set_module_args( |
| 732 |
{ |
| 733 |
"name": "my-node", |
| 734 |
"state": "present", |
| 735 |
"labels": [ |
| 736 |
"a", |
| 737 |
"b", |
| 738 |
"c", |
| 739 |
], |
| 740 |
} |
| 741 |
): |
| 742 |
with raises(AnsibleExitJson) as result: |
| 743 |
jenkins_node.main() |
| 744 |
|
| 745 |
assert not instance.reconfig_node.called |
| 746 |
|
| 747 |
assert result.value.args[0]["configured"] is False |
| 748 |
assert result.value.args[0]["changed"] is False |
| 749 |
|
| 750 |
|
| 751 |
def test_configure_labels_fail_when_contains_space(get_instance, instance): |
| 752 |
instance.node_exists.return_value = True |
| 753 |
instance.get_node_config.return_value = "<slave />" |
| 754 |
|
| 755 |
with set_module_args( |
| 756 |
{ |
| 757 |
"name": "my-node", |
| 758 |
"state": "present", |
| 759 |
"labels": [ |
| 760 |
"a error", |
| 761 |
], |
| 762 |
} |
| 763 |
): |
| 764 |
with raises(AnsibleFailJson): |
| 765 |
jenkins_node.main() |
| 766 |
|
| 767 |
assert not instance.reconfig_node.called |
| 768 |
|
| 769 |
|
| 770 |
@mark.parametrize(["state"], [param(state) for state in ["enabled", "present", "absent"]]) |
| 771 |
def test_raises_error_if_offline_message_when_state_not_disabled(get_instance, instance, state): |
| 772 |
with set_module_args( |
| 773 |
{ |
| 774 |
"name": "my-node", |
| 775 |
"state": state, |
| 776 |
"offline_message": "This is a message...", |
| 777 |
} |
| 778 |
): |
| 779 |
with raises(AnsibleFailJson): |
| 780 |
jenkins_node.main() |
| 781 |
|
| 782 |
assert not instance.disable_node.called |
| 783 |
|
| 784 |
|
| 785 |
def test_set_offline_message_when_equal(get_instance, instance): |
| 786 |
instance.node_exists.return_value = True |
| 787 |
instance.get_node_config.return_value = "<slave />" |
| 788 |
instance.get_node_info.return_value = { |
| 789 |
"offline": True, |
| 790 |
"offlineCauseReason": "This is an old message...", |
| 791 |
} |
| 792 |
|
| 793 |
with set_module_args( |
| 794 |
{ |
| 795 |
"name": "my-node", |
| 796 |
"state": "disabled", |
| 797 |
"offline_message": "This is an old message...", |
| 798 |
} |
| 799 |
): |
| 800 |
with raises(AnsibleExitJson) as result: |
| 801 |
jenkins_node.main() |
| 802 |
|
| 803 |
assert not instance.disable_node.called |
| 804 |
|
| 805 |
assert result.value.args[0]["changed"] is False |
| 806 |
|
| 807 |
|
| 808 |
def test_set_offline_message_when_not_equal_not_offline(get_instance, instance): |
| 809 |
instance.node_exists.return_value = True |
| 810 |
instance.get_node_config.return_value = "<slave />" |
| 811 |
instance.get_node_info.return_value = { |
| 812 |
"offline": False, |
| 813 |
"offlineCauseReason": "This is an old message...", |
| 814 |
} |
| 815 |
|
| 816 |
with set_module_args( |
| 817 |
{ |
| 818 |
"name": "my-node", |
| 819 |
"state": "disabled", |
| 820 |
"offline_message": "This is a new message...", |
| 821 |
} |
| 822 |
): |
| 823 |
with raises(AnsibleExitJson) as result: |
| 824 |
jenkins_node.main() |
| 825 |
|
| 826 |
assert instance.disable_node.call_args == call("my-node", "This is a new message...") |
| 827 |
|
| 828 |
assert result.value.args[0]["changed"] is True |
| 829 |
|
| 830 |
|
| 831 |
# Not calling disable_node when already offline seems like a sensible thing to do. |
| 832 |
# However, we need to call disable_node to set the offline message, so check that |
| 833 |
# we do even when already offline. |
| 834 |
def test_set_offline_message_when_not_equal_offline(get_instance, instance): |
| 835 |
instance.node_exists.return_value = True |
| 836 |
instance.get_node_config.return_value = "<slave />" |
| 837 |
instance.get_node_info.return_value = { |
| 838 |
"offline": True, |
| 839 |
"offlineCauseReason": "This is an old message...", |
| 840 |
} |
| 841 |
|
| 842 |
with set_module_args( |
| 843 |
{ |
| 844 |
"name": "my-node", |
| 845 |
"state": "disabled", |
| 846 |
"offline_message": "This is a new message...", |
| 847 |
} |
| 848 |
): |
| 849 |
with raises(AnsibleExitJson) as result: |
| 850 |
jenkins_node.main() |
| 851 |
|
| 852 |
assert not instance.disable_node.called |
| 853 |
|
| 854 |
assert result.value.args[0]["changed"] is False |