From 365ded3d2089fff98f4d8562fb277112b242645f Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 16 Jan 2018 11:03:17 +0000 Subject: Improve "Libvirt.virsh_create_vm" function Read the command exit code and raise an exception in case the VM boot process went wrong. JIRA: YARDSTICK-941 Change-Id: Ibabaae8983213a33799ad00fa4d9541bbabea857 Signed-off-by: Rodolfo Alonso Hernandez --- yardstick/benchmark/contexts/standalone/model.py | 6 ++++-- yardstick/common/exceptions.py | 4 ++++ .../benchmark/contexts/standalone/test_model.py | 24 ++++++++++++++++------ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/yardstick/benchmark/contexts/standalone/model.py b/yardstick/benchmark/contexts/standalone/model.py index 7eab1c9c8..ffa37fd37 100644 --- a/yardstick/benchmark/contexts/standalone/model.py +++ b/yardstick/benchmark/contexts/standalone/model.py @@ -106,8 +106,10 @@ class Libvirt(object): @staticmethod def virsh_create_vm(connection, cfg): - err = connection.execute("virsh create %s" % cfg)[0] - LOG.info("VM create status: %s", err) + LOG.info('VM create, XML config: %s', cfg) + status, _, error = connection.execute('virsh create %s' % cfg) + if status: + raise exceptions.LibvirtCreateError(error=error) @staticmethod def virsh_destroy_vm(vm_name, connection): diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index e0e5b7e16..517936fdc 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -108,6 +108,10 @@ class OVSSetupError(YardstickException): message = 'OVS setup error. Command: %(command)s. Error: %(error)s.' +class LibvirtCreateError(YardstickException): + message = 'Error creating the virtual machine. Error: %(error)s.' + + class ScenarioConfigContextNameNotFound(YardstickException): message = 'Context name "%(context_name)s" not found' diff --git a/yardstick/tests/unit/benchmark/contexts/standalone/test_model.py b/yardstick/tests/unit/benchmark/contexts/standalone/test_model.py index 005deb88f..d6f563498 100644 --- a/yardstick/tests/unit/benchmark/contexts/standalone/test_model.py +++ b/yardstick/tests/unit/benchmark/contexts/standalone/test_model.py @@ -22,6 +22,7 @@ from xml.etree import ElementTree from yardstick import ssh from yardstick.benchmark.contexts.standalone import model +from yardstick.common import exceptions from yardstick import constants from yardstick.network_services import utils @@ -49,6 +50,12 @@ class ModelLibvirtTestCase(unittest.TestCase): self.pci_address_str = '0001:04:03.2' self.pci_address = utils.PciAddress(self.pci_address_str) self.mac = '00:00:00:00:00:01' + self._mock_ssh = mock.Mock() + self.mock_ssh = self._mock_ssh.start() + self.addCleanup(self._cleanup) + + def _cleanup(self): + self._mock_ssh.stop() # TODO: Remove mocking of yardstick.ssh.SSH (here and elsewhere) # In this case, we are mocking a param to be passed into other methods @@ -62,12 +69,17 @@ class ModelLibvirtTestCase(unittest.TestCase): model.Libvirt.check_if_vm_exists_and_delete("vm_0", ssh_mock) def test_virsh_create_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_create_vm(ssh_mock, "vm_0") + self.mock_ssh.execute = mock.Mock(return_value=(0, 0, 0)) + model.Libvirt.virsh_create_vm(self.mock_ssh, 'vm_0') + self.mock_ssh.execute.assert_called_once_with('virsh create vm_0') + + def test_virsh_create_vm_error(self): + self.mock_ssh.execute = mock.Mock(return_value=(1, 0, 'error_create')) + with self.assertRaises(exceptions.LibvirtCreateError) as exc: + model.Libvirt.virsh_create_vm(self.mock_ssh, 'vm_0') + self.assertEqual('Error creating the virtual machine. Error: ' + 'error_create.', str(exc.exception)) + 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: -- cgit 1.2.3-korg