From f0082849dfe435af23b85150efd8dea06799e999 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: 2015年5月25日 13:22:29 +0200 Subject: [PATCH] Fix version unit test on Python 3 With this change, "tox -e py34" now pass. Changes: * Replace unichr() with six.unichr() * Replace StringIO.StringIO() with six.Bytes() in nova.crypto.generate_key_pair() * On Python 3, replace UserDict.IterableUserDict with collections.UserDict * Replace __builtin__.open with six.moves.builtins.open * Replace ConfigParser import with six.moves.configparser * On Python 3, get the original "queue" module instead of the "Queue" module in nova/virt/libvirt/host.py * Replace urllib2 with six.moves.urllib * tox.ini: "tox -e py34" now only runs nova.tests.unit.test_versions Blueprint nova-python3 Change-Id: I056769c7c5b32276894f7aade8c0a27af81c42ae --- nova/api/validation/parameter_types.py | 4 +++- nova/crypto.py | 4 ++-- nova/scheduler/host_manager.py | 8 ++++++-- nova/tests/unit/test_versions.py | 11 +++++------ nova/version.py | 4 ++-- nova/virt/libvirt/host.py | 3 ++- nova/virt/libvirt/volume.py | 6 +++--- tox.ini | 3 +++ 8 files changed, 26 insertions(+), 17 deletions(-) diff --git a/nova/api/validation/parameter_types.py b/nova/api/validation/parameter_types.py index aba2d0c6abba..a0126e7270f8 100644 --- a/nova/api/validation/parameter_types.py +++ b/nova/api/validation/parameter_types.py @@ -19,6 +19,8 @@ import copy import re import unicodedata +import six + def _is_printable(char): """determine if a unicode code point is printable. @@ -36,7 +38,7 @@ def _is_printable(char): def _get_all_chars(): for i in range(0xFFFF): - yield unichr(i) + yield six.unichr(i) # build a regex that matches all printable characters. This allows # spaces in the middle of the name. Also note that the regexp below diff --git a/nova/crypto.py b/nova/crypto.py index 64907e0c2cd9..937c05a06293 100644 --- a/nova/crypto.py +++ b/nova/crypto.py @@ -27,7 +27,6 @@ import binascii import os import re import string -import StringIO import struct from oslo_concurrency import processutils @@ -38,6 +37,7 @@ from oslo_utils import timeutils import paramiko from pyasn1.codec.der import encoder as der_encoder from pyasn1.type import univ +import six from nova import context from nova import db @@ -166,7 +166,7 @@ def generate_x509_fingerprint(pem_key): def generate_key_pair(bits=2048): key = paramiko.RSAKey.generate(bits) - keyout = StringIO.StringIO() + keyout = six.BytesIO() key.write_private_key(keyout) private_key = keyout.getvalue() public_key = '%s %s Generated-by-Nova' % (key.get_name(), key.get_base64()) diff --git a/nova/scheduler/host_manager.py b/nova/scheduler/host_manager.py index cfbb998372aa..a2fb8aa2f1c7 100644 --- a/nova/scheduler/host_manager.py +++ b/nova/scheduler/host_manager.py @@ -19,7 +19,11 @@ Manage hosts in the current zone. import collections import time -import UserDict +try: + from collections import UserDict as IterableUserDict # Python 3 +except ImportError: + from UserDict import IterableUserDict # Python 2 + import iso8601 from oslo_config import cfg @@ -76,7 +80,7 @@ LOG = logging.getLogger(__name__) HOST_INSTANCE_SEMAPHORE = "host_instance" -class ReadOnlyDict(UserDict.IterableUserDict): +class ReadOnlyDict(IterableUserDict): """A read-only dict.""" def __init__(self, source=None): self.data = {} diff --git a/nova/tests/unit/test_versions.py b/nova/tests/unit/test_versions.py index 8694eac77a47..3a46d8817e5b 100644 --- a/nova/tests/unit/test_versions.py +++ b/nova/tests/unit/test_versions.py @@ -12,10 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. -import __builtin__ -import StringIO - from oslo_config import cfg +import six +from six.moves import builtins from nova import test from nova import version @@ -35,7 +34,7 @@ class VersionTestCase(test.NoDBTestCase): def test_release_file(self): version.loaded = False - real_open = __builtin__.open + real_open = builtins.open real_find_file = cfg.CONF.find_file def fake_find_file(self, name): @@ -49,11 +48,11 @@ class VersionTestCase(test.NoDBTestCase): vendor = ACME Corporation product = ACME Nova package = 1337""" - return StringIO.StringIO(data) + return six.StringIO(data) return real_open(path, *args, **kwargs) - self.stubs.Set(__builtin__, 'open', fake_open) + self.stubs.Set(builtins, 'open', fake_open) self.stubs.Set(cfg.ConfigOpts, 'find_file', fake_find_file) self.assertEqual(version.vendor_string(), "ACME Corporation") diff --git a/nova/version.py b/nova/version.py index 74cf0807ede4..9f4205f5d716 100644 --- a/nova/version.py +++ b/nova/version.py @@ -29,7 +29,7 @@ def _load_config(): # Don't load in global context, since we can't assume # these modules are accessible when distutils uses # this module - import ConfigParser + from six.moves import configparser from oslo_config import cfg @@ -46,7 +46,7 @@ def _load_config(): return try: - cfg = ConfigParser.RawConfigParser() + cfg = configparser.RawConfigParser() cfg.read(cfgfile) if cfg.has_option("Nova", "vendor"): diff --git a/nova/virt/libvirt/host.py b/nova/virt/libvirt/host.py index 9719e4a1e589..f908e842c2a8 100644 --- a/nova/virt/libvirt/host.py +++ b/nova/virt/libvirt/host.py @@ -43,6 +43,7 @@ from oslo_log import log as logging from oslo_utils import excutils from oslo_utils import importutils from oslo_utils import units +import six from nova import context as nova_context from nova import exception @@ -62,7 +63,7 @@ LOG = logging.getLogger(__name__) native_socket = patcher.original('socket') native_threading = patcher.original("threading") -native_Queue = patcher.original("Queue") +native_Queue = patcher.original("queue" if six.PY3 else "Queue") CONF = cfg.CONF CONF.import_opt('host', 'nova.netconf') diff --git a/nova/virt/libvirt/volume.py b/nova/virt/libvirt/volume.py index 30467fc50762..f01a0138bfa9 100644 --- a/nova/virt/libvirt/volume.py +++ b/nova/virt/libvirt/volume.py @@ -22,13 +22,13 @@ import os import platform import re import time -import urllib2 from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log as logging from oslo_utils import strutils import six +from six.moves import urllib import six.moves.urllib.parse as urlparse from nova.compute import arch @@ -1468,8 +1468,8 @@ class LibvirtScalityVolumeDriver(LibvirtBaseVolumeDriver): # turn local path into URL config = 'file://%s' % config try: - urllib2.urlopen(config, timeout=5).close() - except urllib2.URLError as e: + urllib.request.urlopen(config, timeout=5).close() + except urllib.error.URLError as e: msg = _LW("Cannot access 'scality_sofs_config': %s") % e LOG.warn(msg) raise exception.NovaException(msg) diff --git a/tox.ini b/tox.ini index 804b959fd4d7..5d91eb8c4868 100644 --- a/tox.ini +++ b/tox.ini @@ -40,6 +40,9 @@ setenv = OS_TEST_DBAPI_ADMIN_CONNECTION=mysql+pymysql://openstack_citest:openstack_citest@localhost/;postgresql://openstack_citest:openstack_citest@localhost/postgres;sqlite:// deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements-py3.txt +commands = + find . -type f -name "*.pyc" -delete + python -m testtools.run nova.tests.unit.test_versions [testenv:functional] usedevelop = True

AltStyle によって変換されたページ (->オリジナル) /