From 701214481ff1df13b188d7826b44a1c8b51227ea Mon Sep 17 00:00:00 2001 From: Victor Morales Date: Wed, 25 Aug 2021 18:02:53 -0700 Subject: [PATCH] Use more pythonic approach Some functional and unit tests uses range and len built-in functions to iterate over items. Those can be replaced by enumerate which makes the code more readable. Change-Id: Icb507681578dbb50128aaf017bd598d98c26a6d4 --- neutron/tests/functional/agent/linux/test_tc_lib.py | 12 ++++++------ .../scheduler/test_dhcp_agent_scheduler.py | 4 ++-- neutron/tests/unit/extensions/test_segment.py | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/neutron/tests/functional/agent/linux/test_tc_lib.py b/neutron/tests/functional/agent/linux/test_tc_lib.py index b4ce33c1471..72aa4030738 100644 --- a/neutron/tests/functional/agent/linux/test_tc_lib.py +++ b/neutron/tests/functional/agent/linux/test_tc_lib.py @@ -202,18 +202,18 @@ class TcFiltersTestCase(functional_base.BaseSudoTestCase): self.mac_vxlan = [] self.ip = ['10.100.0.1/24', '10.100.0.2/24'] self.ip_vxlan = ['10.200.0.1/24', '10.200.0.2/24'] - for i in range(len(self.ns)): - priv_ip_lib.create_netns(self.ns[i]) - self.addCleanup(self._remove_ns, self.ns[i]) - ip_wrapper = ip_lib.IPWrapper(self.ns[i]) + for i, ns in enumerate(self.ns): + priv_ip_lib.create_netns(ns) + self.addCleanup(self._remove_ns, ns) + ip_wrapper = ip_lib.IPWrapper(ns) if i == 0: ip_wrapper.add_veth(self.device[0], self.device[1], self.ns[1]) ip_wrapper.add_vxlan(self.device_vxlan[i], self.vxlan_id, dev=self.device[i]) - ip_device = ip_lib.IPDevice(self.device[i], self.ns[i]) + ip_device = ip_lib.IPDevice(self.device[i], ns) ip_device.link.set_up() ip_device.addr.add(self.ip[i]) - ip_device_vxlan = ip_lib.IPDevice(self.device_vxlan[i], self.ns[i]) + ip_device_vxlan = ip_lib.IPDevice(self.device_vxlan[i], ns) self.mac_vxlan.append(ip_device_vxlan.link.address) ip_device_vxlan.link.set_up() ip_device_vxlan.addr.add(self.ip_vxlan[i]) diff --git a/neutron/tests/functional/scheduler/test_dhcp_agent_scheduler.py b/neutron/tests/functional/scheduler/test_dhcp_agent_scheduler.py index 0e410140b5d..a07fd0ee8ff 100644 --- a/neutron/tests/functional/scheduler/test_dhcp_agent_scheduler.py +++ b/neutron/tests/functional/scheduler/test_dhcp_agent_scheduler.py @@ -382,8 +382,8 @@ class TestAutoSchedule(test_dhcp_sch.TestDhcpSchedulerBaseTestCase, name='network-%s' % i) for i in range(self.network_count) ] - for i in range(len(self._networks)): - self._networks[i].create() + for net in self._networks: + net.create() network_ids = [net.id for net in self._networks] # pre schedule the networks to the agents defined in diff --git a/neutron/tests/unit/extensions/test_segment.py b/neutron/tests/unit/extensions/test_segment.py index 4ed6d229ec0..c9c4aaddb85 100644 --- a/neutron/tests/unit/extensions/test_segment.py +++ b/neutron/tests/unit/extensions/test_segment.py @@ -769,12 +769,12 @@ class TestMl2HostSegmentMappingOVS(HostSegmentMappingTestCase): physical_networks = ['phys_net1', 'phys_net2'] networks = [] segments = [] - for i in range(len(physical_networks)): + for i, phy_net in enumerate(physical_networks): with self.network() as network: networks.append(network['network']) segments.append(self._test_create_segment( network_id=networks[i]['id'], - physical_network=physical_networks[i], + physical_network=phy_net, segmentation_id=200, network_type=constants.TYPE_VLAN)['segment']) self._register_agent(host, mappings={physical_networks[0]: 'br-eth-1',