Fixes Python 3 compatibility for filter results

Python 2.7 filter method result was a list, while for
Python 3.4 the result is a "filter object". Trying to
use it as a list will result in a TypeError.

Enables some unit tests for gate-nova-python34.

Partially implements blueprint: nova-python3-mitaka

Change-Id: I681b084bc96fc293488ddbc03218b9c449555578
This commit is contained in:
Claudiu Belu 2015-10-08 18:44:04 +03:00
parent 84952379e0
commit 1e447860d1
6 changed files with 47 additions and 62 deletions

View File

@ -105,8 +105,7 @@ class Controller(wsgi.Controller):
if 'server' not in robj.obj:
return robj
link = filter(lambda l: l['rel'] == 'self',
robj.obj['server']['links'])
link = [l for l in robj.obj['server']['links'] if l['rel'] == 'self']
if link:
robj['Location'] = utils.utf8(link[0]['href'])

View File

@ -92,8 +92,7 @@ class ServersController(wsgi.Controller):
if 'server' not in robj.obj:
return robj
link = filter(lambda l: l['rel'] == 'self',
robj.obj['server']['links'])
link = [l for l in robj.obj['server']['links'] if l['rel'] == 'self']
if link:
robj['Location'] = utils.utf8(link[0]['href'])

View File

@ -823,7 +823,7 @@ class API(base.Base):
return not (bdm.get('boot_index') == 0 and
bdm.get('source_type') == 'image')
block_device_mapping = (
block_device_mapping = list(
filter(not_image_and_root_bdm, block_device_mapping))
block_device_mapping = self._merge_bdms_lists(

View File

@ -251,8 +251,8 @@ class IptablesTable(object):
self.remove_chains.add(name)
chain_set.remove(name)
if not wrap:
self.remove_rules += filter(lambda r: r.chain == name, self.rules)
self.rules = filter(lambda r: r.chain != name, self.rules)
self.remove_rules += [r for r in self.rules if r.chain == name]
self.rules = [r for r in self.rules if r.chain != name]
if wrap:
jump_snippet = '-j %s-%s' % (binary_name, name)
@ -260,9 +260,9 @@ class IptablesTable(object):
jump_snippet = '-j %s' % (name,)
if not wrap:
self.remove_rules += filter(lambda r: jump_snippet in r.rule,
self.rules)
self.rules = filter(lambda r: jump_snippet not in r.rule, self.rules)
self.remove_rules += [r for r in self.rules
if jump_snippet in r.rule]
self.rules = [r for r in self.rules if jump_snippet not in r.rule]
def add_rule(self, chain, rule, wrap=True, top=False):
"""Add a rule to the table.
@ -319,7 +319,7 @@ class IptablesTable(object):
if isinstance(regex, six.string_types):
regex = re.compile(regex)
num_rules = len(self.rules)
self.rules = filter(lambda r: not regex.match(str(r)), self.rules)
self.rules = [r for r in self.rules if not regex.match(str(r))]
removed = num_rules - len(self.rules)
if removed > 0:
self.dirty = True
@ -500,26 +500,26 @@ class IptablesManager(object):
current_lines = fake_table
# Remove any trace of our rules
new_filter = filter(lambda line: binary_name not in line,
current_lines)
new_filter = [line for line in current_lines
if binary_name not in line]
top_rules = []
bottom_rules = []
if CONF.iptables_top_regex:
regex = re.compile(CONF.iptables_top_regex)
temp_filter = filter(lambda line: regex.search(line), new_filter)
temp_filter = [line for line in new_filter if regex.search(line)]
for rule_str in temp_filter:
new_filter = filter(lambda s: s.strip() != rule_str.strip(),
new_filter)
new_filter = [s for s in new_filter
if s.strip() != rule_str.strip()]
top_rules = temp_filter
if CONF.iptables_bottom_regex:
regex = re.compile(CONF.iptables_bottom_regex)
temp_filter = filter(lambda line: regex.search(line), new_filter)
temp_filter = [line for line in new_filter if regex.search(line)]
for rule_str in temp_filter:
new_filter = filter(lambda s: s.strip() != rule_str.strip(),
new_filter)
new_filter = [s for s in new_filter
if s.strip() != rule_str.strip()]
bottom_rules = temp_filter
seen_chains = False
@ -552,12 +552,11 @@ class IptablesManager(object):
# ignore [packet:byte] counts at beginning of line
if rule_str.startswith('['):
rule_str = rule_str.split(']', 1)[1]
dup_filter = filter(lambda s: rule_str.strip() in s.strip(),
new_filter)
dup_filter = [s for s in new_filter
if rule_str.strip() in s.strip()]
new_filter = filter(lambda s:
rule_str.strip() not in s.strip(),
new_filter)
new_filter = [s for s in new_filter
if rule_str.strip() not in s.strip()]
# if no duplicates, use original rule
if dup_filter:
# grab the last entry, if there is one
@ -629,9 +628,11 @@ class IptablesManager(object):
# We filter duplicates, letting the *last* occurrence take
# precedence. We also filter out anything in the "remove"
# lists.
new_filter = list(new_filter)
new_filter.reverse()
new_filter = filter(_weed_out_duplicates, new_filter)
new_filter = filter(_weed_out_removes, new_filter)
new_filter = list(new_filter)
new_filter.reverse()
# flush lists, just in case we didn't find something

View File

@ -107,10 +107,8 @@ networks = [network_fields]
def fake_floating_ip_allocate_address(context, project_id, pool,
auto_assigned=False):
ips = filter(lambda i: i['fixed_ip_id'] is None and
i['project_id'] is None and
i['pool'] == pool,
floating_ips)
ips = [i for i in floating_ips if i['fixed_ip_id'] is None and
i['project_id'] is None and i['pool'] == pool]
if not ips:
raise exception.NoMoreFloatingIps()
ips[0]['project_id'] = project_id
@ -119,16 +117,14 @@ def fake_floating_ip_allocate_address(context, project_id, pool,
def fake_floating_ip_deallocate(context, address):
ips = filter(lambda i: i['address'] == address,
floating_ips)
ips = [i for i in floating_ips if i['address'] == address]
if ips:
ips[0]['project_id'] = None
ips[0]['auto_assigned'] = False
def fake_floating_ip_disassociate(context, address):
ips = filter(lambda i: i['address'] == address,
floating_ips)
ips = [i for i in floating_ips if i['address'] == address]
if ips:
fixed_ip_address = None
if ips[0]['fixed_ip']:
@ -140,10 +136,8 @@ def fake_floating_ip_disassociate(context, address):
def fake_floating_ip_fixed_ip_associate(context, floating_address,
fixed_address, host):
float = filter(lambda i: i['address'] == floating_address,
floating_ips)
fixed = filter(lambda i: i['address'] == fixed_address,
fixed_ips)
float = [i for i in floating_ips if i['address'] == floating_address]
fixed = [i for i in fixed_ips if i['address'] == fixed_address]
if float and fixed:
float[0]['fixed_ip'] = fixed[0]
float[0]['fixed_ip_id'] = fixed[0]['id']
@ -161,16 +155,14 @@ def fake_floating_ip_get_by_address(context, address):
if isinstance(address, FakeModel):
# NOTE(tr3buchet): yo dawg, i heard you like addresses
address = address['address']
ips = filter(lambda i: i['address'] == address,
floating_ips)
ips = [i for i in floating_ips if i['address'] == address]
if not ips:
raise exception.FloatingIpNotFoundForAddress(address=address)
return FakeModel(ips[0])
def fake_fixed_ip_associate(context, address, instance_id):
ips = filter(lambda i: i['address'] == address,
fixed_ips)
ips = [i for i in fixed_ips if i['address'] == address]
if not ips:
raise exception.NoMoreFixedIps(net='fake_net')
ips[0]['instance'] = True
@ -178,9 +170,8 @@ def fake_fixed_ip_associate(context, address, instance_id):
def fake_fixed_ip_associate_pool(context, network_id, instance_id):
ips = filter(lambda i: (i['network_id'] == network_id or
i['network_id'] is None) and not i['instance'],
fixed_ips)
ips = [i for i in fixed_ips if not i['instance'] and
(i['network_id'] == network_id or i['network_id'] is None)]
if not ips:
raise exception.NoMoreFixedIps(net=network_id)
ips[0]['instance'] = True
@ -197,8 +188,7 @@ def fake_fixed_ip_create(context, values):
def fake_fixed_ip_disassociate(context, address):
ips = filter(lambda i: i['address'] == address,
fixed_ips)
ips = [i for i in fixed_ips if i['address'] == address]
if ips:
ips[0]['instance_id'] = None
ips[0]['instance'] = None
@ -215,28 +205,25 @@ def fake_fixed_ip_get_all(context):
def fake_fixed_ip_get_by_instance(context, instance_uuid):
ips = filter(lambda i: i['instance_uuid'] == instance_uuid,
fixed_ips)
ips = [i for i in fixed_ips if i['instance_uuid'] == instance_uuid]
return [FakeModel(i) for i in ips]
def fake_fixed_ip_get_by_address(context, address):
ips = filter(lambda i: i['address'] == address,
fixed_ips)
ips = [i for i in fixed_ips if i['address'] == address]
if ips:
return FakeModel(ips[0])
def fake_fixed_ip_update(context, address, values):
ips = filter(lambda i: i['address'] == address,
fixed_ips)
ips = [i for i in fixed_ips if i['address'] == address]
fif = copy.deepcopy(fixed_ip_fields)
if ips:
for key in values:
ips[0][key] = values[key]
if key == 'virtual_interface_id':
vif = filter(lambda x: x['id'] == values[key],
virtual_interfacees)
vif = [v for v in virtual_interfacees
if v['id'] == values[key]]
if not vif:
continue
fif['virtual_interface'] = FakeModel(vif[0])
@ -274,9 +261,8 @@ def fake_virtual_interface_get_by_instance(context, instance_id):
def fake_virtual_interface_get_by_instance_and_network(context,
instance_id,
network_id):
vif = filter(lambda m: m['instance_id'] == instance_id and
m['network_id'] == network_id,
virtual_interfacees)
vif = [v for v in virtual_interfacees if v['instance_id'] == instance_id
and v['network_id'] == network_id]
if not vif:
return None
return FakeModel(vif[0])
@ -291,7 +277,7 @@ def fake_network_create_safe(context, values):
def fake_network_get(context, network_id):
net = filter(lambda n: n['id'] == network_id, networks)
net = [n for n in networks if n['id'] == network_id]
if not net:
return None
return FakeModel(net[0])
@ -302,19 +288,19 @@ def fake_network_get_all(context):
def fake_network_get_all_by_host(context, host):
nets = filter(lambda n: n['host'] == host, networks)
nets = [n for n in networks if n['host'] == host]
return [FakeModel(n) for n in nets]
def fake_network_set_host(context, network_id, host_id):
nets = filter(lambda n: n['id'] == network_id, networks)
nets = [n for n in networks if n['id'] == network_id]
for net in nets:
net['host'] = host_id
return host_id
def fake_network_update(context, network_id, values):
nets = filter(lambda n: n['id'] == network_id, networks)
nets = [n for n in networks if n['id'] == network_id]
for net in nets:
for key in values:
net[key] = values[key]

View File

@ -197,7 +197,8 @@ nova.tests.unit.keymgr.test_key.SymmetricKeyTestCase
nova.tests.unit.keymgr.test_mock_key_mgr.MockKeyManagerTestCase
nova.tests.unit.keymgr.test_single_key_mgr.SingleKeyManagerTestCase
nova.tests.unit.network.test_api.ApiTestCase
nova.tests.unit.network.test_linux_net.LinuxNetworkTestCase
nova.tests.unit.network.test_linux_net.LinuxNetworkTestCase.test_get_dhcp_leases_for_nw00
nova.tests.unit.network.test_linux_net.LinuxNetworkTestCase.test_get_dhcp_leases_for_nw01
nova.tests.unit.network.test_manager.AllocateTestCase
nova.tests.unit.network.test_manager.CommonNetworkTestCase
nova.tests.unit.network.test_manager.FlatDHCPNetworkTestCase
@ -229,7 +230,6 @@ nova.tests.unit.test_metadata.MetadataPasswordTestCase
nova.tests.unit.test_metadata.MetadataTestCase
nova.tests.unit.test_metadata.OpenStackMetadataTestCase
nova.tests.unit.test_nova_manage.CellCommandsTestCase
nova.tests.unit.test_nova_manage.FixedIpCommandsTestCase
nova.tests.unit.test_objectstore.S3APITestCase
nova.tests.unit.test_pipelib.PipelibTest
nova.tests.unit.test_policy.AdminRolePolicyTestCase