From db9b47a0c7fc0e470e82f841aca3d54f2a1cae1b Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Thu, 8 Oct 2015 18:05:14 +0300 Subject: [PATCH] Fixes dict keys and items references for Python 3 Python 3 does not support indexing keys or items iterators. Removes unit tests from tests-py3.txt blacklist. Partially implements blueprint: nova-python3-mitaka Change-Id: I3ec1e775d91e7f56356f744fd80f5d9df36cae1d --- nova/api/ec2/__init__.py | 2 +- nova/api/ec2/apirequest.py | 2 +- .../compute/legacy_v2/contrib/hosts.py | 3 ++- .../legacy_v2/contrib/simple_tenant_usage.py | 2 +- .../openstack/compute/simple_tenant_usage.py | 2 +- nova/api/openstack/wsgi.py | 2 +- nova/compute/api.py | 2 +- nova/db/sqlalchemy/api.py | 2 +- nova/network/linux_net.py | 3 ++- nova/objectstore/s3server.py | 4 ++-- .../unit/compute/test_resource_tracker.py | 2 +- nova/tests/unit/test_flavors.py | 2 +- nova/tests/unit/virt/libvirt/fakelibvirt.py | 2 +- nova/tests/unit/virt/vmwareapi/fake.py | 19 +++++++++++-------- .../tests/unit/virt/vmwareapi/test_vm_util.py | 2 +- nova/tests/unit/virt/xenapi/test_xenapi.py | 2 +- nova/virt/xenapi/fake.py | 4 ++-- nova/virt/xenapi/network_utils.py | 2 +- nova/virt/xenapi/vm_utils.py | 6 +++--- nova/virt/xenapi/vmops.py | 4 ++-- tests-py3.txt | 7 +------ 21 files changed, 38 insertions(+), 38 deletions(-) diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index 71f16f763330..2cee009c81dc 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -577,7 +577,7 @@ def ec2_error_ex(ex, req, code=None, message=None, unexpected=False): if unexpected: # Log filtered environment for unexpected errors. env = req.environ.copy() - for k in env.keys(): + for k in list(env.keys()): if not isinstance(env[k], six.string_types): env.pop(k) log_fun(_LE('Environment: %s'), jsonutils.dumps(env)) diff --git a/nova/api/ec2/apirequest.py b/nova/api/ec2/apirequest.py index 3251a590f9df..3fd505e37371 100644 --- a/nova/api/ec2/apirequest.py +++ b/nova/api/ec2/apirequest.py @@ -72,7 +72,7 @@ class APIRequest(object): for key in args.keys(): # NOTE(vish): Turn numeric dict keys into lists if isinstance(args[key], dict): - if args[key] != {} and args[key].keys()[0].isdigit(): + if args[key] != {} and list(args[key].keys())[0].isdigit(): s = args[key].items() s.sort() args[key] = [v for k, v in s] diff --git a/nova/api/openstack/compute/legacy_v2/contrib/hosts.py b/nova/api/openstack/compute/legacy_v2/contrib/hosts.py index e15be317ad0d..39c65279106d 100644 --- a/nova/api/openstack/compute/legacy_v2/contrib/hosts.py +++ b/nova/api/openstack/compute/legacy_v2/contrib/hosts.py @@ -136,7 +136,8 @@ class HostController(object): # Validate the request if len(params) > 0: # Some extra param was passed. Fail. - explanation = _("Invalid update setting: '%s'") % params.keys()[0] + explanation = _("Invalid update setting: '%s'") % list( + params.keys())[0] raise webob.exc.HTTPBadRequest(explanation=explanation) if orig_status is not None: status = read_enabled(orig_status, _("Invalid status: '%s'")) diff --git a/nova/api/openstack/compute/legacy_v2/contrib/simple_tenant_usage.py b/nova/api/openstack/compute/legacy_v2/contrib/simple_tenant_usage.py index a92cb5086773..5a3d9d8d8080 100644 --- a/nova/api/openstack/compute/legacy_v2/contrib/simple_tenant_usage.py +++ b/nova/api/openstack/compute/legacy_v2/contrib/simple_tenant_usage.py @@ -262,7 +262,7 @@ class SimpleTenantUsageController(object): tenant_id=tenant_id, detailed=True) if len(usage): - usage = usage[0] + usage = list(usage)[0] else: usage = {} return {'tenant_usage': usage} diff --git a/nova/api/openstack/compute/simple_tenant_usage.py b/nova/api/openstack/compute/simple_tenant_usage.py index e9a35ee28dfd..69092385e114 100644 --- a/nova/api/openstack/compute/simple_tenant_usage.py +++ b/nova/api/openstack/compute/simple_tenant_usage.py @@ -263,7 +263,7 @@ class SimpleTenantUsageController(wsgi.Controller): tenant_id=tenant_id, detailed=True) if len(usage): - usage = usage[0] + usage = list(usage)[0] else: usage = {} return {'tenant_usage': usage} diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 1c4531cb438c..668333049a59 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -495,7 +495,7 @@ def action_peek_json(body): raise exception.MalformedRequestBody(reason=msg) # Return the action and the decoded body... - return decoded.keys()[0] + return list(decoded.keys())[0] class ResourceExceptionHandler(object): diff --git a/nova/compute/api.py b/nova/compute/api.py index f37b3d7fa6ff..f5eade129d58 100755 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -2443,7 +2443,7 @@ class API(base.Base): orig_sys_metadata = dict(instance.system_metadata) # Remove the old keys - for key in instance.system_metadata.keys(): + for key in list(instance.system_metadata.keys()): if key.startswith(utils.SM_IMAGE_PROP_PREFIX): del instance.system_metadata[key] diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index b21422e8543d..a3b9facd257d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2202,7 +2202,7 @@ def _exact_instance_filter(query, filters, legal_keys): column_attr = getattr(model, key) if isinstance(value, list): for item in value: - for k, v in item.iteritems(): + for k, v in item.items(): query = query.filter(column_attr.any(key=k)) query = query.filter(column_attr.any(value=v)) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 8e3c24277976..22a7d88a53f8 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -561,7 +561,7 @@ class IptablesManager(object): # if no duplicates, use original rule if dup_filter: # grab the last entry, if there is one - dup = dup_filter[-1] + dup = list(dup_filter)[-1] rule_str = str(dup) else: rule_str = str(rule) @@ -573,6 +573,7 @@ class IptablesManager(object): our_rules += bot_rules + new_filter = list(new_filter) new_filter[rules_index:rules_index] = our_rules new_filter[rules_index:rules_index] = [':%s - [0:0]' % (name,) diff --git a/nova/objectstore/s3server.py b/nova/objectstore/s3server.py index e2abfd1fcd63..b0332abbd630 100644 --- a/nova/objectstore/s3server.py +++ b/nova/objectstore/s3server.py @@ -184,11 +184,11 @@ class BaseRequestHandler(object): def render_xml(self, value): assert isinstance(value, dict) and len(value) == 1 self.set_header("Content-Type", "application/xml; charset=UTF-8") - name = value.keys()[0] + name = list(value.keys())[0] parts = [] parts.append('<' + utils.utf8(name) + ' xmlns="http://doc.s3.amazonaws.com/2006-03-01">') - self._render_parts(value.values()[0], parts) + self._render_parts(list(value.values())[0], parts) parts.append('') self.finish('\n' + ''.join(parts)) diff --git a/nova/tests/unit/compute/test_resource_tracker.py b/nova/tests/unit/compute/test_resource_tracker.py index 712ec4181ceb..6413836ed9bb 100644 --- a/nova/tests/unit/compute/test_resource_tracker.py +++ b/nova/tests/unit/compute/test_resource_tracker.py @@ -606,7 +606,7 @@ class BaseTrackerTestCase(BaseTestCase): def _fake_migration_update(self, ctxt, migration_id, values): # cheat and assume there's only 1 migration present - migration = self._migrations.values()[0] + migration = list(self._migrations.values())[0] migration.update(values) return migration diff --git a/nova/tests/unit/test_flavors.py b/nova/tests/unit/test_flavors.py index df4e98cd68a7..a66d7031250f 100644 --- a/nova/tests/unit/test_flavors.py +++ b/nova/tests/unit/test_flavors.py @@ -188,7 +188,7 @@ class InstanceTypeToolsTest(test.TestCase): _instance_type_p = obj_base.obj_to_primitive(_instance_type) props = flavors.system_metadata_flavor_props.keys() - for key in instance_type_p.keys(): + for key in list(instance_type_p.keys()): if key not in props: del instance_type_p[key] diff --git a/nova/tests/unit/virt/libvirt/fakelibvirt.py b/nova/tests/unit/virt/libvirt/fakelibvirt.py index 7dd57662a923..f40e3d11a9cd 100644 --- a/nova/tests/unit/virt/libvirt/fakelibvirt.py +++ b/nova/tests/unit/virt/libvirt/fakelibvirt.py @@ -868,7 +868,7 @@ class Connection(object): return len(self._running_vms) def listDomainsID(self): - return self._running_vms.keys() + return list(self._running_vms.keys()) def lookupByID(self, id): if id in self._running_vms: diff --git a/nova/tests/unit/virt/vmwareapi/fake.py b/nova/tests/unit/virt/vmwareapi/fake.py index 20a3171a735b..c6e061ddc88b 100644 --- a/nova/tests/unit/virt/vmwareapi/fake.py +++ b/nova/tests/unit/virt/vmwareapi/fake.py @@ -582,7 +582,7 @@ class DatastoreHostMount(DataObject): def __init__(self, value='host-100'): super(DatastoreHostMount, self).__init__() host_ref = (_db_content["HostSystem"] - [_db_content["HostSystem"].keys()[0]].obj) + [list(_db_content["HostSystem"].keys())[0]].obj) host_system = DataObject() host_system.ManagedObjectReference = [host_ref] host_system.value = value @@ -709,7 +709,7 @@ class HostSystem(ManagedObject): create_host_network_system() if not _get_object_refs('HostStorageSystem'): create_host_storage_system() - host_net_key = _db_content["HostNetworkSystem"].keys()[0] + host_net_key = list(_db_content["HostNetworkSystem"].keys())[0] host_net_sys = _db_content["HostNetworkSystem"][host_net_key].obj self.set("configManager.networkSystem", host_net_sys) host_storage_sys_key = _get_object_refs('HostStorageSystem')[0] @@ -767,7 +767,8 @@ class HostSystem(ManagedObject): if _db_content.get("Network", None) is None: create_network() - net_ref = _db_content["Network"][_db_content["Network"].keys()[0]].obj + net_ref = _db_content["Network"][ + list(_db_content["Network"].keys())[0]].obj network_do = DataObject() network_do.ManagedObjectReference = [net_ref] self.set("network", network_do) @@ -870,7 +871,8 @@ class Datacenter(ManagedObject): self.set("vmFolder", "vm_folder_ref") if _db_content.get("Network", None) is None: create_network() - net_ref = _db_content["Network"][_db_content["Network"].keys()[0]].obj + net_ref = _db_content["Network"][ + list(_db_content["Network"].keys())[0]].obj network_do = DataObject() network_do.ManagedObjectReference = [net_ref] self.set("network", network_do) @@ -965,15 +967,16 @@ def create_vm(uuid=None, name=None, devices = [] if vmPathName is None: - vm_path = ds_obj.DatastorePath(_db_content['Datastore'].values()[0]) + vm_path = ds_obj.DatastorePath( + list(_db_content['Datastore'].values())[0]) else: vm_path = ds_obj.DatastorePath.parse(vmPathName) if res_pool_ref is None: - res_pool_ref = _db_content['ResourcePool'].keys()[0] + res_pool_ref = list(_db_content['ResourcePool'].keys())[0] if host_ref is None: - host_ref = _db_content["HostSystem"].keys()[0] + host_ref = list(_db_content["HostSystem"].keys())[0] # Fill in the default path to the vmx file if we were only given a # datastore. Note that if you create a VM with vmPathName '[foo]', when you @@ -1525,7 +1528,7 @@ class FakeVim(object): def _add_port_group(self, method, *args, **kwargs): """Adds a port group to the host system.""" - _host_sk = _db_content["HostSystem"].keys()[0] + _host_sk = list(_db_content["HostSystem"].keys())[0] host_mdo = _db_content["HostSystem"][_host_sk] host_mdo._add_port_group(kwargs.get("portgrp")) diff --git a/nova/tests/unit/virt/vmwareapi/test_vm_util.py b/nova/tests/unit/virt/vmwareapi/test_vm_util.py index 6be69901ebdf..9ec511eb0174 100644 --- a/nova/tests/unit/virt/vmwareapi/test_vm_util.py +++ b/nova/tests/unit/virt/vmwareapi/test_vm_util.py @@ -1564,7 +1564,7 @@ class VMwareVMUtilGetHostRefTestCase(test.NoDBTestCase): self.session = driver.VMwareAPISession() # Create a fake VirtualMachine running on a known host - self.host_ref = fake._db_content['HostSystem'].keys()[0] + self.host_ref = list(fake._db_content['HostSystem'].keys())[0] self.vm_ref = fake.create_vm(host_ref=self.host_ref) @mock.patch.object(vm_util, 'get_vm_ref') diff --git a/nova/tests/unit/virt/xenapi/test_xenapi.py b/nova/tests/unit/virt/xenapi/test_xenapi.py index 284fd7df30b7..de4cce9c12c9 100644 --- a/nova/tests/unit/virt/xenapi/test_xenapi.py +++ b/nova/tests/unit/virt/xenapi/test_xenapi.py @@ -3204,7 +3204,7 @@ class XenAPIAggregateTestCase(stubs.XenAPITestBase): def test_remove_host_from_aggregate_error(self): # Ensure we can remove a host from an aggregate even if in error. values = _create_service_entries(self.context) - fake_zone = values.keys()[0] + fake_zone = list(values.keys())[0] aggr = self.api.create_aggregate(self.context, 'fake_aggregate', fake_zone) # let's mock the fact that the aggregate is ready! diff --git a/nova/virt/xenapi/fake.py b/nova/virt/xenapi/fake.py index 6437126ff371..5f62dc8a6b45 100644 --- a/nova/virt/xenapi/fake.py +++ b/nova/virt/xenapi/fake.py @@ -371,7 +371,7 @@ def _create_vlan(pif_ref, vlan_num, network_ref): def get_all(table): - return _db_content[table].keys() + return list(_db_content[table].keys()) def get_all_records(table): @@ -482,7 +482,7 @@ class SessionBase(object): xenapi_session.apply_session_helpers(self) def pool_get_default_SR(self, _1, pool_ref): - return _db_content['pool'].values()[0]['default-SR'] + return list(_db_content['pool'].values())[0]['default-SR'] def VBD_insert(self, _1, vbd_ref, vdi_ref): vbd_rec = get_record('VBD', vbd_ref) diff --git a/nova/virt/xenapi/network_utils.py b/nova/virt/xenapi/network_utils.py index d838f69886b6..28b857c08f36 100644 --- a/nova/virt/xenapi/network_utils.py +++ b/nova/virt/xenapi/network_utils.py @@ -43,7 +43,7 @@ def find_network_with_bridge(session, bridge): (bridge, bridge)) networks = session.network.get_all_records_where(expr) if len(networks) == 1: - return networks.keys()[0] + return list(networks.keys())[0] elif len(networks) > 1: raise exception.NovaException( _('Found non-unique network for bridge %s') % bridge) diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index 34eac9566b40..3df36963127a 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -758,7 +758,7 @@ def get_sr_path(session, sr_ref=None): # NOTE(bobball): There can only be one PBD for a host/SR pair, but path is # not always present - older versions of XS do not set it. - pbd_ref = pbd_rec.keys()[0] + pbd_ref = list(pbd_rec.keys())[0] device_config = pbd_rec[pbd_ref]['device_config'] if 'path' in device_config: return device_config['path'] @@ -847,7 +847,7 @@ def _find_cached_image(session, image_id, sr_ref): if number_found > 0: if number_found > 1: LOG.warning(_LW("Multiple base images for image: %s"), image_id) - return recs.keys()[0] + return list(recs.keys())[0] def _get_resize_func_name(session): @@ -2222,7 +2222,7 @@ def get_this_vm_uuid(session): 'field "is_control_domain"="true" and ' 'field "resident_on"="%s"' % session.host_ref) - return vms[vms.keys()[0]]['uuid'] + return vms[list(vms.keys())[0]]['uuid'] try: return _get_sys_hypervisor_uuid() except IOError: diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index df5215cd3346..c8ee1863a21d 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -2089,14 +2089,14 @@ class VMOps(object): msg = _('No suitable network for migrate') raise exception.MigrationPreCheckError(reason=msg) - pifkey = pifs.keys()[0] + pifkey = list(pifs.keys())[0] if not (netutils.is_valid_ipv4(pifs[pifkey]['IP']) or netutils.is_valid_ipv6(pifs[pifkey]['IPv6'])): msg = (_('PIF %s does not contain IP address') % pifs[pifkey]['uuid']) raise exception.MigrationPreCheckError(reason=msg) - nwref = pifs[pifs.keys()[0]]['network'] + nwref = pifs[list(pifs.keys())[0]]['network'] try: options = {} migrate_data = self._session.call_xenapi("host.migrate_receive", diff --git a/tests-py3.txt b/tests-py3.txt index 67665d5eb80d..6f1665080191 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -77,7 +77,6 @@ nova.tests.unit.api.openstack.compute.test_floating_ips.FloatingIpTestV2 nova.tests.unit.api.openstack.compute.test_floating_ips.FloatingIpTestV21 nova.tests.unit.api.openstack.compute.test_hide_server_addresses.HideServerAddressesTestV2 nova.tests.unit.api.openstack.compute.test_hide_server_addresses.HideServerAddressesTestV21 -nova.tests.unit.api.openstack.compute.test_hosts.HostTestCaseV20 nova.tests.unit.api.openstack.compute.test_image_metadata.ImageMetaDataTestV2 nova.tests.unit.api.openstack.compute.test_image_metadata.ImageMetaDataTestV21 nova.tests.unit.api.openstack.compute.test_image_size.ImageSizeTestV2 @@ -218,7 +217,6 @@ nova.tests.unit.test_cinder.CinderTestCase nova.tests.unit.test_cinder.CinderV2TestCase nova.tests.unit.test_configdrive2.ConfigDriveTestCase nova.tests.unit.test_fixtures.TestOSAPIFixture -nova.tests.unit.test_flavors.InstanceTypeToolsTest nova.tests.unit.test_hacking.HackingTestCase nova.tests.unit.test_iptables_network.IptablesManagerTestCase nova.tests.unit.test_ipv6.IPv6AccountIdentiferTestCase @@ -282,7 +280,7 @@ nova.tests.unit.virt.libvirt.test_config.LibvirtConfigTest nova.tests.unit.virt.libvirt.test_driver.LibvirtConnTestCase nova.tests.unit.virt.libvirt.test_driver.LibvirtDriverTestCase nova.tests.unit.virt.libvirt.test_driver.LibvirtVolumeSnapshotTestCase -nova.tests.unit.virt.libvirt.test_fakelibvirt.FakeLibvirtTests +nova.tests.unit.virt.libvirt.test_fakelibvirt.FakeLibvirtTests.test_numa_topology_generation nova.tests.unit.virt.libvirt.test_firewall.IptablesFirewallTestCase nova.tests.unit.virt.libvirt.test_imagebackend.EncryptedLvmTestCase nova.tests.unit.virt.libvirt.test_imagebackend.LvmTestCase @@ -307,7 +305,6 @@ nova.tests.unit.virt.vmwareapi.test_images.VMwareImagesTestCase nova.tests.unit.virt.vmwareapi.test_network_util.GetNetworkWithTheNameTestCase nova.tests.unit.virt.vmwareapi.test_read_write_util.ReadWriteUtilTestCase nova.tests.unit.virt.vmwareapi.test_vim_util.VMwareVIMUtilTestCase -nova.tests.unit.virt.vmwareapi.test_vm_util.VMwareVMUtilGetHostRefTestCase nova.tests.unit.virt.vmwareapi.test_vm_util.VMwareVMUtilTestCase nova.tests.unit.virt.vmwareapi.test_vmops.VMwareVMOpsTestCase nova.tests.unit.virt.vmwareapi.test_volumeops.VMwareVolumeOpsTestCase @@ -319,7 +316,6 @@ nova.tests.unit.virt.xenapi.image.test_vdi_through_dev.TestTarGzProducer nova.tests.unit.virt.xenapi.image.test_vdi_through_dev.TestUploadToGlanceAsRawTgz nova.tests.unit.virt.xenapi.test_agent.FileInjectionTestCase nova.tests.unit.virt.xenapi.test_driver.XenAPIDriverTestCase -nova.tests.unit.virt.xenapi.test_network_utils.NetworkUtilsTestCase nova.tests.unit.virt.xenapi.test_vm_utils.CreateCachedImageTestCase nova.tests.unit.virt.xenapi.test_vm_utils.CreateVmRecordTestCase nova.tests.unit.virt.xenapi.test_vm_utils.CreateVmTestCase @@ -343,7 +339,6 @@ nova.tests.unit.virt.xenapi.test_xenapi.XenAPILiveMigrateTestCase nova.tests.unit.virt.xenapi.test_xenapi.XenAPIMigrateInstance nova.tests.unit.virt.xenapi.test_xenapi.XenAPISRSelectionTestCase nova.tests.unit.virt.xenapi.test_xenapi.XenAPIVMTestCase -nova.tests.unit.virt.xenapi.test_xenapi.XenAPIVolumeTestCase nova.tests.unit.volume.encryptors.test_cryptsetup.CryptsetupEncryptorTestCase nova.tests.unit.volume.encryptors.test_luks.LuksEncryptorTestCase nova.tests.unit.volume.test_cinder.CinderApiTestCase