| 1 |
# Copyright (c) Jiri Hnidek ([email protected]) |
| 2 |
# |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import json |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible.module_utils import basic |
| 12 |
|
| 13 |
from ansible_collections.community.general.plugins.modules import redhat_subscription |
| 14 |
|
| 15 |
TESTED_MODULE = redhat_subscription.__name__ |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def patch_redhat_subscription(mocker): |
| 20 |
""" |
| 21 |
Function used for mocking some parts of redhat_subscription module |
| 22 |
""" |
| 23 |
mocker.patch("ansible_collections.community.general.plugins.modules.redhat_subscription.Rhsm.REDHAT_REPO") |
| 24 |
mocker.patch("ansible_collections.community.general.plugins.modules.redhat_subscription.isfile", return_value=False) |
| 25 |
mocker.patch("ansible_collections.community.general.plugins.modules.redhat_subscription.unlink", return_value=True) |
| 26 |
mocker.patch( |
| 27 |
"ansible_collections.community.general.plugins.modules.redhat_subscription.AnsibleModule.get_bin_path", |
| 28 |
return_value="/testbin/subscription-manager", |
| 29 |
) |
| 30 |
mocker.patch( |
| 31 |
"ansible_collections.community.general.plugins.modules.redhat_subscription.Rhsm._can_connect_to_dbus", |
| 32 |
return_value=False, |
| 33 |
) |
| 34 |
mocker.patch( |
| 35 |
"ansible_collections.community.general.plugins.modules.redhat_subscription.Rhsm._has_dbus_interface", |
| 36 |
return_value=False, |
| 37 |
) |
| 38 |
mocker.patch("ansible_collections.community.general.plugins.modules.redhat_subscription.getuid", return_value=0) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize("patch_ansible_module", [{}], indirect=["patch_ansible_module"]) |
| 42 |
@pytest.mark.usefixtures("patch_ansible_module") |
| 43 |
def test_without_required_parameters_unregistered(mocker, capfd, patch_redhat_subscription): |
| 44 |
""" |
| 45 |
Failure must occurs when all parameters are missing |
| 46 |
""" |
| 47 |
mocker.patch.object(basic.AnsibleModule, "run_command", return_value=(1, "This system is not yet registered.", "")) |
| 48 |
|
| 49 |
with pytest.raises(SystemExit): |
| 50 |
redhat_subscription.main() |
| 51 |
out, err = capfd.readouterr() |
| 52 |
results = json.loads(out) |
| 53 |
assert results["failed"] |
| 54 |
assert "state is present but any of the following are missing" in results["msg"] |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize("patch_ansible_module", [{}], indirect=["patch_ansible_module"]) |
| 58 |
@pytest.mark.usefixtures("patch_ansible_module") |
| 59 |
def test_without_required_parameters_registered(mocker, capfd, patch_redhat_subscription): |
| 60 |
""" |
| 61 |
System already registered, no parameters required (state=present is the |
| 62 |
default) |
| 63 |
""" |
| 64 |
mocker.patch.object( |
| 65 |
basic.AnsibleModule, |
| 66 |
"run_command", |
| 67 |
return_value=(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 68 |
) |
| 69 |
|
| 70 |
with pytest.raises(SystemExit): |
| 71 |
redhat_subscription.main() |
| 72 |
out, err = capfd.readouterr() |
| 73 |
results = json.loads(out) |
| 74 |
assert "changed" in results |
| 75 |
if "msg" in results: |
| 76 |
assert results["msg"] == "System already registered." |
| 77 |
|
| 78 |
|
| 79 |
TEST_CASES = [ |
| 80 |
# Test the case, when the system is already registered |
| 81 |
[ |
| 82 |
{ |
| 83 |
"state": "present", |
| 84 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 85 |
"username": "admin", |
| 86 |
"password": "admin", |
| 87 |
"org_id": "admin", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"id": "test_already_registered_system", |
| 91 |
"run_command.calls": [ |
| 92 |
( |
| 93 |
# Calling of following command will be asserted |
| 94 |
["/testbin/subscription-manager", "identity"], |
| 95 |
# Was return code checked? |
| 96 |
{"check_rc": False}, |
| 97 |
# Mock of returned code, stdout and stderr |
| 98 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 99 |
) |
| 100 |
], |
| 101 |
"changed": False, |
| 102 |
"msg": "System already registered.", |
| 103 |
}, |
| 104 |
], |
| 105 |
# Already registered system without credentials specified |
| 106 |
[ |
| 107 |
{ |
| 108 |
"state": "present", |
| 109 |
}, |
| 110 |
{ |
| 111 |
"id": "test_already_registered_system", |
| 112 |
"run_command.calls": [ |
| 113 |
( |
| 114 |
["/testbin/subscription-manager", "identity"], |
| 115 |
{"check_rc": False}, |
| 116 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 117 |
) |
| 118 |
], |
| 119 |
"changed": False, |
| 120 |
"msg": "System already registered.", |
| 121 |
}, |
| 122 |
], |
| 123 |
# Test simple registration using username and password |
| 124 |
[ |
| 125 |
{ |
| 126 |
"state": "present", |
| 127 |
"server_hostname": "satellite.company.com", |
| 128 |
"username": "admin", |
| 129 |
"password": "admin", |
| 130 |
}, |
| 131 |
{ |
| 132 |
"id": "test_registeration_username_password", |
| 133 |
"run_command.calls": [ |
| 134 |
(["/testbin/subscription-manager", "identity"], {"check_rc": False}, (1, "", "")), |
| 135 |
( |
| 136 |
["/testbin/subscription-manager", "config", "--server.hostname=satellite.company.com"], |
| 137 |
{"check_rc": True}, |
| 138 |
(0, "", ""), |
| 139 |
), |
| 140 |
( |
| 141 |
["/testbin/subscription-manager", "register", "--username", "admin", "--password", "admin"], |
| 142 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 143 |
(0, "", ""), |
| 144 |
), |
| 145 |
], |
| 146 |
"changed": True, |
| 147 |
"msg": "System successfully registered to 'satellite.company.com'.", |
| 148 |
}, |
| 149 |
], |
| 150 |
# Test simple registration using token |
| 151 |
[ |
| 152 |
{ |
| 153 |
"state": "present", |
| 154 |
"server_hostname": "satellite.company.com", |
| 155 |
"token": "fake_token", |
| 156 |
}, |
| 157 |
{ |
| 158 |
"id": "test_registeration_token", |
| 159 |
"run_command.calls": [ |
| 160 |
(["/testbin/subscription-manager", "identity"], {"check_rc": False}, (1, "", "")), |
| 161 |
( |
| 162 |
["/testbin/subscription-manager", "config", "--server.hostname=satellite.company.com"], |
| 163 |
{"check_rc": True}, |
| 164 |
(0, "", ""), |
| 165 |
), |
| 166 |
( |
| 167 |
["/testbin/subscription-manager", "register", "--token", "fake_token"], |
| 168 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 169 |
(0, "", ""), |
| 170 |
), |
| 171 |
], |
| 172 |
"changed": True, |
| 173 |
"msg": "System successfully registered to 'satellite.company.com'.", |
| 174 |
}, |
| 175 |
], |
| 176 |
# Test unregistration, when system is unregistered |
| 177 |
[ |
| 178 |
{ |
| 179 |
"state": "absent", |
| 180 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 181 |
"username": "admin", |
| 182 |
"password": "admin", |
| 183 |
}, |
| 184 |
{ |
| 185 |
"id": "test_unregisteration", |
| 186 |
"run_command.calls": [ |
| 187 |
( |
| 188 |
["/testbin/subscription-manager", "identity"], |
| 189 |
{"check_rc": False}, |
| 190 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 191 |
), |
| 192 |
(["/testbin/subscription-manager", "unregister"], {"check_rc": True}, (0, "", "")), |
| 193 |
], |
| 194 |
"changed": True, |
| 195 |
"msg": "System successfully unregistered from subscription.rhsm.redhat.com.", |
| 196 |
}, |
| 197 |
], |
| 198 |
# Test unregistration of already unregistered system |
| 199 |
[ |
| 200 |
{ |
| 201 |
"state": "absent", |
| 202 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 203 |
"username": "admin", |
| 204 |
"password": "admin", |
| 205 |
}, |
| 206 |
{ |
| 207 |
"id": "test_unregisteration_of_unregistered_system", |
| 208 |
"run_command.calls": [ |
| 209 |
( |
| 210 |
["/testbin/subscription-manager", "identity"], |
| 211 |
{"check_rc": False}, |
| 212 |
(1, "This system is not yet registered.", ""), |
| 213 |
) |
| 214 |
], |
| 215 |
"changed": False, |
| 216 |
"msg": "System already unregistered.", |
| 217 |
}, |
| 218 |
], |
| 219 |
# Test registration using activation key |
| 220 |
[ |
| 221 |
{ |
| 222 |
"state": "present", |
| 223 |
"server_hostname": "satellite.company.com", |
| 224 |
"activationkey": "some-activation-key", |
| 225 |
"org_id": "admin", |
| 226 |
}, |
| 227 |
{ |
| 228 |
"id": "test_registeration_activation_key", |
| 229 |
"run_command.calls": [ |
| 230 |
( |
| 231 |
["/testbin/subscription-manager", "identity"], |
| 232 |
{"check_rc": False}, |
| 233 |
(1, "This system is not yet registered.", ""), |
| 234 |
), |
| 235 |
( |
| 236 |
["/testbin/subscription-manager", "config", "--server.hostname=satellite.company.com"], |
| 237 |
{"check_rc": True}, |
| 238 |
(0, "", ""), |
| 239 |
), |
| 240 |
( |
| 241 |
[ |
| 242 |
"/testbin/subscription-manager", |
| 243 |
"register", |
| 244 |
"--org", |
| 245 |
"admin", |
| 246 |
"--activationkey", |
| 247 |
"some-activation-key", |
| 248 |
], |
| 249 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 250 |
(0, "", ""), |
| 251 |
), |
| 252 |
], |
| 253 |
"changed": True, |
| 254 |
"msg": "System successfully registered to 'satellite.company.com'.", |
| 255 |
}, |
| 256 |
], |
| 257 |
# Test of registration using username and password with auto-attach option |
| 258 |
[ |
| 259 |
{"state": "present", "username": "admin", "password": "admin", "org_id": "admin", "auto_attach": "true"}, |
| 260 |
{ |
| 261 |
"id": "test_registeration_username_password_auto_attach", |
| 262 |
"run_command.calls": [ |
| 263 |
( |
| 264 |
["/testbin/subscription-manager", "identity"], |
| 265 |
{"check_rc": False}, |
| 266 |
(1, "This system is not yet registered.", ""), |
| 267 |
), |
| 268 |
( |
| 269 |
[ |
| 270 |
"/testbin/subscription-manager", |
| 271 |
"register", |
| 272 |
"--org", |
| 273 |
"admin", |
| 274 |
"--auto-attach", |
| 275 |
"--username", |
| 276 |
"admin", |
| 277 |
"--password", |
| 278 |
"admin", |
| 279 |
], |
| 280 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 281 |
(0, "", ""), |
| 282 |
), |
| 283 |
], |
| 284 |
"changed": True, |
| 285 |
"msg": "System successfully registered to 'None'.", |
| 286 |
}, |
| 287 |
], |
| 288 |
# Test of force registration despite the system is already registered |
| 289 |
[ |
| 290 |
{"state": "present", "username": "admin", "password": "admin", "org_id": "admin", "force_register": "true"}, |
| 291 |
{ |
| 292 |
"id": "test_force_registeration_username_password", |
| 293 |
"run_command.calls": [ |
| 294 |
( |
| 295 |
["/testbin/subscription-manager", "identity"], |
| 296 |
{"check_rc": False}, |
| 297 |
(0, "This system already registered.", ""), |
| 298 |
), |
| 299 |
( |
| 300 |
[ |
| 301 |
"/testbin/subscription-manager", |
| 302 |
"register", |
| 303 |
"--force", |
| 304 |
"--org", |
| 305 |
"admin", |
| 306 |
"--username", |
| 307 |
"admin", |
| 308 |
"--password", |
| 309 |
"admin", |
| 310 |
], |
| 311 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 312 |
(0, "", ""), |
| 313 |
), |
| 314 |
], |
| 315 |
"changed": True, |
| 316 |
"msg": "System successfully registered to 'None'.", |
| 317 |
}, |
| 318 |
], |
| 319 |
# Test of registration with arguments that are not part of register options but needs to be configured |
| 320 |
[ |
| 321 |
{ |
| 322 |
"state": "present", |
| 323 |
"username": "admin", |
| 324 |
"password": "admin", |
| 325 |
"org_id": "admin", |
| 326 |
"force_register": "true", |
| 327 |
"server_prefix": "/rhsm", |
| 328 |
"server_port": "443", |
| 329 |
}, |
| 330 |
{ |
| 331 |
"id": "test_arguments_not_in_register_options", |
| 332 |
"run_command.calls": [ |
| 333 |
( |
| 334 |
["/testbin/subscription-manager", "identity"], |
| 335 |
{"check_rc": False}, |
| 336 |
(0, "This system already registered.", ""), |
| 337 |
), |
| 338 |
( |
| 339 |
["/testbin/subscription-manager", "config", "--server.port=443", "--server.prefix=/rhsm"], |
| 340 |
{"check_rc": True}, |
| 341 |
(0, "", ""), |
| 342 |
), |
| 343 |
( |
| 344 |
[ |
| 345 |
"/testbin/subscription-manager", |
| 346 |
"register", |
| 347 |
"--force", |
| 348 |
"--org", |
| 349 |
"admin", |
| 350 |
"--username", |
| 351 |
"admin", |
| 352 |
"--password", |
| 353 |
"admin", |
| 354 |
], |
| 355 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 356 |
(0, "", ""), |
| 357 |
), |
| 358 |
], |
| 359 |
"changed": True, |
| 360 |
"msg": "System successfully registered to 'None'.", |
| 361 |
}, |
| 362 |
], |
| 363 |
# Test of registration using username, password and proxy options |
| 364 |
[ |
| 365 |
{ |
| 366 |
"state": "present", |
| 367 |
"username": "admin", |
| 368 |
"password": "admin", |
| 369 |
"org_id": "admin", |
| 370 |
"force_register": "true", |
| 371 |
"server_proxy_hostname": "proxy.company.com", |
| 372 |
"server_proxy_scheme": "https", |
| 373 |
"server_proxy_port": "12345", |
| 374 |
"server_proxy_user": "proxy_user", |
| 375 |
"server_proxy_password": "secret_proxy_password", |
| 376 |
}, |
| 377 |
{ |
| 378 |
"id": "test_registeration_username_password_proxy_options", |
| 379 |
"run_command.calls": [ |
| 380 |
( |
| 381 |
["/testbin/subscription-manager", "identity"], |
| 382 |
{"check_rc": False}, |
| 383 |
(0, "This system already registered.", ""), |
| 384 |
), |
| 385 |
( |
| 386 |
[ |
| 387 |
"/testbin/subscription-manager", |
| 388 |
"config", |
| 389 |
"--server.proxy_hostname=proxy.company.com", |
| 390 |
"--server.proxy_password=secret_proxy_password", |
| 391 |
"--server.proxy_port=12345", |
| 392 |
"--server.proxy_scheme=https", |
| 393 |
"--server.proxy_user=proxy_user", |
| 394 |
], |
| 395 |
{"check_rc": True}, |
| 396 |
(0, "", ""), |
| 397 |
), |
| 398 |
( |
| 399 |
[ |
| 400 |
"/testbin/subscription-manager", |
| 401 |
"register", |
| 402 |
"--force", |
| 403 |
"--org", |
| 404 |
"admin", |
| 405 |
"--username", |
| 406 |
"admin", |
| 407 |
"--password", |
| 408 |
"admin", |
| 409 |
], |
| 410 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 411 |
(0, "", ""), |
| 412 |
), |
| 413 |
], |
| 414 |
"changed": True, |
| 415 |
"msg": "System successfully registered to 'None'.", |
| 416 |
}, |
| 417 |
], |
| 418 |
# Test of registration using username and password and attach to pool ID and quantities |
| 419 |
[ |
| 420 |
{ |
| 421 |
"state": "present", |
| 422 |
"username": "admin", |
| 423 |
"password": "admin", |
| 424 |
"org_id": "admin", |
| 425 |
"pool_ids": [{"ff8080816b8e967f016b8e99632804a6": 2}, {"ff8080816b8e967f016b8e99747107e9": 4}], |
| 426 |
}, |
| 427 |
{ |
| 428 |
"id": "test_registeration_username_password_pool_ids_quantities", |
| 429 |
"run_command.calls": [ |
| 430 |
( |
| 431 |
["/testbin/subscription-manager", "identity"], |
| 432 |
{"check_rc": False}, |
| 433 |
(1, "This system is not yet registered.", ""), |
| 434 |
), |
| 435 |
( |
| 436 |
[ |
| 437 |
"/testbin/subscription-manager", |
| 438 |
"register", |
| 439 |
"--org", |
| 440 |
"admin", |
| 441 |
"--username", |
| 442 |
"admin", |
| 443 |
"--password", |
| 444 |
"admin", |
| 445 |
], |
| 446 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 447 |
(0, "", ""), |
| 448 |
), |
| 449 |
( |
| 450 |
[ |
| 451 |
"subscription-manager list --available", |
| 452 |
{"check_rc": True, "environ_update": {"LANGUAGE": "C", "LC_ALL": "C"}}, |
| 453 |
( |
| 454 |
0, |
| 455 |
""" |
| 456 |
+-------------------------------------------+ |
| 457 |
Available Subscriptions |
| 458 |
+-------------------------------------------+ |
| 459 |
Subscription Name: SP Smart Management (A: ADDON1) |
| 460 |
Provides: SP Addon 1 bits |
| 461 |
SKU: sp-with-addon-1 |
| 462 |
Contract: 1 |
| 463 |
Pool ID: ff8080816b8e967f016b8e99747107e9 |
| 464 |
Provides Management: Yes |
| 465 |
Available: 10 |
| 466 |
Suggested: 1 |
| 467 |
Service Type: |
| 468 |
Roles: |
| 469 |
Service Level: |
| 470 |
Usage: |
| 471 |
Add-ons: ADDON1 |
| 472 |
Subscription Type: Standard |
| 473 |
Starts: 25.6.2019 |
| 474 |
Ends: 24.6.2020 |
| 475 |
Entitlement Type: Physical |
| 476 |
|
| 477 |
Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) |
| 478 |
Provides: SP Server Bits |
| 479 |
SKU: sp-server-prem-prod |
| 480 |
Contract: 0 |
| 481 |
Pool ID: ff8080816b8e967f016b8e99632804a6 |
| 482 |
Provides Management: Yes |
| 483 |
Available: 5 |
| 484 |
Suggested: 1 |
| 485 |
Service Type: L1-L3 |
| 486 |
Roles: SP Server |
| 487 |
Service Level: Premium |
| 488 |
Usage: Production |
| 489 |
Add-ons: |
| 490 |
Subscription Type: Standard |
| 491 |
Starts: 06/25/19 |
| 492 |
Ends: 06/24/20 |
| 493 |
Entitlement Type: Physical |
| 494 |
""", |
| 495 |
"", |
| 496 |
), |
| 497 |
] |
| 498 |
), |
| 499 |
( |
| 500 |
[ |
| 501 |
"/testbin/subscription-manager", |
| 502 |
"attach", |
| 503 |
"--pool", |
| 504 |
"ff8080816b8e967f016b8e99632804a6", |
| 505 |
"--quantity", |
| 506 |
"2", |
| 507 |
], |
| 508 |
{"check_rc": True}, |
| 509 |
(0, "", ""), |
| 510 |
), |
| 511 |
( |
| 512 |
[ |
| 513 |
"/testbin/subscription-manager", |
| 514 |
"attach", |
| 515 |
"--pool", |
| 516 |
"ff8080816b8e967f016b8e99747107e9", |
| 517 |
"--quantity", |
| 518 |
"4", |
| 519 |
], |
| 520 |
{"check_rc": True}, |
| 521 |
(0, "", ""), |
| 522 |
), |
| 523 |
], |
| 524 |
"changed": True, |
| 525 |
"msg": "System successfully registered to 'None'.", |
| 526 |
}, |
| 527 |
], |
| 528 |
# Test of registration using username and password and attach to pool ID without quantities |
| 529 |
[ |
| 530 |
{ |
| 531 |
"state": "present", |
| 532 |
"username": "admin", |
| 533 |
"password": "admin", |
| 534 |
"org_id": "admin", |
| 535 |
"pool_ids": ["ff8080816b8e967f016b8e99632804a6", "ff8080816b8e967f016b8e99747107e9"], |
| 536 |
}, |
| 537 |
{ |
| 538 |
"id": "test_registeration_username_password_pool_ids", |
| 539 |
"run_command.calls": [ |
| 540 |
( |
| 541 |
["/testbin/subscription-manager", "identity"], |
| 542 |
{"check_rc": False}, |
| 543 |
(1, "This system is not yet registered.", ""), |
| 544 |
), |
| 545 |
( |
| 546 |
[ |
| 547 |
"/testbin/subscription-manager", |
| 548 |
"register", |
| 549 |
"--org", |
| 550 |
"admin", |
| 551 |
"--username", |
| 552 |
"admin", |
| 553 |
"--password", |
| 554 |
"admin", |
| 555 |
], |
| 556 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 557 |
(0, "", ""), |
| 558 |
), |
| 559 |
( |
| 560 |
[ |
| 561 |
"subscription-manager list --available", |
| 562 |
{"check_rc": True, "environ_update": {"LANGUAGE": "C", "LC_ALL": "C"}}, |
| 563 |
( |
| 564 |
0, |
| 565 |
""" |
| 566 |
+-------------------------------------------+ |
| 567 |
Available Subscriptions |
| 568 |
+-------------------------------------------+ |
| 569 |
Subscription Name: SP Smart Management (A: ADDON1) |
| 570 |
Provides: SP Addon 1 bits |
| 571 |
SKU: sp-with-addon-1 |
| 572 |
Contract: 1 |
| 573 |
Pool ID: ff8080816b8e967f016b8e99747107e9 |
| 574 |
Provides Management: Yes |
| 575 |
Available: 10 |
| 576 |
Suggested: 1 |
| 577 |
Service Type: |
| 578 |
Roles: |
| 579 |
Service Level: |
| 580 |
Usage: |
| 581 |
Add-ons: ADDON1 |
| 582 |
Subscription Type: Standard |
| 583 |
Starts: 25.6.2019 |
| 584 |
Ends: 24.6.2020 |
| 585 |
Entitlement Type: Physical |
| 586 |
|
| 587 |
Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) |
| 588 |
Provides: SP Server Bits |
| 589 |
SKU: sp-server-prem-prod |
| 590 |
Contract: 0 |
| 591 |
Pool ID: ff8080816b8e967f016b8e99632804a6 |
| 592 |
Provides Management: Yes |
| 593 |
Available: 5 |
| 594 |
Suggested: 1 |
| 595 |
Service Type: L1-L3 |
| 596 |
Roles: SP Server |
| 597 |
Service Level: Premium |
| 598 |
Usage: Production |
| 599 |
Add-ons: |
| 600 |
Subscription Type: Standard |
| 601 |
Starts: 06/25/19 |
| 602 |
Ends: 06/24/20 |
| 603 |
Entitlement Type: Physical |
| 604 |
""", |
| 605 |
"", |
| 606 |
), |
| 607 |
] |
| 608 |
), |
| 609 |
( |
| 610 |
["/testbin/subscription-manager", "attach", "--pool", "ff8080816b8e967f016b8e99632804a6"], |
| 611 |
{"check_rc": True}, |
| 612 |
(0, "", ""), |
| 613 |
), |
| 614 |
( |
| 615 |
["/testbin/subscription-manager", "attach", "--pool", "ff8080816b8e967f016b8e99747107e9"], |
| 616 |
{"check_rc": True}, |
| 617 |
(0, "", ""), |
| 618 |
), |
| 619 |
], |
| 620 |
"changed": True, |
| 621 |
"msg": "System successfully registered to 'None'.", |
| 622 |
}, |
| 623 |
], |
| 624 |
# Test of registration using username and password and attach to pool ID (one pool) |
| 625 |
[ |
| 626 |
{ |
| 627 |
"state": "present", |
| 628 |
"username": "admin", |
| 629 |
"password": "admin", |
| 630 |
"org_id": "admin", |
| 631 |
"pool_ids": ["ff8080816b8e967f016b8e99632804a6"], |
| 632 |
}, |
| 633 |
{ |
| 634 |
"id": "test_registeration_username_password_one_pool_id", |
| 635 |
"run_command.calls": [ |
| 636 |
( |
| 637 |
["/testbin/subscription-manager", "identity"], |
| 638 |
{"check_rc": False}, |
| 639 |
(1, "This system is not yet registered.", ""), |
| 640 |
), |
| 641 |
( |
| 642 |
[ |
| 643 |
"/testbin/subscription-manager", |
| 644 |
"register", |
| 645 |
"--org", |
| 646 |
"admin", |
| 647 |
"--username", |
| 648 |
"admin", |
| 649 |
"--password", |
| 650 |
"admin", |
| 651 |
], |
| 652 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 653 |
(0, "", ""), |
| 654 |
), |
| 655 |
( |
| 656 |
[ |
| 657 |
"subscription-manager list --available", |
| 658 |
{"check_rc": True, "environ_update": {"LANGUAGE": "C", "LC_ALL": "C"}}, |
| 659 |
( |
| 660 |
0, |
| 661 |
""" |
| 662 |
+-------------------------------------------+ |
| 663 |
Available Subscriptions |
| 664 |
+-------------------------------------------+ |
| 665 |
Subscription Name: SP Smart Management (A: ADDON1) |
| 666 |
Provides: SP Addon 1 bits |
| 667 |
SKU: sp-with-addon-1 |
| 668 |
Contract: 1 |
| 669 |
Pool ID: ff8080816b8e967f016b8e99747107e9 |
| 670 |
Provides Management: Yes |
| 671 |
Available: 10 |
| 672 |
Suggested: 1 |
| 673 |
Service Type: |
| 674 |
Roles: |
| 675 |
Service Level: |
| 676 |
Usage: |
| 677 |
Add-ons: ADDON1 |
| 678 |
Subscription Type: Standard |
| 679 |
Starts: 25.6.2019 |
| 680 |
Ends: 24.6.2020 |
| 681 |
Entitlement Type: Physical |
| 682 |
|
| 683 |
Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) |
| 684 |
Provides: SP Server Bits |
| 685 |
SKU: sp-server-prem-prod |
| 686 |
Contract: 0 |
| 687 |
Pool ID: ff8080816b8e967f016b8e99632804a6 |
| 688 |
Provides Management: Yes |
| 689 |
Available: 5 |
| 690 |
Suggested: 1 |
| 691 |
Service Type: L1-L3 |
| 692 |
Roles: SP Server |
| 693 |
Service Level: Premium |
| 694 |
Usage: Production |
| 695 |
Add-ons: |
| 696 |
Subscription Type: Standard |
| 697 |
Starts: 06/25/19 |
| 698 |
Ends: 06/24/20 |
| 699 |
Entitlement Type: Physical |
| 700 |
""", |
| 701 |
"", |
| 702 |
), |
| 703 |
] |
| 704 |
), |
| 705 |
( |
| 706 |
[ |
| 707 |
"/testbin/subscription-manager", |
| 708 |
"attach", |
| 709 |
"--pool", |
| 710 |
"ff8080816b8e967f016b8e99632804a6", |
| 711 |
], |
| 712 |
{"check_rc": True}, |
| 713 |
(0, "", ""), |
| 714 |
), |
| 715 |
], |
| 716 |
"changed": True, |
| 717 |
"msg": "System successfully registered to 'None'.", |
| 718 |
}, |
| 719 |
], |
| 720 |
# Test attaching different set of pool IDs |
| 721 |
[ |
| 722 |
{ |
| 723 |
"state": "present", |
| 724 |
"pool_ids": [{"ff8080816b8e967f016b8e99632804a6": 2}, {"ff8080816b8e967f016b8e99747107e9": 4}], |
| 725 |
}, |
| 726 |
{ |
| 727 |
"id": "test_attaching_different_pool_ids", |
| 728 |
"run_command.calls": [ |
| 729 |
( |
| 730 |
["/testbin/subscription-manager", "identity"], |
| 731 |
{"check_rc": False}, |
| 732 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 733 |
), |
| 734 |
( |
| 735 |
"subscription-manager list --consumed", |
| 736 |
{"check_rc": True, "environ_update": {"LANGUAGE": "C", "LC_ALL": "C"}}, |
| 737 |
( |
| 738 |
0, |
| 739 |
""" |
| 740 |
+-------------------------------------------+ |
| 741 |
Consumed Subscriptions |
| 742 |
+-------------------------------------------+ |
| 743 |
Subscription Name: Multi-Attribute Stackable (4 cores, no content) |
| 744 |
Provides: Multi-Attribute Limited Product (no content) |
| 745 |
SKU: cores4-multiattr |
| 746 |
Contract: 1 |
| 747 |
Account: 12331131231 |
| 748 |
Serial: 7807912223970164816 |
| 749 |
Pool ID: ff8080816b8e967f016b8e995f5103b5 |
| 750 |
Provides Management: No |
| 751 |
Active: True |
| 752 |
Quantity Used: 1 |
| 753 |
Service Type: Level 3 |
| 754 |
Roles: |
| 755 |
Service Level: Premium |
| 756 |
Usage: |
| 757 |
Add-ons: |
| 758 |
Status Details: Subscription is current |
| 759 |
Subscription Type: Stackable |
| 760 |
Starts: 06/25/19 |
| 761 |
Ends: 06/24/20 |
| 762 |
Entitlement Type: Physical |
| 763 |
""", |
| 764 |
"", |
| 765 |
), |
| 766 |
), |
| 767 |
( |
| 768 |
[ |
| 769 |
"/testbin/subscription-manager", |
| 770 |
"remove", |
| 771 |
"--serial=7807912223970164816", |
| 772 |
], |
| 773 |
{"check_rc": True}, |
| 774 |
(0, "", ""), |
| 775 |
), |
| 776 |
( |
| 777 |
[ |
| 778 |
"subscription-manager list --available", |
| 779 |
{"check_rc": True, "environ_update": {"LANGUAGE": "C", "LC_ALL": "C"}}, |
| 780 |
( |
| 781 |
0, |
| 782 |
""" |
| 783 |
+-------------------------------------------+ |
| 784 |
Available Subscriptions |
| 785 |
+-------------------------------------------+ |
| 786 |
Subscription Name: SP Smart Management (A: ADDON1) |
| 787 |
Provides: SP Addon 1 bits |
| 788 |
SKU: sp-with-addon-1 |
| 789 |
Contract: 1 |
| 790 |
Pool ID: ff8080816b8e967f016b8e99747107e9 |
| 791 |
Provides Management: Yes |
| 792 |
Available: 10 |
| 793 |
Suggested: 1 |
| 794 |
Service Type: |
| 795 |
Roles: |
| 796 |
Service Level: |
| 797 |
Usage: |
| 798 |
Add-ons: ADDON1 |
| 799 |
Subscription Type: Standard |
| 800 |
Starts: 25.6.2019 |
| 801 |
Ends: 24.6.2020 |
| 802 |
Entitlement Type: Physical |
| 803 |
|
| 804 |
Subscription Name: SP Server Premium (S: Premium, U: Production, R: SP Server) |
| 805 |
Provides: SP Server Bits |
| 806 |
SKU: sp-server-prem-prod |
| 807 |
Contract: 0 |
| 808 |
Pool ID: ff8080816b8e967f016b8e99632804a6 |
| 809 |
Provides Management: Yes |
| 810 |
Available: 5 |
| 811 |
Suggested: 1 |
| 812 |
Service Type: L1-L3 |
| 813 |
Roles: SP Server |
| 814 |
Service Level: Premium |
| 815 |
Usage: Production |
| 816 |
Add-ons: |
| 817 |
Subscription Type: Standard |
| 818 |
Starts: 06/25/19 |
| 819 |
Ends: 06/24/20 |
| 820 |
Entitlement Type: Physical |
| 821 |
|
| 822 |
Subscription Name: Multi-Attribute Stackable (4 cores, no content) |
| 823 |
Provides: Multi-Attribute Limited Product (no content) |
| 824 |
SKU: cores4-multiattr |
| 825 |
Contract: 1 |
| 826 |
Pool ID: ff8080816b8e967f016b8e995f5103b5 |
| 827 |
Provides Management: No |
| 828 |
Available: 10 |
| 829 |
Suggested: 1 |
| 830 |
Service Type: Level 3 |
| 831 |
Roles: |
| 832 |
Service Level: Premium |
| 833 |
Usage: |
| 834 |
Add-ons: |
| 835 |
Subscription Type: Stackable |
| 836 |
Starts: 11.7.2019 |
| 837 |
Ends: 10.7.2020 |
| 838 |
Entitlement Type: Physical |
| 839 |
""", |
| 840 |
"", |
| 841 |
), |
| 842 |
] |
| 843 |
), |
| 844 |
( |
| 845 |
[ |
| 846 |
"/testbin/subscription-manager", |
| 847 |
"attach", |
| 848 |
"--pool", |
| 849 |
"ff8080816b8e967f016b8e99632804a6", |
| 850 |
"--quantity", |
| 851 |
"2", |
| 852 |
], |
| 853 |
{"check_rc": True}, |
| 854 |
(0, "", ""), |
| 855 |
), |
| 856 |
( |
| 857 |
[ |
| 858 |
"/testbin/subscription-manager", |
| 859 |
"attach", |
| 860 |
"--pool", |
| 861 |
"ff8080816b8e967f016b8e99747107e9", |
| 862 |
"--quantity", |
| 863 |
"4", |
| 864 |
], |
| 865 |
{"check_rc": True}, |
| 866 |
(0, "", ""), |
| 867 |
), |
| 868 |
], |
| 869 |
"changed": True, |
| 870 |
}, |
| 871 |
], |
| 872 |
] |
| 873 |
|
| 874 |
|
| 875 |
TEST_CASES_IDS: list[str] = [item[1]["id"] for item in TEST_CASES] # type: ignore |
| 876 |
|
| 877 |
|
| 878 |
@pytest.mark.parametrize( |
| 879 |
"patch_ansible_module, testcase", TEST_CASES, ids=TEST_CASES_IDS, indirect=["patch_ansible_module"] |
| 880 |
) |
| 881 |
@pytest.mark.usefixtures("patch_ansible_module") |
| 882 |
def test_redhat_subscription(mocker, capfd, patch_redhat_subscription, testcase): |
| 883 |
""" |
| 884 |
Run unit tests for test cases listen in TEST_CASES |
| 885 |
""" |
| 886 |
|
| 887 |
# Mock function used for running commands first |
| 888 |
call_results = [item[2] for item in testcase["run_command.calls"]] |
| 889 |
mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results) |
| 890 |
|
| 891 |
# Try to run test case |
| 892 |
with pytest.raises(SystemExit): |
| 893 |
redhat_subscription.main() |
| 894 |
|
| 895 |
out, err = capfd.readouterr() |
| 896 |
results = json.loads(out) |
| 897 |
|
| 898 |
assert "changed" in results |
| 899 |
assert results["changed"] == testcase["changed"] |
| 900 |
if "msg" in results: |
| 901 |
assert results["msg"] == testcase["msg"] |
| 902 |
|
| 903 |
assert basic.AnsibleModule.run_command.call_count == len(testcase["run_command.calls"]) |
| 904 |
if basic.AnsibleModule.run_command.call_count: |
| 905 |
call_args_list = [(item[0][0], item[1]) for item in basic.AnsibleModule.run_command.call_args_list] |
| 906 |
expected_call_args_list = [(item[0], item[1]) for item in testcase["run_command.calls"]] |
| 907 |
assert call_args_list == expected_call_args_list |
| 908 |
|
| 909 |
|
| 910 |
SYSPURPOSE_TEST_CASES = [ |
| 911 |
# Test setting syspurpose attributes (system is already registered) |
| 912 |
# and synchronization with candlepin server |
| 913 |
[ |
| 914 |
{ |
| 915 |
"state": "present", |
| 916 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 917 |
"username": "admin", |
| 918 |
"password": "admin", |
| 919 |
"org_id": "admin", |
| 920 |
"syspurpose": { |
| 921 |
"role": "AwesomeOS", |
| 922 |
"usage": "Production", |
| 923 |
"service_level_agreement": "Premium", |
| 924 |
"addons": ["ADDON1", "ADDON2"], |
| 925 |
"sync": True, |
| 926 |
}, |
| 927 |
}, |
| 928 |
{ |
| 929 |
"id": "test_setting_syspurpose_attributes", |
| 930 |
"existing_syspurpose": {}, |
| 931 |
"expected_syspurpose": { |
| 932 |
"role": "AwesomeOS", |
| 933 |
"usage": "Production", |
| 934 |
"service_level_agreement": "Premium", |
| 935 |
"addons": ["ADDON1", "ADDON2"], |
| 936 |
}, |
| 937 |
"run_command.calls": [ |
| 938 |
( |
| 939 |
["/testbin/subscription-manager", "identity"], |
| 940 |
{"check_rc": False}, |
| 941 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 942 |
), |
| 943 |
( |
| 944 |
["/testbin/subscription-manager", "status"], |
| 945 |
{"check_rc": False}, |
| 946 |
( |
| 947 |
0, |
| 948 |
""" |
| 949 |
+-------------------------------------------+ |
| 950 |
System Status Details |
| 951 |
+-------------------------------------------+ |
| 952 |
Overall Status: Current |
| 953 |
|
| 954 |
System Purpose Status: Matched |
| 955 |
""", |
| 956 |
"", |
| 957 |
), |
| 958 |
), |
| 959 |
], |
| 960 |
"changed": True, |
| 961 |
"msg": "Syspurpose attributes changed.", |
| 962 |
}, |
| 963 |
], |
| 964 |
# Test setting unspupported attributes |
| 965 |
[ |
| 966 |
{ |
| 967 |
"state": "present", |
| 968 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 969 |
"username": "admin", |
| 970 |
"password": "admin", |
| 971 |
"org_id": "admin", |
| 972 |
"syspurpose": { |
| 973 |
"foo": "Bar", |
| 974 |
"role": "AwesomeOS", |
| 975 |
"usage": "Production", |
| 976 |
"service_level_agreement": "Premium", |
| 977 |
"addons": ["ADDON1", "ADDON2"], |
| 978 |
"sync": True, |
| 979 |
}, |
| 980 |
}, |
| 981 |
{ |
| 982 |
"id": "test_setting_syspurpose_wrong_attributes", |
| 983 |
"existing_syspurpose": {}, |
| 984 |
"expected_syspurpose": {}, |
| 985 |
"run_command.calls": [], |
| 986 |
"failed": True, |
| 987 |
}, |
| 988 |
], |
| 989 |
# Test setting addons not a list |
| 990 |
[ |
| 991 |
{ |
| 992 |
"state": "present", |
| 993 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 994 |
"username": "admin", |
| 995 |
"password": "admin", |
| 996 |
"org_id": "admin", |
| 997 |
"syspurpose": { |
| 998 |
"role": "AwesomeOS", |
| 999 |
"usage": "Production", |
| 1000 |
"service_level_agreement": "Premium", |
| 1001 |
"addons": "ADDON1", |
| 1002 |
"sync": True, |
| 1003 |
}, |
| 1004 |
}, |
| 1005 |
{ |
| 1006 |
"id": "test_setting_syspurpose_addons_not_list", |
| 1007 |
"existing_syspurpose": {}, |
| 1008 |
"expected_syspurpose": { |
| 1009 |
"role": "AwesomeOS", |
| 1010 |
"usage": "Production", |
| 1011 |
"service_level_agreement": "Premium", |
| 1012 |
"addons": ["ADDON1"], |
| 1013 |
}, |
| 1014 |
"run_command.calls": [ |
| 1015 |
( |
| 1016 |
["/testbin/subscription-manager", "identity"], |
| 1017 |
{"check_rc": False}, |
| 1018 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 1019 |
), |
| 1020 |
( |
| 1021 |
["/testbin/subscription-manager", "status"], |
| 1022 |
{"check_rc": False}, |
| 1023 |
( |
| 1024 |
0, |
| 1025 |
""" |
| 1026 |
+-------------------------------------------+ |
| 1027 |
System Status Details |
| 1028 |
+-------------------------------------------+ |
| 1029 |
Overall Status: Current |
| 1030 |
|
| 1031 |
System Purpose Status: Matched |
| 1032 |
""", |
| 1033 |
"", |
| 1034 |
), |
| 1035 |
), |
| 1036 |
], |
| 1037 |
"changed": True, |
| 1038 |
"msg": "Syspurpose attributes changed.", |
| 1039 |
}, |
| 1040 |
], |
| 1041 |
# Test setting syspurpose attributes (system is already registered) |
| 1042 |
# without synchronization with candlepin server. Some syspurpose attributes were set |
| 1043 |
# in the past |
| 1044 |
[ |
| 1045 |
{ |
| 1046 |
"state": "present", |
| 1047 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 1048 |
"username": "admin", |
| 1049 |
"password": "admin", |
| 1050 |
"org_id": "admin", |
| 1051 |
"syspurpose": { |
| 1052 |
"role": "AwesomeOS", |
| 1053 |
"service_level_agreement": "Premium", |
| 1054 |
"addons": ["ADDON1", "ADDON2"], |
| 1055 |
"sync": False, |
| 1056 |
}, |
| 1057 |
}, |
| 1058 |
{ |
| 1059 |
"id": "test_changing_syspurpose_attributes", |
| 1060 |
"existing_syspurpose": { |
| 1061 |
"role": "CoolOS", |
| 1062 |
"usage": "Production", |
| 1063 |
"service_level_agreement": "Super", |
| 1064 |
"addons": [], |
| 1065 |
"foo": "bar", |
| 1066 |
}, |
| 1067 |
"expected_syspurpose": { |
| 1068 |
"role": "AwesomeOS", |
| 1069 |
"service_level_agreement": "Premium", |
| 1070 |
"addons": ["ADDON1", "ADDON2"], |
| 1071 |
"foo": "bar", |
| 1072 |
}, |
| 1073 |
"run_command.calls": [ |
| 1074 |
( |
| 1075 |
["/testbin/subscription-manager", "identity"], |
| 1076 |
{"check_rc": False}, |
| 1077 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 1078 |
), |
| 1079 |
], |
| 1080 |
"changed": True, |
| 1081 |
"msg": "Syspurpose attributes changed.", |
| 1082 |
}, |
| 1083 |
], |
| 1084 |
# Test trying to set syspurpose attributes (system is already registered) |
| 1085 |
# without synchronization with candlepin server. Some syspurpose attributes were set |
| 1086 |
# in the past. Syspurpose attributes are same as before |
| 1087 |
[ |
| 1088 |
{ |
| 1089 |
"state": "present", |
| 1090 |
"server_hostname": "subscription.rhsm.redhat.com", |
| 1091 |
"username": "admin", |
| 1092 |
"password": "admin", |
| 1093 |
"org_id": "admin", |
| 1094 |
"syspurpose": { |
| 1095 |
"role": "AwesomeOS", |
| 1096 |
"service_level_agreement": "Premium", |
| 1097 |
"addons": ["ADDON1", "ADDON2"], |
| 1098 |
"sync": False, |
| 1099 |
}, |
| 1100 |
}, |
| 1101 |
{ |
| 1102 |
"id": "test_not_changing_syspurpose_attributes", |
| 1103 |
"existing_syspurpose": { |
| 1104 |
"role": "AwesomeOS", |
| 1105 |
"service_level_agreement": "Premium", |
| 1106 |
"addons": ["ADDON1", "ADDON2"], |
| 1107 |
}, |
| 1108 |
"expected_syspurpose": { |
| 1109 |
"role": "AwesomeOS", |
| 1110 |
"service_level_agreement": "Premium", |
| 1111 |
"addons": ["ADDON1", "ADDON2"], |
| 1112 |
}, |
| 1113 |
"run_command.calls": [ |
| 1114 |
( |
| 1115 |
["/testbin/subscription-manager", "identity"], |
| 1116 |
{"check_rc": False}, |
| 1117 |
(0, "system identity: b26df632-25ed-4452-8f89-0308bfd167cb", ""), |
| 1118 |
), |
| 1119 |
], |
| 1120 |
"changed": False, |
| 1121 |
"msg": "System already registered.", |
| 1122 |
}, |
| 1123 |
], |
| 1124 |
# Test of registration using username and password with auto-attach option, when |
| 1125 |
# syspurpose attributes are set |
| 1126 |
[ |
| 1127 |
{ |
| 1128 |
"state": "present", |
| 1129 |
"username": "admin", |
| 1130 |
"password": "admin", |
| 1131 |
"org_id": "admin", |
| 1132 |
"auto_attach": "true", |
| 1133 |
"syspurpose": { |
| 1134 |
"role": "AwesomeOS", |
| 1135 |
"usage": "Testing", |
| 1136 |
"service_level_agreement": "Super", |
| 1137 |
"addons": ["ADDON1"], |
| 1138 |
"sync": False, |
| 1139 |
}, |
| 1140 |
}, |
| 1141 |
{ |
| 1142 |
"id": "test_registeration_username_password_auto_attach_syspurpose", |
| 1143 |
"existing_syspurpose": None, |
| 1144 |
"expected_syspurpose": { |
| 1145 |
"role": "AwesomeOS", |
| 1146 |
"usage": "Testing", |
| 1147 |
"service_level_agreement": "Super", |
| 1148 |
"addons": ["ADDON1"], |
| 1149 |
}, |
| 1150 |
"run_command.calls": [ |
| 1151 |
( |
| 1152 |
["/testbin/subscription-manager", "identity"], |
| 1153 |
{"check_rc": False}, |
| 1154 |
(1, "This system is not yet registered.", ""), |
| 1155 |
), |
| 1156 |
( |
| 1157 |
[ |
| 1158 |
"/testbin/subscription-manager", |
| 1159 |
"register", |
| 1160 |
"--org", |
| 1161 |
"admin", |
| 1162 |
"--auto-attach", |
| 1163 |
"--username", |
| 1164 |
"admin", |
| 1165 |
"--password", |
| 1166 |
"admin", |
| 1167 |
], |
| 1168 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 1169 |
(0, "", ""), |
| 1170 |
), |
| 1171 |
], |
| 1172 |
"changed": True, |
| 1173 |
"msg": "System successfully registered to 'None'.", |
| 1174 |
}, |
| 1175 |
], |
| 1176 |
# Test of registration using username and password with auto-attach option, when |
| 1177 |
# syspurpose attributes are set. Syspurpose attributes are also synchronized |
| 1178 |
# in this case |
| 1179 |
[ |
| 1180 |
{ |
| 1181 |
"state": "present", |
| 1182 |
"username": "admin", |
| 1183 |
"password": "admin", |
| 1184 |
"org_id": "admin", |
| 1185 |
"auto_attach": "true", |
| 1186 |
"syspurpose": { |
| 1187 |
"role": "AwesomeOS", |
| 1188 |
"usage": "Testing", |
| 1189 |
"service_level_agreement": "Super", |
| 1190 |
"addons": ["ADDON1"], |
| 1191 |
"sync": True, |
| 1192 |
}, |
| 1193 |
}, |
| 1194 |
{ |
| 1195 |
"id": "test_registeration_username_password_auto_attach_syspurpose_sync", |
| 1196 |
"existing_syspurpose": None, |
| 1197 |
"expected_syspurpose": { |
| 1198 |
"role": "AwesomeOS", |
| 1199 |
"usage": "Testing", |
| 1200 |
"service_level_agreement": "Super", |
| 1201 |
"addons": ["ADDON1"], |
| 1202 |
}, |
| 1203 |
"run_command.calls": [ |
| 1204 |
( |
| 1205 |
["/testbin/subscription-manager", "identity"], |
| 1206 |
{"check_rc": False}, |
| 1207 |
(1, "This system is not yet registered.", ""), |
| 1208 |
), |
| 1209 |
( |
| 1210 |
[ |
| 1211 |
"/testbin/subscription-manager", |
| 1212 |
"register", |
| 1213 |
"--org", |
| 1214 |
"admin", |
| 1215 |
"--auto-attach", |
| 1216 |
"--username", |
| 1217 |
"admin", |
| 1218 |
"--password", |
| 1219 |
"admin", |
| 1220 |
], |
| 1221 |
{"check_rc": True, "expand_user_and_vars": False}, |
| 1222 |
(0, "", ""), |
| 1223 |
), |
| 1224 |
( |
| 1225 |
["/testbin/subscription-manager", "status"], |
| 1226 |
{"check_rc": False}, |
| 1227 |
( |
| 1228 |
0, |
| 1229 |
""" |
| 1230 |
+-------------------------------------------+ |
| 1231 |
System Status Details |
| 1232 |
+-------------------------------------------+ |
| 1233 |
Overall Status: Current |
| 1234 |
|
| 1235 |
System Purpose Status: Matched |
| 1236 |
""", |
| 1237 |
"", |
| 1238 |
), |
| 1239 |
), |
| 1240 |
], |
| 1241 |
"changed": True, |
| 1242 |
"msg": "System successfully registered to 'None'.", |
| 1243 |
}, |
| 1244 |
], |
| 1245 |
] |
| 1246 |
|
| 1247 |
|
| 1248 |
SYSPURPOSE_TEST_CASES_IDS: list[str] = [item[1]["id"] for item in SYSPURPOSE_TEST_CASES] # type: ignore |
| 1249 |
|
| 1250 |
|
| 1251 |
@pytest.mark.parametrize( |
| 1252 |
"patch_ansible_module, testcase", |
| 1253 |
SYSPURPOSE_TEST_CASES, |
| 1254 |
ids=SYSPURPOSE_TEST_CASES_IDS, |
| 1255 |
indirect=["patch_ansible_module"], |
| 1256 |
) |
| 1257 |
@pytest.mark.usefixtures("patch_ansible_module") |
| 1258 |
def test_redhat_subscription_syspurpose( |
| 1259 |
mocker, capfd, patch_redhat_subscription, patch_ansible_module, testcase, tmpdir |
| 1260 |
): |
| 1261 |
""" |
| 1262 |
Run unit tests for test cases listen in SYSPURPOSE_TEST_CASES (syspurpose specific cases) |
| 1263 |
""" |
| 1264 |
|
| 1265 |
# Mock function used for running commands first |
| 1266 |
call_results = [item[2] for item in testcase["run_command.calls"]] |
| 1267 |
mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results) |
| 1268 |
|
| 1269 |
mock_syspurpose_file = tmpdir.mkdir("syspurpose").join("syspurpose.json") |
| 1270 |
# When there there are some existing syspurpose attributes specified, then |
| 1271 |
# write them to the file first |
| 1272 |
if testcase["existing_syspurpose"] is not None: |
| 1273 |
mock_syspurpose_file.write(json.dumps(testcase["existing_syspurpose"])) |
| 1274 |
else: |
| 1275 |
mock_syspurpose_file.write("{}") |
| 1276 |
|
| 1277 |
redhat_subscription.SysPurpose.SYSPURPOSE_FILE_PATH = str(mock_syspurpose_file) |
| 1278 |
|
| 1279 |
# Try to run test case |
| 1280 |
with pytest.raises(SystemExit): |
| 1281 |
redhat_subscription.main() |
| 1282 |
|
| 1283 |
out, err = capfd.readouterr() |
| 1284 |
results = json.loads(out) |
| 1285 |
|
| 1286 |
if "failed" in testcase: |
| 1287 |
assert results["failed"] == testcase["failed"] |
| 1288 |
else: |
| 1289 |
assert "changed" in results |
| 1290 |
assert results["changed"] == testcase["changed"] |
| 1291 |
if "msg" in results: |
| 1292 |
assert results["msg"] == testcase["msg"] |
| 1293 |
|
| 1294 |
mock_file_content = mock_syspurpose_file.read_text("utf-8") |
| 1295 |
current_syspurpose = json.loads(mock_file_content) |
| 1296 |
assert current_syspurpose == testcase["expected_syspurpose"] |
| 1297 |
|
| 1298 |
assert basic.AnsibleModule.run_command.call_count == len(testcase["run_command.calls"]) |
| 1299 |
if basic.AnsibleModule.run_command.call_count: |
| 1300 |
call_args_list = [(item[0][0], item[1]) for item in basic.AnsibleModule.run_command.call_args_list] |
| 1301 |
expected_call_args_list = [(item[0], item[1]) for item in testcase["run_command.calls"]] |
| 1302 |
assert call_args_list == expected_call_args_list |