Call zypper refresh after adding/modifying a repository

Calling `zypper --gpg-auto-import-keys refresh` is required after
adding/modifying a repository because `--gpg-auto-import-keys` doesn't
do anything when called with `zypper ar` or `zypper mr`.
Without calling `zypper --gpg-auto-import-keys refresh` here, calling
`zypper ref` after adding/removing would still ask for
accepting/rejecting the gpg key.
This commit is contained in:
Mihai Dinca 2016-05-27 12:00:02 +02:00
parent c8b4f338d8
commit c7ae5907ee
2 changed files with 245 additions and 18 deletions

View file

@ -765,6 +765,7 @@ def mod_repo(repo, **kwargs):
added = True
# Modify added or existing repo according to the options
trigger_refresh = False
cmd_opt = []
global_cmd_opt = []
@ -788,16 +789,23 @@ def mod_repo(repo, **kwargs):
if kwargs.get('gpgautoimport') is True:
global_cmd_opt.append('--gpg-auto-import-keys')
trigger_refresh = True
if cmd_opt:
cmd_opt = global_cmd_opt + ['mr'] + cmd_opt + [repo]
__zypper__.refreshable.xml.call(*cmd_opt)
# If repo nor added neither modified, error should be thrown
if not added and not cmd_opt:
if not added and not cmd_opt and not trigger_refresh:
raise CommandExecutionError(
'Specified arguments did not result in modification of repo'
)
elif trigger_refresh:
# when used with "zypper ar --refresh" or "zypper mr --refresh"
# --gpg-auto-import-keys is not doing anything
# so we need to specifically refresh here with --gpg-auto-import-keys
refresh_opts = global_cmd_opt + ['refresh'] + [repo]
__zypper__.xml.call(*refresh_opts)
return get_repo(repo)

View file

@ -11,6 +11,7 @@ from salttesting import TestCase, skipIf
from salttesting.mock import (
Mock,
MagicMock,
call,
patch,
NO_MOCK,
NO_MOCK_REASON
@ -59,6 +60,28 @@ class ZypperTestCase(TestCase):
Test cases for salt.modules.zypper
'''
def setUp(self):
self.new_repo_config = dict(
name='mock-repo-name',
url='http://repo.url/some/path'
)
self.zypper_patcher_config = {
'_get_configured_repos': Mock(
side_effect=[
Mock(**{'sections.return_value': []}),
Mock(
**{
'sections.return_value': [
self.new_repo_config['name']
]
}
),
],
),
'__zypper__': Mock(),
'get_repo': Mock()
}
def test_list_upgrades(self):
'''
List package upgrades
@ -414,37 +437,233 @@ class ZypperTestCase(TestCase):
self.assertEqual(r_info['enabled'], alias == 'SLE12-SP1-x86_64-Update')
self.assertEqual(r_info['autorefresh'], alias == 'SLE12-SP1-x86_64-Update')
def test_modify_repo_gpg_auto_import_keys_parameter_position(self):
def test_add_repo_and_do_not_refresh_repo(self):
'''
Tests if when modifying a repo, --gpg-auto-import-keys is a global option
Test mod_repo adds the new repo and nothing else
:return:
'''
zypper_patcher = patch.multiple(
'salt.modules.zypper',
**{
'_get_configured_repos': Mock(
**{
'return_value.sections.return_value': ['mock-repo-name']
}
),
'__zypper__': Mock(),
'get_repo': Mock()
}
'salt.modules.zypper', **self.zypper_patcher_config)
url = self.new_repo_config['url']
name = self.new_repo_config['name']
with zypper_patcher:
zypper.mod_repo(name, **{'url': url})
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[call('ar', url, name)]
)
zypper.__zypper__.refreshable.xml.call.assert_not_called()
def test_repo_exists_nothing_to_change_do_not_refresh(self):
'''
Test mod_repo detects the repo already exists,
no modification was requested and no refresh requested either
:return:
'''
url = self.new_repo_config['url']
name = self.new_repo_config['name']
self.zypper_patcher_config['_get_configured_repos'] = Mock(
**{'return_value.sections.return_value': [name]}
)
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
with zypper_patcher:
with self.assertRaisesRegexp(
Exception,
'Specified arguments did not result in modification of repo'
):
zypper.mod_repo(name, **{'url': url})
zypper.__zypper__.xml.call.assert_not_called()
def test_add_repo_modify_repo_do_not_refresh(self):
'''
Test mod_repo adds the new repo and call modify to update autorefresh
:return:
'''
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
url = self.new_repo_config['url']
name = self.new_repo_config['name']
with zypper_patcher:
zypper.mod_repo(
'mock-repo-name',
name,
**{
'disabled': False,
'url': 'http://repo.url/some/path',
'gpgkey': 'http://repo.key',
'url': url,
'refresh': True
}
)
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[
call('ar', url, name)
]
)
zypper.__zypper__.refreshable.xml.call.assert_called_once_with(
'mr', '--refresh', name
)
def test_modify_repo_do_not_refresh(self):
'''
Test mod_repo detects the repository exists,
calls modify to update 'autorefresh' but does not call refresh
:return:
'''
url = self.new_repo_config['url']
name = self.new_repo_config['name']
self.zypper_patcher_config['_get_configured_repos'] = Mock(
**{'return_value.sections.return_value': [name]}
)
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
with zypper_patcher:
zypper.mod_repo(
name,
**{
'url': url,
'refresh': True
}
)
zypper.__zypper__.xml.call.assert_not_called()
zypper.__zypper__.refreshable.xml.call.assert_called_once_with(
'mr', '--refresh', name
)
def test_add_repo_and_refresh_repo_with_gpgautoimport(self):
'''
Test mod_repo adds the new repo and refreshes the repo with
`zypper --gpg-auto-import-keys refresh <repo-name>`
:return:
'''
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
url = self.new_repo_config['url']
name = self.new_repo_config['name']
with zypper_patcher:
zypper.mod_repo(
name,
**{
'url': url,
'gpgautoimport': True
}
)
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[
call('ar', url, name),
call('--gpg-auto-import-keys', 'refresh', name)
]
)
zypper.__zypper__.refreshable.xml.call.assert_not_called()
def test_nothing_to_modify_and_refresh_repo_with_gpgautoimport(self):
'''
Test mod_repo detects the repo already exists,
has nothing to modify and refreshes the repo with
`zypper --gpg-auto-import-keys refresh <repo-name>`
:return:
'''
url = self.new_repo_config['url']
name = self.new_repo_config['name']
self.zypper_patcher_config['_get_configured_repos'] = Mock(
**{'return_value.sections.return_value': [name]}
)
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
with zypper_patcher:
zypper.mod_repo(
name,
**{
'url': url,
'gpgautoimport': True
}
)
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[
call('--gpg-auto-import-keys', 'refresh', name)
]
)
zypper.__zypper__.refreshable.xml.call.assert_not_called()
def test_add_repo_modify_repo_and_refresh_repo_with_gpgautoimport(self):
'''
Test mod_repo adds the new repo,
modifies the new repo when refresh is True and
refreshes the repo with
`zypper --gpg-auto-import-keys refresh <repo-name>`
:return:
'''
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
url = self.new_repo_config['url']
name = self.new_repo_config['name']
with zypper_patcher:
zypper.mod_repo(
name,
**{
'url': url,
'refresh': True,
'gpgautoimport': True
}
)
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[
call('ar', url, name),
call('--gpg-auto-import-keys', 'refresh', name)
]
)
zypper.__zypper__.refreshable.xml.call.assert_called_once_with(
'--gpg-auto-import-keys', 'mr', '--refresh', 'mock-repo-name'
'--gpg-auto-import-keys', 'mr', '--refresh', name
)
def test_modify_existing_repo_and_refresh_repo_with_gpgautoimport(self):
'''
Test mod_repo detects the repo already exists,
modifies the existing repo when refresh is True and
refreshes the repo with
`zypper --gpg-auto-import-keys refresh <repo-name>`
:return:
'''
url = self.new_repo_config['url']
name = self.new_repo_config['name']
self.zypper_patcher_config['_get_configured_repos'] = Mock(
**{'return_value.sections.return_value': [name]}
)
zypper_patcher = patch.multiple(
'salt.modules.zypper', **self.zypper_patcher_config)
with zypper_patcher:
zypper.mod_repo(
name,
**{
'url': url,
'refresh': True,
'gpgautoimport': True
}
)
self.assertEqual(
zypper.__zypper__.xml.call.call_args_list,
[
call('--gpg-auto-import-keys', 'refresh', name)
]
)
zypper.__zypper__.refreshable.xml.call.assert_called_once_with(
'--gpg-auto-import-keys', 'mr', '--refresh', name
)
if __name__ == '__main__':