Add support for Python 3
[yardstick.git] / tests / unit / test_ssh.py
index 88638a0..1c63c00 100644 (file)
 # yardstick comment: this file is a modified copy of
 # rally/tests/unit/common/test_sshutils.py
 
+from __future__ import absolute_import
 import os
 import socket
 import unittest
-from cStringIO import StringIO
+from io import StringIO
 
 import mock
+from oslo_utils import encodeutils
 
 from yardstick import ssh
 
@@ -274,7 +276,9 @@ class SSHRunTestCase(unittest.TestCase):
         fake_stdin.close = mock.Mock(side_effect=close)
         self.test_client.run("cmd", stdin=fake_stdin)
         call = mock.call
-        send_calls = [call("line1"), call("line2"), call("e2")]
+        send_calls = [call(encodeutils.safe_encode("line1", "utf-8")),
+                      call(encodeutils.safe_encode("line2", "utf-8")),
+                      call(encodeutils.safe_encode("e2", "utf-8"))]
         self.assertEqual(send_calls, self.fake_session.send.mock_calls)
 
     @mock.patch("yardstick.ssh.select")
@@ -288,10 +292,10 @@ class SSHRunTestCase(unittest.TestCase):
         self.fake_session.exit_status_ready.side_effect = [0, 0, 0, True]
         self.fake_session.send_ready.return_value = True
         self.fake_session.send.side_effect = len
-        fake_stdin = StringIO("line1\nline2\n")
+        fake_stdin = StringIO(u"line1\nline2\n")
         self.test_client.run("cmd", stdin=fake_stdin, keep_stdin_open=True)
         call = mock.call
-        send_calls = [call("line1\nline2\n")]
+        send_calls = [call(encodeutils.safe_encode("line1\nline2\n", "utf-8"))]
         self.assertEqual(send_calls, self.fake_session.send.mock_calls)
 
     @mock.patch("yardstick.ssh.select")
@@ -310,12 +314,38 @@ class SSHRunTestCase(unittest.TestCase):
 
     @mock.patch("yardstick.ssh.open", create=True)
     def test__put_file_shell(self, mock_open):
-        self.test_client.run = mock.Mock()
-        self.test_client._put_file_shell("localfile", "remotefile", 0o42)
+        with mock.patch.object(self.test_client, "run") as run_mock:
+            self.test_client._put_file_shell("localfile", "remotefile", 0o42)
+            run_mock.assert_called_once_with(
+                'cat > "remotefile"&& chmod -- 042 "remotefile"',
+                stdin=mock_open.return_value.__enter__.return_value)
 
-        self.test_client.run.assert_called_once_with(
-            'cat > "remotefile"&& chmod -- 042 "remotefile"',
-            stdin=mock_open.return_value.__enter__.return_value)
+    @mock.patch("yardstick.ssh.open", create=True)
+    def test__put_file_shell_space(self, mock_open):
+        with mock.patch.object(self.test_client, "run") as run_mock:
+            self.test_client._put_file_shell("localfile",
+                                             "filename with space", 0o42)
+            run_mock.assert_called_once_with(
+                'cat > "filename with space"&& chmod -- 042 "filename with '
+                'space"',
+                stdin=mock_open.return_value.__enter__.return_value)
+
+    @mock.patch("yardstick.ssh.open", create=True)
+    def test__put_file_shell_tilde(self, mock_open):
+        with mock.patch.object(self.test_client, "run") as run_mock:
+            self.test_client._put_file_shell("localfile", "~/remotefile", 0o42)
+            run_mock.assert_called_once_with(
+                'cat > ~/"remotefile"&& chmod -- 042 ~/"remotefile"',
+                stdin=mock_open.return_value.__enter__.return_value)
+
+    @mock.patch("yardstick.ssh.open", create=True)
+    def test__put_file_shell_tilde_spaces(self, mock_open):
+        with mock.patch.object(self.test_client, "run") as run_mock:
+            self.test_client._put_file_shell("localfile", "~/file with space",
+                                             0o42)
+            run_mock.assert_called_once_with(
+                'cat > ~/"file with space"&& chmod -- 042 ~/"file with space"',
+                stdin=mock_open.return_value.__enter__.return_value)
 
     @mock.patch("yardstick.ssh.os.stat")
     def test__put_file_sftp(self, mock_stat):
@@ -367,5 +397,6 @@ class SSHRunTestCase(unittest.TestCase):
 def main():
     unittest.main()
 
+
 if __name__ == '__main__':
     main()