Convert SSH custom exceptions to Yardstick exceptions 47/58147/1
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Fri, 1 Jun 2018 11:21:20 +0000 (12:21 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Fri, 1 Jun 2018 11:27:09 +0000 (12:27 +0100)
JIRA: YARDSTICK-1215

Change-Id: I5ecfd3dccd91b07cd8de5309dfa1a372eff16ed0
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
yardstick/common/exceptions.py
yardstick/error.py
yardstick/network_services/helpers/dpdkbindnic_helper.py
yardstick/ssh.py
yardstick/tests/unit/network_services/helpers/test_dpdkbindnic_helper.py
yardstick/tests/unit/network_services/nfvi/test_resource.py
yardstick/tests/unit/test_ssh.py

index e6a1f1f..a9f9fff 100644 (file)
@@ -137,6 +137,14 @@ class LibvirtQemuImageCreateError(YardstickException):
                '%(base_image)s. 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'
 
index 9b84de1..f22e7e0 100644 (file)
 # 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
index 05b822c..e98a738 100644 (file)
@@ -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))
index d7adc0d..6b5e6fa 100644 (file)
@@ -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 """
index 367072e..0278c20 100644 (file)
@@ -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)
 
index 9f337c6..de96794 100644 (file)
@@ -17,10 +17,10 @@ import errno
 import mock
 import unittest
 
+from yardstick.common import exceptions
 from yardstick.network_services.nfvi.resource import ResourceProfile
 from yardstick.network_services.nfvi import resource, collectd
-from yardstick.common.exceptions import ResourceCommandError
-from yardstick import ssh
+
 
 class TestResourceProfile(unittest.TestCase):
     VNFD = {'vnfd:vnfd-catalog':
@@ -134,8 +134,8 @@ class TestResourceProfile(unittest.TestCase):
         self.assertIsNone(self.resource_profile._start_collectd(ssh_mock,
                                                                 "/opt/nsb_bin"))
 
-        ssh_mock.execute = mock.Mock(side_effect=ssh.SSHError)
-        with self.assertRaises(ssh.SSHError):
+        ssh_mock.execute = mock.Mock(side_effect=exceptions.SSHError)
+        with self.assertRaises(exceptions.SSHError):
             self.resource_profile._start_collectd(ssh_mock, "/opt/nsb_bin")
 
         ssh_mock.execute = mock.Mock(return_value=(1, "", ""))
@@ -148,11 +148,11 @@ class TestResourceProfile(unittest.TestCase):
         self.assertIsNone(self.resource_profile._start_rabbitmq(ssh_mock))
 
         ssh_mock.execute = mock.Mock(return_value=(0, "", ""))
-        with self.assertRaises(ResourceCommandError):
+        with self.assertRaises(exceptions.ResourceCommandError):
             self.resource_profile._start_rabbitmq(ssh_mock)
 
         ssh_mock.execute = mock.Mock(return_value=(1, "", ""))
-        with self.assertRaises(ResourceCommandError):
+        with self.assertRaises(exceptions.ResourceCommandError):
             self.resource_profile._start_rabbitmq(ssh_mock)
 
     def test__prepare_collectd_conf(self):
index f922900..080d278 100644 (file)
 #    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)