From 34529cd92268339c4d46f78bc3b4e457d9f31683 Mon Sep 17 00:00:00 2001 From: Niranjan M.R Date: Oct 04 2017 16:59:43 +0000 Subject: Add support to run commands in background Minor fixes based on review comments Signed-off-by: Niranjan M.R --- diff --git a/pytest_multihost/host.py b/pytest_multihost/host.py index 826372d..a095a43 100644 --- a/pytest_multihost/host.py +++ b/pytest_multihost/host.py @@ -201,7 +201,7 @@ class BaseHost(object): def run_command(self, argv, set_env=True, stdin_text=None, log_stdout=True, raiseonerr=True, - cwd=None): + cwd=None, bg=False): """Run the given command on this host Returns a Command instance. The command will have already run in the @@ -218,6 +218,7 @@ class BaseHost(object): :param raiseonerr: If true, an exception will be raised if the command does not exit with return code 0 :param cwd: The working directory for the command + :param bg: If True, runs command in background """ command = self.transport.start_shell(argv, log_stdout=log_stdout) # Set working directory @@ -247,11 +248,12 @@ class BaseHost(object): if stdin_text: command.stdin.write(stdin_text) command.stdin.flush() - - command.wait(raiseonerr=raiseonerr) + if not bg: + command.wait(raiseonerr=raiseonerr) return command + class Host(BaseHost): """A Unix host""" command_prelude = 'set -e\n' diff --git a/test_pytestmultihost/test_localhost.py b/test_pytestmultihost/test_localhost.py index c51a2ea..180aa73 100644 --- a/test_pytestmultihost/test_localhost.py +++ b/test_pytestmultihost/test_localhost.py @@ -224,3 +224,11 @@ class TestLocalhost(object): host = multihost_badpassword.host with pytest.raises((AuthenticationException, RuntimeError)): echo = host.run_command(['echo', 'hello', 'world']) + + def test_background(self, multihost): + host = multihost.host + run_nc = 'nc -l 12080 > /tmp/filename.out' + cmd = host.run_command(run_nc, bg=True, raiseonerr=False) + send_file = 'nc localhost 12080 < /root/anaconda-ks.cfg' + cmd = host.run_command(send_file) + assert cmd.returncode == 0