From 61614653d6b35b3d6b4c8802c737c02cb9968f4a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 May 2016 16:29:51 +0200 Subject: [PATCH] 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 --- tox.ini | 4 ++++ trove/cluster/service.py | 2 +- trove/guestagent/db/models.py | 5 ++--- trove/module/models.py | 6 +++++- trove/tests/unittests/taskmanager/test_models.py | 5 +++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/tox.ini b/tox.ini index e1acfef888..539e951d60 100644 --- a/tox.ini +++ b/tox.ini @@ -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 diff --git a/trove/cluster/service.py b/trove/cluster/service.py index 09c49c0268..73ca5eacbb 100644 --- a/trove/cluster/service.py +++ b/trove/cluster/service.py @@ -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) diff --git a/trove/guestagent/db/models.py b/trove/guestagent/db/models.py index 234cbc412a..6a2cb73c2c 100644 --- a/trove/guestagent/db/models.py +++ b/trove/guestagent/db/models.py @@ -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 diff --git a/trove/module/models.py b/trove/module/models.py index 2558fa9f11..c955d4c152 100644 --- a/trove/module/models.py +++ b/trove/module/models.py @@ -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) diff --git a/trove/tests/unittests/taskmanager/test_models.py b/trove/tests/unittests/taskmanager/test_models.py index 1698e6e0be..a385a7f55b 100644 --- a/trove/tests/unittests/taskmanager/test_models.py +++ b/trove/tests/unittests/taskmanager/test_models.py @@ -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(