diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..8f07a4b183 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -53,6 +53,11 @@ Common Compute ~~~~~~~ +- [SSH] Fix ``ShellOutSSHClient.run()`` returning the command's standard output in + place of its standard error. + (#2259) + [Abed Alawiyeh - @abedalawieh] + - [OVH] Remove the static location mapping and use the location code returned by the API as both the location ID and name, avoiding failures for codes not known to Libcloud. diff --git a/libcloud/compute/ssh.py b/libcloud/compute/ssh.py index aab2886bf1..cff27151ed 100644 --- a/libcloud/compute/ssh.py +++ b/libcloud/compute/ssh.py @@ -903,7 +903,7 @@ def _run_remote_shell_command(self, cmd): stdout, stderr = child.communicate() stdout_str = cast(str, stdout) - stderr_str = cast(str, stdout) + stderr_str = cast(str, stderr) return (stdout_str, stderr_str, child.returncode) diff --git a/libcloud/test/compute/test_ssh_client.py b/libcloud/test/compute/test_ssh_client.py index 0d1d6e9366..16bb6b05d4 100644 --- a/libcloud/test/compute/test_ssh_client.py +++ b/libcloud/test/compute/test_ssh_client.py @@ -890,6 +890,24 @@ def test_get_base_ssh_command(self): cmd3, ["ssh", "-i", "/home/my.key", "-oConnectTimeout=5", "root@localhost"] ) + def test_run_returns_stdout_and_stderr(self): + class MockChild: + returncode = 0 + + def communicate(*args, **kwargs): + return (b"out", b"err") + + def mock_popen(*args, **kwargs): + return MockChild() + + with patch("subprocess.Popen", mock_popen): + client = ShellOutSSHClient(hostname="localhost", username="root") + stdout, stderr, status = client.run("ls") + + self.assertEqual(stdout, b"out") + self.assertEqual(stderr, b"err") + self.assertEqual(status, 0) + if __name__ == "__main__": sys.exit(unittest.main())