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 'noaction' flag for install/remove in the opkg execution module #50306

Merged
merged 10 commits into from
Oct 31, 2018
10 changes: 10 additions & 0 deletions salt/modules/opkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ def refresh_db(failhard=False, **kwargs): # pylint: disable=unused-argument
return ret


def _append_noaction_if_testmode(cmd, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call it just _noaction 😉 but this is also OK.

'''
Adds the --noaction flag to the command if it's running in the test mode.
'''
if bool(kwargs.get('test') or __opts__.get('test')):
cmd.append('--noaction')


def install(name=None,
refresh=False,
pkgs=None,
Expand Down Expand Up @@ -366,6 +374,7 @@ def install(name=None,
to_reinstall = []
to_downgrade = []

_append_noaction_if_testmode(cmd_prefix, **kwargs)
if pkg_params is None or len(pkg_params) == 0:
return {}
elif pkg_type == 'file':
Expand Down Expand Up @@ -540,6 +549,7 @@ def remove(name=None, pkgs=None, **kwargs): # pylint: disable=unused-argument
if not targets:
return {}
cmd = ['opkg', 'remove']
_append_noaction_if_testmode(cmd, **kwargs)
if kwargs.get('remove_dependencies', False):
cmd.append('--force-removal-of-dependent-packages')
if kwargs.get('auto_remove_deps', False):
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/modules/test_opkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ def test_install(self):
with patch.multiple(opkg, **patch_kwargs):
self.assertEqual(opkg.install('vim:7.4'), INSTALLED)

def test_install_noaction(self):
'''
Test - Install packages.
'''
with patch('salt.modules.opkg.list_pkgs', MagicMock(return_value=({}))):
ret_value = {'retcode': 0}
mock = MagicMock(return_value=ret_value)
patch_kwargs = {
'__salt__': {
'cmd.run_all': mock,
'pkg_resource.parse_targets': MagicMock(return_value=({'vim': '7.4'}, 'repository')),
'restartcheck.restartcheck': MagicMock(return_value='No packages seem to need to be restarted')
}
}
with patch.multiple(opkg, **patch_kwargs):
self.assertEqual(opkg.install('vim:7.4', test=True), {})

def test_remove(self):
'''
Test - Remove packages.
Expand All @@ -160,6 +177,23 @@ def test_remove(self):
with patch.multiple(opkg, **patch_kwargs):
self.assertEqual(opkg.remove('vim'), REMOVED)

def test_remove_noaction(self):
'''
Test - Remove packages.
'''
with patch('salt.modules.opkg.list_pkgs', MagicMock(return_value=({}))):
ret_value = {'retcode': 0}
mock = MagicMock(return_value=ret_value)
patch_kwargs = {
'__salt__': {
'cmd.run_all': mock,
'pkg_resource.parse_targets': MagicMock(return_value=({'vim': '7.4'}, 'repository')),
'restartcheck.restartcheck': MagicMock(return_value='No packages seem to need to be restarted')
}
}
with patch.multiple(opkg, **patch_kwargs):
self.assertEqual(opkg.remove('vim:7.4', test=True), {})

def test_info_installed(self):
'''
Test - Return the information of the named package(s) installed on the system.
Expand Down