python3: Fix traceback while running tests

The testsuite is full of the following:

TypeError: 'dict_keys' object does not support indexing

This is due to the fact in python3 dict methods dict.keys(),
dict.items() and dict.values() return “views” instead of lists.

Change-Id: Ifa5383e6485fdbabf363fd1442877b2452346c1c
Signed-off-by: Chuck Short <chuck.short@canonical.com>
This commit is contained in:
Chuck Short
2013-06-19 20:46:46 -05:00
parent 89fb1503e9
commit 3044671b36
18 changed files with 39 additions and 39 deletions

View File

@@ -201,7 +201,7 @@ class ManagerWithFind(six.with_metaclass(abc.ABCMeta, Manager)):
the Python side. the Python side.
""" """
found = [] found = []
searches = kwargs.items() searches = list(kwargs.items())
for obj in self.list(): for obj in self.list():
try: try:
@@ -270,8 +270,8 @@ class Resource(object):
return self.__dict__[k] return self.__dict__[k]
def __repr__(self): def __repr__(self):
reprkeys = sorted(k for k in self.__dict__.keys() if k[0] != '_' and reprkeys = sorted(k for k in list(self.__dict__.keys()) if k[0] != '_'
k != 'manager') and k != 'manager')
info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys)
return "<%s %s>" % (self.__class__.__name__, info) return "<%s %s>" % (self.__class__.__name__, info)

View File

@@ -387,7 +387,7 @@ def get_client_class(version):
client_path = version_map[str(version)] client_path = version_map[str(version)]
except (KeyError, ValueError): except (KeyError, ValueError):
msg = "Invalid client version '%s'. must be one of: %s" % ( msg = "Invalid client version '%s'. must be one of: %s" % (
(version, ', '.join(version_map.keys()))) (version, ', '.join(list(version_map.keys()))))
raise exceptions.UnsupportedVersion(msg) raise exceptions.UnsupportedVersion(msg)
return utils.import_class(client_path) return utils.import_class(client_path)

View File

@@ -143,7 +143,7 @@ def from_response(response, body):
message = "n/a" message = "n/a"
details = "n/a" details = "n/a"
if hasattr(body, 'keys'): if hasattr(body, 'keys'):
error = body[body.keys()[0]] error = body[list(body.keys())[0]]
message = error.get('message', None) message = error.get('message', None)
details = error.get('details', None) details = error.get('details', None)
return cls(code=response.status_code, message=message, details=details, return cls(code=response.status_code, message=message, details=details,

View File

@@ -29,7 +29,7 @@ class Extension(utils.HookableMixin):
def _parse_extension_module(self): def _parse_extension_module(self):
self.manager_class = None self.manager_class = None
for attr_name, attr_value in self.module.__dict__.items(): for attr_name, attr_value in list(self.module.__dict__.items()):
if attr_name in self.SUPPORTED_HOOKS: if attr_name in self.SUPPORTED_HOOKS:
self.add_hook(attr_name, attr_value) self.add_hook(attr_name, attr_value)
elif utils.safe_issubclass(attr_value, base.Manager): elif utils.safe_issubclass(attr_value, base.Manager):

View File

@@ -465,9 +465,9 @@ class OpenStackCinderShell(object):
""" """
commands = set() commands = set()
options = set() options = set()
for sc_str, sc in self.subcommands.items(): for sc_str, sc in list(self.subcommands.items()):
commands.add(sc_str) commands.add(sc_str)
for option in sc._optionals._option_string_actions.keys(): for option in list(sc._optionals._option_string_actions.keys()):
options.add(option) options.add(option)
commands.remove('bash-completion') commands.remove('bash-completion')

View File

@@ -10,7 +10,7 @@ from __future__ import print_function
def assert_has_keys(dict, required=[], optional=[]): def assert_has_keys(dict, required=[], optional=[]):
keys = dict.keys() keys = list(dict.keys())
for k in required: for k in required:
try: try:
assert k in keys assert k in keys

View File

@@ -215,10 +215,10 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_volumes_1234_action(self, body, **kw): def post_volumes_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(body.keys()) == 1 assert len(list(body.keys())) == 1
action = body.keys()[0] action = list(body.keys())[0]
if action == 'os-attach': if action == 'os-attach':
assert body[action].keys() == ['instance_uuid', 'mountpoint'] assert list(body[action].keys()) == ['instance_uuid', 'mountpoint']
elif action == 'os-detach': elif action == 'os-detach':
assert body[action] is None assert body[action] is None
elif action == 'os-reserve': elif action == 'os-reserve':
@@ -226,10 +226,10 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-unreserve': elif action == 'os-unreserve':
assert body[action] is None assert body[action] is None
elif action == 'os-initialize_connection': elif action == 'os-initialize_connection':
assert body[action].keys() == ['connector'] assert list(body[action].keys()) == ['connector']
return (202, {}, {'connection_info': 'foos'}) return (202, {}, {'connection_info': 'foos'})
elif action == 'os-terminate_connection': elif action == 'os-terminate_connection':
assert body[action].keys() == ['connector'] assert list(body[action].keys()) == ['connector']
elif action == 'os-begin_detaching': elif action == 'os-begin_detaching':
assert body[action] is None assert body[action] is None
elif action == 'os-roll_detaching': elif action == 'os-roll_detaching':
@@ -265,7 +265,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_sets_test(self, body, **kw): def put_os_quota_sets_test(self, body, **kw):
assert body.keys() == ['quota_set'] assert list(body.keys()) == ['quota_set']
fakes.assert_has_keys(body['quota_set'], fakes.assert_has_keys(body['quota_set'],
required=['tenant_id']) required=['tenant_id'])
return (200, {}, {'quota_set': { return (200, {}, {'quota_set': {
@@ -288,7 +288,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_class_sets_test(self, body, **kw): def put_os_quota_class_sets_test(self, body, **kw):
assert body.keys() == ['quota_class_set'] assert list(body.keys()) == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'], fakes.assert_has_keys(body['quota_class_set'],
required=['class_name']) required=['class_name'])
return (200, {}, {'quota_class_set': { return (200, {}, {'quota_class_set': {
@@ -321,7 +321,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'extra_specs': {}}}) 'extra_specs': {}}})
def post_types_1_extra_specs(self, body, **kw): def post_types_1_extra_specs(self, body, **kw):
assert body.keys() == ['extra_specs'] assert list(body.keys()) == ['extra_specs']
return (200, {}, {'extra_specs': {'k': 'v'}}) return (200, {}, {'extra_specs': {'k': 'v'}})
def delete_types_1_extra_specs_k(self, **kw): def delete_types_1_extra_specs_k(self, **kw):

View File

@@ -222,10 +222,10 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_volumes_1234_action(self, body, **kw): def post_volumes_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(body.keys()) == 1 assert len(list(body.keys())) == 1
action = body.keys()[0] action = list(body.keys())[0]
if action == 'os-attach': if action == 'os-attach':
assert body[action].keys() == ['instance_uuid', 'mountpoint'] assert list(body[action].keys()) == ['instance_uuid', 'mountpoint']
elif action == 'os-detach': elif action == 'os-detach':
assert body[action] is None assert body[action] is None
elif action == 'os-reserve': elif action == 'os-reserve':
@@ -233,10 +233,10 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-unreserve': elif action == 'os-unreserve':
assert body[action] is None assert body[action] is None
elif action == 'os-initialize_connection': elif action == 'os-initialize_connection':
assert body[action].keys() == ['connector'] assert list(body[action].keys()) == ['connector']
return (202, {}, {'connection_info': 'foos'}) return (202, {}, {'connection_info': 'foos'})
elif action == 'os-terminate_connection': elif action == 'os-terminate_connection':
assert body[action].keys() == ['connector'] assert list(body[action].keys()) == ['connector']
elif action == 'os-begin_detaching': elif action == 'os-begin_detaching':
assert body[action] is None assert body[action] is None
elif action == 'os-roll_detaching': elif action == 'os-roll_detaching':
@@ -272,7 +272,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_sets_test(self, body, **kw): def put_os_quota_sets_test(self, body, **kw):
assert body.keys() == ['quota_set'] assert list(body.keys()) == ['quota_set']
fakes.assert_has_keys(body['quota_set'], fakes.assert_has_keys(body['quota_set'],
required=['tenant_id']) required=['tenant_id'])
return (200, {}, {'quota_set': { return (200, {}, {'quota_set': {
@@ -295,7 +295,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_class_sets_test(self, body, **kw): def put_os_quota_class_sets_test(self, body, **kw):
assert body.keys() == ['quota_class_set'] assert list(body.keys()) == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'], fakes.assert_has_keys(body['quota_class_set'],
required=['class_name']) required=['class_name'])
return (200, {}, {'quota_class_set': { return (200, {}, {'quota_class_set': {
@@ -328,7 +328,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'extra_specs': {}}}) 'extra_specs': {}}})
def post_types_1_extra_specs(self, body, **kw): def post_types_1_extra_specs(self, body, **kw):
assert body.keys() == ['extra_specs'] assert list(body.keys()) == ['extra_specs']
return (200, {}, {'extra_specs': {'k': 'v'}}) return (200, {}, {'extra_specs': {'k': 'v'}})
def delete_types_1_extra_specs_k(self, **kw): def delete_types_1_extra_specs_k(self, **kw):

View File

@@ -217,7 +217,7 @@ def find_resource(manager, name_or_id):
def _format_servers_list_networks(server): def _format_servers_list_networks(server):
output = [] output = []
for (network, addresses) in server.networks.items(): for (network, addresses) in list(server.networks.items()):
if len(addresses) == 0: if len(addresses) == 0:
continue continue
addresses_csv = ', '.join(addresses) addresses_csv = ', '.join(addresses)

View File

@@ -11,7 +11,7 @@ class Limits(base.Resource):
@property @property
def absolute(self): def absolute(self):
for (name, value) in self._info['absolute'].items(): for (name, value) in list(self._info['absolute'].items()):
yield AbsoluteLimit(name, value) yield AbsoluteLimit(name, value)
@property @property

View File

@@ -46,7 +46,7 @@ class QuotaClassSetManager(base.Manager):
'volumes': volumes, 'volumes': volumes,
'gigabytes': gigabytes}} 'gigabytes': gigabytes}}
for key in body['quota_class_set'].keys(): for key in list(body['quota_class_set'].keys()):
if body['quota_class_set'][key] is None: if body['quota_class_set'][key] is None:
body['quota_class_set'].pop(key) body['quota_class_set'].pop(key)

View File

@@ -45,7 +45,7 @@ class QuotaSetManager(base.Manager):
'snapshots': snapshots, 'snapshots': snapshots,
'gigabytes': gigabytes}} 'gigabytes': gigabytes}}
for key in body['quota_set'].keys(): for key in list(body['quota_set'].keys()):
if body['quota_set'][key] is None: if body['quota_set'][key] is None:
body['quota_set'].pop(key) body['quota_set'].pop(key)

View File

@@ -83,7 +83,7 @@ def _print_volume_snapshot(snapshot):
def _translate_keys(collection, convert): def _translate_keys(collection, convert):
for item in collection: for item in collection:
keys = item.__dict__.keys() keys = list(item.__dict__.keys())
for from_key, to_key in convert: for from_key, to_key in convert:
if from_key in keys and to_key not in keys: if from_key in keys and to_key not in keys:
setattr(item, to_key, item._info[from_key]) setattr(item, to_key, item._info[from_key])
@@ -306,7 +306,7 @@ def do_metadata(cs, args):
if args.action == 'set': if args.action == 'set':
cs.volumes.set_metadata(volume, metadata) cs.volumes.set_metadata(volume, metadata)
elif args.action == 'unset': elif args.action == 'unset':
cs.volumes.delete_metadata(volume, metadata.keys()) cs.volumes.delete_metadata(volume, list(metadata.keys()))
@utils.arg( @utils.arg(
@@ -491,7 +491,7 @@ def do_type_key(cs, args):
if args.action == 'set': if args.action == 'set':
vtype.set_keys(keypair) vtype.set_keys(keypair)
elif args.action == 'unset': elif args.action == 'unset':
vtype.unset_keys(keypair.keys()) vtype.unset_keys(list(keypair.keys()))
def do_endpoints(cs, args): def do_endpoints(cs, args):

View File

@@ -11,7 +11,7 @@ class Limits(base.Resource):
@property @property
def absolute(self): def absolute(self):
for (name, value) in self._info['absolute'].items(): for (name, value) in list(self._info['absolute'].items()):
yield AbsoluteLimit(name, value) yield AbsoluteLimit(name, value)
@property @property

View File

@@ -44,7 +44,7 @@ class QuotaClassSetManager(base.Manager):
'volumes': volumes, 'volumes': volumes,
'gigabytes': gigabytes}} 'gigabytes': gigabytes}}
for key in body['quota_class_set'].keys(): for key in list(body['quota_class_set'].keys()):
if body['quota_class_set'][key] is None: if body['quota_class_set'][key] is None:
body['quota_class_set'].pop(key) body['quota_class_set'].pop(key)

View File

@@ -43,7 +43,7 @@ class QuotaSetManager(base.Manager):
'snapshots': snapshots, 'snapshots': snapshots,
'gigabytes': gigabytes}} 'gigabytes': gigabytes}}
for key in body['quota_set'].keys(): for key in list(body['quota_set'].keys()):
if body['quota_set'][key] is None: if body['quota_set'][key] is None:
body['quota_set'].pop(key) body['quota_set'].pop(key)

View File

@@ -75,7 +75,7 @@ def _print_volume_snapshot(snapshot):
def _translate_keys(collection, convert): def _translate_keys(collection, convert):
for item in collection: for item in collection:
keys = item.__dict__.keys() keys = list(item.__dict__.keys())
for from_key, to_key in convert: for from_key, to_key in convert:
if from_key in keys and to_key not in keys: if from_key in keys and to_key not in keys:
setattr(item, to_key, item._info[from_key]) setattr(item, to_key, item._info[from_key])
@@ -351,7 +351,7 @@ def do_metadata(cs, args):
if args.action == 'set': if args.action == 'set':
cs.volumes.set_metadata(volume, metadata) cs.volumes.set_metadata(volume, metadata)
elif args.action == 'unset': elif args.action == 'unset':
cs.volumes.delete_metadata(volume, metadata.keys()) cs.volumes.delete_metadata(volume, list(metadata.keys()))
@utils.arg('--all-tenants', @utils.arg('--all-tenants',
@@ -557,7 +557,7 @@ def do_type_key(cs, args):
if args.action == 'set': if args.action == 'set':
vtype.set_keys(keypair) vtype.set_keys(keypair)
elif args.action == 'unset': elif args.action == 'unset':
vtype.unset_keys(keypair.keys()) vtype.unset_keys(list(keypair.keys()))
def do_endpoints(cs, args): def do_endpoints(cs, args):

View File

@@ -267,7 +267,7 @@ class NovaTestResult(testtools.TestResult):
if not self.last_written or (self._now() - time).total_seconds() > 2.0: if not self.last_written or (self._now() - time).total_seconds() > 2.0:
diff = 3.0 diff = 3.0
while diff > 2.0: while diff > 2.0:
classes = self.results.keys() classes =list(self.results.keys())
oldest = min(classes, key=lambda x: self.last_time[x]) oldest = min(classes, key=lambda x: self.last_time[x])
diff = (self._now() - self.last_time[oldest]).total_seconds() diff = (self._now() - self.last_time[oldest]).total_seconds()
self.writeTestCase(oldest) self.writeTestCase(oldest)