Merge "Change AutoConnectSSH to return error code by default"
authorAbhijit Sinha <abhijit.sinha@intel.com>
Fri, 3 Aug 2018 13:23:40 +0000 (13:23 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Fri, 3 Aug 2018 13:23:40 +0000 (13:23 +0000)
yardstick/ssh.py
yardstick/tests/unit/test_ssh.py

index 69428f3..8bdc32c 100644 (file)
@@ -499,7 +499,7 @@ class AutoConnectSSH(SSH):
         """ Don't close anything, just force creation of a new client """
         self._client = False
 
-    def execute(self, cmd, stdin=None, timeout=3600, raise_on_error=True):
+    def execute(self, cmd, stdin=None, timeout=3600, raise_on_error=False):
         self._connect()
         return super(AutoConnectSSH, self).execute(cmd, stdin, timeout,
                                                    raise_on_error)
index b727e82..71929f1 100644 (file)
@@ -617,3 +617,26 @@ class TestAutoConnectSSH(unittest.TestCase):
 
         auto_connect_ssh.put_file('a', 'b')
         mock_put_sftp.assert_called_once()
+
+    def test_execute(self):
+        auto_connect_ssh = AutoConnectSSH('user1', 'host1')
+        auto_connect_ssh._client = mock.Mock()
+        auto_connect_ssh.run = mock.Mock(return_value=0)
+        exit_code, _, _ = auto_connect_ssh.execute('')
+        self.assertEqual(exit_code, 0)
+
+    def _mock_run(self, *args, **kwargs):
+        if args[0] == 'ls':
+            if kwargs.get('raise_on_error'):
+                raise exceptions.SSHError(error_msg='Command error')
+            return 1
+        return 0
+
+    def test_execute_command_error(self):
+        auto_connect_ssh = AutoConnectSSH('user1', 'host1')
+        auto_connect_ssh._client = mock.Mock()
+        auto_connect_ssh.run = mock.Mock(side_effect=self._mock_run)
+        self.assertRaises(exceptions.SSHError, auto_connect_ssh.execute, 'ls',
+                          raise_on_error=True)
+        exit_code, _, _ = auto_connect_ssh.execute('ls')
+        self.assertNotEqual(exit_code, 0)