remove rc from allocation dict if value is 0

update_qos_minbw_allocation now deletes the resource class keys if the
value for the give resource class is zero, as placement can't handle 0
value updates, see the referenced story.

Change-Id: Iaf23eb313507139c2de76146b82d148bcce9b3d6
Closes-Bug: #1894825
Related-Bug: #1882804
Story: 2008111
(cherry picked from commit a61f9553d2)
This commit is contained in:
elajkat 2020-09-08 13:08:45 +02:00 committed by Lajos Katona
parent a175451e0e
commit 73d868b077
2 changed files with 49 additions and 17 deletions

View File

@ -768,7 +768,16 @@ class PlacementAPIClient(object):
for drctn, min_kbps_diff in minbw_alloc_diff.items():
orig_kbps = body['allocations'][rp_uuid]['resources'][drctn]
new_kbps = orig_kbps + min_kbps_diff
body['allocations'][rp_uuid]['resources'][drctn] = new_kbps
if new_kbps > 0:
body['allocations'][rp_uuid]['resources'][drctn] = new_kbps
else:
# Remove the resource class if the new value for min_kbps
# is 0
resources = body['allocations'][rp_uuid]['resources']
if len(resources) > 1:
resources.pop(drctn, None)
else:
body['allocations'].pop(rp_uuid)
try:
# Update allocations has no return body, but leave the loop
return self.update_allocation(consumer_uuid, body)

View File

@ -727,15 +727,18 @@ class TestPlacementAPIClient(base.BaseTestCase):
}}
)
def test_update_qos_minbw_allocation(self):
def _get_allocation_response(self, resources):
mock_rsp_get = mock.Mock()
mock_rsp_get.json = lambda: {
'allocations': {
RESOURCE_PROVIDER_UUID: {
'resources': {'a': 3, 'b': 2}
}
RESOURCE_PROVIDER_UUID: resources
}
}
return mock_rsp_get
def test_update_qos_minbw_allocation(self):
mock_rsp_get = self._get_allocation_response(
{'resources': {'a': 3, 'b': 2}})
self.placement_fixture.mock_get.side_effect = [mock_rsp_get]
self.placement_api_client.update_qos_minbw_allocation(
consumer_uuid=CONSUMER_UUID,
@ -775,10 +778,7 @@ class TestPlacementAPIClient(base.BaseTestCase):
)
def test_update_qos_minbw_allocation_max_retries(self):
mock_rsp_get = mock.Mock()
mock_rsp_get.json = lambda: {
'allocations': {RESOURCE_PROVIDER_UUID: {'c': 3}}
}
mock_rsp_get = self._get_allocation_response({'c': 3})
self.placement_fixture.mock_get.side_effect = 10 * [mock_rsp_get]
mock_rsp_put = mock.Mock()
mock_rsp_put.json = lambda: {
@ -795,10 +795,7 @@ class TestPlacementAPIClient(base.BaseTestCase):
self.assertEqual(10, self.placement_fixture.mock_put.call_count)
def test_update_qos_minbwallocation_generation_conflict_solved(self):
mock_rsp_get = mock.Mock()
mock_rsp_get.json = lambda: {
'allocations': {RESOURCE_PROVIDER_UUID: {'c': 3}}
}
mock_rsp_get = self._get_allocation_response({'c': 3})
self.placement_fixture.mock_get.side_effect = 2 * [mock_rsp_get]
mock_rsp_put = mock.Mock()
mock_rsp_put.json = lambda: {
@ -815,10 +812,7 @@ class TestPlacementAPIClient(base.BaseTestCase):
self.assertEqual(2, self.placement_fixture.mock_put.call_count)
def test_update_qos_minbw_allocation_other_conflict(self):
mock_rsp_get = mock.Mock()
mock_rsp_get.json = lambda: {
'allocations': {RESOURCE_PROVIDER_UUID: {'c': 3}}
}
mock_rsp_get = self._get_allocation_response({'c': 3})
self.placement_fixture.mock_get.side_effect = 10*[mock_rsp_get]
mock_rsp_put = mock.Mock()
mock_rsp_put.text = ''
@ -834,3 +828,32 @@ class TestPlacementAPIClient(base.BaseTestCase):
rp_uuid=RESOURCE_PROVIDER_UUID,
)
self.placement_fixture.mock_put.assert_called_once()
def test_update_qos_minbw_allocation_to_zero(self):
mock_rsp_get = self._get_allocation_response(
{'resources': {'a': 3, 'b': 2}})
self.placement_fixture.mock_get.side_effect = [mock_rsp_get]
self.placement_api_client.update_qos_minbw_allocation(
consumer_uuid=CONSUMER_UUID,
minbw_alloc_diff={'a': -3, 'b': -2},
rp_uuid=RESOURCE_PROVIDER_UUID
)
self.placement_fixture.mock_put.assert_called_once_with(
'/allocations/%s' % CONSUMER_UUID,
{'allocations': {}})
def test_update_qos_minbw_allocation_one_class_to_zero(self):
mock_rsp_get = self._get_allocation_response(
{'resources': {'a': 3, 'b': 2}})
self.placement_fixture.mock_get.side_effect = [mock_rsp_get]
self.placement_api_client.update_qos_minbw_allocation(
consumer_uuid=CONSUMER_UUID,
minbw_alloc_diff={'a': -3, 'b': 1},
rp_uuid=RESOURCE_PROVIDER_UUID
)
self.placement_fixture.mock_put.assert_called_once_with(
'/allocations/%s' % CONSUMER_UUID,
{'allocations': {
RESOURCE_PROVIDER_UUID: {
'resources': {'b': 3}}
}})