Debian Codegraph @

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

raw
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 typing as t
8 from unittest import mock
9
10 import pytest
11 from ansible.module_utils import basic
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
20 from ansible_collections.community.general.plugins.modules import pacman
21 from ansible_collections.community.general.plugins.modules.pacman import (
22 Package,
23 VersionTuple,
24 )
25
26
27 def get_bin_path(self, arg, required=False):
28 """Mock AnsibleModule.get_bin_path"""
29 return arg
30
31
32 # This inventory data is tightly coupled with the inventory test and the mock_valid_inventory fixture
33 valid_inventory = {
34 "installed_pkgs": {
35 "file": "5.41-1",
36 "filesystem": "2021.11.11-1",
37 "findutils": "4.8.0-1",
38 "gawk": "5.1.1-1",
39 "gettext": "0.21-1",
40 "grep": "3.7-1",
41 "gzip": "1.11-1",
42 "pacman": "6.0.1-2",
43 "pacman-mirrorlist": "20211114-1",
44 "sed": "4.8-1",
45 "sqlite": "3.36.0-1",
46 },
47 "installed_groups": {"base-devel": {"gawk", "grep", "file", "findutils", "pacman", "sed", "gzip", "gettext"}},
48 "available_pkgs": {
49 "acl": "2.3.1-1",
50 "amd-ucode": "20211027.1d00989-1",
51 "archlinux-keyring": "20211028-1",
52 "argon2": "20190702-3",
53 "attr": "2.5.1-1",
54 "audit": "3.0.6-5",
55 "autoconf": "2.71-1",
56 "automake": "1.16.5-1",
57 "b43-fwcutter": "019-3",
58 "gawk": "5.1.1-1",
59 "grep": "3.7-1",
60 "sqlite": "3.37.0-1",
61 "sudo": "1.9.8.p2-3",
62 },
63 "available_groups": {
64 "base-devel": {
65 "libtool",
66 "gawk",
67 "which",
68 "texinfo",
69 "fakeroot",
70 "grep",
71 "findutils",
72 "autoconf",
73 "gzip",
74 "pkgconf",
75 "flex",
76 "patch",
77 "groff",
78 "m4",
79 "bison",
80 "gcc",
81 "gettext",
82 "make",
83 "file",
84 "pacman",
85 "sed",
86 "automake",
87 "sudo",
88 "binutils",
89 },
90 "some-group": {"libtool", "sudo", "binutils"},
91 },
92 "upgradable_pkgs": {
93 "sqlite": VersionTuple(current="3.36.0-1", latest="3.37.0-1"),
94 },
95 "pkg_reasons": {
96 "file": "explicit",
97 "filesystem": "explicit",
98 "findutils": "explicit",
99 "gawk": "explicit",
100 "gettext": "explicit",
101 "grep": "explicit",
102 "gzip": "explicit",
103 "pacman": "explicit",
104 "pacman-mirrorlist": "dependency",
105 "sed": "explicit",
106 "sqlite": "explicit",
107 },
108 }
109
110 empty_inventory: dict[str, dict[str, t.Any]] = {
111 "installed_pkgs": {},
112 "available_pkgs": {},
113 "installed_groups": {},
114 "available_groups": {},
115 "upgradable_pkgs": {},
116 "pkg_reasons": {},
117 }
118
119
120 class TestPacman:
121 @pytest.fixture(autouse=True)
122 def run_command(self, mocker):
123 self.mock_run_command = mocker.patch.object(basic.AnsibleModule, "run_command", autospec=True)
124
125 @pytest.fixture
126 def mock_package_list(self, mocker):
127 return mocker.patch.object(pacman.Pacman, "package_list", autospec=True)
128
129 @pytest.fixture(autouse=True)
130 def common(self, mocker):
131 self.mock_module = mocker.patch.multiple(
132 basic.AnsibleModule,
133 exit_json=exit_json,
134 fail_json=fail_json,
135 get_bin_path=get_bin_path,
136 )
137
138 @pytest.fixture
139 def mock_empty_inventory(self, mocker):
140 inv = empty_inventory
141 return mocker.patch.object(pacman.Pacman, "_build_inventory", return_value=inv)
142
143 @pytest.fixture
144 def mock_valid_inventory(self, mocker):
145 return mocker.patch.object(pacman.Pacman, "_build_inventory", return_value=valid_inventory)
146
147 def test_fail_without_required_args(self):
148 with pytest.raises(AnsibleFailJson) as e:
149 with set_module_args({}):
150 pacman.main()
151 assert e.match(r"one of the following is required")
152
153 def test_success(self, mock_empty_inventory):
154 with set_module_args({"update_cache": True}): # Simplest args to let init go through
155 P = pacman.Pacman(pacman.setup_module())
156 with pytest.raises(AnsibleExitJson):
157 P.success()
158
159 def test_fail(self, mock_empty_inventory):
160 with set_module_args({"update_cache": True}):
161 P = pacman.Pacman(pacman.setup_module())
162
163 args = dict(msg="msg", stdout="something", stderr="somethingelse", cmd=["command", "with", "args"], rc=1)
164 with pytest.raises(AnsibleFailJson) as e:
165 P.fail(**args)
166
167 assert all(item in e.value.args[0] for item in args)
168
169 @pytest.mark.parametrize(
170 "expected, run_command_side_effect, raises",
171 [
172 (
173 # Regular run
174 valid_inventory,
175 [
176 [ # pacman --query
177 0,
178 """file 5.41-1
179 filesystem 2021.11.11-1
180 findutils 4.8.0-1
181 gawk 5.1.1-1
182 gettext 0.21-1
183 grep 3.7-1
184 gzip 1.11-1
185 pacman 6.0.1-2
186 pacman-mirrorlist 20211114-1
187 sed 4.8-1
188 sqlite 3.36.0-1
189 """,
190 "",
191 ],
192 ( # pacman --query --group
193 0,
194 """base-devel file
195 base-devel findutils
196 base-devel gawk
197 base-devel gettext
198 base-devel grep
199 base-devel gzip
200 base-devel pacman
201 base-devel sed
202 """,
203 "",
204 ),
205 ( # pacman --sync --list
206 0,
207 """core acl 2.3.1-1 [installed]
208 core amd-ucode 20211027.1d00989-1
209 core archlinux-keyring 20211028-1 [installed]
210 core argon2 20190702-3 [installed]
211 core attr 2.5.1-1 [installed]
212 core audit 3.0.6-5 [installed: 3.0.6-2]
213 core autoconf 2.71-1
214 core automake 1.16.5-1
215 core b43-fwcutter 019-3
216 core gawk 5.1.1-1 [installed]
217 core grep 3.7-1 [installed]
218 core sqlite 3.37.0-1 [installed: 3.36.0-1]
219 code sudo 1.9.8.p2-3
220 """,
221 "",
222 ),
223 ( # pacman --sync --group --group
224 0,
225 """base-devel autoconf
226 base-devel automake
227 base-devel binutils
228 base-devel bison
229 base-devel fakeroot
230 base-devel file
231 base-devel findutils
232 base-devel flex
233 base-devel gawk
234 base-devel gcc
235 base-devel gettext
236 base-devel grep
237 base-devel groff
238 base-devel gzip
239 base-devel libtool
240 base-devel m4
241 base-devel make
242 base-devel pacman
243 base-devel patch
244 base-devel pkgconf
245 base-devel sed
246 base-devel sudo
247 base-devel texinfo
248 base-devel which
249 some-group libtool
250 some-group sudo
251 some-group binutils
252 """,
253 "",
254 ),
255 ( # pacman --query --upgrades
256 0,
257 """sqlite 3.36.0-1 -> 3.37.0-1
258 systemd 249.6-3 -> 249.7-2 [ignored]
259 """,
260 "",
261 ),
262 ( # pacman --query --explicit
263 0,
264 """file 5.41-1
265 filesystem 2021.11.11-1
266 findutils 4.8.0-1
267 gawk 5.1.1-1
268 gettext 0.21-1
269 grep 3.7-1
270 gzip 1.11-1
271 pacman 6.0.1-2
272 sed 4.8-1
273 sqlite 3.36.0-1
274 """,
275 "",
276 ),
277 ( # pacman --query --deps
278 0,
279 """pacman-mirrorlist 20211114-1
280 """,
281 "",
282 ),
283 ],
284 None,
285 ),
286 (
287 # All good, but call to --query --upgrades return 1. aka nothing to upgrade
288 # with a pacman warning
289 empty_inventory,
290 [
291 (0, "", ""),
292 (0, "", ""),
293 (0, "", ""),
294 (0, "", ""),
295 (
296 1,
297 "",
298 "warning: config file /etc/pacman.conf, line 34: directive 'TotalDownload' in section 'options' not recognized.",
299 ),
300 (0, "", ""),
301 (0, "", ""),
302 ],
303 None,
304 ),
305 (
306 # failure
307 empty_inventory,
308 [
309 (0, "", ""),
310 (0, "", ""),
311 (0, "", ""),
312 (0, "", ""),
313 (
314 1,
315 "partial\npkg\\nlist",
316 "some warning",
317 ),
318 (0, "", ""),
319 (0, "", ""),
320 ],
321 AnsibleFailJson,
322 ),
323 ],
324 )
325 def test_build_inventory(self, expected, run_command_side_effect, raises):
326 self.mock_run_command.side_effect = run_command_side_effect
327
328 with set_module_args({"update_cache": True}):
329 if raises:
330 with pytest.raises(raises):
331 P = pacman.Pacman(pacman.setup_module())
332 P._build_inventory()
333 else:
334 P = pacman.Pacman(pacman.setup_module())
335 assert P._build_inventory() == expected
336
337 @pytest.mark.parametrize("check_mode_value", [True, False])
338 def test_upgrade_check_empty_inventory(self, mock_empty_inventory, check_mode_value):
339 with set_module_args({"upgrade": True, "_ansible_check_mode": check_mode_value}):
340 P = pacman.Pacman(pacman.setup_module())
341 with pytest.raises(AnsibleExitJson) as e:
342 P.run()
343 assert self.mock_run_command.call_count == 0
344 out = e.value.args[0]
345 assert "packages" not in out
346 assert not out["changed"]
347 assert "diff" not in out
348
349 def test_update_db_check(self, mock_empty_inventory):
350 with set_module_args({"update_cache": True, "_ansible_check_mode": True}):
351 P = pacman.Pacman(pacman.setup_module())
352
353 with pytest.raises(AnsibleExitJson) as e:
354 P.run()
355 assert self.mock_run_command.call_count == 0
356 out = e.value.args[0]
357 assert "packages" not in out
358 assert out["changed"]
359
360 @pytest.mark.parametrize(
361 "module_args,expected_calls,changed",
362 [
363 (
364 {},
365 [
366 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "a\nb\nc", ""),
367 (["pacman", "--sync", "--refresh"], {"check_rc": False}, 0, "stdout", "stderr"),
368 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "b\na\nc", ""),
369 ],
370 False,
371 ),
372 (
373 {"force": True},
374 [
375 (["pacman", "--sync", "--refresh", "--refresh"], {"check_rc": False}, 0, "stdout", "stderr"),
376 ],
377 True,
378 ),
379 (
380 {"update_cache_extra_args": "--some-extra args"}, # shlex test
381 [
382 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "a\nb\nc", ""),
383 (
384 ["pacman", "--sync", "--refresh", "--some-extra", "args"],
385 {"check_rc": False},
386 0,
387 "stdout",
388 "stderr",
389 ),
390 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "a changed\nb\nc", ""),
391 ],
392 True,
393 ),
394 (
395 {"force": True, "update_cache_extra_args": "--some-extra args"},
396 [
397 (
398 ["pacman", "--sync", "--refresh", "--some-extra", "args", "--refresh"],
399 {"check_rc": False},
400 0,
401 "stdout",
402 "stderr",
403 ),
404 ],
405 True,
406 ),
407 (
408 # root + cachedir + config: all commands get prefixed with the global options
409 {"root": "/mnt", "cachedir": "/mnt/var/cache/pacman/pkg", "config": "/alt/pacman.conf"},
410 [
411 (
412 [
413 "pacman",
414 "--config",
415 "/alt/pacman.conf",
416 "--root",
417 "/mnt",
418 "--cachedir",
419 "/mnt/var/cache/pacman/pkg",
420 "--sync",
421 "--list",
422 ],
423 {"check_rc": True},
424 0,
425 "a\nb\nc",
426 "",
427 ),
428 (
429 [
430 "pacman",
431 "--config",
432 "/alt/pacman.conf",
433 "--root",
434 "/mnt",
435 "--cachedir",
436 "/mnt/var/cache/pacman/pkg",
437 "--sync",
438 "--refresh",
439 ],
440 {"check_rc": False},
441 0,
442 "stdout",
443 "stderr",
444 ),
445 (
446 [
447 "pacman",
448 "--config",
449 "/alt/pacman.conf",
450 "--root",
451 "/mnt",
452 "--cachedir",
453 "/mnt/var/cache/pacman/pkg",
454 "--sync",
455 "--list",
456 ],
457 {"check_rc": True},
458 0,
459 "a\nb\nc",
460 "",
461 ),
462 ],
463 False,
464 ),
465 (
466 # config only (no root/cachedir)
467 {"config": "/alt/pacman.conf"},
468 [
469 (
470 ["pacman", "--config", "/alt/pacman.conf", "--sync", "--list"],
471 {"check_rc": True},
472 0,
473 "a\nb\nc",
474 "",
475 ),
476 (
477 ["pacman", "--config", "/alt/pacman.conf", "--sync", "--refresh"],
478 {"check_rc": False},
479 0,
480 "stdout",
481 "stderr",
482 ),
483 (
484 ["pacman", "--config", "/alt/pacman.conf", "--sync", "--list"],
485 {"check_rc": True},
486 0,
487 "a\nb\nc",
488 "",
489 ),
490 ],
491 False,
492 ),
493 (
494 # Test whether pacman --sync --list is not called more than twice
495 {"upgrade": True},
496 [
497 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "core foo 1.0.0-1 [installed]", ""),
498 (["pacman", "--sync", "--refresh"], {"check_rc": False}, 0, "stdout", "stderr"),
499 (["pacman", "--sync", "--list"], {"check_rc": True}, 0, "core foo 1.0.0-1 [installed]", ""),
500 # The following is _build_inventory:
501 (["pacman", "--query"], {"check_rc": True}, 0, "foo 1.0.0-1", ""),
502 (["pacman", "--query", "--groups"], {"check_rc": True}, 0, "", ""),
503 (["pacman", "--sync", "--groups", "--groups"], {"check_rc": True}, 0, "", ""),
504 (["pacman", "--query", "--upgrades"], {"check_rc": False}, 0, "", ""),
505 (["pacman", "--query", "--explicit"], {"check_rc": True}, 0, "foo 1.0.0-1", ""),
506 (["pacman", "--query", "--deps"], {"check_rc": True}, 0, "", ""),
507 ],
508 False,
509 ),
510 ],
511 )
512 def test_update_db(self, module_args, expected_calls, changed):
513 args = {"update_cache": True}
514 args.update(module_args)
515 with set_module_args(args):
516 self.mock_run_command.side_effect = [
517 (rc, stdout, stderr) for expected_call, kwargs, rc, stdout, stderr in expected_calls
518 ]
519 with pytest.raises(AnsibleExitJson) as e:
520 P = pacman.Pacman(pacman.setup_module())
521 P.run()
522
523 self.mock_run_command.assert_has_calls(
524 [
525 mock.call(mock.ANY, expected_call, **kwargs)
526 for expected_call, kwargs, rc, stdout, stderr in expected_calls
527 ]
528 )
529 out = e.value.args[0]
530 assert out["cache_updated"] == changed
531 assert out["changed"] == changed
532
533 @pytest.mark.parametrize(
534 "check_mode_value, run_command_data, upgrade_extra_args",
535 [
536 # just check
537 (True, None, None),
538 (
539 # for real
540 False,
541 {
542 "args": ["pacman", "--sync", "--sysupgrade", "--quiet", "--noconfirm"],
543 "return_value": [0, "stdout", "stderr"],
544 },
545 None,
546 ),
547 (
548 # with extra args
549 False,
550 {
551 "args": [
552 "pacman",
553 "--sync",
554 "--sysupgrade",
555 "--quiet",
556 "--noconfirm",
557 "--some",
558 "value",
559 ],
560 "return_value": [0, "stdout", "stderr"],
561 },
562 "--some value",
563 ),
564 ],
565 )
566 def test_upgrade(self, mock_valid_inventory, check_mode_value, run_command_data, upgrade_extra_args):
567 args = {"upgrade": True, "_ansible_check_mode": check_mode_value}
568 if upgrade_extra_args:
569 args["upgrade_extra_args"] = upgrade_extra_args
570 with set_module_args(args):
571 if run_command_data and "return_value" in run_command_data:
572 self.mock_run_command.return_value = run_command_data["return_value"]
573
574 P = pacman.Pacman(pacman.setup_module())
575
576 with pytest.raises(AnsibleExitJson) as e:
577 P.run()
578 out = e.value.args[0]
579
580 if check_mode_value:
581 assert self.mock_run_command.call_count == 0
582
583 if run_command_data and "args" in run_command_data:
584 self.mock_run_command.assert_called_with(mock.ANY, run_command_data["args"], check_rc=False)
585 assert out["stdout"] == "stdout"
586 assert out["stderr"] == "stderr"
587
588 assert len(out["packages"]) == 1 and "sqlite" in out["packages"]
589 assert out["changed"]
590 assert out["diff"]["before"] and out["diff"]["after"]
591
592 def test_upgrade_fail(self, mock_valid_inventory):
593 with set_module_args({"upgrade": True}):
594 self.mock_run_command.return_value = [1, "stdout", "stderr"]
595 P = pacman.Pacman(pacman.setup_module())
596
597 with pytest.raises(AnsibleFailJson) as e:
598 P.run()
599 assert self.mock_run_command.call_count == 1
600 out = e.value.args[0]
601 assert out["failed"]
602 assert out["stdout"] == "stdout"
603 assert out["stderr"] == "stderr"
604
605 @pytest.mark.parametrize(
606 "state, pkg_names, expected, run_command_data, raises",
607 [
608 # regular packages, no resolving required
609 (
610 "present",
611 ["acl", "attr"],
612 [Package(name="acl", source="acl"), Package(name="attr", source="attr")],
613 None,
614 None,
615 ),
616 (
617 # group expansion
618 "present",
619 ["acl", "some-group", "attr"],
620 [
621 Package(name="acl", source="acl"),
622 Package(name="binutils", source="binutils"),
623 Package(name="libtool", source="libtool"),
624 Package(name="sudo", source="sudo"),
625 Package(name="attr", source="attr"),
626 ],
627 None,
628 None,
629 ),
630 (
631 # <repo>/<pkgname> format -> call to pacman to resolve
632 "present",
633 ["community/elixir"],
634 [Package(name="elixir", source="community/elixir")],
635 {
636 "calls": [
637 mock.call(
638 mock.ANY,
639 ["pacman", "--sync", "--print-format", "%n", "community/elixir"],
640 check_rc=False,
641 )
642 ],
643 "side_effect": [(0, "elixir", "")],
644 },
645 None,
646 ),
647 (
648 # catch all -> call to pacman to resolve (--sync and --upgrade)
649 "present",
650 ["somepackage-12.3-x86_64.pkg.tar.zst"],
651 [
652 Package(
653 name="somepackage",
654 source="somepackage-12.3-x86_64.pkg.tar.zst",
655 source_is_URL=True,
656 )
657 ],
658 {
659 "calls": [
660 mock.call(
661 mock.ANY,
662 [
663 "pacman",
664 "--sync",
665 "--print-format",
666 "%n",
667 "somepackage-12.3-x86_64.pkg.tar.zst",
668 ],
669 check_rc=False,
670 ),
671 mock.call(
672 mock.ANY,
673 [
674 "pacman",
675 "--upgrade",
676 "--print-format",
677 "%n",
678 "somepackage-12.3-x86_64.pkg.tar.zst",
679 ],
680 check_rc=False,
681 ),
682 ],
683 "side_effect": [(1, "", "nope"), (0, "somepackage", "")],
684 },
685 None,
686 ),
687 (
688 # install a package that doesn't exist. call pacman twice and give up
689 "present",
690 ["unknown-package"],
691 [],
692 {
693 # no call validation, since it will fail
694 "side_effect": [(1, "", "nope"), (1, "", "stillnope")],
695 },
696 AnsibleFailJson,
697 ),
698 (
699 # Edge case: resolve a pkg that doesn't exist when trying to remove it (state == absent).
700 # will fallback to file + url format but not complain since it is already not there
701 # Can happen if a pkg is removed for the repos (or if a repo is disabled/removed)
702 "absent",
703 ["unknown-package-to-remove"],
704 [],
705 {
706 "calls": [
707 mock.call(
708 mock.ANY,
709 ["pacman", "--sync", "--print-format", "%n", "unknown-package-to-remove"],
710 check_rc=False,
711 ),
712 mock.call(
713 mock.ANY,
714 ["pacman", "--upgrade", "--print-format", "%n", "unknown-package-to-remove"],
715 check_rc=False,
716 ),
717 ],
718 "side_effect": [(1, "", "nope"), (1, "", "stillnope")],
719 },
720 None, # Doesn't fail
721 ),
722 ],
723 )
724 def test_package_list(self, mock_valid_inventory, state, pkg_names, expected, run_command_data, raises):
725 with set_module_args({"name": pkg_names, "state": state}):
726 P = pacman.Pacman(pacman.setup_module())
727 P.inventory = P._build_inventory()
728 if run_command_data:
729 self.mock_run_command.side_effect = run_command_data["side_effect"]
730
731 if raises:
732 with pytest.raises(raises):
733 P.package_list()
734 else:
735 assert sorted(P.package_list()) == sorted(expected)
736 if run_command_data:
737 assert self.mock_run_command.mock_calls == run_command_data["calls"]
738
739 @pytest.mark.parametrize("check_mode_value", [True, False])
740 @pytest.mark.parametrize(
741 "name, state, package_list",
742 [
743 (["already-absent"], "absent", [Package("already-absent", "already-absent")]),
744 (["grep"], "present", [Package("grep", "grep")]),
745 ],
746 )
747 def test_op_packages_nothing_to_do(
748 self, mock_valid_inventory, mock_package_list, check_mode_value, name, state, package_list
749 ):
750 with set_module_args({"name": name, "state": state, "_ansible_check_mode": check_mode_value}):
751 mock_package_list.return_value = package_list
752 P = pacman.Pacman(pacman.setup_module())
753 with pytest.raises(AnsibleExitJson) as e:
754 P.run()
755 out = e.value.args[0]
756 assert not out["changed"]
757 assert "packages" in out
758 assert "diff" not in out
759 assert self.mock_run_command.call_count == 0
760
761 @pytest.mark.parametrize(
762 "module_args, expected_packages, package_list_out, run_command_data, raises",
763 [
764 (
765 # remove pkg: Check mode -- call to print format but that's it
766 {"_ansible_check_mode": True, "name": ["grep"], "state": "absent"},
767 ["grep-version"],
768 [Package("grep", "grep")],
769 {
770 "calls": [
771 mock.call(
772 mock.ANY,
773 [
774 "pacman",
775 "--remove",
776 "--noconfirm",
777 "--noprogressbar",
778 "--print-format",
779 "%n-%v",
780 "grep",
781 ],
782 check_rc=False,
783 ),
784 ],
785 "side_effect": [(0, "grep-version", "")],
786 },
787 AnsibleExitJson,
788 ),
789 (
790 # remove pkg for real now -- with 2 packages
791 {"name": ["grep", "gawk"], "state": "absent"},
792 ["grep-version", "gawk-anotherversion"],
793 [Package("grep", "grep"), Package("gawk", "gawk")],
794 {
795 "calls": [
796 mock.call(
797 mock.ANY,
798 [
799 "pacman",
800 "--remove",
801 "--noconfirm",
802 "--noprogressbar",
803 "--print-format",
804 "%n-%v",
805 "grep",
806 "gawk",
807 ],
808 check_rc=False,
809 ),
810 mock.call(
811 mock.ANY,
812 ["pacman", "--remove", "--noconfirm", "--noprogressbar", "grep", "gawk"],
813 check_rc=False,
814 ),
815 ],
816 "side_effect": [
817 (0, "grep-version\ngawk-anotherversion", ""),
818 (0, "stdout", "stderr"),
819 ],
820 },
821 AnsibleExitJson,
822 ),
823 (
824 # remove pkg force + extra_args
825 {
826 "name": ["grep"],
827 "state": "absent",
828 "force": True,
829 "extra_args": "--some --extra arg",
830 },
831 ["grep-version"],
832 [Package("grep", "grep")],
833 {
834 "calls": [
835 mock.call(
836 mock.ANY,
837 [
838 "pacman",
839 "--remove",
840 "--noconfirm",
841 "--noprogressbar",
842 "--some",
843 "--extra",
844 "arg",
845 "--nodeps",
846 "--nodeps",
847 "--print-format",
848 "%n-%v",
849 "grep",
850 ],
851 check_rc=False,
852 ),
853 mock.call(
854 mock.ANY,
855 [
856 "pacman",
857 "--remove",
858 "--noconfirm",
859 "--noprogressbar",
860 "--some",
861 "--extra",
862 "arg",
863 "--nodeps",
864 "--nodeps",
865 "grep",
866 ],
867 check_rc=False,
868 ),
869 ],
870 "side_effect": [
871 (0, "grep-version", ""),
872 (0, "stdout", "stderr"),
873 ],
874 },
875 AnsibleExitJson,
876 ),
877 (
878 # remove pkg -- Failure to list
879 {"name": ["grep"], "state": "absent"},
880 ["grep-3.7-1"],
881 [Package("grep", "grep")],
882 {
883 "calls": [
884 mock.call(
885 mock.ANY,
886 [
887 "pacman",
888 "--remove",
889 "--noconfirm",
890 "--noprogressbar",
891 "--print-format",
892 "%n-%v",
893 "grep",
894 ],
895 check_rc=False,
896 )
897 ],
898 "side_effect": [
899 (1, "stdout", "stderr"),
900 ],
901 },
902 AnsibleFailJson,
903 ),
904 (
905 # remove pkg -- Failure to remove
906 {"name": ["grep"], "state": "absent"},
907 ["grep-3.7-1"],
908 [Package("grep", "grep")],
909 {
910 "calls": [
911 mock.call(
912 mock.ANY,
913 [
914 "pacman",
915 "--remove",
916 "--noconfirm",
917 "--noprogressbar",
918 "--print-format",
919 "%n-%v",
920 "grep",
921 ],
922 check_rc=False,
923 ),
924 mock.call(
925 mock.ANY,
926 ["pacman", "--remove", "--noconfirm", "--noprogressbar", "grep"],
927 check_rc=False,
928 ),
929 ],
930 "side_effect": [
931 (0, "grep", ""),
932 (1, "stdout", "stderr"),
933 ],
934 },
935 AnsibleFailJson,
936 ),
937 (
938 # install pkg: Check mode
939 {"_ansible_check_mode": True, "name": ["sudo"], "state": "present"},
940 ["sudo"],
941 [Package("sudo", "sudo")],
942 {
943 "calls": [
944 mock.call(
945 mock.ANY,
946 [
947 "pacman",
948 "--noconfirm",
949 "--noprogressbar",
950 "--needed",
951 "--sync",
952 "--print-format",
953 "%n %v",
954 "sudo",
955 ],
956 check_rc=False,
957 ),
958 ],
959 "side_effect": [(0, "sudo version", "")],
960 },
961 AnsibleExitJson,
962 ),
963 (
964 # Install pkgs: one regular, one already installed, one file URL and one https URL
965 {
966 "name": [
967 "sudo",
968 "grep",
969 "./somepackage-12.3-x86_64.pkg.tar.zst",
970 "http://example.com/otherpkg-1.2-x86_64.pkg.tar.zst",
971 ],
972 "state": "present",
973 },
974 ["otherpkg", "somepackage", "sudo"],
975 [
976 Package("sudo", "sudo"),
977 Package("grep", "grep"),
978 Package("somepackage", "./somepackage-12.3-x86_64.pkg.tar.zst", source_is_URL=True),
979 Package(
980 "otherpkg",
981 "http://example.com/otherpkg-1.2-x86_64.pkg.tar.zst",
982 source_is_URL=True,
983 ),
984 ],
985 {
986 "calls": [
987 mock.call(
988 mock.ANY,
989 [
990 "pacman",
991 "--noconfirm",
992 "--noprogressbar",
993 "--needed",
994 "--sync",
995 "--print-format",
996 "%n %v",
997 "sudo",
998 ],
999 check_rc=False,
1000 ),
1001 mock.call(
1002 mock.ANY,
1003 [
1004 "pacman",
1005 "--noconfirm",
1006 "--noprogressbar",
1007 "--needed",
1008 "--upgrade",
1009 "--print-format",
1010 "%n %v",
1011 "./somepackage-12.3-x86_64.pkg.tar.zst",
1012 "http://example.com/otherpkg-1.2-x86_64.pkg.tar.zst",
1013 ],
1014 check_rc=False,
1015 ),
1016 mock.call(
1017 mock.ANY,
1018 [
1019 "pacman",
1020 "--noconfirm",
1021 "--noprogressbar",
1022 "--needed",
1023 "--sync",
1024 "sudo",
1025 ],
1026 check_rc=False,
1027 ),
1028 mock.call(
1029 mock.ANY,
1030 [
1031 "pacman",
1032 "--noconfirm",
1033 "--noprogressbar",
1034 "--needed",
1035 "--upgrade",
1036 "./somepackage-12.3-x86_64.pkg.tar.zst",
1037 "http://example.com/otherpkg-1.2-x86_64.pkg.tar.zst",
1038 ],
1039 check_rc=False,
1040 ),
1041 ],
1042 "side_effect": [
1043 (0, "sudo version", ""),
1044 (0, "somepackage 12.3\notherpkg 1.2", ""),
1045 (0, "", ""),
1046 (0, "", ""),
1047 ],
1048 },
1049 AnsibleExitJson,
1050 ),
1051 (
1052 # install pkg, extra_args
1053 {"name": ["sudo"], "state": "present", "extra_args": "--some --thing else"},
1054 ["sudo"],
1055 [Package("sudo", "sudo")],
1056 {
1057 "calls": [
1058 mock.call(
1059 mock.ANY,
1060 [
1061 "pacman",
1062 "--noconfirm",
1063 "--noprogressbar",
1064 "--needed",
1065 "--some",
1066 "--thing",
1067 "else",
1068 "--sync",
1069 "--print-format",
1070 "%n %v",
1071 "sudo",
1072 ],
1073 check_rc=False,
1074 ),
1075 mock.call(
1076 mock.ANY,
1077 [
1078 "pacman",
1079 "--noconfirm",
1080 "--noprogressbar",
1081 "--needed",
1082 "--some",
1083 "--thing",
1084 "else",
1085 "--sync",
1086 "sudo",
1087 ],
1088 check_rc=False,
1089 ),
1090 ],
1091 "side_effect": [(0, "sudo version", ""), (0, "", "")],
1092 },
1093 AnsibleExitJson,
1094 ),
1095 (
1096 # install pkg with root: global options come before the operation flags
1097 {"name": ["sudo"], "state": "present", "root": "/mnt", "cachedir": "/mnt/var/cache/pacman/pkg"},
1098 ["sudo"],
1099 [Package("sudo", "sudo")],
1100 {
1101 "calls": [
1102 mock.call(
1103 mock.ANY,
1104 [
1105 "pacman",
1106 "--root",
1107 "/mnt",
1108 "--cachedir",
1109 "/mnt/var/cache/pacman/pkg",
1110 "--noconfirm",
1111 "--noprogressbar",
1112 "--needed",
1113 "--sync",
1114 "--print-format",
1115 "%n %v",
1116 "sudo",
1117 ],
1118 check_rc=False,
1119 ),
1120 mock.call(
1121 mock.ANY,
1122 [
1123 "pacman",
1124 "--root",
1125 "/mnt",
1126 "--cachedir",
1127 "/mnt/var/cache/pacman/pkg",
1128 "--noconfirm",
1129 "--noprogressbar",
1130 "--needed",
1131 "--sync",
1132 "sudo",
1133 ],
1134 check_rc=False,
1135 ),
1136 ],
1137 "side_effect": [(0, "sudo version", ""), (0, "", "")],
1138 },
1139 AnsibleExitJson,
1140 ),
1141 (
1142 # latest pkg: Check mode
1143 {"_ansible_check_mode": True, "name": ["sqlite"], "state": "latest"},
1144 ["sqlite"],
1145 [Package("sqlite", "sqlite")],
1146 {
1147 "calls": [
1148 mock.call(
1149 mock.ANY,
1150 [
1151 "pacman",
1152 "--noconfirm",
1153 "--noprogressbar",
1154 "--needed",
1155 "--sync",
1156 "--print-format",
1157 "%n %v",
1158 "sqlite",
1159 ],
1160 check_rc=False,
1161 ),
1162 ],
1163 "side_effect": [(0, "sqlite new-version", "")],
1164 },
1165 AnsibleExitJson,
1166 ),
1167 (
1168 # latest pkg -- one already latest
1169 {"name": ["sqlite", "grep"], "state": "latest"},
1170 ["sqlite"],
1171 [Package("sqlite", "sqlite")],
1172 {
1173 "calls": [
1174 mock.call(
1175 mock.ANY,
1176 [
1177 "pacman",
1178 "--noconfirm",
1179 "--noprogressbar",
1180 "--needed",
1181 "--sync",
1182 "--print-format",
1183 "%n %v",
1184 "sqlite",
1185 ],
1186 check_rc=False,
1187 ),
1188 mock.call(
1189 mock.ANY,
1190 [
1191 "pacman",
1192 "--noconfirm",
1193 "--noprogressbar",
1194 "--needed",
1195 "--sync",
1196 "sqlite",
1197 ],
1198 check_rc=False,
1199 ),
1200 ],
1201 "side_effect": [(0, "sqlite new-version", ""), (0, "", "")],
1202 },
1203 AnsibleExitJson,
1204 ),
1205 ],
1206 )
1207 def test_op_packages(
1208 self,
1209 mock_valid_inventory,
1210 mock_package_list,
1211 module_args,
1212 expected_packages,
1213 package_list_out,
1214 run_command_data,
1215 raises,
1216 ):
1217 with set_module_args(module_args):
1218 self.mock_run_command.side_effect = run_command_data["side_effect"]
1219 mock_package_list.return_value = package_list_out
1220
1221 P = pacman.Pacman(pacman.setup_module())
1222 with pytest.raises(raises) as e:
1223 P.run()
1224 out = e.value.args[0]
1225
1226 assert self.mock_run_command.mock_calls == run_command_data["calls"]
1227 if raises == AnsibleExitJson:
1228 assert out["packages"] == expected_packages
1229 assert out["changed"]
1230 assert "packages" in out
1231 assert "diff" in out
1232 else:
1233 assert out["stdout"] == "stdout"
1234 assert out["stderr"] == "stderr"