diff --git a/salt/modules/yumpkg.py b/salt/modules/yumpkg.py index 43e2d12b9a05..fc14808d2fab 100644 --- a/salt/modules/yumpkg.py +++ b/salt/modules/yumpkg.py @@ -3117,12 +3117,18 @@ def _get_patches(installed_only=False): for line in salt.utils.itertools.split(ret, os.linesep): inst, advisory_id, sev, pkg = re.match(r'([i|\s]) ([^\s]+) +([^\s]+) +([^\s]+)', line).groups() - if inst != 'i' and installed_only: - continue - patches[advisory_id] = { - 'installed': True if inst == 'i' else False, - 'summary': pkg - } + if not advisory_id in patches: + patches[advisory_id] = { + 'installed': True if inst == 'i' else False, + 'summary': [pkg] + } + else: + patches[advisory_id]['summary'].append(pkg) + if inst != 'i': + patches[advisory_id]['installed'] = False + + if installed_only: + patches = {k: v for k, v in patches.items() if v['installed']} return patches diff --git a/tests/unit/modules/test_yumpkg.py b/tests/unit/modules/test_yumpkg.py index 445c4bfd6e95..4d9c43dd904c 100644 --- a/tests/unit/modules/test_yumpkg.py +++ b/tests/unit/modules/test_yumpkg.py @@ -233,6 +233,54 @@ def _add_data(data, key, value): self.assertTrue(pkgs.get(pkg_name)) self.assertEqual(pkgs[pkg_name], [pkg_attr]) + def test_list_patches(self): + ''' + Test patches listing. + + :return: + ''' + def _add_data(data, key, value): + data.setdefault(key, []).append(value) + + yum_out = [ + 'i my-fake-patch-not-installed-1234 recommended spacewalk-usix-2.7.5.2-2.2.noarch', + ' my-fake-patch-not-installed-1234 recommended spacewalksd-5.0.26.2-21.2.x86_64', + 'i my-fake-patch-not-installed-1234 recommended suseRegisterInfo-3.1.1-18.2.x86_64', + 'i my-fake-patch-installed-1234 recommended my-package-one-1.1-0.1.x86_64', + 'i my-fake-patch-installed-1234 recommended my-package-two-1.1-0.1.x86_64', + ] + + expected_patches = { + 'my-fake-patch-not-installed-1234': { + 'installed': False, + 'summary': [ + 'spacewalk-usix-2.7.5.2-2.2.noarch', + 'spacewalksd-5.0.26.2-21.2.x86_64', + 'suseRegisterInfo-3.1.1-18.2.x86_64', + ] + }, + 'my-fake-patch-installed-1234': { + 'installed': True, + 'summary': [ + 'my-package-one-1.1-0.1.x86_64', + 'my-package-two-1.1-0.1.x86_64', + ] + } + } + + with patch.dict(yumpkg.__grains__, {'osarch': 'x86_64'}), \ + patch.dict(yumpkg.__salt__, {'cmd.run_stdout': MagicMock(return_value=os.linesep.join(yum_out))}): + patches = yumpkg.list_patches() + self.assertFalse(patches['my-fake-patch-not-installed-1234']['installed']) + self.assertTrue(len(patches['my-fake-patch-not-installed-1234']['summary']) == 3) + for _patch in expected_patches['my-fake-patch-not-installed-1234']['summary']: + self.assertTrue(_patch in patches['my-fake-patch-not-installed-1234']['summary']) + + self.assertTrue(patches['my-fake-patch-installed-1234']['installed']) + self.assertTrue(len(patches['my-fake-patch-installed-1234']['summary']) == 2) + for _patch in expected_patches['my-fake-patch-installed-1234']['summary']: + self.assertTrue(_patch in patches['my-fake-patch-installed-1234']['summary']) + def test_latest_version_with_options(self): with patch.object(yumpkg, 'list_pkgs', MagicMock(return_value={})):