From a36cfd8bf1fe14f6ccc47885dea87cd17a2167cb Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Wed, 18 Sep 2019 16:55:49 -0700 Subject: [PATCH 1/2] Porting PR #53080 to 2019.2.1 --- salt/modules/win_pkg.py | 5 +++ tests/unit/modules/test_win_pkg.py | 69 +++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/salt/modules/win_pkg.py b/salt/modules/win_pkg.py index 37af1f91183c..023d3493018f 100644 --- a/salt/modules/win_pkg.py +++ b/salt/modules/win_pkg.py @@ -1447,6 +1447,11 @@ def install(name=None, refresh=False, pkgs=None, **kwargs): 'extra_install_flags': kwargs.get('extra_install_flags') } } + elif len(pkg_params) == 1: + # A dict of packages was passed, but it contains only 1 key, so we need + # to add the 'extra_install_flags' + for pkg in pkg_params: + pkg_params[pkg]['extra_install_flags'] = kwargs.get('extra_install_flags') # Get a list of currently installed software for comparison at the end old = list_pkgs(saltenv=saltenv, refresh=refresh, versions_as_list=True) diff --git a/tests/unit/modules/test_win_pkg.py b/tests/unit/modules/test_win_pkg.py index 50881a634845..50cd3a8e1106 100644 --- a/tests/unit/modules/test_win_pkg.py +++ b/tests/unit/modules/test_win_pkg.py @@ -9,13 +9,16 @@ # Import Salt Testing Libs from tests.support.mixins import LoaderModuleMockMixin from tests.support.mock import MagicMock, patch -from tests.support.unit import TestCase +from tests.support.unit import TestCase, skipIf # Import Salt Libs import salt.modules.pkg_resource as pkg_resource import salt.modules.win_pkg as win_pkg +import salt.utils.data +import salt.utils.platform +@skipIf(not salt.utils.platform.is_windows(), "Must be on Windows!") class WinPkgInstallTestCase(TestCase, LoaderModuleMockMixin): ''' Test cases for salt.modules.win_pkg @@ -151,3 +154,67 @@ def test_pkg_install_existing_with_version(self): expected = {} result = win_pkg.install(name='nsis', version='3.03') self.assertDictEqual(expected, result) + + def test_pkg_install_name(self): + ''' + test pkg.install name extra_install_flags + ''' + + ret__get_package_info = {'3.03': {'uninstaller': '%program.exe', 'reboot': False, + 'msiexec': False, + 'installer': 'runme.exe', + 'uninstall_flags': '/S', 'locale': 'en_US', 'install_flags': '/s', + 'full_name': 'Firebox 3.03 (x86 en-US)'}} + + mock_cmd_run_all = MagicMock(return_value={'retcode': 0}) + with patch.object(salt.utils.data, 'is_true', MagicMock(return_value=True)),\ + patch.object(win_pkg, '_get_package_info', MagicMock(return_value=ret__get_package_info)),\ + patch.dict(win_pkg.__salt__, {'pkg_resource.parse_targets': + MagicMock(return_value=[{'firebox': '3.03'}, None]), + 'cp.is_cached': + MagicMock(return_value='C:\\fake\\path.exe'), + 'cmd.run_all': mock_cmd_run_all}): + ret = win_pkg.install(name='firebox', version='3.03', extra_install_flags='-e True -test_flag True') + self.assertTrue('-e True -test_flag True' in str(mock_cmd_run_all.call_args[0])) + + def test_pkg_install_single_pkg(self): + ''' + test pkg.install pkg with extra_install_flags + ''' + ret__get_package_info = {'3.03': {'uninstaller': '%program.exe', 'reboot': False, + 'msiexec': False, + 'installer': 'runme.exe', + 'uninstall_flags': '/S', 'locale': 'en_US', 'install_flags': '/s', + 'full_name': 'Firebox 3.03 (x86 en-US)'}} + + mock_cmd_run_all = MagicMock(return_value={'retcode': 0}) + with patch.object(salt.utils.data, 'is_true', MagicMock(return_value=True)), \ + patch.object(win_pkg, '_get_package_info', MagicMock(return_value=ret__get_package_info)), \ + patch.dict(win_pkg.__salt__, {'pkg_resource.parse_targets': + MagicMock(return_value=[{'firebox': '3.03'}, None]), + 'cp.is_cached': + MagicMock(return_value='C:\\fake\\path.exe'), + 'cmd.run_all': mock_cmd_run_all}): + ret = win_pkg.install(pkgs=['firebox'], version='3.03', extra_install_flags='-e True -test_flag True') + self.assertTrue('-e True -test_flag True' in str(mock_cmd_run_all.call_args[0])) + + def test_pkg_install_multiple_pkgs(self): + ''' + test pkg.install pkg with extra_install_flags + ''' + ret__get_package_info = {'3.03': {'uninstaller': '%program.exe', 'reboot': False, + 'msiexec': False, + 'installer': 'runme.exe', + 'uninstall_flags': '/S', 'locale': 'en_US', 'install_flags': '/s', + 'full_name': 'Firebox 3.03 (x86 en-US)'}} + + mock_cmd_run_all = MagicMock(return_value={'retcode': 0}) + with patch.object(salt.utils.data, 'is_true', MagicMock(return_value=True)), \ + patch.object(win_pkg, '_get_package_info', MagicMock(return_value=ret__get_package_info)), \ + patch.dict(win_pkg.__salt__, {'pkg_resource.parse_targets': + MagicMock(return_value=[{'firebox': '3.03', 'got': '3.03'}, None]), + 'cp.is_cached': + MagicMock(return_value='C:\\fake\\path.exe'), + 'cmd.run_all': mock_cmd_run_all}): + ret = win_pkg.install(pkgs=['firebox', 'got'], extra_install_flags='-e True -test_flag True') + self.assertFalse('-e True -test_flag True' in str(mock_cmd_run_all.call_args[0])) From 1c4c1f14612066df14005fa1aa8367418d7b0c22 Mon Sep 17 00:00:00 2001 From: Charles McMarrow <48689152+cmcmarrow@users.noreply.github.com> Date: Tue, 10 Dec 2019 15:00:20 -0700 Subject: [PATCH 2/2] Update win_pkg.py --- salt/modules/win_pkg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/modules/win_pkg.py b/salt/modules/win_pkg.py index 023d3493018f..4d98b6a1325f 100644 --- a/salt/modules/win_pkg.py +++ b/salt/modules/win_pkg.py @@ -1450,8 +1450,8 @@ def install(name=None, refresh=False, pkgs=None, **kwargs): elif len(pkg_params) == 1: # A dict of packages was passed, but it contains only 1 key, so we need # to add the 'extra_install_flags' - for pkg in pkg_params: - pkg_params[pkg]['extra_install_flags'] = kwargs.get('extra_install_flags') + pkg = next(iter(pkg_params)) + pkg_params[pkg]['extra_install_flags'] = kwargs.get('extra_install_flags') # Get a list of currently installed software for comparison at the end old = list_pkgs(saltenv=saltenv, refresh=refresh, versions_as_list=True)