Browse Source

Don't specify exactly IP in portforwarding functional test

In Port Forwarding functional tests, port is created in setUp()
method. This port had specified exactly IP address and subnet to use.
This was proposed in [1] to fix issue when new port created in same
subnet got sometimes IP address outside of subnet range.
Unfortunatelly that fix was not enough and caused other issues
as sometimes this "fixed" IP address used for port might be
used e.g. by router_interface port and that caused sometimes
"409 conflict" response from Neutron sometimes.

This patch adds additional method in
neutron.tests.unit.db.test_db_base_plugin_v2 module to find first
free IP address from given subnet.
This method is now used in port forwarding tests module to choose
IP address for second created port in the subnet so there should be
no conflicts with IP addresses anymore.

[1] https://review.openstack.org/#/c/631815

Change-Id: Iee2f72acf6814a0f8f76d240862429a8b63c3b09
Closes-bug: #1813540
tags/14.0.0.0b2
Slawek Kaplonski 8 months ago
parent
commit
414bdd4c59

+ 2
- 13
neutron/tests/functional/services/portforwarding/test_port_forwarding.py View File

@@ -13,7 +13,6 @@
13 13
 import threading
14 14
 
15 15
 import mock
16
-import netaddr
17 16
 from neutron_lib.api.definitions import floating_ip_port_forwarding as apidef
18 17
 from neutron_lib.callbacks import exceptions as c_exc
19 18
 from neutron_lib import exceptions as lib_exc
@@ -99,15 +98,9 @@ class PortForwardingTestCase(PortForwardingTestCaseBase):
99 98
         self._set_router_gw(self.router['id'], self.ext_net['id'])
100 99
         self._add_router_interface(self.router['id'], self.subnet['id'])
101 100
         self.fip = self._create_floatingip(self.ext_net['id'])
102
-        # We choose an IP address in the middle of the subnet so
103
-        # tests that update the IP in the port,
104
-        # like test_concurrent_create_port_forwarding_update_port(),
105
-        # don't accidentally choose an invalid IP address in
106
-        # the subnet, like the broadcast address.
107 101
         self.port = self._create_port(
108 102
             self.fmt, self.net['id'],
109
-            fixed_ips=[{'subnet_id': self.subnet['id'],
110
-                        'ip_address': '10.0.0.100'}]).json['port']
103
+            fixed_ips=[{'subnet_id': self.subnet['id']}]).json['port']
111 104
         self.port_forwarding = {
112 105
             apidef.RESOURCE_NAME:
113 106
                 {apidef.EXTERNAL_PORT: 2225,
@@ -449,11 +442,7 @@ class PortForwardingTestCase(PortForwardingTestCaseBase):
449 442
                           funcs, args_list)
450 443
 
451 444
     def test_concurrent_create_port_forwarding_update_port(self):
452
-        # The initial IP address of the port is in the middle of the
453
-        # subnet range, so adding 2 to it should also produce a
454
-        # valid IP address.
455
-        new_ip = str(
456
-            netaddr.IPAddress(self.port['fixed_ips'][0]['ip_address']) + 2)
445
+        new_ip = self._find_ip_address(self.subnet)
457 446
         funcs = [self.pf_plugin.create_floatingip_port_forwarding,
458 447
                  self._update_port]
459 448
         args_list = [(self.context, self.fip['id'], self.port_forwarding),

+ 15
- 0
neutron/tests/unit/db/test_db_base_plugin_v2.py View File

@@ -289,6 +289,21 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
289 289
         data = self._deserializers[ctype].deserialize(response.body)['body']
290 290
         return data
291 291
 
292
+    def _find_ip_address(self, subnet):
293
+        network_ports = self._list_ports(
294
+            "json", 200, subnet['network_id']).json['ports']
295
+        used_ips = set()
296
+        for port in network_ports:
297
+            for ip in port['fixed_ips']:
298
+                if ip['subnet_id'] == subnet['id']:
299
+                    used_ips.add(ip['ip_address'])
300
+
301
+        for pool in subnet['allocation_pools']:
302
+            ips_range = netaddr.IPRange(pool['start'], pool['end'])
303
+            for ip in ips_range:
304
+                if ip not in used_ips:
305
+                    return str(ip)
306
+
292 307
     def _create_bulk_from_list(self, fmt, resource, objects, **kwargs):
293 308
         """Creates a bulk request from a list of objects."""
294 309
         collection = "%ss" % resource

Loading…
Cancel
Save