From e20362f6f053eaa4144583604e6aac3d62838419 Mon Sep 17 00:00:00 2001 From: Matei Albu Date: Mon, 11 Feb 2019 16:08:39 +0100 Subject: [PATCH] Make aptpkg.info return only installed packages On debian systems a package can have several other states apart from installed (ii or hi), like rc (removed but config files present) or pn (purged not installed). This returns only installed packages (ii or hi). --- salt/modules/aptpkg.py | 4 ++++ tests/unit/modules/test_aptpkg.py | 25 +++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py index a8a33dbe74c3..95b52cbc4b36 100644 --- a/salt/modules/aptpkg.py +++ b/salt/modules/aptpkg.py @@ -2791,6 +2791,8 @@ def info_installed(*names, **kwargs): ret = dict() for pkg_name, pkg_nfo in __salt__['lowpkg.info'](*names, failhard=failhard).items(): t_nfo = dict() + if pkg_nfo.get('status', 'ii')[1] != 'i': + continue # return only packages that are really installed # Translate dpkg-specific keys to a common structure for key, value in pkg_nfo.items(): if key == 'package': @@ -2803,6 +2805,8 @@ def info_installed(*names, **kwargs): t_nfo['packager'] = value elif key == 'homepage': t_nfo['url'] = value + elif key == 'status': + continue # only installed pkgs are returned, no need for status else: t_nfo[key] = value diff --git a/tests/unit/modules/test_aptpkg.py b/tests/unit/modules/test_aptpkg.py index 1e963ee5dbd0..4f9eb3bf1340 100644 --- a/tests/unit/modules/test_aptpkg.py +++ b/tests/unit/modules/test_aptpkg.py @@ -90,7 +90,26 @@ 'name': 'wget', 'section': 'web', 'source': 'wget', - 'version': '1.15-1ubuntu1.14.04.2' + 'version': '1.15-1ubuntu1.14.04.2', + 'status': 'ii', + }, + 'apache2': { + 'architecture': 'amd64', + 'description': """Apache HTTP Server + The Apache HTTP Server Project's goal is to build a secure, efficient and + extensible HTTP server as standards-compliant open source software. The + result has long been the number one web server on the Internet. + . + Installing this package results in a full installation, including the + configuration files, init scripts and support scripts.""", + 'homepage': 'http://httpd.apache.org/', + 'install_date': '2016-08-30T22:20:15Z', + 'maintainer': 'Ubuntu Developers ', + 'name': 'apache2', + 'section': 'httpd', + 'source': 'apache2', + 'version': '2.4.18-2ubuntu3.9', + 'status': 'rc', } } @@ -244,14 +263,16 @@ def test_info_installed(self): 'url': 'homepage' } - installed = copy.deepcopy(LOWPKG_INFO) + installed = copy.deepcopy({'wget': LOWPKG_INFO['wget']}) for name in names: if installed['wget'].get(names[name], False): installed['wget'][name] = installed['wget'].pop(names[name]) mock = MagicMock(return_value=LOWPKG_INFO) with patch.dict(aptpkg.__salt__, {'lowpkg.info': mock}): + del installed['wget']['status'] self.assertEqual(aptpkg.info_installed('wget'), installed) + self.assertEqual(len(aptpkg.info_installed()), 1) def test_owner(self): '''