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
This commit is contained in:
Victor Stinner 2015-05-25 13:22:29 +02:00
parent 52823c48ef
commit f0082849df
8 changed files with 26 additions and 17 deletions

View File

@ -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

View File

@ -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())

View File

@ -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 = {}

View File

@ -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")

View File

@ -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"):

View File

@ -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')

View File

@ -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)

View File

@ -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