Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for name, pkgs and diff_attr parameters to zypperpkg.upgrade()/yumpkg.upgrade() - backport 3000 #545

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/62030.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix inconsitency regarding name and pkgs parameters between zypperpkg.upgrade() and yumpkg.upgrade()
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).
1 change: 1 addition & 0 deletions changelog/62032.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix attr=all handling in pkg.list_pkgs() (yum/zypper).
7 changes: 4 additions & 3 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def list_pkgs(versions_as_list=False, **kwargs):
return {}

attr = kwargs.get('attr')
if attr is not None:
if attr is not None and attr != "all":
attr = salt.utils.args.split_input(attr)

contextkey = 'pkg.list_pkgs'
Expand Down 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
71 changes: 67 additions & 4 deletions salt/modules/zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def list_pkgs(versions_as_list=False, root=None, includes=None, **kwargs):
return {}

attr = kwargs.get('attr')
if attr is not None:
if attr is not None and attr != "all":
attr = salt.utils.args.split_input(attr)

includes = includes if includes else []
Expand Down Expand Up @@ -1667,7 +1667,9 @@ def install(name=None,
return ret


def upgrade(refresh=True,
def upgrade(name=None,
pkgs=None,
refresh=True,
dryrun=False,
dist_upgrade=False,
fromrepo=None,
Expand All @@ -1676,6 +1678,7 @@ def upgrade(refresh=True,
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 All @@ -1694,6 +1697,27 @@ def upgrade(refresh=True,

Run a full system upgrade, a zypper upgrade

name
The name of the package to be installed. Note that this parameter is
ignored if ``pkgs`` is passed or if ``dryrun`` is set to True.

CLI Example:

.. code-block:: bash

salt '*' pkg.install name=<package name>

pkgs
A list of packages to install from a software repository. Must be
passed as a python list. Note that this parameter is ignored if
``dryrun`` is set to True.

CLI Examples:

.. code-block:: bash

salt '*' pkg.install pkgs='["foo", "bar"]'

refresh
force a refresh if set to True (default).
If set to False it depends on zypper if a refresh is
Expand Down Expand Up @@ -1725,18 +1749,45 @@ def upgrade(refresh=True,
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 @@ -1768,10 +1819,22 @@ def upgrade(refresh=True,
log.info('Executing debugsolver and performing a dry-run dist-upgrade')
__zypper__(systemd_scope=_systemd_scope(), root=root).allow_vendor_change(allowvendorchange, novendorchange).noraise.call(*cmd_update + ['--debug-solver'])

old = list_pkgs(root=root)
if not dist_upgrade:
if name or pkgs:
try:
(pkg_params, _) = __salt__["pkg_resource.parse_targets"](
name=name, pkgs=pkgs, sources=None, **kwargs
)
if pkg_params:
cmd_update.extend(pkg_params.keys())

except MinionError as exc:
raise CommandExecutionError(exc)

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
27 changes: 25 additions & 2 deletions tests/unit/modules/test_zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
'''

def setup_loader_modules(self):
return {zypper: {'rpm': None}, pkg_resource: {}}
return {zypper: {'rpm': None, '__salt__': {'pkg_resource.parse_targets': {pkg_resource.parse_targets}}},
pkg_resource: {'__grains__': {'os': 'SUSE'}}}

def setUp(self):
self.new_repo_config = dict(
Expand Down Expand Up @@ -770,6 +771,29 @@ 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',
MagicMock(side_effect=[{"vim": "1.1"}, {"vim": "1.2"}])):
ret = zypper.upgrade(name="vim")
self.assertDictEqual(ret, {"vim": {"old": "1.1", "new": "1.2"}})
zypper_mock.assert_any_call('update', '--auto-agree-with-licenses', 'vim')

with patch.dict(zypper.__salt__,
{'pkg_resource.parse_targets': MagicMock(return_value=({'vim': "1.1"}, 'repository'))}):
with patch('salt.modules.zypperpkg.list_pkgs',
MagicMock(side_effect=[{"vim": "1.1"}, {"vim": "1.2"}])):
ret = zypper.upgrade(pkgs=["vim"])
self.assertDictEqual(ret, {"vim": {"old": "1.1", "new": "1.2"}})
zypper_mock.assert_any_call('update', '--auto-agree-with-licenses', 'vim')

with patch('salt.modules.zypperpkg.list_pkgs',
MagicMock(side_effect=[{"kernel-default": "1.1"}, {"kernel-default": "1.1,1.2"}])):
ret = zypper.upgrade()
Expand All @@ -782,7 +806,6 @@ def test_upgrade_success(self):
self.assertDictEqual(ret, {"vim": {"old": "1.1", "new": "1.1,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.1"}])):
ret = zypper.upgrade(dist_upgrade=True, dryrun=True)
zypper_mock.assert_any_call('dist-upgrade', '--auto-agree-with-licenses', '--dry-run')
Expand Down