Skip to content

Commit

Permalink
Merge "Fix `sshutils' to execute commands with args"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Apr 27, 2015
2 parents d824295 + 96909b0 commit e7f5d79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rally/common/sshutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def run(self, cmd, stdin=None, stdout=None, stderr=None,
def _run(self, client, cmd, stdin=None, stdout=None, stderr=None,
raise_on_error=True, timeout=3600):

if isinstance(cmd, (list, tuple)):
cmd = " ".join(six.moves.shlex_quote(str(p)) for p in cmd)

transport = client.get_transport()
session = transport.open_session()
session.exec_command(cmd)
Expand Down Expand Up @@ -229,7 +232,7 @@ def _run(self, client, cmd, stdin=None, stdout=None, stderr=None,
def execute(self, cmd, stdin=None, timeout=3600):
"""Execute the specified command on the server.
:param cmd: Command to be executed.
:param cmd: Command to be executed, can be a list.
:param stdin: Open file to be sent on process stdin.
:param timeout: Timeout for execution of the command.
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/common/test_sshutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ def test_execute(self, m_select):
self.assertEqual((127, "ok", "error"), self.ssh.execute("cmd"))
self.fake_session.exec_command.assert_called_once_with("cmd")

@mock.patch("rally.common.sshutils.select")
def test_execute_args(self, m_select):
m_select.select.return_value = ([], [], [])
self.fake_session.recv_ready.side_effect = [1, 0, 0]
self.fake_session.recv_stderr_ready.side_effect = [1, 0]
self.fake_session.recv.return_value = "ok"
self.fake_session.recv_stderr.return_value = "error"
self.fake_session.exit_status_ready.return_value = 1
self.fake_session.recv_exit_status.return_value = 127

result = self.ssh.execute(["cmd", "arg1", "arg2 with space"])
self.assertEqual((127, "ok", "error"), result)
self.fake_session.exec_command.assert_called_once_with(
"cmd arg1 'arg2 with space'")

@mock.patch("rally.common.sshutils.select")
def test_run(self, m_select):
m_select.select.return_value = ([], [], [])
Expand Down

0 comments on commit e7f5d79

Please sign in to comment.