From 493fbf719c982846250c5006c0e033625fbace1a Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Wed, 11 Apr 2018 09:29:50 -0500 Subject: [PATCH] libvirt: get_inventory => update_provider_tree Port the libvirt compute driver's get_inventory method to update_provider_tree. Change-Id: I60810aeebc07bae7195ea9433d923c479505f6ed --- nova/tests/unit/virt/libvirt/test_driver.py | 93 +++++++++++++-------- nova/virt/libvirt/driver.py | 21 ++++- 2 files changed, 76 insertions(+), 38 deletions(-) diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py index c29d3ba3502c..2a2ac11e06e5 100644 --- a/nova/tests/unit/virt/libvirt/test_driver.py +++ b/nova/tests/unit/virt/libvirt/test_driver.py @@ -57,8 +57,10 @@ from six.moves import builtins from six.moves import range from nova.api.metadata import base as instance_metadata +from nova.api.openstack.placement.objects import resource_provider as rp_object from nova.compute import manager from nova.compute import power_state +from nova.compute import provider_tree from nova.compute import task_states from nova.compute import vm_states import nova.conf @@ -17225,59 +17227,82 @@ class HostStateTestCase(test.NoDBTestCase): HostStateTestCase.numa_topology._to_dict())) -class TestGetInventory(test.NoDBTestCase): +class TestUpdateProviderTree(test.NoDBTestCase): + vcpus = 24 + memory_mb = 1024 + disk_gb = 200 + def setUp(self): - super(TestGetInventory, self).setUp() + super(TestUpdateProviderTree, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.driver = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True) + # create compute node resource provider + self.cn_rp = rp_object.ResourceProvider( + uuid=uuids.cn, + name='compute-node', + ) - @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vgpu_total') - @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_local_gb_info', - return_value={'total': 200}) - @mock.patch('nova.virt.libvirt.host.Host.get_memory_mb_total', - return_value=1024) - @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vcpu_total', - return_value=24) - def _test_get_inventory(self, mock_vcpu, mock_mem, mock_disk, mock_vgpus, - total_vgpus=0): - mock_vgpus.return_value = total_vgpus - expected_inv = { + def _pt_with_cn_rp(): + """Create a provider tree instance having compute node resource + provider. + """ + + pt = provider_tree.ProviderTree() + pt.new_root(self.cn_rp.name, self.cn_rp.uuid, generation=0) + return pt + + self.pt = _pt_with_cn_rp() + + def _get_inventory(self): + return { rc_fields.ResourceClass.VCPU: { - 'total': 24, + 'total': self.vcpus, 'min_unit': 1, - 'max_unit': 24, + 'max_unit': self.vcpus, 'step_size': 1, }, rc_fields.ResourceClass.MEMORY_MB: { - 'total': 1024, + 'total': self.memory_mb, 'min_unit': 1, - 'max_unit': 1024, + 'max_unit': self.memory_mb, 'step_size': 1, }, rc_fields.ResourceClass.DISK_GB: { - 'total': 200, + 'total': self.disk_gb, 'min_unit': 1, - 'max_unit': 200, + 'max_unit': self.disk_gb, 'step_size': 1, }, } - if total_vgpus > 0: - expected_inv.update({ - rc_fields.ResourceClass.VGPU: { - 'total': total_vgpus, - 'min_unit': 1, - 'max_unit': total_vgpus, - 'step_size': 1, - } - }) - inv = self.driver.get_inventory(mock.sentinel.nodename) - self.assertEqual(expected_inv, inv) - def test_get_inventory(self): - self._test_get_inventory() + @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vgpu_total') + @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_local_gb_info', + return_value={'total': disk_gb}) + @mock.patch('nova.virt.libvirt.host.Host.get_memory_mb_total', + return_value=memory_mb) + @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vcpu_total', + return_value=vcpus) + def _test_update_provider_tree(self, mock_vcpu, mock_mem, mock_disk, + mock_vgpus, total_vgpus=0): + mock_vgpus.return_value = total_vgpus + self.driver.update_provider_tree(self.pt, + self.cn_rp.name) - def test_get_inventory_with_vgpus(self): - self._test_get_inventory(total_vgpus=8) + def test_update_provider_tree(self): + self._test_update_provider_tree() + self.assertEqual(self._get_inventory(), + (self.pt.data(self.cn_rp.uuid)).inventory) + + def test_update_provider_tree_with_vgpus(self): + self._test_update_provider_tree(total_vgpus=8) + inventory = self._get_inventory() + # Add VGPU in the expected inventory + inventory[rc_fields.ResourceClass.VGPU] = {'step_size': 1, + 'min_unit': 1, + 'max_unit': 8, + 'total': 8} + self.assertEqual(inventory, + (self.pt.data(self.cn_rp.uuid)).inventory) class LibvirtDriverTestCase(test.NoDBTestCase): diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 804e328e0232..759de5c0d4f6 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -6293,9 +6293,22 @@ class LibvirtDriver(driver.ComputeDriver): def refresh_instance_security_rules(self, instance): self.firewall_driver.refresh_instance_security_rules(instance) - def get_inventory(self, nodename): - """Return a dict, keyed by resource class, of inventory information for - the supplied node. + def update_provider_tree(self, provider_tree, nodename): + """Update a ProviderTree object with current resource provider and + inventory information. + + :param nova.compute.provider_tree.ProviderTree provider_tree: + A nova.compute.provider_tree.ProviderTree object representing all + the providers in the tree associated with the compute node, and any + sharing providers (those with the ``MISC_SHARES_VIA_AGGREGATE`` + trait) associated via aggregate with any of those providers (but + not *their* tree- or aggregate-associated providers), as currently + known by placement. + + :param nodename: + String name of the compute node (i.e. + ComputeNode.hypervisor_hostname) for which the caller is requesting + updated provider information. """ disk_gb = int(self._get_local_gb_info()['total']) memory_mb = int(self._host.get_memory_mb_total()) @@ -6347,7 +6360,7 @@ class LibvirtDriver(driver.ComputeDriver): 'step_size': 1, } - return result + provider_tree.update_inventory(nodename, result) def get_available_resource(self, nodename): """Retrieve resource information.