Port more unit tests to Python 3

* Replace string.find(a, b) with a.find(b)
* NamedTemporaryFile: open file in text mode rather than opening it
  in binary mode.
* Module.process_contents(): if contents is a Unicode string, first
  encode it to UTF-8 before hashing the content to MD5.
* ClusterController: replace dict.items()[0] with next(iter(dict.items()))
* tox: run the following tests on Python 3.4

  - mysql/test_common.py
  - taskmanager/test_models.py
  - module/test_module_models.py
  - test_cluster_controller.py

Partially implements: blueprint trove-python3
Change-Id: I5372520f9717b4f0279c4b436ca88c160ddf894b
This commit is contained in:
Victor Stinner 2016-05-26 16:29:51 +02:00
parent f9df5af18d
commit 61614653d6
5 changed files with 15 additions and 7 deletions

View File

@ -38,6 +38,7 @@ commands =
trove/tests/unittests/backup/test_backup_models.py \
trove/tests/unittests/cluster/test_cassandra_cluster.py \
trove/tests/unittests/cluster/test_cluster.py \
trove/tests/unittests/cluster/test_cluster_controller.py \
trove/tests/unittests/cluster/test_cluster_models.py \
trove/tests/unittests/cluster/test_cluster_pxc_controller.py \
trove/tests/unittests/cluster/test_cluster_redis_controller.py \
@ -81,7 +82,9 @@ commands =
trove/tests/unittests/mgmt/test_datastores.py \
trove/tests/unittests/mgmt/test_models.py \
trove/tests/unittests/module/test_module_controller.py \
trove/tests/unittests/module/test_module_models.py \
trove/tests/unittests/module/test_module_views.py \
trove/tests/unittests/mysql/test_common.py \
trove/tests/unittests/mysql/test_user_controller.py \
trove/tests/unittests/network/test_neutron_driver.py \
trove/tests/unittests/quota/test_quota.py \
@ -91,6 +94,7 @@ commands =
trove/tests/unittests/taskmanager/test_clusters.py \
trove/tests/unittests/taskmanager/test_galera_clusters.py \
trove/tests/unittests/taskmanager/test_manager.py \
trove/tests/unittests/taskmanager/test_models.py \
trove/tests/unittests/taskmanager/test_vertica_clusters.py \
trove/tests/unittests/upgrade/test_controller.py \
trove/tests/unittests/upgrade/test_models.py

View File

@ -63,7 +63,7 @@ class ClusterController(wsgi.Controller):
" one action specified in body"))
context = req.environ[wsgi.CONTEXT_KEY]
cluster = models.Cluster.load(context, id)
cluster.action(context, req, *body.items()[0])
cluster.action(context, req, *next(iter(body.items())))
view = views.load_view(cluster, req=req, load_servers=False)
wsgi_result = wsgi.Result(view.data(), 202)

View File

@ -15,7 +15,6 @@
import abc
import re
import string
import netaddr
from six import u
@ -573,7 +572,7 @@ class ValidatedMySQLDatabase(MySQLDatabase):
if any([not value,
not self._is_valid(value),
not self.dbname.match(value),
string.find("%r" % value, "\\") != -1]):
("%r" % value).find("\\") != -1]):
raise ValueError(_("'%s' is not a valid database name.") % value)
elif len(value) > 64:
msg = _("Database name '%s' is too long. Max length = 64.")
@ -966,7 +965,7 @@ class MySQLUser(Base):
def _is_valid(self, value):
if (not value or
self.not_supported_chars.search(value) or
string.find("%r" % value, "\\") != -1):
("%r" % value).find("\\") != -1):
return False
else:
return True

View File

@ -18,6 +18,7 @@
from datetime import datetime
import hashlib
import six
from sqlalchemy.sql.expression import or_
from trove.common import cfg
@ -217,7 +218,10 @@ class Module(object):
# be stored in a text field in the Trove database.
@staticmethod
def process_contents(contents):
md5 = hashlib.md5(contents).hexdigest()
md5 = contents
if isinstance(md5, six.text_type):
md5 = md5.encode('utf-8')
md5 = hashlib.md5(md5).hexdigest()
encrypted_contents = crypto_utils.encrypt_data(
contents, Modules.ENCRYPT_KEY)
return md5, crypto_utils.encode_data(encrypted_contents)

View File

@ -193,10 +193,11 @@ class FreshInstanceTasksTest(trove_testtools.TestCase):
self.orig_DBI_find_by = DBInstance.find_by
self.userdata = "hello moto"
self.guestconfig_content = "guest config"
with NamedTemporaryFile(suffix=".cloudinit", delete=False) as f:
with NamedTemporaryFile(mode="w", suffix=".cloudinit",
delete=False) as f:
self.cloudinit = f.name
f.write(self.userdata)
with NamedTemporaryFile(delete=False) as f:
with NamedTemporaryFile(mode="w", delete=False) as f:
self.guestconfig = f.name
f.write(self.guestconfig_content)
self.freshinstancetasks = taskmanager_models.FreshInstanceTasks(