pacman: implement change detection for update_cache=true; add cache_updated return value (#4337)

* Implement change detection for update_cache=true. Add cache_updated return value.

* ...

* Make sure pacman --sync --list is called only as often as necessary.
This commit is contained in:
Felix Fontein 2022-03-15 05:38:59 +01:00 committed by GitHub
parent ca2c64b5ca
commit cf4d68ac50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 13 deletions

View file

@ -329,33 +329,75 @@ class TestPacman:
assert out["changed"]
@pytest.mark.parametrize(
"module_args,expected_call",
"module_args,expected_calls,changed",
[
({}, ["pacman", "--sync", "--refresh"]),
({"force": True}, ["pacman", "--sync", "--refresh", "--refresh"]),
(
{"update_cache_extra_args": "--some-extra args"},
["pacman", "--sync", "--refresh", "--some-extra", "args"], # shlex test
{},
[
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'a\nb\nc', ''),
(["pacman", "--sync", "--refresh"], {'check_rc': False}, 0, 'stdout', 'stderr'),
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'b\na\nc', ''),
],
False,
),
(
{"force": True},
[
(["pacman", "--sync", "--refresh", "--refresh"], {'check_rc': False}, 0, 'stdout', 'stderr'),
],
True,
),
(
{"update_cache_extra_args": "--some-extra args"}, # shlex test
[
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'a\nb\nc', ''),
(["pacman", "--sync", "--refresh", "--some-extra", "args"], {'check_rc': False}, 0, 'stdout', 'stderr'),
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'a changed\nb\nc', ''),
],
True,
),
(
{"force": True, "update_cache_extra_args": "--some-extra args"},
["pacman", "--sync", "--refresh", "--some-extra", "args", "--refresh"],
[
(["pacman", "--sync", "--refresh", "--some-extra", "args", "--refresh"], {'check_rc': False}, 0, 'stdout', 'stderr'),
],
True,
),
(
# Test whether pacman --sync --list is not called more than twice
{"upgrade": True},
[
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'core foo 1.0.0-1 [installed]', ''),
(["pacman", "--sync", "--refresh"], {'check_rc': False}, 0, 'stdout', 'stderr'),
(["pacman", "--sync", "--list"], {'check_rc': True}, 0, 'core foo 1.0.0-1 [installed]', ''),
# The following is _build_inventory:
(["pacman", "--query"], {'check_rc': True}, 0, 'foo 1.0.0-1', ''),
(["pacman", "--query", "--groups"], {'check_rc': True}, 0, '', ''),
(["pacman", "--sync", "--groups", "--groups"], {'check_rc': True}, 0, '', ''),
(["pacman", "--query", "--upgrades"], {'check_rc': False}, 0, '', ''),
],
False,
),
],
)
def test_update_db(self, mock_empty_inventory, module_args, expected_call):
def test_update_db(self, module_args, expected_calls, changed):
args = {"update_cache": True}
args.update(module_args)
set_module_args(args)
self.mock_run_command.return_value = [0, "stdout", "stderr"]
self.mock_run_command.side_effect = [
(rc, stdout, stderr) for expected_call, kwargs, rc, stdout, stderr in expected_calls
]
with pytest.raises(AnsibleExitJson) as e:
P = pacman.Pacman(pacman.setup_module())
P.run()
self.mock_run_command.assert_called_with(mock.ANY, expected_call, check_rc=False)
self.mock_run_command.assert_has_calls([
mock.call(mock.ANY, expected_call, **kwargs) for expected_call, kwargs, rc, stdout, stderr in expected_calls
])
out = e.value.args[0]
assert out["changed"]
assert out["cache_updated"] == changed
assert out["changed"] == changed
@pytest.mark.parametrize(
"check_mode_value, run_command_data, upgrade_extra_args",