Browse Source

segments: Fix resource provider inventories update

Related-Bug: #1828543
Depends-On: https://review.opendev.org/663978
Change-Id: I4275779bd8d353fbaa80c646515819b0a34edebb
tags/15.0.0.0b1
elajkat 1 year ago
parent
commit
95023227b7
2 changed files with 34 additions and 26 deletions
  1. +9
    -6
      neutron/services/segments/plugin.py
  2. +25
    -20
      neutron/tests/unit/extensions/test_segment.py

+ 9
- 6
neutron/services/segments/plugin.py View File

@@ -217,9 +217,10 @@ class NovaSegmentNotifier(object):
ipv4_inventory = self.p_client.get_inventory(event.segment_id,
IPV4_RESOURCE_CLASS)
if event.total:
ipv4_inventory['total'] += event.total
ipv4_inventory[IPV4_RESOURCE_CLASS]['total'] += event.total
if event.reserved:
ipv4_inventory['reserved'] += event.reserved
ipv4_inventory[IPV4_RESOURCE_CLASS]['reserved'] += \
event.reserved
try:
self.p_client.update_resource_provider_inventory(
event.segment_id, ipv4_inventory, IPV4_RESOURCE_CLASS)
@@ -251,10 +252,12 @@ class NovaSegmentNotifier(object):
self.p_client.associate_aggregates(segment_id, [aggregate_uuid])
for mapping in segment_host_mappings:
self.n_client.aggregates.add_host(aggregate.id, mapping.host)
ipv4_inventory = {'total': total, 'reserved': reserved,
'min_unit': 1, 'max_unit': 1, 'step_size': 1,
'allocation_ratio': 1.0,
'resource_class': IPV4_RESOURCE_CLASS}
ipv4_inventory = {
IPV4_RESOURCE_CLASS: {
'total': total, 'reserved': reserved, 'min_unit': 1,
'max_unit': 1, 'step_size': 1, 'allocation_ratio': 1.0,
}
}
self.p_client.update_resource_provider_inventories(
segment_id, ipv4_inventory)



+ 25
- 20
neutron/tests/unit/extensions/test_segment.py View File

@@ -1714,16 +1714,17 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):
total, reserved = self._calculate_inventory_total_and_reserved(
subnet['subnet'])
inventory, _ = self._get_inventory(total, reserved)
inventory_dict = inventory[seg_plugin.IPV4_RESOURCE_CLASS]
self.mock_p_client.update_resource_provider_inventories.\
assert_called_with(segment_id, inventory)
self.assertEqual(
inventory['total'],
inventory_dict['total'],
self.mock_p_client.update_resource_provider_inventories.
call_args[0][1]['total'])
call_args[0][1][seg_plugin.IPV4_RESOURCE_CLASS]['total'])
self.assertEqual(
inventory['reserved'],
inventory_dict['reserved'],
self.mock_p_client.update_resource_provider_inventories.
call_args[0][1]['reserved'])
call_args[0][1][seg_plugin.IPV4_RESOURCE_CLASS]['reserved'])
self.mock_p_client.reset_mock()
self.mock_p_client.get_inventory.side_effect = None
self.mock_n_client.reset_mock()
@@ -1788,26 +1789,29 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):
if subnet:
total, reserved = self._calculate_inventory_total_and_reserved(
subnet)
inventory['total'] += total - original_total
inventory['reserved'] += reserved - original_reserved
ipv4_classname = seg_plugin.IPV4_RESOURCE_CLASS
inventory[ipv4_classname]['total'] += total - original_total
inventory[ipv4_classname]['reserved'] += reserved - original_reserved
self.mock_p_client.update_resource_provider_inventory.\
assert_called_with(segment_id, inventory,
seg_plugin.IPV4_RESOURCE_CLASS)
self.assertEqual(
inventory['total'],
inventory[ipv4_classname]['total'],
self.mock_p_client.update_resource_provider_inventory.
call_args[0][1]['total'])
call_args[0][1][ipv4_classname]['total'])
self.assertEqual(
inventory['reserved'],
inventory[ipv4_classname]['reserved'],
self.mock_p_client.update_resource_provider_inventory.
call_args[0][1]['reserved'])
call_args[0][1][ipv4_classname]['reserved'])
self.mock_p_client.reset_mock()
self.mock_n_client.reset_mock()

def _get_inventory(self, total, reserved):
inventory = {'total': total, 'reserved': reserved, 'min_unit': 1,
'max_unit': 1, 'step_size': 1, 'allocation_ratio': 1.0,
'resource_class': seg_plugin.IPV4_RESOURCE_CLASS}
inventory = {
seg_plugin.IPV4_RESOURCE_CLASS: {
'total': total, 'reserved': reserved, 'min_unit': 1,
'max_unit': 1, 'step_size': 1, 'allocation_ratio': 1.0
}}
return inventory, copy.deepcopy(inventory)

def _test_second_subnet_association_with_segment(self):
@@ -2046,20 +2050,20 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):

def _assert_inventory_update_port(self, segment_id, inventory,
num_fixed_ips):
inventory['reserved'] += num_fixed_ips
inventory[seg_plugin.IPV4_RESOURCE_CLASS]['reserved'] += num_fixed_ips
self.mock_p_client.get_inventory.assert_called_with(
segment_id, seg_plugin.IPV4_RESOURCE_CLASS)
self.mock_p_client.update_resource_provider_inventory.\
assert_called_with(segment_id, inventory,
seg_plugin.IPV4_RESOURCE_CLASS)
self.assertEqual(
inventory['total'],
inventory[seg_plugin.IPV4_RESOURCE_CLASS]['total'],
self.mock_p_client.update_resource_provider_inventory.
call_args[0][1]['total'])
call_args[0][1][seg_plugin.IPV4_RESOURCE_CLASS]['total'])
self.assertEqual(
inventory['reserved'],
inventory[seg_plugin.IPV4_RESOURCE_CLASS]['reserved'],
self.mock_p_client.update_resource_provider_inventory.
call_args[0][1]['reserved'])
call_args[0][1][seg_plugin.IPV4_RESOURCE_CLASS]['reserved'])
self.mock_p_client.reset_mock()
self.mock_n_client.reset_mock()

@@ -2113,9 +2117,10 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):
segment_id, before_create_inventory, port = self._test_create_port(
**kwargs)
self.mock_p_client.reset_mock()
ipv4_classname = seg_plugin.IPV4_RESOURCE_CLASS
inventory, original_inventory = self._get_inventory(
before_create_inventory['total'],
before_create_inventory['reserved'] + 1)
before_create_inventory[ipv4_classname]['total'],
before_create_inventory[ipv4_classname]['reserved'] + 1)
self.mock_p_client.get_inventory.return_value = inventory
self._delete('ports', port['port']['id'])
self.batch_notifier._notify()


Loading…
Cancel
Save