From 70642e495db3f14d7b4e5db84e5e5ade88b18536 Mon Sep 17 00:00:00 2001 From: Alessandro -oggei- Ogier Date: Thu, 7 Sep 2017 17:04:39 +0200 Subject: [PATCH 1/3] better qemu_static parameter mangle in deboostrap management, tests --- salt/modules/genesis.py | 17 ++++++++++++----- tests/unit/modules/genesis_test.py | 8 +++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/salt/modules/genesis.py b/salt/modules/genesis.py index eceaba5bb5d2..aab6aebb1e5a 100644 --- a/salt/modules/genesis.py +++ b/salt/modules/genesis.py @@ -17,10 +17,10 @@ from pipes import quote as _cmd_quote # Import salt libs -import salt.utils import salt.utils.yast import salt.utils.preseed import salt.utils.kickstart +import salt.utils.validate.path import salt.syspaths from salt.exceptions import SaltInvocationError @@ -403,6 +403,11 @@ def _bootstrap_deb( log.error('Required tool debootstrap is not installed.') return False + if static_qemu and not salt.utils.validate.path.is_readable(static_qemu): + log.error('Required tool qemu not ' + 'present/readable at: {0}'.format(static_qemu)) + return False + if isinstance(pkgs, (list, tuple)): pkgs = ','.join(pkgs) if isinstance(exclude_pkgs, (list, tuple)): @@ -427,11 +432,13 @@ def _bootstrap_deb( __salt__['cmd.run'](deb_args, python_shell=False) - __salt__['cmd.run']( - 'cp {qemu} {root}/usr/bin/'.format( - qemu=_cmd_quote(static_qemu), root=_cmd_quote(root) + if static_qemu: + __salt__['cmd.run']( + 'cp {qemu} {root}/usr/bin/'.format( + qemu=_cmd_quote(static_qemu), root=_cmd_quote(root) + ) ) - ) + env = {'DEBIAN_FRONTEND': 'noninteractive', 'DEBCONF_NONINTERACTIVE_SEEN': 'true', 'LC_ALL': 'C', diff --git a/tests/unit/modules/genesis_test.py b/tests/unit/modules/genesis_test.py index 784bb8ad8446..303acaefd715 100644 --- a/tests/unit/modules/genesis_test.py +++ b/tests/unit/modules/genesis_test.py @@ -97,9 +97,11 @@ def test_bootstrap(self): 'cmd.run': MagicMock(), 'disk.blkid': MagicMock(return_value={})}): with patch('salt.modules.genesis.salt.utils.which', return_value=True): - param_set['params'].update(common_parms) - self.assertEqual(genesis.bootstrap(**param_set['params']), None) - genesis.__salt__['cmd.run'].assert_any_call(param_set['cmd'], python_shell=False) + with patch('salt.modules.genesis.salt.utils.validate.path.is_readable', + return_value=True): + param_set['params'].update(common_parms) + self.assertEqual(genesis.bootstrap(**param_set['params']), None) + genesis.__salt__['cmd.run'].assert_any_call(param_set['cmd'], python_shell=False) with patch.object(genesis, '_bootstrap_pacman', return_value='A') as pacman_patch: with patch.dict(genesis.__salt__, {'mount.umount': MagicMock(), From 51c7a1ba00441572c69da95cbd9db1118ea5e1f8 Mon Sep 17 00:00:00 2001 From: Alessandro -oggei- Ogier Date: Fri, 8 Sep 2017 10:27:29 +0200 Subject: [PATCH 2/3] only check if static_qemu is_executable() --- salt/modules/genesis.py | 2 +- salt/utils/validate/path.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/salt/modules/genesis.py b/salt/modules/genesis.py index aab6aebb1e5a..24653ddac275 100644 --- a/salt/modules/genesis.py +++ b/salt/modules/genesis.py @@ -403,7 +403,7 @@ def _bootstrap_deb( log.error('Required tool debootstrap is not installed.') return False - if static_qemu and not salt.utils.validate.path.is_readable(static_qemu): + if static_qemu and not salt.utils.validate.path.is_executable(static_qemu): log.error('Required tool qemu not ' 'present/readable at: {0}'.format(static_qemu)) return False diff --git a/salt/utils/validate/path.py b/salt/utils/validate/path.py index 1385b9bbce0d..87f2d789c7b0 100644 --- a/salt/utils/validate/path.py +++ b/salt/utils/validate/path.py @@ -64,3 +64,14 @@ def is_readable(path): # The path does not exist return False + + +def is_executable(path): + ''' + Check if a given path is executable by the current user. + + :param path: The path to check + :returns: True or False + ''' + + return os.access(path, os.X_OK) From 496f14a7e7b5fca1e3c84291bf5d94fe20060610 Mon Sep 17 00:00:00 2001 From: Alessandro -oggei- Ogier Date: Fri, 8 Sep 2017 21:37:09 +0200 Subject: [PATCH 3/3] forgot to mock the proper one --- tests/unit/modules/genesis_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/modules/genesis_test.py b/tests/unit/modules/genesis_test.py index 303acaefd715..5fe7c833c706 100644 --- a/tests/unit/modules/genesis_test.py +++ b/tests/unit/modules/genesis_test.py @@ -97,7 +97,7 @@ def test_bootstrap(self): 'cmd.run': MagicMock(), 'disk.blkid': MagicMock(return_value={})}): with patch('salt.modules.genesis.salt.utils.which', return_value=True): - with patch('salt.modules.genesis.salt.utils.validate.path.is_readable', + with patch('salt.modules.genesis.salt.utils.validate.path.is_executable', return_value=True): param_set['params'].update(common_parms) self.assertEqual(genesis.bootstrap(**param_set['params']), None)