Fix #416: error for non-posix remote shell. Fix remote.py test errors.

This commit is contained in:
Darko Poljak 2016-03-18 21:57:50 +01:00
parent 4233a0913a
commit 1dfd6671e7
2 changed files with 50 additions and 8 deletions

View file

@ -130,12 +130,29 @@ class Remote(object):
# FIXME: replace this by -o SendEnv name -o SendEnv name ... to ssh? # FIXME: replace this by -o SendEnv name -o SendEnv name ... to ssh?
# can't pass environment to remote side, so prepend command with # can't pass environment to remote side, so prepend command with
# variable declarations # variable declarations
# cdist command prepended with variable assignments expects
# posix shell (bourne, bash) at the remote as user default shell.
# If remote user shell isn't poxis shell, but for e.g. csh/tcsh
# then these var assignments are not var assignments for this
# remote shell, it tries to execute it as a command and fails.
# So really do this by default:
# /bin/sh -c 'export <var assignments>; command'
# so that constructed remote command isn't dependent on remote
# shell. Do this only if env is not None. env breaks this.
# Explicitly use /bin/sh, because var assignments assume poxis
# shell already.
# This leaves the posibility to write script that needs to be run
# remotely in e.g. csh and setting up CDIST_REMOTE_SHELL to e.g.
# /bin/csh will execute this script in the right way.
if env: if env:
remote_env = ["%s=%s" % item for item in env.items()] cmd.append("/bin/sh")
cmd.extend(remote_env) cmd.append("-c")
remote_env = [" export %s=%s;" % item for item in env.items()]
cmd.extend(command) string_cmd = " ".join(remote_env) + " ".join(command)
cmd.append(string_cmd)
else:
cmd.extend(command)
return self._run_command(cmd, env=env, return_output=return_output) return self._run_command(cmd, env=env, return_output=return_output)
def _run_command(self, command, env=None, return_output=False): def _run_command(self, command, env=None, return_output=False):

View file

@ -39,7 +39,7 @@ class RemoteTestCase(test.CdistTestCase):
user = getpass.getuser() user = getpass.getuser()
remote_exec = "ssh -o User=%s -q" % user remote_exec = "ssh -o User=%s -q" % user
remote_copy = "scp -o User=%s -q" % user remote_copy = "scp -o User=%s -q" % user
self.remote = remote.Remote(self.target_host, self.base_path, remote_exec, remote_copy) self.remote = remote.Remote(self.target_host, base_path=self.base_path, remote_exec=remote_exec, remote_copy=remote_copy)
def tearDown(self): def tearDown(self):
shutil.rmtree(self.temp_dir) shutil.rmtree(self.temp_dir)
@ -125,7 +125,7 @@ class RemoteTestCase(test.CdistTestCase):
os.chmod(remote_exec_path, 0o755) os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path remote_exec = remote_exec_path
remote_copy = "echo" remote_copy = "echo"
r = remote.Remote(self.target_host, self.base_path, remote_exec, remote_copy) r = remote.Remote(self.target_host, base_path=self.base_path, remote_exec=remote_exec, remote_copy=remote_copy)
self.assertEqual(r.run('/bin/true', return_output=True), "%s\n" % self.target_host) self.assertEqual(r.run('/bin/true', return_output=True), "%s\n" % self.target_host)
def test_run_script_target_host_in_env(self): def test_run_script_target_host_in_env(self):
@ -135,8 +135,33 @@ class RemoteTestCase(test.CdistTestCase):
os.chmod(remote_exec_path, 0o755) os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path remote_exec = remote_exec_path
remote_copy = "echo" remote_copy = "echo"
r = remote.Remote(self.target_host, self.base_path, remote_exec, remote_copy) r = remote.Remote(self.target_host, base_path=self.base_path, remote_exec=remote_exec, remote_copy=remote_copy)
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd: with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/true"]) fd.writelines(["#!/bin/sh\n", "/bin/true"])
self.assertEqual(r.run_script(script, return_output=True), "%s\n" % self.target_host) self.assertEqual(r.run_script(script, return_output=True), "%s\n" % self.target_host)
def test_run_script_with_env_target_host_in_env(self):
handle, script = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", 'if [ "$__object" ]; then echo $__object; else echo no_env; fi\n'])
os.chmod(script, 0o755)
handle, remote_exec_path = self.mkstemp(dir=self.temp_dir)
with os.fdopen(handle, 'w') as fd:
fd.writelines(["#!/bin/sh\n", 'shift; cmd=$1; shift; $cmd "$@"\n'])
os.chmod(remote_exec_path, 0o755)
remote_exec = remote_exec_path
remote_copy = "echo"
r = remote.Remote(self.target_host, base_path=self.base_path, remote_exec=remote_exec, remote_copy=remote_copy)
output = r.run_script(script, return_output=True)
self.assertEqual(output, "no_env\n")
env = {
'__object': 'test_object',
}
output = r.run_script(script, env=env, return_output=True)
self.assertEqual(output, "test_object\n")
if __name__ == '__main__':
import unittest
unittest.main()