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
(cherry picked from commit 20ffffe3be6c7d94e9cc3338a57bbf5014f33d93)
  • Loading branch information
agraul committed Jun 14, 2022
1 parent 10687b4 commit 31f6d80
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 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 @@ -1827,6 +1827,7 @@ def upgrade(
normalize=True,
minimal=False,
obsoletes=True,
diff_attr=None,
**kwargs
):
"""
Expand Down Expand Up @@ -1983,7 +1984,7 @@ def upgrade(
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 @@ -2015,7 +2016,7 @@ def upgrade(
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
39 changes: 37 additions & 2 deletions salt/modules/zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,7 @@ def upgrade(
skip_verify=False,
no_recommends=False,
root=None,
diff_attr=None,
**kwargs
): # pylint: disable=unused-argument
"""
Expand Down Expand Up @@ -1875,18 +1876,52 @@ def upgrade(
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 @@ -1934,12 +1969,12 @@ def upgrade(
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
24 changes: 17 additions & 7 deletions tests/pytests/unit/modules/test_zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,24 +331,30 @@ def test_allow_vendor_change(


@pytest.mark.parametrize(
"package,pre_version,post_version,fromrepo_param,name_param,pkgs_param",
"package,pre_version,post_version,fromrepo_param,name_param,pkgs_param,diff_attr_param",
[
("vim", "1.1", "1.2", [], "", []),
("kernel-default", "1.1", "1.1,1.2", ["dummy", "dummy2"], "", []),
("vim", "1.1", "1.2", [], "vim", []),
("vim", "1.1", "1.2", [], "", [], "all"),
("kernel-default", "1.1", "1.1,1.2", ["dummy", "dummy2"], "", [], None),
("vim", "1.1", "1.2", [], "vim", [], None),
],
)
@patch.object(zypper, "refresh_db", MagicMock(return_value=True))
def test_upgrade(
package, pre_version, post_version, fromrepo_param, name_param, pkgs_param
package,
pre_version,
post_version,
fromrepo_param,
name_param,
pkgs_param,
diff_attr_param,
):
with patch(
"salt.modules.zypperpkg.__zypper__.noraise.call"
) as zypper_mock, patch.object(
zypper,
"list_pkgs",
MagicMock(side_effect=[{package: pre_version}, {package: post_version}]),
):
) as list_pkgs_mock:
expected_call = ["update", "--auto-agree-with-licenses"]
for repo in fromrepo_param:
expected_call.extend(["--repo", repo])
Expand All @@ -359,10 +365,14 @@ def test_upgrade(
expected_call.append(name_param)

result = zypper.upgrade(
name=name_param, pkgs=pkgs_param, fromrepo=fromrepo_param
name=name_param,
pkgs=pkgs_param,
fromrepo=fromrepo_param,
diff_attr=diff_attr_param,
)
zypper_mock.assert_any_call(*expected_call)
assert result == {package: {"old": pre_version, "new": post_version}}
list_pkgs_mock.assert_any_call(root=None, attr=diff_attr_param)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 31f6d80

Please sign in to comment.