From 8fa190c0d4ede0acc8ed4a462a5ebb751380143c Mon Sep 17 00:00:00 2001 From: Shuangtai Tian Date: Thu, 13 Mar 2014 14:13:54 +0800 Subject: [PATCH] Use the list when get information from libvirt If the libvirt adds new elements to the domain info struct or others, when use the new libvirt, there will be a valueError (too many values to unpack). When we get information from libvirt, we should use the original variable format. This change is for compatibility with upcoming versions of libvirt which change this signature. Change-Id: I0271a260a53fe8f5fc17b2934ebe3a3c9ee0c130 Closes-Bug: #1291805 --- nova/tests/virt/libvirt/test_libvirt.py | 22 ++++++++++++++++++++++ nova/virt/libvirt/driver.py | 24 ++++++++++-------------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/nova/tests/virt/libvirt/test_libvirt.py b/nova/tests/virt/libvirt/test_libvirt.py index 336a0a9ada20..7abadec5791a 100644 --- a/nova/tests/virt/libvirt/test_libvirt.py +++ b/nova/tests/virt/libvirt/test_libvirt.py @@ -6391,6 +6391,28 @@ 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"} + + 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) + def test_create_domain_define_xml_fails(self): """Tests that the xml is logged when defining the domain fails.""" fake_xml = "this is a test" diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 9db1ec7e56cd..e0c04ee0cee5 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -878,8 +878,7 @@ class LibvirtDriver(driver.ComputeDriver): # If the instance is already shut off, we get this: # Code=55 Error=Requested operation is not valid: # domain is not running - (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() - state = LIBVIRT_POWER_STATE[state] + state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] if state == power_state.SHUTDOWN: is_okay = True elif errcode == libvirt.VIR_ERR_OPERATION_TIMEOUT: @@ -1482,8 +1481,7 @@ class LibvirtDriver(driver.ComputeDriver): snapshot_name = uuid.uuid4().hex - (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() - state = LIBVIRT_POWER_STATE[state] + state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] # NOTE(rmk): Live snapshots require QEMU 1.3 and Libvirt 1.0.0. # These restrictions can be relaxed as other configurations @@ -1985,8 +1983,7 @@ class LibvirtDriver(driver.ComputeDriver): :returns: True if the reboot succeeded """ dom = self._lookup_by_name(instance["name"]) - (state, _max_mem, _mem, _cpus, _t) = dom.info() - state = LIBVIRT_POWER_STATE[state] + state = LIBVIRT_POWER_STATE[dom.info()[0]] old_domid = dom.ID() # NOTE(vish): This check allows us to reboot an instance that # is already shutdown. @@ -1999,8 +1996,7 @@ class LibvirtDriver(driver.ComputeDriver): pci_manager.get_instance_pci_devs(instance)) for x in xrange(CONF.libvirt.wait_soft_reboot_seconds): dom = self._lookup_by_name(instance["name"]) - (state, _max_mem, _mem, _cpus, _t) = dom.info() - state = LIBVIRT_POWER_STATE[state] + state = LIBVIRT_POWER_STATE[dom.info()[0]] new_domid = dom.ID() # NOTE(ivoks): By checking domain IDs, we make sure we are @@ -3475,12 +3471,12 @@ class LibvirtDriver(driver.ComputeDriver): """ virt_dom = self._lookup_by_name(instance['name']) - (state, max_mem, mem, num_cpu, cpu_time) = virt_dom.info() - return {'state': LIBVIRT_POWER_STATE[state], - 'max_mem': max_mem, - 'mem': mem, - 'num_cpu': num_cpu, - 'cpu_time': cpu_time, + dom_info = virt_dom.info() + return {'state': LIBVIRT_POWER_STATE[dom_info[0]], + 'max_mem': dom_info[1], + 'mem': dom_info[2], + 'num_cpu': dom_info[3], + 'cpu_time': dom_info[4], 'id': virt_dom.ID()} def _create_domain(self, xml=None, domain=None,