Port scheduler host manager to Python 3

* Rewrite ReadOnlyDict using collections.Mapping: collections.Mapping
  has no method to modify the dictionary and is available on Python 2
  and Python 3. Add also a __repr__() method to ease debug. Remove
  ReadOnlyDict.update() method: a read-only dictionary must not be
  modifiable.
* test_host_manager.py: sort dictionaries using a key function to
  have a reliable order for the list of dictionaries. On Python 3,
  the hash function is now randomized.
* tox.ini. add cinder/tests/unit/scheduler/ tests to Python 3.4

Blueprint cinder-python3
Change-Id: I34d98d6a75fef907251719f0d46b025d8e8b2b65
This commit is contained in:
Victor Stinner 2015-10-02 10:24:54 +02:00
parent c7255b6af1
commit 3a7f1ffb95
3 changed files with 34 additions and 29 deletions

View File

@ -17,7 +17,7 @@
Manage hosts in the current zone.
"""
import UserDict
import collections
from oslo_config import cfg
from oslo_log import log as logging
@ -57,36 +57,25 @@ CONF.import_opt('max_over_subscription_ratio', 'cinder.volume.driver')
LOG = logging.getLogger(__name__)
class ReadOnlyDict(UserDict.IterableUserDict):
class ReadOnlyDict(collections.Mapping):
"""A read-only dict."""
def __init__(self, source=None):
self.data = {}
self.update(source)
def __setitem__(self, key, item):
raise TypeError
def __delitem__(self, key):
raise TypeError
def clear(self):
raise TypeError
def pop(self, key, *args):
raise TypeError
def popitem(self):
raise TypeError
def update(self, source=None):
if source is None:
return
elif isinstance(source, UserDict.UserDict):
self.data = source.data
elif isinstance(source, type({})):
self.data = source
if source is not None:
self.data = dict(source)
else:
raise TypeError
self.data = {}
def __getitem__(self, key):
return self.data[key]
def __iter__(self):
return iter(self.data)
def __len__(self):
return len(self.data)
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.data)
class HostState(object):

View File

@ -389,8 +389,13 @@ class HostManagerTestCase(test.TestCase):
'provisioned_capacity_gb': 9300},
}
]
def sort_func(data):
return data['name']
self.assertEqual(len(expected), len(res))
self.assertEqual(sorted(expected), sorted(res))
self.assertEqual(sorted(expected, key=sort_func),
sorted(res, key=sort_func))
class HostStateTestCase(test.TestCase):

11
tox.ini
View File

@ -31,6 +31,17 @@ commands =
python -m testtools.run \
cinder.tests.unit.image.test_glance \
cinder.tests.unit.keymgr.test_mock_key_mgr \
cinder.tests.unit.scheduler.test_allocated_capacity_weigher \
cinder.tests.unit.scheduler.test_capacity_weigher \
cinder.tests.unit.scheduler.test_chance_weigher \
cinder.tests.unit.scheduler.test_filter_scheduler \
cinder.tests.unit.scheduler.test_goodness_weigher \
cinder.tests.unit.scheduler.test_host_filters \
cinder.tests.unit.scheduler.test_host_manager \
cinder.tests.unit.scheduler.test_rpcapi \
cinder.tests.unit.scheduler.test_scheduler \
cinder.tests.unit.scheduler.test_scheduler_options \
cinder.tests.unit.scheduler.test_volume_number_weigher \
cinder.tests.unit.targets.test_base_iscsi_driver \
cinder.tests.unit.targets.test_cxt_driver \
cinder.tests.unit.targets.test_iser_driver \