summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nova/tests/unit/virt/libvirt/test_driver.py95
-rw-r--r--nova/virt/libvirt/driver.py21
2 files changed, 77 insertions, 39 deletions
diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py
index 221bd10..1a50618 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
57from six.moves import range 57from six.moves import range
58 58
59from nova.api.metadata import base as instance_metadata 59from nova.api.metadata import base as instance_metadata
60from nova.api.openstack.placement.objects import resource_provider as rp_object
60from nova.compute import manager 61from nova.compute import manager
61from nova.compute import power_state 62from nova.compute import power_state
63from nova.compute import provider_tree
62from nova.compute import task_states 64from nova.compute import task_states
63from nova.compute import vm_states 65from nova.compute import vm_states
64import nova.conf 66import nova.conf
@@ -17225,59 +17227,82 @@ class HostStateTestCase(test.NoDBTestCase):
17225 HostStateTestCase.numa_topology._to_dict())) 17227 HostStateTestCase.numa_topology._to_dict()))
17226 17228
17227 17229
17228class TestGetInventory(test.NoDBTestCase): 17230class TestUpdateProviderTree(test.NoDBTestCase):
17231 vcpus = 24
17232 memory_mb = 1024
17233 disk_gb = 200
17234
17229 def setUp(self): 17235 def setUp(self):
17230 super(TestGetInventory, self).setUp() 17236 super(TestUpdateProviderTree, self).setUp()
17231 self.useFixture(fakelibvirt.FakeLibvirtFixture()) 17237 self.useFixture(fakelibvirt.FakeLibvirtFixture())
17232 self.driver = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True) 17238 self.driver = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
17239 # create compute node resource provider
17240 self.cn_rp = rp_object.ResourceProvider(
17241 uuid=uuids.cn,
17242 name='compute-node',
17243 )
17233 17244
17234 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vgpu_total') 17245 def _pt_with_cn_rp():
17235 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_local_gb_info', 17246 """Create a provider tree instance having compute node resource
17236 return_value={'total': 200}) 17247 provider.
17237 @mock.patch('nova.virt.libvirt.host.Host.get_memory_mb_total', 17248 """
17238 return_value=1024) 17249
17239 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vcpu_total', 17250 pt = provider_tree.ProviderTree()
17240 return_value=24) 17251 pt.new_root(self.cn_rp.name, self.cn_rp.uuid, generation=0)
17241 def _test_get_inventory(self, mock_vcpu, mock_mem, mock_disk, mock_vgpus, 17252 return pt
17242 total_vgpus=0): 17253
17243 mock_vgpus.return_value = total_vgpus 17254 self.pt = _pt_with_cn_rp()
17244 expected_inv = { 17255
17256 def _get_inventory(self):
17257 return {
17245 rc_fields.ResourceClass.VCPU: { 17258 rc_fields.ResourceClass.VCPU: {
17246 'total': 24, 17259 'total': self.vcpus,
17247 'min_unit': 1, 17260 'min_unit': 1,
17248 'max_unit': 24, 17261 'max_unit': self.vcpus,
17249 'step_size': 1, 17262 'step_size': 1,
17250 }, 17263 },
17251 rc_fields.ResourceClass.MEMORY_MB: { 17264 rc_fields.ResourceClass.MEMORY_MB: {
17252 'total': 1024, 17265 'total': self.memory_mb,
17253 'min_unit': 1, 17266 'min_unit': 1,
17254 'max_unit': 1024, 17267 'max_unit': self.memory_mb,
17255 'step_size': 1, 17268 'step_size': 1,
17256 }, 17269 },
17257 rc_fields.ResourceClass.DISK_GB: { 17270 rc_fields.ResourceClass.DISK_GB: {
17258 'total': 200, 17271 'total': self.disk_gb,
17259 'min_unit': 1, 17272 'min_unit': 1,
17260 'max_unit': 200, 17273 'max_unit': self.disk_gb,
17261 'step_size': 1, 17274 'step_size': 1,
17262 }, 17275 },
17263 } 17276 }
17264 if total_vgpus > 0:
17265 expected_inv.update({
17266 rc_fields.ResourceClass.VGPU: {
17267 'total': total_vgpus,
17268 'min_unit': 1,
17269 'max_unit': total_vgpus,
17270 'step_size': 1,
17271 }
17272 })
17273 inv = self.driver.get_inventory(mock.sentinel.nodename)
17274 self.assertEqual(expected_inv, inv)
17275 17277
17276 def test_get_inventory(self): 17278 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vgpu_total')
17277 self._test_get_inventory() 17279 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_local_gb_info',
17278 17280 return_value={'total': disk_gb})
17279 def test_get_inventory_with_vgpus(self): 17281 @mock.patch('nova.virt.libvirt.host.Host.get_memory_mb_total',
17280 self._test_get_inventory(total_vgpus=8) 17282 return_value=memory_mb)
17283 @mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_vcpu_total',
17284 return_value=vcpus)
17285 def _test_update_provider_tree(self, mock_vcpu, mock_mem, mock_disk,
17286 mock_vgpus, total_vgpus=0):
17287 mock_vgpus.return_value = total_vgpus
17288 self.driver.update_provider_tree(self.pt,
17289 self.cn_rp.name)
17290
17291 def test_update_provider_tree(self):
17292 self._test_update_provider_tree()
17293 self.assertEqual(self._get_inventory(),
17294 (self.pt.data(self.cn_rp.uuid)).inventory)
17295
17296 def test_update_provider_tree_with_vgpus(self):
17297 self._test_update_provider_tree(total_vgpus=8)
17298 inventory = self._get_inventory()
17299 # Add VGPU in the expected inventory
17300 inventory[rc_fields.ResourceClass.VGPU] = {'step_size': 1,
17301 'min_unit': 1,
17302 'max_unit': 8,
17303 'total': 8}
17304 self.assertEqual(inventory,
17305 (self.pt.data(self.cn_rp.uuid)).inventory)
17281 17306
17282 17307
17283class LibvirtDriverTestCase(test.NoDBTestCase): 17308class LibvirtDriverTestCase(test.NoDBTestCase):
diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py
index 2871948..4e3de84 100644
--- a/nova/virt/libvirt/driver.py
+++ b/nova/virt/libvirt/driver.py
@@ -6299,9 +6299,22 @@ class LibvirtDriver(driver.ComputeDriver):
6299 def refresh_instance_security_rules(self, instance): 6299 def refresh_instance_security_rules(self, instance):
6300 self.firewall_driver.refresh_instance_security_rules(instance) 6300 self.firewall_driver.refresh_instance_security_rules(instance)
6301 6301
6302 def get_inventory(self, nodename): 6302 def update_provider_tree(self, provider_tree, nodename):
6303 """Return a dict, keyed by resource class, of inventory information for 6303 """Update a ProviderTree object with current resource provider and
6304 the supplied node. 6304 inventory information.
6305
6306 :param nova.compute.provider_tree.ProviderTree provider_tree:
6307 A nova.compute.provider_tree.ProviderTree object representing all
6308 the providers in the tree associated with the compute node, and any
6309 sharing providers (those with the ``MISC_SHARES_VIA_AGGREGATE``
6310 trait) associated via aggregate with any of those providers (but
6311 not *their* tree- or aggregate-associated providers), as currently
6312 known by placement.
6313
6314 :param nodename:
6315 String name of the compute node (i.e.
6316 ComputeNode.hypervisor_hostname) for which the caller is requesting
6317 updated provider information.
6305 """ 6318 """
6306 disk_gb = int(self._get_local_gb_info()['total']) 6319 disk_gb = int(self._get_local_gb_info()['total'])
6307 memory_mb = int(self._host.get_memory_mb_total()) 6320 memory_mb = int(self._host.get_memory_mb_total())
@@ -6353,7 +6366,7 @@ class LibvirtDriver(driver.ComputeDriver):
6353 'step_size': 1, 6366 'step_size': 1,
6354 } 6367 }
6355 6368
6356 return result 6369 provider_tree.update_inventory(nodename, result)
6357 6370
6358 def get_available_resource(self, nodename): 6371 def get_available_resource(self, nodename):
6359 """Retrieve resource information. 6372 """Retrieve resource information.