Merge "Add timeout option to ssh_execute"
This commit is contained in:
commit
e1cde97028
@ -466,7 +466,7 @@ def trycmd(*args, **kwargs):
|
|||||||
|
|
||||||
def ssh_execute(ssh, cmd, process_input=None,
|
def ssh_execute(ssh, cmd, process_input=None,
|
||||||
addl_env=None, check_exit_code=True,
|
addl_env=None, check_exit_code=True,
|
||||||
binary=False):
|
binary=False, timeout=None):
|
||||||
"""Run a command through SSH.
|
"""Run a command through SSH.
|
||||||
|
|
||||||
.. versionchanged:: 1.9
|
.. versionchanged:: 1.9
|
||||||
@ -481,7 +481,8 @@ def ssh_execute(ssh, cmd, process_input=None,
|
|||||||
# This is (probably) fixable if we need it...
|
# This is (probably) fixable if we need it...
|
||||||
raise InvalidArgumentError(_('process_input not supported over SSH'))
|
raise InvalidArgumentError(_('process_input not supported over SSH'))
|
||||||
|
|
||||||
stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
|
stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(
|
||||||
|
cmd, timeout=timeout)
|
||||||
channel = stdout_stream.channel
|
channel = stdout_stream.channel
|
||||||
|
|
||||||
# NOTE(justinsb): This seems suspicious...
|
# NOTE(justinsb): This seems suspicious...
|
||||||
|
@ -21,6 +21,7 @@ import multiprocessing
|
|||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import resource
|
import resource
|
||||||
|
import socket
|
||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@ -599,7 +600,9 @@ class FakeSshConnection(object):
|
|||||||
self.out = out
|
self.out = out
|
||||||
self.err = err
|
self.err = err
|
||||||
|
|
||||||
def exec_command(self, cmd):
|
def exec_command(self, cmd, timeout=None):
|
||||||
|
if timeout:
|
||||||
|
raise socket.timeout()
|
||||||
stdout = FakeSshStream(self.out)
|
stdout = FakeSshStream(self.out)
|
||||||
stdout.setup_channel(self.rc)
|
stdout.setup_channel(self.rc)
|
||||||
return (six.BytesIO(),
|
return (six.BytesIO(),
|
||||||
@ -618,6 +621,12 @@ class SshExecuteTestCase(test_base.BaseTestCase):
|
|||||||
processutils.ssh_execute,
|
processutils.ssh_execute,
|
||||||
None, 'ls', process_input='important')
|
None, 'ls', process_input='important')
|
||||||
|
|
||||||
|
def test_timeout_error(self):
|
||||||
|
self.assertRaises(socket.timeout,
|
||||||
|
processutils.ssh_execute,
|
||||||
|
FakeSshConnection(0), 'ls',
|
||||||
|
timeout=10)
|
||||||
|
|
||||||
def test_works(self):
|
def test_works(self):
|
||||||
out, err = processutils.ssh_execute(FakeSshConnection(0), 'ls')
|
out, err = processutils.ssh_execute(FakeSshConnection(0), 'ls')
|
||||||
self.assertEqual('stdout', out)
|
self.assertEqual('stdout', out)
|
||||||
|
Loading…
Reference in New Issue
Block a user