Skip to content

Commit

Permalink
Add diff_attr parameter to zypper/yum upgrade
Browse files Browse the repository at this point in the history
diff_attr works just like it does for pkg.install. Having the
option to return additional attributes can remove the need for a
follow-up list_pkgs call.

Fixes: saltstack/salt#62031
  • Loading branch information
tiltingpenguin committed Jul 6, 2022
1 parent d0d2770 commit 2a75870
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/62031.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `diff_attr` parameter to pkg.upgrade() (zypper/yum).
5 changes: 3 additions & 2 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,7 @@ def upgrade(name=None,
normalize=True,
minimal=False,
obsoletes=True,
diff_attr=None,
**kwargs):
'''
Run a full system upgrade (a ``yum upgrade`` or ``dnf upgrade``), or
Expand Down Expand Up @@ -1898,7 +1899,7 @@ def upgrade(name=None,
if salt.utils.data.is_true(refresh):
refresh_db(**kwargs)

old = list_pkgs()
old = list_pkgs(attr=diff_attr)

targets = []
if name or pkgs:
Expand Down Expand Up @@ -1935,7 +1936,7 @@ def upgrade(name=None,
cmd.extend(targets)
result = _call_yum(cmd)
__context__.pop('pkg.list_pkgs', None)
new = list_pkgs()
new = list_pkgs(attr=diff_attr)
ret = salt.utils.data.compare_dicts(old, new)

if result['retcode'] != 0:
Expand Down
32 changes: 30 additions & 2 deletions salt/modules/zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,7 @@ def upgrade(name=None,
skip_verify=False,
no_recommends=False,
root=None,
diff_attr=None,
**kwargs): # pylint: disable=unused-argument
'''
.. versionchanged:: 2015.8.12,2016.3.3,2016.11.0
Expand Down Expand Up @@ -1748,18 +1749,45 @@ def upgrade(name=None,
root
Operate on a different root directory.
diff_attr:
If a list of package attributes is specified, returned value will
contain them, eg.::
{'<package>': {
'old': {
'version': '<old-version>',
'arch': '<old-arch>'},
'new': {
'version': '<new-version>',
'arch': '<new-arch>'}}}
Valid attributes are: ``epoch``, ``version``, ``release``, ``arch``,
``install_date``, ``install_date_time_t``.
If ``all`` is specified, all valid attributes will be returned.
Returns a dictionary containing the changes:
.. code-block:: python
{'<package>': {'old': '<old-version>',
'new': '<new-version>'}}
If an attribute list is specified in ``diff_attr``, the dict will also contain
any specified attribute, eg.::
.. code-block:: python
{'<package>': {
'old': {
'version': '<old-version>',
'arch': '<old-arch>'},
'new': {
'version': '<new-version>',
'arch': '<new-arch>'}}}
CLI Example:
.. code-block:: bash
salt '*' pkg.upgrade
salt '*' pkg.upgrade name=mypackage
salt '*' pkg.upgrade pkgs='["package1", "package2"]'
salt '*' pkg.upgrade dist_upgrade=True fromrepo='["MyRepoName"]' novendorchange=True
salt '*' pkg.upgrade dist_upgrade=True dryrun=True
'''
Expand Down Expand Up @@ -1803,10 +1831,10 @@ def upgrade(name=None,
except MinionError as exc:
raise CommandExecutionError(exc)

old = list_pkgs(root=root)
old = list_pkgs(root=root, attr=diff_attr)
__zypper__(systemd_scope=_systemd_scope(), root=root).allow_vendor_change(allowvendorchange, novendorchange).noraise.call(*cmd_update)
_clean_cache()
new = list_pkgs(root=root)
new = list_pkgs(root=root, attr=diff_attr)
ret = salt.utils.data.compare_dicts(old, new)

if __zypper__.exit_code not in __zypper__.SUCCESS_EXIT_CODES:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/modules/test_zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,13 @@ def test_upgrade_success(self):
self.assertDictEqual(ret, {"vim": {"old": "1.1", "new": "1.2"}})
zypper_mock.assert_any_call('update', '--auto-agree-with-licenses')

with patch(
"salt.modules.zypperpkg.list_pkgs",
MagicMock(side_effect=[{"vim": "1.1"}, {"vim": "1.2"}]),
) as list_pkgs_mock:
ret = zypper.upgrade(diff_attr="all")
list_pkgs_mock.assert_any_call(root=None, attr="all")

with patch.dict(zypper.__salt__,
{'pkg_resource.parse_targets': MagicMock(return_value=({'vim': "1.1"}, 'repository'))}):
with patch('salt.modules.zypperpkg.list_pkgs',
Expand Down

0 comments on commit 2a75870

Please sign in to comment.