Improve "Libvirt.virsh_destroy_vm" function 95/50695/17
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 16 Jan 2018 11:19:05 +0000 (11:19 +0000)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Wed, 21 Mar 2018 09:15:37 +0000 (09:15 +0000)
Read the command exit code and log a warning in case the VM destroy
process went wrong.

JIRA: YARDSTICK-943

Change-Id: I2750b8d4a8f67af081c1988510cf5aca848a2cf1
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
yardstick/benchmark/contexts/standalone/model.py
yardstick/tests/unit/benchmark/contexts/standalone/test_model.py

index ffa37fd..f18d090 100644 (file)
@@ -113,7 +113,10 @@ class Libvirt(object):
 
     @staticmethod
     def virsh_destroy_vm(vm_name, connection):
-        connection.execute("virsh destroy %s" % vm_name)
+        LOG.info('VM destroy, VM name: %s', vm_name)
+        status, _, error = connection.execute('virsh destroy %s' % vm_name)
+        if status:
+            LOG.warning('Error destroying VM %s. Error: %s', vm_name, error)
 
     @staticmethod
     def _add_interface_address(interface, pci_address):
index d6f5634..b1dcee2 100644 (file)
@@ -13,8 +13,8 @@
 # limitations under the License.
 
 import copy
-import os
 import mock
+import os
 import unittest
 import uuid
 
@@ -82,12 +82,18 @@ class ModelLibvirtTestCase(unittest.TestCase):
         self.mock_ssh.execute.assert_called_once_with('virsh create vm_0')
 
     def test_virsh_destroy_vm(self):
-        with mock.patch("yardstick.ssh.SSH") as ssh:
-            ssh_mock = mock.Mock(autospec=ssh.SSH)
-            ssh_mock.execute = mock.Mock(return_value=(0, "a", ""))
-            ssh.return_value = ssh_mock
-        # NOTE(ralonsoh): this test doesn't cover function execution.
-        model.Libvirt.virsh_destroy_vm("vm_0", ssh_mock)
+        self.mock_ssh.execute = mock.Mock(return_value=(0, 0, 0))
+        model.Libvirt.virsh_destroy_vm('vm_0', self.mock_ssh)
+        self.mock_ssh.execute.assert_called_once_with('virsh destroy vm_0')
+
+    @mock.patch.object(model, 'LOG')
+    def test_virsh_destroy_vm_error(self, mock_logger):
+        self.mock_ssh.execute = mock.Mock(return_value=(1, 0, 'error_destroy'))
+        mock_logger.warning = mock.Mock()
+        model.Libvirt.virsh_destroy_vm('vm_0', self.mock_ssh)
+        mock_logger.warning.assert_called_once_with(
+            'Error destroying VM %s. Error: %s', 'vm_0', 'error_destroy')
+        self.mock_ssh.execute.assert_called_once_with('virsh destroy vm_0')
 
     def test_add_interface_address(self):
         xml = ElementTree.ElementTree(