Do not post allocations that are zero

When posting allocations for VMs with a non-local disk, the code was
sending an allocation of DISK_GB = 0, which violated the min_unit
resource constraint. Since it is pointless to enter an allocation of
zero units, this patch removes any items in the generated allocations
dict if the value is zero.

Closes-Bug: #1647316

Change-Id: I59eff4310e67ceb74086f5ade1a637f8cccec7ed
This commit is contained in:
EdLeafe 2016-12-05 16:15:53 +00:00
parent af87734011
commit 200666ad19
2 changed files with 20 additions and 1 deletions

View File

@ -114,11 +114,13 @@ def _instance_to_allocations_dict(instance):
disk = ((0 if is_bfv else instance.flavor.root_gb) +
instance.flavor.swap +
instance.flavor.ephemeral_gb)
return {
alloc_dict = {
MEMORY_MB: instance.flavor.memory_mb,
VCPU: instance.flavor.vcpus,
DISK_GB: disk,
}
# Remove any zero allocations.
return {key: val for key, val in alloc_dict.items() if val}
def _extract_inventory_in_use(body):

View File

@ -928,6 +928,23 @@ class TestAllocations(SchedulerReportClientTestCase):
}
self.assertEqual(expected, result)
@mock.patch('nova.compute.utils.is_volume_backed_instance')
def test_instance_to_allocations_dict_zero_disk(self, mock_vbi):
mock_vbi.return_value = True
inst = objects.Instance(
uuid=uuids.inst,
flavor=objects.Flavor(root_gb=10,
swap=0,
ephemeral_gb=0,
memory_mb=1024,
vcpus=2))
result = report._instance_to_allocations_dict(inst)
expected = {
'MEMORY_MB': 1024,
'VCPU': 2,
}
self.assertEqual(expected, result)
@mock.patch('nova.scheduler.client.report.SchedulerReportClient.'
'put')
@mock.patch('nova.scheduler.client.report.SchedulerReportClient.'