Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_rhsm_repository.py

raw
1 # Author: Pino Toscano ([email protected])
2 # Largely adapted from test_rhsm_repository by
3 # Jiri Hnidek ([email protected])
4 #
5 # Copyright (c) Pino Toscano ([email protected])
6 # Copyright (c) Jiri Hnidek ([email protected])
7 #
8 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
9 # SPDX-License-Identifier: GPL-3.0-or-later
10
11 from __future__ import annotations
12
13 import copy
14 import fnmatch
15 import itertools
16 import json
17
18 import pytest
19 from ansible.module_utils import basic
20
21 from ansible_collections.community.general.plugins.modules import rhsm_repository
22
23 TESTED_MODULE = rhsm_repository.__name__
24
25
26 @pytest.fixture
27 def patch_rhsm_repository(mocker):
28 """
29 Function used for mocking some parts of rhsm_repository module
30 """
31 mocker.patch(
32 "ansible_collections.community.general.plugins.modules.rhsm_repository.AnsibleModule.get_bin_path",
33 return_value="/testbin/subscription-manager",
34 )
35 mocker.patch("ansible_collections.community.general.plugins.modules.rhsm_repository.os.getuid", return_value=0)
36
37
38 class Repos:
39 """
40 Helper class to represent a list of repositories
41
42 Each repository is an object with few properties.
43 """
44
45 _SUBMAN_OUT_HEADER = """+----------------------------------------------------------+
46 Available Repositories in /etc/yum.repos.d/redhat.repo
47 +----------------------------------------------------------+
48 """
49
50 def __init__(self, repos):
51 self.repos = repos
52
53 def to_subman_list_output(self):
54 """
55 Return a string mimicking the output of `subscription-manager repos --list`
56 """
57 out = self._SUBMAN_OUT_HEADER
58 for repo in self.repos:
59 out += (
60 f"Repo ID: {repo['id']}\n"
61 f"Repo Name: {repo['name']}\n"
62 f"Repo URL: {repo['url']}\n"
63 f"Enabled: {'1' if repo['enabled'] else '0'}\n"
64 "\n"
65 )
66
67 return out
68
69 def copy(self):
70 """
71 Clone the object; used to do changes (enable(), disable()) without
72 affecting the original object.
73 """
74 return copy.deepcopy(self)
75
76 def _set_status(self, repo_id, status):
77 for repo in self.repos:
78 if fnmatch.fnmatch(repo["id"], repo_id):
79 repo["enabled"] = status
80
81 def enable(self, repo_ids):
82 """
83 Enable the specified IDs.
84
85 'repo_ids' can be either a string or a list of strings representing
86 an ID (wildcard included).
87
88 Returns the same object, so calls to this can be chained.
89 """
90 if not isinstance(repo_ids, list):
91 repo_ids = [repo_ids]
92 for repo_id in repo_ids:
93 self._set_status(repo_id, True)
94 return self
95
96 def disable(self, repo_ids):
97 """
98 Disable the specified IDs.
99
100 'repo_ids' can be either a string or a list of strings representing
101 an ID (wildcard included).
102
103 Returns the same object, so calls to this can be chained.
104 """
105 if not isinstance(repo_ids, list):
106 repo_ids = [repo_ids]
107 for repo_id in repo_ids:
108 self._set_status(repo_id, False)
109 return self
110
111 def _filter_by_status(self, filter, status):
112 return [repo["id"] for repo in self.repos if repo["enabled"] == status and fnmatch.fnmatch(repo["id"], filter)]
113
114 def ids_enabled(self, filter="*"):
115 """
116 Get a list with the enabled repositories.
117
118 'filter' is a wildcard expression.
119 """
120 return self._filter_by_status(filter, True)
121
122 def ids_disabled(self, filter="*"):
123 """
124 Get a list with the disabled repositories.
125
126 'filter' is a wildcard expression.
127 """
128 return self._filter_by_status(filter, False)
129
130 def to_list(self):
131 """
132 Get the list of repositories.
133 """
134 return self.repos
135
136
137 def flatten(iter_of_iters):
138 return list(itertools.chain.from_iterable(iter_of_iters))
139
140
141 # List with test repositories, directly from the Candlepin test data.
142 REPOS_LIST = [
143 {
144 "id": "never-enabled-content-801",
145 "name": "never-enabled-content-801",
146 "url": "https://candlepin.local/foo/path/never_enabled/801-100",
147 "enabled": False,
148 },
149 {
150 "id": "never-enabled-content-100000000000060",
151 "name": "never-enabled-content-100000000000060",
152 "url": "https://candlepin.local/foo/path/never_enabled/100000000000060-100",
153 "enabled": False,
154 },
155 {
156 "id": "awesomeos-x86_64-1000000000000023",
157 "name": "awesomeos-x86_64-1000000000000023",
158 "url": "https://candlepin.local/path/to/awesomeos/x86_64/1000000000000023-11124",
159 "enabled": False,
160 },
161 {
162 "id": "awesomeos-ppc64-100000000000011",
163 "name": "awesomeos-ppc64-100000000000011",
164 "url": "https://candlepin.local/path/to/awesomeos/ppc64/100000000000011-11126",
165 "enabled": False,
166 },
167 {
168 "id": "awesomeos-99000",
169 "name": "awesomeos-99000",
170 "url": "https://candlepin.local/path/to/generic/awesomeos/99000-11113",
171 "enabled": True,
172 },
173 {
174 "id": "content-label-27060",
175 "name": "content-27060",
176 "url": "https://candlepin.local/foo/path/common/27060-1111",
177 "enabled": True,
178 },
179 {
180 "id": "content-label-no-gpg-32060",
181 "name": "content-nogpg-32060",
182 "url": "https://candlepin.local/foo/path/no_gpg/32060-234",
183 "enabled": False,
184 },
185 {
186 "id": "awesomeos-1000000000000023",
187 "name": "awesomeos-1000000000000023",
188 "url": "https://candlepin.local/path/to/generic/awesomeos/1000000000000023-11113",
189 "enabled": False,
190 },
191 {
192 "id": "awesomeos-x86-100000000000020",
193 "name": "awesomeos-x86-100000000000020",
194 "url": "https://candlepin.local/path/to/awesomeos/x86/100000000000020-11120",
195 "enabled": False,
196 },
197 {
198 "id": "awesomeos-x86_64-99000",
199 "name": "awesomeos-x86_64-99000",
200 "url": "https://candlepin.local/path/to/awesomeos/x86_64/99000-11124",
201 "enabled": True,
202 },
203 {
204 "id": "awesomeos-s390x-99000",
205 "name": "awesomeos-s390x-99000",
206 "url": "https://candlepin.local/path/to/awesomeos/s390x/99000-11121",
207 "enabled": False,
208 },
209 {
210 "id": "awesomeos-modifier-37080",
211 "name": "awesomeos-modifier-37080",
212 "url": "https://candlepin.local/example.com/awesomeos-modifier/37080-1112",
213 "enabled": False,
214 },
215 {
216 "id": "awesomeos-i686-99000",
217 "name": "awesomeos-i686-99000",
218 "url": "https://candlepin.local/path/to/awesomeos/i686/99000-11123",
219 "enabled": False,
220 },
221 {
222 "id": "fake-content-38072",
223 "name": "fake-content-38072",
224 "url": "https://candlepin.local/path/to/fake-content/38072-3902",
225 "enabled": True,
226 },
227 ]
228
229
230 # A static object with the list of repositories, used as reference to query
231 # the repositories, and create (by copy()) new Repos objects.
232 REPOS = Repos(REPOS_LIST)
233
234 # The mock string for the output of `subscription-manager repos --list`.
235 REPOS_LIST_OUTPUT = REPOS.to_subman_list_output()
236
237 # MUST match what's in the Rhsm class in the module.
238 SUBMAN_KWARGS = {
239 "environ_update": dict(LANG="C", LC_ALL="C", LC_MESSAGES="C"),
240 "expand_user_and_vars": False,
241 "use_unsafe_shell": False,
242 }
243
244
245 TEST_CASES = [
246 # enable a disabled repository
247 [
248 {
249 "name": "awesomeos-1000000000000023",
250 },
251 {
252 "id": "test_enable_single",
253 "run_command.calls": [
254 (
255 [
256 "/testbin/subscription-manager",
257 "repos",
258 "--list",
259 ],
260 SUBMAN_KWARGS,
261 (0, REPOS_LIST_OUTPUT, ""),
262 ),
263 (
264 [
265 "/testbin/subscription-manager",
266 "repos",
267 "--enable",
268 "awesomeos-1000000000000023",
269 ],
270 SUBMAN_KWARGS,
271 (0, "", ""),
272 ),
273 ],
274 "changed": True,
275 "repositories": REPOS.copy().enable("awesomeos-1000000000000023"),
276 },
277 ],
278 # enable an already enabled repository
279 [
280 {
281 "name": "fake-content-38072",
282 },
283 {
284 "id": "test_enable_already_enabled",
285 "run_command.calls": [
286 (
287 [
288 "/testbin/subscription-manager",
289 "repos",
290 "--list",
291 ],
292 SUBMAN_KWARGS,
293 (0, REPOS_LIST_OUTPUT, ""),
294 ),
295 ],
296 "changed": False,
297 "repositories": REPOS.copy(),
298 },
299 ],
300 # enable two disabled repositories
301 [
302 {
303 "name": ["awesomeos-1000000000000023", "content-label-no-gpg-32060"],
304 },
305 {
306 "id": "test_enable_multiple",
307 "run_command.calls": [
308 (
309 [
310 "/testbin/subscription-manager",
311 "repos",
312 "--list",
313 ],
314 SUBMAN_KWARGS,
315 (0, REPOS_LIST_OUTPUT, ""),
316 ),
317 (
318 [
319 "/testbin/subscription-manager",
320 "repos",
321 "--enable",
322 "awesomeos-1000000000000023",
323 "--enable",
324 "content-label-no-gpg-32060",
325 ],
326 SUBMAN_KWARGS,
327 (0, "", ""),
328 ),
329 ],
330 "changed": True,
331 "repositories": REPOS.copy().enable("awesomeos-1000000000000023").enable("content-label-no-gpg-32060"),
332 },
333 ],
334 # enable two repositories, one disabled and one already enabled
335 [
336 {
337 "name": ["awesomeos-1000000000000023", "fake-content-38072"],
338 },
339 {
340 "id": "test_enable_multiple_mixed",
341 "run_command.calls": [
342 (
343 [
344 "/testbin/subscription-manager",
345 "repos",
346 "--list",
347 ],
348 SUBMAN_KWARGS,
349 (0, REPOS_LIST_OUTPUT, ""),
350 ),
351 (
352 [
353 "/testbin/subscription-manager",
354 "repos",
355 "--enable",
356 "awesomeos-1000000000000023",
357 "--enable",
358 "fake-content-38072",
359 ],
360 SUBMAN_KWARGS,
361 (0, "", ""),
362 ),
363 ],
364 "changed": True,
365 "repositories": REPOS.copy().enable("awesomeos-1000000000000023"),
366 },
367 ],
368 # purge everything but never-enabled-content-801 (disabled)
369 [
370 {
371 "name": "never-enabled-content-801",
372 "purge": True,
373 },
374 {
375 "id": "test_purge_everything_but_one_disabled",
376 "run_command.calls": [
377 (
378 [
379 "/testbin/subscription-manager",
380 "repos",
381 "--list",
382 ],
383 SUBMAN_KWARGS,
384 (0, REPOS_LIST_OUTPUT, ""),
385 ),
386 (
387 [
388 "/testbin/subscription-manager",
389 "repos",
390 "--enable",
391 "never-enabled-content-801",
392 ]
393 + flatten([["--disable", i] for i in REPOS.ids_enabled() if i != "never-enabled-content-801"]),
394 SUBMAN_KWARGS,
395 (0, "", ""),
396 ),
397 ],
398 "changed": True,
399 "repositories": REPOS.copy().disable("*").enable("never-enabled-content-801"),
400 },
401 ],
402 # purge everything but awesomeos-99000 (already enabled)
403 [
404 {
405 "name": "awesomeos-99000",
406 "purge": True,
407 },
408 {
409 "id": "test_purge_everything_but_one_enabled",
410 "run_command.calls": [
411 (
412 [
413 "/testbin/subscription-manager",
414 "repos",
415 "--list",
416 ],
417 SUBMAN_KWARGS,
418 (0, REPOS_LIST_OUTPUT, ""),
419 ),
420 (
421 [
422 "/testbin/subscription-manager",
423 "repos",
424 "--enable",
425 "awesomeos-99000",
426 "--disable",
427 "content-label-27060",
428 "--disable",
429 "awesomeos-x86_64-99000",
430 "--disable",
431 "fake-content-38072",
432 ],
433 SUBMAN_KWARGS,
434 (0, "", ""),
435 ),
436 ],
437 "changed": True,
438 "repositories": REPOS.copy().disable("*").enable("awesomeos-99000"),
439 },
440 ],
441 # enable everything, then purge everything but content-label-27060
442 [
443 {
444 "name": "content-label-27060",
445 "purge": True,
446 },
447 {
448 "id": "test_enable_everything_purge_everything_but_one_enabled",
449 "run_command.calls": [
450 (
451 [
452 "/testbin/subscription-manager",
453 "repos",
454 "--list",
455 ],
456 SUBMAN_KWARGS,
457 (0, REPOS.copy().enable("*").to_subman_list_output(), ""),
458 ),
459 (
460 [
461 "/testbin/subscription-manager",
462 "repos",
463 "--enable",
464 "content-label-27060",
465 "--disable",
466 "never-enabled-content-801",
467 "--disable",
468 "never-enabled-content-100000000000060",
469 "--disable",
470 "awesomeos-x86_64-1000000000000023",
471 "--disable",
472 "awesomeos-ppc64-100000000000011",
473 "--disable",
474 "awesomeos-99000",
475 "--disable",
476 "content-label-no-gpg-32060",
477 "--disable",
478 "awesomeos-1000000000000023",
479 "--disable",
480 "awesomeos-x86-100000000000020",
481 "--disable",
482 "awesomeos-x86_64-99000",
483 "--disable",
484 "awesomeos-s390x-99000",
485 "--disable",
486 "awesomeos-modifier-37080",
487 "--disable",
488 "awesomeos-i686-99000",
489 "--disable",
490 "fake-content-38072",
491 ],
492 SUBMAN_KWARGS,
493 (0, "", ""),
494 ),
495 ],
496 "changed": True,
497 "repositories": REPOS.copy().disable("*").enable("content-label-27060"),
498 },
499 ],
500 # enable all awesomeos-*
501 [
502 {
503 "name": "awesomeos-*",
504 },
505 {
506 "id": "test_enable_all_awesomeos_star",
507 "run_command.calls": [
508 (
509 [
510 "/testbin/subscription-manager",
511 "repos",
512 "--list",
513 ],
514 SUBMAN_KWARGS,
515 (0, REPOS_LIST_OUTPUT, ""),
516 ),
517 (
518 [
519 "/testbin/subscription-manager",
520 "repos",
521 "--enable",
522 "awesomeos-x86_64-1000000000000023",
523 "--enable",
524 "awesomeos-ppc64-100000000000011",
525 "--enable",
526 "awesomeos-99000",
527 "--enable",
528 "awesomeos-1000000000000023",
529 "--enable",
530 "awesomeos-x86-100000000000020",
531 "--enable",
532 "awesomeos-x86_64-99000",
533 "--enable",
534 "awesomeos-s390x-99000",
535 "--enable",
536 "awesomeos-modifier-37080",
537 "--enable",
538 "awesomeos-i686-99000",
539 ],
540 SUBMAN_KWARGS,
541 (0, "", ""),
542 ),
543 ],
544 "changed": True,
545 "repositories": REPOS.copy().enable("awesomeos-*"),
546 },
547 ],
548 # purge everything but awesomeos-*
549 [
550 {
551 "name": REPOS.ids_enabled("awesomeos-*"),
552 "purge": True,
553 },
554 {
555 "id": "test_purge_everything_but_awesomeos_list",
556 "run_command.calls": [
557 (
558 [
559 "/testbin/subscription-manager",
560 "repos",
561 "--list",
562 ],
563 SUBMAN_KWARGS,
564 (0, REPOS_LIST_OUTPUT, ""),
565 ),
566 (
567 [
568 "/testbin/subscription-manager",
569 "repos",
570 "--enable",
571 "awesomeos-99000",
572 "--enable",
573 "awesomeos-x86_64-99000",
574 "--disable",
575 "content-label-27060",
576 "--disable",
577 "fake-content-38072",
578 ],
579 SUBMAN_KWARGS,
580 (0, "", ""),
581 ),
582 ],
583 "changed": True,
584 "repositories": REPOS.copy().disable("*").enable(REPOS.ids_enabled("awesomeos-*")),
585 },
586 ],
587 # enable a repository that does not exist
588 [
589 {
590 "name": "repo-that-does-not-exist",
591 },
592 {
593 "id": "test_enable_nonexisting",
594 "run_command.calls": [
595 (
596 [
597 "/testbin/subscription-manager",
598 "repos",
599 "--list",
600 ],
601 SUBMAN_KWARGS,
602 (0, REPOS_LIST_OUTPUT, ""),
603 ),
604 ],
605 "failed": True,
606 "msg": "repo-that-does-not-exist is not a valid repository ID",
607 },
608 ],
609 # disable an enabled repository
610 [
611 {
612 "name": "awesomeos-99000",
613 "state": "disabled",
614 },
615 {
616 "id": "test_disable_single",
617 "run_command.calls": [
618 (
619 [
620 "/testbin/subscription-manager",
621 "repos",
622 "--list",
623 ],
624 SUBMAN_KWARGS,
625 (0, REPOS_LIST_OUTPUT, ""),
626 ),
627 (
628 [
629 "/testbin/subscription-manager",
630 "repos",
631 "--disable",
632 "awesomeos-99000",
633 ],
634 SUBMAN_KWARGS,
635 (0, "", ""),
636 ),
637 ],
638 "changed": True,
639 "repositories": REPOS.copy().disable("awesomeos-99000"),
640 },
641 ],
642 # disable an already disabled repository
643 [
644 {
645 "name": "never-enabled-content-801",
646 "state": "disabled",
647 },
648 {
649 "id": "test_disable_already_disabled",
650 "run_command.calls": [
651 (
652 [
653 "/testbin/subscription-manager",
654 "repos",
655 "--list",
656 ],
657 SUBMAN_KWARGS,
658 (0, REPOS_LIST_OUTPUT, ""),
659 ),
660 ],
661 "changed": False,
662 "repositories": REPOS.copy(),
663 },
664 ],
665 # disable an already disabled repository, and purge
666 [
667 {
668 "name": "never-enabled-content-801",
669 "state": "disabled",
670 "purge": True,
671 },
672 {
673 "id": "test_disable_already_disabled_and_purge",
674 "run_command.calls": [
675 (
676 [
677 "/testbin/subscription-manager",
678 "repos",
679 "--list",
680 ],
681 SUBMAN_KWARGS,
682 (0, REPOS_LIST_OUTPUT, ""),
683 ),
684 (
685 [
686 "/testbin/subscription-manager",
687 "repos",
688 ]
689 + flatten([["--disable", i] for i in REPOS.ids_enabled()]),
690 SUBMAN_KWARGS,
691 (0, "", ""),
692 ),
693 ],
694 "changed": True,
695 "repositories": REPOS.copy().disable("*"),
696 },
697 ],
698 # disable an enabled repository, and purge
699 [
700 {
701 "name": "awesomeos-99000",
702 "state": "disabled",
703 "purge": True,
704 },
705 {
706 "id": "test_disable_single_and_purge",
707 "run_command.calls": [
708 (
709 [
710 "/testbin/subscription-manager",
711 "repos",
712 "--list",
713 ],
714 SUBMAN_KWARGS,
715 (0, REPOS_LIST_OUTPUT, ""),
716 ),
717 (
718 [
719 "/testbin/subscription-manager",
720 "repos",
721 ]
722 + flatten([["--disable", i] for i in REPOS.ids_enabled()]),
723 SUBMAN_KWARGS,
724 (0, "", ""),
725 ),
726 ],
727 "changed": True,
728 "repositories": REPOS.copy().disable("*"),
729 },
730 ],
731 # disable a repository that does not exist
732 [
733 {
734 "name": "repo-that-does-not-exist",
735 "state": "disabled",
736 },
737 {
738 "id": "test_disable_nonexisting",
739 "run_command.calls": [
740 (
741 [
742 "/testbin/subscription-manager",
743 "repos",
744 "--list",
745 ],
746 SUBMAN_KWARGS,
747 (0, REPOS_LIST_OUTPUT, ""),
748 ),
749 ],
750 "failed": True,
751 "msg": "repo-that-does-not-exist is not a valid repository ID",
752 },
753 ],
754 ]
755
756
757 TEST_CASES_IDS: list[str] = [item[1]["id"] for item in TEST_CASES] # type: ignore
758
759
760 @pytest.mark.parametrize(
761 "patch_ansible_module, testcase", TEST_CASES, ids=TEST_CASES_IDS, indirect=["patch_ansible_module"]
762 )
763 @pytest.mark.usefixtures("patch_ansible_module")
764 def test_rhsm_repository(mocker, capfd, patch_rhsm_repository, testcase):
765 """
766 Run unit tests for test cases listen in TEST_CASES
767 """
768
769 # Mock function used for running commands first
770 call_results = [item[2] for item in testcase["run_command.calls"]]
771 mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
772
773 # Try to run test case
774 with pytest.raises(SystemExit):
775 rhsm_repository.main()
776
777 out, err = capfd.readouterr()
778 results = json.loads(out)
779
780 if "failed" in testcase:
781 assert results["failed"] == testcase["failed"]
782 assert results["msg"] == testcase["msg"]
783 else:
784 assert "changed" in results
785 assert results["changed"] == testcase["changed"]
786 assert results["repositories"] == testcase["repositories"].to_list()
787
788 assert basic.AnsibleModule.run_command.call_count == len(testcase["run_command.calls"])
789 # FIXME ideally we need also to compare the actual calls with the expected
790 # ones; the problem is that the module uses a dict to collect the repositories
791 # to enable and disable, so the order of the --enable/--disable parameters to
792 # `subscription-manager repos` is not stable