From 570a84e92c88b1c7d9fd48d107bc3fad75e32efe Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Mon, 7 Apr 2014 00:50:09 -0400 Subject: [PATCH] Correct test boundary for libvirt_driver.get_info The test_get_domain_info_with_more_return() unit test was causing failure on some systems where lxml was not capable of handling None for the xml to parse. However, our unit tests should not be testing libvirt; the unit tests in libvirt already do that. Our unit tests should simply test the code boundaries of the unit of code that they wish to stress, and not anything further. This patch changes the unit test in question to only test the code within the libvirt_driver.LibvirtDriver.get_info() method, and nothing more, and in doing so, we remove any calls at all to libvirt and mock out where the code passes the boundaries of the get_info() method. Change-Id: I022cc331a980c2a4d98f16d2d77b24839b528464 Closes-bug: #1301602 --- nova/tests/virt/libvirt/test_libvirt.py | 41 +++++++++++++------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/nova/tests/virt/libvirt/test_libvirt.py b/nova/tests/virt/libvirt/test_libvirt.py index 500863c138..45df3aec00 100644 --- a/nova/tests/virt/libvirt/test_libvirt.py +++ b/nova/tests/virt/libvirt/test_libvirt.py @@ -55,6 +55,7 @@ from nova.openstack.common import units from nova.openstack.common import uuidutils from nova.pci import pci_manager from nova import test +from nova.tests import fake_instance from nova.tests import fake_network import nova.tests.image.fake from nova.tests import matchers @@ -6443,27 +6444,27 @@ class LibvirtConnTestCase(test.TestCase): self.mox.ReplayAll() self.assertTrue(conn._is_storage_shared_with('foo', '/path')) - def test_get_domain_info_with_more_return(self): - mock_domain = libvirt.virDomain('qemu:///system', None) - instance = {"name": "instancename", "id": "instanceid", - "uuid": "875a8070-d0b9-4949-8b31-104d125c9a64"} - + @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._lookup_by_name') + def test_get_domain_info_with_more_return(self, lookup_mock): + instance = fake_instance.fake_instance_obj(mock.sentinel.ctx) + dom_mock = mock.MagicMock() + dom_mock.info.return_value = [ + 1, 2048, 737, 8, 12345, 888888 + ] + dom_mock.ID.return_value = mock.sentinel.instance_id + lookup_mock.return_value = dom_mock conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) - with contextlib.nested( - mock.patch.object(mock_domain, 'info', - return_value=[1, 2048, 737, 8, 12345, 888888]), - mock.patch.object(mock_domain, 'ID', return_value="123456"), - mock.patch.object(conn, '_lookup_by_name', - return_value=mock_domain), - ) as (mock_info, mock_ID, mock_domain): - info = conn.get_info(instance) - expect = {'state': 1, - 'max_mem': 2048, - 'mem': 737, - 'num_cpu': 8, - 'cpu_time': 12345, - 'id': '123456'} - self.assertEqual(expect, info) + info = conn.get_info(instance) + expect = {'state': 1, + 'max_mem': 2048, + 'mem': 737, + 'num_cpu': 8, + 'cpu_time': 12345, + 'id': mock.sentinel.instance_id} + self.assertEqual(expect, info) + dom_mock.info.assert_called_once_with() + dom_mock.ID.assert_called_once_with() + lookup_mock.assert_called_once_with(instance['name']) def test_create_domain_define_xml_fails(self): """Tests that the xml is logged when defining the domain fails."""