From 1f09104b0d493492f414e427a606a637f1081670 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 15 Oct 2018 15:40:52 -0500 Subject: [PATCH] Add unit test for pkg.install with epoch --- tests/unit/modules/test_yumpkg.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/unit/modules/test_yumpkg.py b/tests/unit/modules/test_yumpkg.py index 28b6e1294c..c6d5e4d2f6 100644 --- a/tests/unit/modules/test_yumpkg.py +++ b/tests/unit/modules/test_yumpkg.py @@ -16,6 +16,7 @@ from tests.support.mock import ( ) # Import Salt libs +import salt.modules.rpm import salt.modules.yumpkg as yumpkg import salt.modules.pkg_resource as pkg_resource @@ -569,6 +570,51 @@ class YumTestCase(TestCase, LoaderModuleMockMixin): python_shell=False, redirect_stderr=True) + def test_install_with_epoch(self): + ''' + Tests that we properly identify a version containing an epoch as an + upgrade instead of a downgrade. + ''' + name = 'foo' + old = '8:3.8.12-6.n.el7' + new = '9:3.8.12-4.n.el7' + list_pkgs_mock = MagicMock(side_effect=lambda **kwargs: {name: [old] if kwargs.get('versions_as_list', False) else old}) + cmd_mock = MagicMock(return_value={ + 'pid': 12345, + 'retcode': 0, + 'stdout': '', + 'stderr': '', + }) + salt_mock = { + 'cmd.run_all': cmd_mock, + 'lowpkg.version_cmp': salt.modules.rpm.version_cmp, + 'pkg_resource.parse_targets': MagicMock(return_value=( + {name: new}, 'repository' + )), + } + full_pkg_string = '-'.join((name, new[2:])) + with patch.object(yumpkg, 'list_pkgs', list_pkgs_mock), \ + patch('salt.utils.systemd.has_scope', + MagicMock(return_value=False)), \ + patch.dict(yumpkg.__salt__, salt_mock): + + # Test yum + expected = ['yum', '-y', 'install', full_pkg_string] + with patch.dict(yumpkg.__grains__, {'os': 'CentOS', 'osrelease': 7}): + yumpkg.install('foo', version=new) + call = cmd_mock.mock_calls[0][1][0] + assert call == expected, call + + # Test dnf + expected = ['dnf', '-y', '--best', '--allowerasing', + 'install', full_pkg_string] + yumpkg.__context__.pop('yum_bin') + cmd_mock.reset_mock() + with patch.dict(yumpkg.__grains__, {'os': 'Fedora', 'osrelease': 27}): + yumpkg.install('foo', version=new) + call = cmd_mock.mock_calls[0][1][0] + assert call == expected, call + def test_upgrade_with_options(self): with patch.object(yumpkg, 'list_pkgs', MagicMock(return_value={})), \ patch('salt.utils.systemd.has_scope', MagicMock(return_value=False)):