X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=tests%2Funit%2Ftest_ssh.py;h=236736baad1013974d7cddc08c3b065cab4d90ea;hb=d35ed0fb93098162af4e925d8499554510f73d10;hp=045ac0f1bef00ea916c919314dbd8101efb6d7cd;hpb=45db0fdabb4585b96756a390650181a3c46facf7;p=yardstick.git diff --git a/tests/unit/test_ssh.py b/tests/unit/test_ssh.py index 045ac0f1b..236736baa 100644 --- a/tests/unit/test_ssh.py +++ b/tests/unit/test_ssh.py @@ -16,12 +16,14 @@ # 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 @@ -50,6 +52,62 @@ class SSHTestCase(unittest.TestCase): self.assertEqual("kf", test_ssh.key_filename) self.assertEqual("secret", test_ssh.password) + @mock.patch("yardstick.ssh.SSH._get_pkey") + def test_ssh_from_node(self, mock_ssh__get_pkey): + mock_ssh__get_pkey.return_value = "pkey" + node = { + "user": "root", "ip": "example.net", "ssh_port": 33, + "key_filename": "kf", "password": "secret" + } + test_ssh = ssh.SSH.from_node(node) + self.assertEqual("root", test_ssh.user) + self.assertEqual("example.net", test_ssh.host) + self.assertEqual(33, test_ssh.port) + self.assertEqual("kf", test_ssh.key_filename) + self.assertEqual("secret", test_ssh.password) + + @mock.patch("yardstick.ssh.SSH._get_pkey") + def test_ssh_from_node_password_default(self, mock_ssh__get_pkey): + mock_ssh__get_pkey.return_value = "pkey" + node = { + "user": "root", "ip": "example.net", "ssh_port": 33, + "key_filename": "kf" + } + test_ssh = ssh.SSH.from_node(node) + self.assertEqual("root", test_ssh.user) + self.assertEqual("example.net", test_ssh.host) + self.assertEqual(33, test_ssh.port) + self.assertEqual("kf", test_ssh.key_filename) + self.assertIsNone(test_ssh.password) + + @mock.patch("yardstick.ssh.SSH._get_pkey") + def test_ssh_from_node_ssh_port_default(self, mock_ssh__get_pkey): + mock_ssh__get_pkey.return_value = "pkey" + node = { + "user": "root", "ip": "example.net", + "key_filename": "kf", "password": "secret" + } + test_ssh = ssh.SSH.from_node(node) + self.assertEqual("root", test_ssh.user) + self.assertEqual("example.net", test_ssh.host) + self.assertEqual(ssh.SSH_PORT, test_ssh.port) + self.assertEqual("kf", test_ssh.key_filename) + self.assertEqual("secret", test_ssh.password) + + @mock.patch("yardstick.ssh.SSH._get_pkey") + def test_ssh_from_node_key_filename_default(self, mock_ssh__get_pkey): + mock_ssh__get_pkey.return_value = "pkey" + node = { + "user": "root", "ip": "example.net", "ssh_port": 33, + "password": "secret" + } + test_ssh = ssh.SSH.from_node(node) + self.assertEqual("root", test_ssh.user) + self.assertEqual("example.net", test_ssh.host) + self.assertEqual(33, test_ssh.port) + self.assertIsNone(test_ssh.key_filename) + self.assertEqual("secret", test_ssh.password) + def test_construct_default(self): self.assertEqual("root", self.test_client.user) self.assertEqual("example.net", self.test_client.host) @@ -274,7 +332,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 +348,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") @@ -393,5 +453,6 @@ class SSHRunTestCase(unittest.TestCase): def main(): unittest.main() + if __name__ == '__main__': main()