let chdir support relative path in more modules (#16736)
This commit is contained in:
parent
28b4931e3d
commit
d9e1e374b2
2 changed files with 15 additions and 4 deletions
|
@ -2311,14 +2311,13 @@ class AnsibleModule(object):
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
)
|
)
|
||||||
|
|
||||||
if cwd and os.path.isdir(cwd):
|
|
||||||
kwargs['cwd'] = cwd
|
|
||||||
|
|
||||||
# store the pwd
|
# store the pwd
|
||||||
prev_dir = os.getcwd()
|
prev_dir = os.getcwd()
|
||||||
|
|
||||||
# make sure we're in the right working directory
|
# make sure we're in the right working directory
|
||||||
if cwd and os.path.isdir(cwd):
|
if cwd and os.path.isdir(cwd):
|
||||||
|
cwd = os.path.abspath(os.path.expanduser(cwd))
|
||||||
|
kwargs['cwd'] = cwd
|
||||||
try:
|
try:
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
except (OSError, IOError):
|
except (OSError, IOError):
|
||||||
|
@ -2330,7 +2329,6 @@ class AnsibleModule(object):
|
||||||
old_umask = os.umask(umask)
|
old_umask = os.umask(umask)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if self._debug:
|
if self._debug:
|
||||||
self.log('Executing: ' + clean_args)
|
self.log('Executing: ' + clean_args)
|
||||||
cmd = subprocess.Popen(args, **kwargs)
|
cmd = subprocess.Popen(args, **kwargs)
|
||||||
|
|
|
@ -61,6 +61,12 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
||||||
if path == '/inaccessible':
|
if path == '/inaccessible':
|
||||||
raise OSError(errno.EPERM, "Permission denied: '/inaccessible'")
|
raise OSError(errno.EPERM, "Permission denied: '/inaccessible'")
|
||||||
|
|
||||||
|
def mock_os_abspath(path):
|
||||||
|
if path.startswith('/'):
|
||||||
|
return path
|
||||||
|
else:
|
||||||
|
return self.os.getcwd.return_value + '/' + path
|
||||||
|
|
||||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||||
# unittest doesn't have a clean place to use a context manager, so we have to enter/exit manually
|
# unittest doesn't have a clean place to use a context manager, so we have to enter/exit manually
|
||||||
self.stdin_swap = swap_stdin_and_argv(stdin_data=args)
|
self.stdin_swap = swap_stdin_and_argv(stdin_data=args)
|
||||||
|
@ -78,6 +84,7 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
||||||
self.os.path.isdir.return_value = True
|
self.os.path.isdir.return_value = True
|
||||||
self.os.chdir.side_effect = mock_os_chdir
|
self.os.chdir.side_effect = mock_os_chdir
|
||||||
self.os.read.side_effect = mock_os_read
|
self.os.read.side_effect = mock_os_read
|
||||||
|
self.os.path.abspath.side_effect = mock_os_abspath
|
||||||
|
|
||||||
self.subprocess = patch('ansible.module_utils.basic.subprocess').start()
|
self.subprocess = patch('ansible.module_utils.basic.subprocess').start()
|
||||||
self.cmd = Mock()
|
self.cmd = Mock()
|
||||||
|
@ -128,6 +135,12 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
|
||||||
self.assertEqual(self.os.chdir.mock_calls,
|
self.assertEqual(self.os.chdir.mock_calls,
|
||||||
[call('/new'), call('/old'), ])
|
[call('/new'), call('/old'), ])
|
||||||
|
|
||||||
|
def test_cwd_relative_path(self):
|
||||||
|
self.os.getcwd.return_value = '/old'
|
||||||
|
self.module.run_command('/bin/ls', cwd='sub-dir')
|
||||||
|
self.assertEqual(self.os.chdir.mock_calls,
|
||||||
|
[call('/old/sub-dir'), call('/old'), ])
|
||||||
|
|
||||||
def test_cwd_not_a_dir(self):
|
def test_cwd_not_a_dir(self):
|
||||||
self.os.getcwd.return_value = '/old'
|
self.os.getcwd.return_value = '/old'
|
||||||
self.os.path.isdir.side_effect = lambda d: d != '/not-a-dir'
|
self.os.path.isdir.side_effect = lambda d: d != '/not-a-dir'
|
||||||
|
|
Loading…
Reference in a new issue