diff --git a/config_tempest/services/network.py b/config_tempest/services/network.py index 4cb74f47..2be8a04f 100644 --- a/config_tempest/services/network.py +++ b/config_tempest/services/network.py @@ -65,20 +65,29 @@ class NetworkService(VersionedService): ''.format(self._public_network_id)) def _discover_network(self): - LOG.info("No network supplied, trying auto discover for network") + LOG.info("No network supplied, trying auto discover for an external " + "network while prioritizing the one called public, if not " + "found, the network discovered last will be used.") network_list = self.client.list_networks() + for network in network_list['networks']: if network['router:external'] and network['subnets']: - LOG.info("Found network, using: %s", network['id']) + if network['status'] != 'ACTIVE': + continue self._public_network_id = network['id'] self._public_network_name = network['name'] - break - + # usually the external network we use is called 'public' + if network['name'] == 'public': + # we found a network called public, end the loop + break # Couldn't find an existing external network - else: + if not self._public_network_name: LOG.error("No external networks found. " "Please note that any test that relies on external " "connectivity would most likely fail.") + return + LOG.info("Setting %s as the public network for tempest", + self._public_network_id) @staticmethod def get_service_type(): diff --git a/config_tempest/tests/services/test_network.py b/config_tempest/tests/services/test_network.py index f21af89e..d2d85bc6 100644 --- a/config_tempest/tests/services/test_network.py +++ b/config_tempest/tests/services/test_network.py @@ -35,6 +35,18 @@ class TestNetworkService(BaseServiceTest): 'label': 'my_fake_label', 'name': 'tempest-network', 'admin_state_up': True, + }, { + 'provider:physical_network': None, + 'id': 'c034f7f8-b860-4ffd', + 'router:external': True, + 'availability_zone_hints': [], + 'availability_zones': [], + 'ipv4_address_scope': None, + 'status': 'ACTIVE', + 'subnets': ['fake_subnet 2'], + 'label': 'public_fake_label', + 'name': 'public', + 'admin_state_up': True, }] } @@ -70,15 +82,18 @@ class TestNetworkService(BaseServiceTest): return_mock = mock.Mock(return_value=self.FAKE_NETWORK_LIST) self.Service.client.list_networks = return_mock self.Service._discover_network() - self.assertEqual(self.Service._public_network_id, '1ea533d7-4c65-4f25') - self.assertEqual(self.Service._public_network_name, 'tempest-network') + self.assertEqual(self.Service._public_network_id, + 'c034f7f8-b860-4ffd') + self.assertEqual(self.Service._public_network_name, 'public') @mock.patch('config_tempest.services.network.LOG') def test_create_network_auto_discover_not_found(self, mock_logging): - # delete subnets => network will not be found + # delete subnets, set DOWN status => networks will not be found self.FAKE_NETWORK_LIST['networks'][0]['subnets'] = [] + self.FAKE_NETWORK_LIST['networks'][1]['status'] = 'DOWN' return_mock = mock.Mock(return_value=self.FAKE_NETWORK_LIST) self.Service.client.list_networks = return_mock + self.Service._public_network_name = None self.Service._discover_network() # check if LOG.error was called self.assertTrue(mock_logging.error.called) diff --git a/releasenotes/notes/prioritize-external-network-named-public-fe84c304e536177e.yaml b/releasenotes/notes/prioritize-external-network-named-public-fe84c304e536177e.yaml new file mode 100644 index 00000000..34ca899d --- /dev/null +++ b/releasenotes/notes/prioritize-external-network-named-public-fe84c304e536177e.yaml @@ -0,0 +1,9 @@ +--- +fixes: + - | + python-tempestconf will prioritize the external network named public when + auto-discovering an external network for tempest. The public network is + the network we end up using in most of the cases. This will help avoid + potential conflicts with other available external networks that are not + meant to be used by tempest. If no external network with name public is + found, the network discovered last will be used.