Fix remote command execution in common.utils 79/58579/4
authorMiikka Koistinen <miikka.koistinen@nokia.com>
Fri, 15 Jun 2018 08:54:14 +0000 (11:54 +0300)
committerMiikka Koistinen <miikka.koistinen@nokia.com>
Tue, 31 Jul 2018 12:24:54 +0000 (15:24 +0300)
yardstick.common.utils get_port_mac and get_port_ip both raise a
RuntimeError on positive remote command exit status. This commit fixes
them to use the error raising mechanism in yardstick.ssh.SSH.

Additionally, the class AutoConnectSSH class needed an additional
argument to allow the raising mechanism to work correctly.

JIRA: YARDSTICK-1240

Change-Id: Idad125ebbd668cef10a6149eb3e601a437a8d40d
Signed-off-by: Miikka Koistinen <miikka.koistinen@nokia.com>
yardstick/common/utils.py
yardstick/ssh.py
yardstick/tests/unit/common/test_utils.py

index 6c5389c..068271c 100644 (file)
@@ -194,20 +194,16 @@ def parse_ini_file(path):
 
 def get_port_mac(sshclient, port):
     cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port
-    status, stdout, stderr = sshclient.execute(cmd)
+    _, stdout, _ = sshclient.execute(cmd, raise_on_error=True)
 
-    if status:
-        raise RuntimeError(stderr)
     return stdout.rstrip()
 
 
 def get_port_ip(sshclient, port):
     cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' " \
         "|cut -d ':' -f2 " % port
-    status, stdout, stderr = sshclient.execute(cmd)
+    _, stdout, _ = sshclient.execute(cmd, raise_on_error=True)
 
-    if status:
-        raise RuntimeError(stderr)
     return stdout.rstrip()
 
 
index e6a26ab..69428f3 100644 (file)
@@ -499,9 +499,10 @@ 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):
+    def execute(self, cmd, stdin=None, timeout=3600, raise_on_error=True):
         self._connect()
-        return super(AutoConnectSSH, self).execute(cmd, stdin, timeout)
+        return super(AutoConnectSSH, self).execute(cmd, stdin, timeout,
+                                                   raise_on_error)
 
     def run(self, cmd, stdin=None, stdout=None, stderr=None,
             raise_on_error=True, timeout=3600,
index b634ff4..eae0a32 100644 (file)
@@ -1346,3 +1346,35 @@ class SendSocketCommandTestCase(unittest.TestCase):
         mock_socket_obj.connect_ex.assert_called_once_with(('host', 22))
         mock_socket_obj.sendall.assert_called_once_with(six.b('command'))
         mock_socket_obj.close.assert_called_once()
+
+
+class GetPortMacTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.ssh_client = mock.Mock()
+        self.ssh_client.execute.return_value = (0, 'foo    ', '')
+
+    def test_ssh_client_execute_called(self):
+        utils.get_port_mac(self.ssh_client, 99)
+        self.ssh_client.execute.assert_called_once_with(
+            "ifconfig |grep HWaddr |grep 99 |awk '{print $5}' ",
+            raise_on_error=True)
+
+    def test_return_value(self):
+        self.assertEqual('foo', utils.get_port_mac(self.ssh_client, 99))
+
+
+class GetPortIPTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.ssh_client = mock.Mock()
+        self.ssh_client.execute.return_value = (0, 'foo    ', '')
+
+    def test_ssh_client_execute_called(self):
+        utils.get_port_ip(self.ssh_client, 99)
+        self.ssh_client.execute.assert_called_once_with(
+            "ifconfig 99 |grep 'inet addr' |awk '{print $2}' |cut -d ':' -f2 ",
+            raise_on_error=True)
+
+    def test_return_value(self):
+        self.assertEqual('foo', utils.get_port_ip(self.ssh_client, 99))