diff --git a/changelog/65152.fixed.md b/changelog/65152.fixed.md new file mode 100644 index 000000000000..dfa2dac34622 --- /dev/null +++ b/changelog/65152.fixed.md @@ -0,0 +1 @@ +speed up yumpkg list_pkgs by not requiring digest or signature verification on lookup. diff --git a/salt/modules/yumpkg.py b/salt/modules/yumpkg.py index 281fc3bc223d..192ea61635a3 100644 --- a/salt/modules/yumpkg.py +++ b/salt/modules/yumpkg.py @@ -746,6 +746,8 @@ def list_pkgs(versions_as_list=False, **kwargs): cmd = [ "rpm", "-qa", + "--nodigest", + "--nosignature", "--queryformat", salt.utils.pkg.rpm.QUERYFORMAT.replace("%{REPOID}", "(none)") + "\n", ] diff --git a/tests/pytests/functional/modules/test_yumpkg.py b/tests/pytests/functional/modules/test_yumpkg.py new file mode 100644 index 000000000000..36b357a61705 --- /dev/null +++ b/tests/pytests/functional/modules/test_yumpkg.py @@ -0,0 +1,41 @@ +import pytest + +import salt.modules.cmdmod +import salt.modules.pkg_resource +import salt.modules.yumpkg +import salt.utils.pkg.rpm + + +@pytest.fixture +def configure_loader_modules(minion_opts): + return { + salt.modules.yumpkg: { + "__salt__": { + "cmd.run": salt.modules.cmdmod.run, + "pkg_resource.add_pkg": salt.modules.pkg_resource.add_pkg, + "pkg_resource.format_pkg_list": salt.modules.pkg_resource.format_pkg_list, + }, + "__grains__": {"osarch": salt.utils.pkg.rpm.get_osarch()}, + }, + } + + +@pytest.mark.slow_test +def test_yum_list_pkgs(grains): + """ + compare the output of rpm -qa vs the return of yumpkg.list_pkgs, + make sure that any changes to ympkg.list_pkgs still returns. + """ + + if grains["os_family"] != "RedHat": + pytest.skip("Skip if not RedHat") + cmd = [ + "rpm", + "-qa", + "--queryformat", + "%{NAME}\n", + ] + known_pkgs = salt.modules.cmdmod.run(cmd, python_shell=False) + listed_pkgs = salt.modules.yumpkg.list_pkgs() + for line in known_pkgs.splitlines(): + assert any(line in d for d in listed_pkgs) diff --git a/tests/pytests/unit/modules/test_yumpkg.py b/tests/pytests/unit/modules/test_yumpkg.py index 1354ee5d2d0d..d2f3a2869eca 100644 --- a/tests/pytests/unit/modules/test_yumpkg.py +++ b/tests/pytests/unit/modules/test_yumpkg.py @@ -118,9 +118,10 @@ def _add_data(data, key, value): "openssh_|-(none)_|-6.6.1p1_|-33.el7_3_|-x86_64_|-(none)_|-1487838485", "virt-what_|-(none)_|-1.13_|-8.el7_|-x86_64_|-(none)_|-1487838486", ] + cmd_mod = MagicMock(return_value=os.linesep.join(rpm_out)) with patch.dict(yumpkg.__grains__, {"osarch": "x86_64"}), patch.dict( yumpkg.__salt__, - {"cmd.run": MagicMock(return_value=os.linesep.join(rpm_out))}, + {"cmd.run": cmd_mod}, ), patch.dict(yumpkg.__salt__, {"pkg_resource.add_pkg": _add_data}), patch.dict( yumpkg.__salt__, {"pkg_resource.format_pkg_list": pkg_resource.format_pkg_list}, @@ -147,6 +148,18 @@ def _add_data(data, key, value): }.items(): assert pkgs.get(pkg_name) is not None assert pkgs[pkg_name] == [pkg_version] + cmd_mod.assert_called_once_with( + [ + "rpm", + "-qa", + "--nodigest", + "--nosignature", + "--queryformat", + "%{NAME}_|-%{EPOCH}_|-%{VERSION}_|-%{RELEASE}_|-%{ARCH}_|-(none)_|-%{INSTALLTIME}\n", + ], + output_loglevel="trace", + python_shell=False, + ) def test_list_pkgs_no_context():