From d19daf93623d07d50977f1dc0e126c91383b3bd1 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 1 Jun 2018 12:21:20 +0100 Subject: Convert SSH custom exceptions to Yardstick exceptions JIRA: YARDSTICK-1215 Change-Id: I5ecfd3dccd91b07cd8de5309dfa1a372eff16ed0 Signed-off-by: Rodolfo Alonso Hernandez (cherry picked from commit dde8fa628483deec352e176cea8decdbf6bed7e8) --- .../helpers/test_dpdkbindnic_helper.py | 5 ++- yardstick/common/exceptions.py | 8 ++++ yardstick/error.py | 10 ----- .../network_services/helpers/dpdkbindnic_helper.py | 7 ++-- yardstick/ssh.py | 49 +++++++++++----------- yardstick/tests/unit/test_ssh.py | 30 ++++++------- 6 files changed, 53 insertions(+), 56 deletions(-) diff --git a/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py b/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py index 367072e84..0278c20dc 100644 --- a/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py +++ b/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py @@ -17,7 +17,8 @@ import unittest import os -from yardstick.error import IncorrectConfig, SSHError +from yardstick.common import exceptions +from yardstick.error import IncorrectConfig from yardstick.error import IncorrectNodeSetup from yardstick.error import IncorrectSetup from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkInterface @@ -244,7 +245,7 @@ class TestDpdkNode(unittest.TestCase): mock_ssh_helper = mock.Mock() mock_ssh_helper.execute.return_value = 0, '', '' - mock_intf_type().check.side_effect = SSHError + mock_intf_type().check.side_effect = exceptions.SSHError dpdk_node = DpdkNode(NAME, self.INTERFACES, mock_ssh_helper) diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index fb4d33a59..233e250fc 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -112,6 +112,14 @@ class LibvirtCreateError(YardstickException): message = 'Error creating the virtual machine. Error: %(error)s.' +class SSHError(YardstickException): + message = '%(error_msg)s' + + +class SSHTimeout(SSHError): + pass + + class ScenarioConfigContextNameNotFound(YardstickException): message = 'Context name "%(context_name)s" not found' diff --git a/yardstick/error.py b/yardstick/error.py index 9b84de1af..f22e7e049 100644 --- a/yardstick/error.py +++ b/yardstick/error.py @@ -13,16 +13,6 @@ # limitations under the License. -class SSHError(Exception): - """Class handles ssh connection error exception""" - pass - - -class SSHTimeout(SSHError): - """Class handles ssh connection timeout exception""" - pass - - class IncorrectConfig(Exception): """Class handles incorrect configuration during setup""" pass diff --git a/yardstick/network_services/helpers/dpdkbindnic_helper.py b/yardstick/network_services/helpers/dpdkbindnic_helper.py index 05b822c2e..e98a738c3 100644 --- a/yardstick/network_services/helpers/dpdkbindnic_helper.py +++ b/yardstick/network_services/helpers/dpdkbindnic_helper.py @@ -18,12 +18,12 @@ import re from collections import defaultdict from itertools import chain +from yardstick.common import exceptions from yardstick.common.utils import validate_non_string_sequence from yardstick.error import IncorrectConfig from yardstick.error import IncorrectSetup from yardstick.error import IncorrectNodeSetup -from yardstick.error import SSHTimeout -from yardstick.error import SSHError + NETWORK_KERNEL = 'network_kernel' NETWORK_DPDK = 'network_dpdk' @@ -98,7 +98,8 @@ class DpdkInterface(object): # if we don't find all the keys then don't update pass - except (IncorrectNodeSetup, SSHError, SSHTimeout): + except (IncorrectNodeSetup, exceptions.SSHError, + exceptions.SSHTimeout): raise IncorrectConfig( "Unable to probe missing interface fields '%s', on node %s " "SSH Error" % (', '.join(self.missing_fields), self.dpdk_node.node_key)) diff --git a/yardstick/ssh.py b/yardstick/ssh.py index d7adc0d05..6b5e6faf4 100644 --- a/yardstick/ssh.py +++ b/yardstick/ssh.py @@ -62,15 +62,13 @@ Eventlet: sshclient = eventlet.import_patched("yardstick.ssh") """ -from __future__ import absolute_import -import os import io +import logging +import os +import re import select import socket import time -import re - -import logging import paramiko from chainmap import ChainMap @@ -78,6 +76,7 @@ from oslo_utils import encodeutils from scp import SCPClient import six +from yardstick.common import exceptions from yardstick.common.utils import try_int, NON_NONE_DEFAULT, make_dict_from_map from yardstick.network_services.utils import provision_tool @@ -90,12 +89,12 @@ def convert_key_to_str(key): return k.getvalue() -class SSHError(Exception): - pass - - -class SSHTimeout(SSHError): - pass +# class SSHError(Exception): +# pass +# +# +# class SSHTimeout(SSHError): +# pass class SSH(object): @@ -193,7 +192,7 @@ class SSH(object): return key_class.from_private_key(key) except paramiko.SSHException as e: errors.append(e) - raise SSHError("Invalid pkey: %s" % errors) + raise exceptions.SSHError(error_msg='Invalid pkey: %s' % errors) @property def is_connected(self): @@ -214,10 +213,10 @@ class SSH(object): return self._client except Exception as e: message = ("Exception %(exception_type)s was raised " - "during connect. Exception value is: %(exception)r") + "during connect. Exception value is: %(exception)r" % + {"exception": e, "exception_type": type(e)}) self._client = False - raise SSHError(message % {"exception": e, - "exception_type": type(e)}) + raise exceptions.SSHError(error_msg=message) def _make_dict(self): return { @@ -334,11 +333,11 @@ class SSH(object): break if timeout and (time.time() - timeout) > start_time: - args = {"cmd": cmd, "host": self.host} - raise SSHTimeout("Timeout executing command " - "'%(cmd)s' on host %(host)s" % args) + message = ('Timeout executing command %(cmd)s on host %(host)s' + % {"cmd": cmd, "host": self.host}) + raise exceptions.SSHTimeout(error_msg=message) if e: - raise SSHError("Socket error.") + raise exceptions.SSHError(error_msg='Socket error') exit_status = session.recv_exit_status() if exit_status != 0 and raise_on_error: @@ -346,7 +345,7 @@ class SSH(object): details = fmt % {"cmd": cmd, "status": exit_status} if stderr_data: details += " Last stderr data: '%s'." % stderr_data - raise SSHError(details) + raise exceptions.SSHError(error_msg=details) return exit_status def execute(self, cmd, stdin=None, timeout=3600): @@ -377,11 +376,12 @@ class SSH(object): while True: try: return self.execute("uname") - except (socket.error, SSHError) as e: + except (socket.error, exceptions.SSHError) as e: self.log.debug("Ssh is still unavailable: %r", e) time.sleep(interval) if time.time() > end_time: - raise SSHTimeout("Timeout waiting for '%s'" % self.host) + raise exceptions.SSHTimeout( + error_msg='Timeout waiting for "%s"' % self.host) def put(self, files, remote_path=b'.', recursive=False): client = self._get_client() @@ -486,11 +486,12 @@ class AutoConnectSSH(SSH): while True: try: return self._get_client() - except (socket.error, SSHError) as e: + except (socket.error, exceptions.SSHError) as e: self.log.debug("Ssh is still unavailable: %r", e) time.sleep(interval) if time.time() > end_time: - raise SSHTimeout("Timeout waiting for '%s'" % self.host) + raise exceptions.SSHTimeout( + error_msg='Timeout waiting for "%s"' % self.host) def drop_connection(self): """ Don't close anything, just force creation of a new client """ diff --git a/yardstick/tests/unit/test_ssh.py b/yardstick/tests/unit/test_ssh.py index f92290070..080d27837 100644 --- a/yardstick/tests/unit/test_ssh.py +++ b/yardstick/tests/unit/test_ssh.py @@ -13,10 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -# 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 @@ -26,8 +22,8 @@ from itertools import count import mock from oslo_utils import encodeutils +from yardstick.common import exceptions from yardstick import ssh -from yardstick.ssh import SSHError, SSHTimeout from yardstick.ssh import SSH from yardstick.ssh import AutoConnectSSH @@ -127,7 +123,7 @@ class SSHTestCase(unittest.TestCase): dss = mock_paramiko.dsskey.DSSKey rsa.from_private_key.side_effect = mock_paramiko.SSHException dss.from_private_key.side_effect = mock_paramiko.SSHException - self.assertRaises(ssh.SSHError, self.test_client._get_pkey, "key") + self.assertRaises(exceptions.SSHError, self.test_client._get_pkey, "key") @mock.patch("yardstick.ssh.six.moves.StringIO") @mock.patch("yardstick.ssh.paramiko") @@ -194,7 +190,7 @@ class SSHTestCase(unittest.TestCase): test_ssh = ssh.SSH("admin", "example.net", pkey="key") - with self.assertRaises(SSHError) as raised: + with self.assertRaises(exceptions.SSHError) as raised: test_ssh._get_client() self.assertEqual(mock_paramiko.SSHClient.call_count, 1) @@ -245,18 +241,18 @@ class SSHTestCase(unittest.TestCase): @mock.patch("yardstick.ssh.time") def test_wait_timeout(self, mock_time): mock_time.time.side_effect = [1, 50, 150] - self.test_client.execute = mock.Mock(side_effect=[ssh.SSHError, - ssh.SSHError, + self.test_client.execute = mock.Mock(side_effect=[exceptions.SSHError, + exceptions.SSHError, 0]) - self.assertRaises(ssh.SSHTimeout, self.test_client.wait) + self.assertRaises(exceptions.SSHTimeout, self.test_client.wait) self.assertEqual([mock.call("uname")] * 2, self.test_client.execute.mock_calls) @mock.patch("yardstick.ssh.time") def test_wait(self, mock_time): mock_time.time.side_effect = [1, 50, 100] - self.test_client.execute = mock.Mock(side_effect=[ssh.SSHError, - ssh.SSHError, + self.test_client.execute = mock.Mock(side_effect=[exceptions.SSHError, + exceptions.SSHError, 0]) self.test_client.wait() self.assertEqual([mock.call("uname")] * 3, @@ -333,7 +329,7 @@ class SSHRunTestCase(unittest.TestCase): def test_run_nonzero_status(self, mock_select): mock_select.select.return_value = ([], [], []) self.fake_session.recv_exit_status.return_value = 1 - self.assertRaises(ssh.SSHError, self.test_client.run, "cmd") + self.assertRaises(exceptions.SSHError, self.test_client.run, "cmd") self.assertEqual(1, self.test_client.run("cmd", raise_on_error=False)) @mock.patch("yardstick.ssh.select") @@ -401,7 +397,7 @@ class SSHRunTestCase(unittest.TestCase): def test_run_select_error(self, mock_select): self.fake_session.exit_status_ready.return_value = False mock_select.select.return_value = ([], [], [True]) - self.assertRaises(ssh.SSHError, self.test_client.run, "cmd") + self.assertRaises(exceptions.SSHError, self.test_client.run, "cmd") @mock.patch("yardstick.ssh.time") @mock.patch("yardstick.ssh.select") @@ -409,7 +405,7 @@ class SSHRunTestCase(unittest.TestCase): mock_time.time.side_effect = [1, 3700] mock_select.select.return_value = ([], [], []) self.fake_session.exit_status_ready.return_value = False - self.assertRaises(ssh.SSHTimeout, self.test_client.run, "cmd") + self.assertRaises(exceptions.SSHTimeout, self.test_client.run, "cmd") @mock.patch("yardstick.ssh.open", create=True) def test__put_file_shell(self, mock_open): @@ -529,9 +525,9 @@ class TestAutoConnectSSH(unittest.TestCase): auto_connect_ssh = AutoConnectSSH('user1', 'host1', wait=10) auto_connect_ssh._get_client = mock__get_client = mock.Mock() - mock__get_client.side_effect = SSHError + mock__get_client.side_effect = exceptions.SSHError - with self.assertRaises(SSHTimeout): + with self.assertRaises(exceptions.SSHTimeout): auto_connect_ssh._connect() self.assertEqual(mock_time.time.call_count, 12) -- cgit 1.2.3-korg