From 200666ad19e44fb76aab29ed664767885b259822 Mon Sep 17 00:00:00 2001 From: EdLeafe Date: Mon, 5 Dec 2016 16:15:53 +0000 Subject: [PATCH] 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 --- nova/scheduler/client/report.py | 4 +++- nova/tests/unit/scheduler/client/test_report.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/nova/scheduler/client/report.py b/nova/scheduler/client/report.py index 5acbbd3314ca..be2ac6ae79ed 100644 --- a/nova/scheduler/client/report.py +++ b/nova/scheduler/client/report.py @@ -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): diff --git a/nova/tests/unit/scheduler/client/test_report.py b/nova/tests/unit/scheduler/client/test_report.py index 1c30ee34224a..b60620dbbf0b 100644 --- a/nova/tests/unit/scheduler/client/test_report.py +++ b/nova/tests/unit/scheduler/client/test_report.py @@ -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.'