From e31b137ef40a54dd5ff58c9a5cfeaf5745853a32 Mon Sep 17 00:00:00 2001 From: Sahid Orentino Ferdjaoui Date: Fri, 12 Jun 2015 03:29:18 -0400 Subject: [PATCH] libvirt: make unit tests concise by setup guest object Instead of re-instancing + mocking a new guest object each time manually this commit introduces an attribute guest which will be resetted and well configured every tests executed . Change-Id: Iaba9db2b6110375d02dfd07665003279ec2a3d41 --- nova/tests/unit/virt/libvirt/test_guest.py | 221 ++++++--------------- 1 file changed, 66 insertions(+), 155 deletions(-) diff --git a/nova/tests/unit/virt/libvirt/test_guest.py b/nova/tests/unit/virt/libvirt/test_guest.py index a9d46871e54d..b4116425468d 100644 --- a/nova/tests/unit/virt/libvirt/test_guest.py +++ b/nova/tests/unit/virt/libvirt/test_guest.py @@ -43,14 +43,14 @@ class GuestTestCase(test.NoDBTestCase): self.host = host.Host("qemu:///system") self.context = context.get_admin_context() - def test_repr(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - domain.ID.return_value = 99 - domain.UUIDString.return_value = "UUID" - domain.name.return_value = "foo" + self.domain = mock.Mock(spec=fakelibvirt.virDomain) + self.guest = libvirt_guest.Guest(self.domain) - guest = libvirt_guest.Guest(domain) - self.assertEqual("", repr(guest)) + def test_repr(self): + self.domain.ID.return_value = 99 + self.domain.UUIDString.return_value = "UUID" + self.domain.name.return_value = "foo" + self.assertEqual("", repr(self.guest)) @mock.patch.object(fakelibvirt.Connection, 'defineXML') def test_create(self, mock_define): @@ -65,39 +65,26 @@ class GuestTestCase(test.NoDBTestCase): "foo", self.host) def test_launch(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - - guest = libvirt_guest.Guest(domain) - guest.launch() - - domain.createWithFlags.assert_called_once_with(0) + self.guest.launch() + self.domain.createWithFlags.assert_called_once_with(0) def test_launch_and_pause(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - - guest = libvirt_guest.Guest(domain) - guest.launch(pause=True) - - domain.createWithFlags.assert_called_once_with( + self.guest.launch(pause=True) + self.domain.createWithFlags.assert_called_once_with( fakelibvirt.VIR_DOMAIN_START_PAUSED) @mock.patch.object(encodeutils, 'safe_decode') def test_launch_exception(self, mock_safe_decode): - domain = mock.Mock(spec=fakelibvirt.virDomain) - domain.createWithFlags.side_effect = test.TestingException + self.domain.createWithFlags.side_effect = test.TestingException mock_safe_decode.return_value = "" - - guest = libvirt_guest.Guest(domain) - self.assertRaises(test.TestingException, guest.launch) + self.assertRaises(test.TestingException, self.guest.launch) self.assertEqual(1, mock_safe_decode.called) @mock.patch.object(utils, 'execute') @mock.patch.object(libvirt_guest.Guest, 'get_interfaces') def test_enable_hairpin(self, mock_get_interfaces, mock_execute): mock_get_interfaces.return_value = ["vnet0", "vnet1"] - - guest = libvirt_guest.Guest(mock.MagicMock()) - guest.enable_hairpin() + self.guest.enable_hairpin() mock_execute.assert_has_calls([ mock.call( 'tee', '/sys/class/net/vnet0/brport/hairpin_mode', @@ -114,14 +101,11 @@ class GuestTestCase(test.NoDBTestCase): mock_get_interfaces.return_value = ["foo"] mock_execute.side_effect = test.TestingException('oops') - guest = libvirt_guest.Guest(mock.MagicMock()) - self.assertRaises(test.TestingException, guest.enable_hairpin) + self.assertRaises(test.TestingException, self.guest.enable_hairpin) self.assertEqual(1, mock_safe_decode.called) def test_get_interfaces(self): - dom = mock.Mock(spec=fakelibvirt.virDomain) - dom.XMLDesc.return_value = """ - + self.domain.XMLDesc.return_value = """ @@ -131,211 +115,138 @@ class GuestTestCase(test.NoDBTestCase): """ - guest = libvirt_guest.Guest(dom) - self.assertEqual(["vnet0", "vnet1"], guest.get_interfaces()) + self.assertEqual(["vnet0", "vnet1"], self.guest.get_interfaces()) def test_get_interfaces_exception(self): - dom = mock.Mock(spec=fakelibvirt.virDomain) - dom.XMLDesc.return_value = "" - guest = libvirt_guest.Guest(dom) - self.assertEqual([], guest.get_interfaces()) + self.domain.XMLDesc.return_value = "" + self.assertEqual([], self.guest.get_interfaces()) def test_poweroff(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - - guest = libvirt_guest.Guest(domain) - guest.poweroff() - - domain.destroy.assert_called_once_with() + self.guest.poweroff() + self.domain.destroy.assert_called_once_with() def test_resume(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - - guest = libvirt_guest.Guest(domain) - guest.resume() - - domain.resume.assert_called_once_with() + self.guest.resume() + self.domain.resume.assert_called_once_with() def test_get_vcpus_info(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - domain.vcpus.return_value = ([(0, 1, 10290000000L, 2)], + self.domain.vcpus.return_value = ([(0, 1, 10290000000L, 2)], [(True, True)]) - guest = libvirt_guest.Guest(domain) - vcpus = list(guest.get_vcpus_info()) + vcpus = list(self.guest.get_vcpus_info()) self.assertEqual(0, vcpus[0].id) self.assertEqual(2, vcpus[0].cpu) self.assertEqual(1, vcpus[0].state) self.assertEqual(10290000000L, vcpus[0].time) def test_delete_configuration(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - - guest = libvirt_guest.Guest(domain) - guest.delete_configuration() - - domain.undefineFlags.assert_called_once_with( + self.guest.delete_configuration() + self.domain.undefineFlags.assert_called_once_with( fakelibvirt.VIR_DOMAIN_UNDEFINE_MANAGED_SAVE) def test_delete_configuration_exception(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - domain.undefineFlags.side_effect = fakelibvirt.libvirtError('oops') - domain.ID.return_value = 1 - - guest = libvirt_guest.Guest(domain) - guest.delete_configuration() - - domain.undefine.assert_called_once_with() + self.domain.undefineFlags.side_effect = fakelibvirt.libvirtError( + 'oops') + self.domain.ID.return_value = 1 + self.guest.delete_configuration() + self.domain.undefine.assert_called_once_with() def test_attach_device(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.attach_device(conf) - - domain.attachDeviceFlags.assert_called_once_with("", flags=0) + self.guest.attach_device(conf) + self.domain.attachDeviceFlags.assert_called_once_with( + "", flags=0) def test_attach_device_persistent(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.attach_device(conf, persistent=True) - - domain.attachDeviceFlags.assert_called_once_with( + self.guest.attach_device(conf, persistent=True) + self.domain.attachDeviceFlags.assert_called_once_with( "", flags=fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG) def test_attach_device_live(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.attach_device(conf, live=True) - - domain.attachDeviceFlags.assert_called_once_with( + self.guest.attach_device(conf, live=True) + self.domain.attachDeviceFlags.assert_called_once_with( "", flags=fakelibvirt.VIR_DOMAIN_AFFECT_LIVE) def test_attach_device_persistent_live(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.attach_device(conf, persistent=True, live=True) - - domain.attachDeviceFlags.assert_called_once_with( + self.guest.attach_device(conf, persistent=True, live=True) + self.domain.attachDeviceFlags.assert_called_once_with( "", flags=(fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG | fakelibvirt.VIR_DOMAIN_AFFECT_LIVE)) def test_detach_device(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.detach_device(conf) - - domain.detachDeviceFlags.assert_called_once_with("", flags=0) + self.guest.detach_device(conf) + self.domain.detachDeviceFlags.assert_called_once_with( + "", flags=0) def test_detach_device_persistent(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.detach_device(conf, persistent=True) - - domain.detachDeviceFlags.assert_called_once_with( + self.guest.detach_device(conf, persistent=True) + self.domain.detachDeviceFlags.assert_called_once_with( "", flags=fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG) def test_detach_device_live(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.detach_device(conf, live=True) - - domain.detachDeviceFlags.assert_called_once_with( + self.guest.detach_device(conf, live=True) + self.domain.detachDeviceFlags.assert_called_once_with( "", flags=fakelibvirt.VIR_DOMAIN_AFFECT_LIVE) def test_detach_device_persistent_live(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) conf = mock.Mock(spec=vconfig.LibvirtConfigGuestDevice) conf.to_xml.return_value = "" - - guest = libvirt_guest.Guest(domain) - guest.detach_device(conf, persistent=True, live=True) - - domain.detachDeviceFlags.assert_called_once_with( + self.guest.detach_device(conf, persistent=True, live=True) + self.domain.detachDeviceFlags.assert_called_once_with( "", flags=(fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG | fakelibvirt.VIR_DOMAIN_AFFECT_LIVE)) def test_get_xml_desc(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.get_xml_desc() - - domain.XMLDesc.assert_called_once_with(flags=0) + self.guest.get_xml_desc() + self.domain.XMLDesc.assert_called_once_with(flags=0) def test_get_xml_desc_dump_inactive(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.get_xml_desc(dump_inactive=True) - - domain.XMLDesc.assert_called_once_with( + self.guest.get_xml_desc(dump_inactive=True) + self.domain.XMLDesc.assert_called_once_with( flags=fakelibvirt.VIR_DOMAIN_XML_INACTIVE) def test_get_xml_desc_dump_sensitive(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.get_xml_desc(dump_sensitive=True) - - domain.XMLDesc.assert_called_once_with( + self.guest.get_xml_desc(dump_sensitive=True) + self.domain.XMLDesc.assert_called_once_with( flags=fakelibvirt.VIR_DOMAIN_XML_SECURE) def test_get_xml_desc_dump_inactive_dump_sensitive(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.get_xml_desc(dump_inactive=True, dump_sensitive=True) - - domain.XMLDesc.assert_called_once_with( + self.guest.get_xml_desc(dump_inactive=True, dump_sensitive=True) + self.domain.XMLDesc.assert_called_once_with( flags=(fakelibvirt.VIR_DOMAIN_XML_INACTIVE | fakelibvirt.VIR_DOMAIN_XML_SECURE)) def test_get_xml_desc_dump_migratable(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.get_xml_desc(dump_migratable=True) - - domain.XMLDesc.assert_called_once_with( + self.guest.get_xml_desc(dump_migratable=True) + self.domain.XMLDesc.assert_called_once_with( flags=fakelibvirt.VIR_DOMAIN_XML_MIGRATABLE) def test_has_persistent_configuration(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) self.assertTrue( - guest.has_persistent_configuration()) - domain.isPersistent.assert_called_once_with() + self.guest.has_persistent_configuration()) + self.domain.isPersistent.assert_called_once_with() def test_save_memory_state(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) - guest.save_memory_state() - - domain.managedSave.assert_called_once_with(0) + self.guest.save_memory_state() + self.domain.managedSave.assert_called_once_with(0) def test_get_block_device(self): - domain = mock.Mock(spec=fakelibvirt.virDomain) - guest = libvirt_guest.Guest(domain) disk = 'vda' - - gblock = guest.get_block_device(disk) + gblock = self.guest.get_block_device(disk) self.assertEqual(disk, gblock._disk) - self.assertEqual(guest, gblock._guest) + self.assertEqual(self.guest, gblock._guest) class GuestBlockTestCase(test.NoDBTestCase):