Port guestagent test_dbaas to Python 3

* Use encodeutils.exception_to_unicode() to get the message of
  exceptions. On Python 3, the message attribute of exceptions has
  been removed.
* NamedTemporaryFile: open files in text mode, not in binary mode.
* test_dbaas: set rowcount in execute() mocks to avoid comparison
  with a mock which now raises a TypeError on Python 3.
* replace "rowcount <= limit" with
  "limit is not None and rowcount <= limit" to avoid comparison
  between int and None (TypeError on Python 3)
* Replace urllib.quote() with six.moves.urllib.parse.quote
* blacklist-py3.txt: remove guestagent.test_dbaas to run it
  on Python 3

Partially implements: blueprint trove-python3
Change-Id: Ibdd34485ce4f2fb3572dcd7f57cbf9e5b6f42a13
This commit is contained in:
Victor Stinner 2016-06-30 15:42:42 +02:00
parent ecf4b675e8
commit 207991af39
5 changed files with 17 additions and 12 deletions

View File

@ -2,7 +2,6 @@
# all unit tests will pass on Python 3.
backup.test_backup_models
guestagent.test_cassandra_manager
guestagent.test_dbaas
guestagent.test_mongodb_manager
guestagent.test_operating_system
guestagent.test_volume

View File

@ -14,6 +14,7 @@
# under the License.
from oslo_log import log as logging
from oslo_utils import encodeutils
from trove.common import cfg
from trove.common import exception
@ -248,8 +249,9 @@ class DB2Admin(object):
next_marker = None
LOG.debug("databases = %s." % str(databases))
except exception.ProcessExecutionError as pe:
err_msg = encodeutils.exception_to_unicode(pe)
LOG.exception(_("An error occurred listing databases: %s.") %
pe.message)
err_msg)
pass
return databases, next_marker

View File

@ -372,7 +372,7 @@ class VerticaApp(object):
temp_function=tempfile.NamedTemporaryFile):
"""Write the configuration contents to vertica.cnf file."""
LOG.debug('Defining config holder at %s.' % system.VERTICA_CONF)
tempfile = temp_function(delete=False)
tempfile = temp_function('w', delete=False)
try:
config.write(tempfile)
tempfile.close()
@ -549,7 +549,7 @@ class VerticaApp(object):
all_keys = '\n'.join(public_keys) + "\n"
try:
with tempfile.NamedTemporaryFile(delete=False) as tempkeyfile:
with tempfile.NamedTemporaryFile("w", delete=False) as tempkeyfile:
tempkeyfile.write(all_keys)
copy_key_cmd = (("install -o %(user)s -m 600 %(source)s %(target)s"
) % {'user': user, 'source': tempkeyfile.name,

View File

@ -21,10 +21,11 @@ from collections import defaultdict
import os
import re
import six
import urllib
import uuid
from oslo_log import log as logging
from oslo_utils import encodeutils
from six.moves import urllib
import sqlalchemy
from sqlalchemy import exc
from sqlalchemy import interfaces
@ -364,9 +365,10 @@ class BaseMySqlAdmin(object):
user.name = username # Could possibly throw a BadRequest here.
except ValueError as ve:
LOG.exception(_("Error Getting user information"))
err_msg = encodeutils.exception_to_unicode(ve)
raise exception.BadRequest(_("Username %(user)s is not valid"
": %(reason)s") %
{'user': username, 'reason': ve.message}
{'user': username, 'reason': err_msg}
)
with self.local_sql_client(self.mysql_app.get_engine()) as client:
q = sql_query.Query()
@ -465,7 +467,7 @@ class BaseMySqlAdmin(object):
mysql_db.collate = database[2]
databases.append(mysql_db.serialize())
LOG.debug("databases = " + str(databases))
if database_names.rowcount <= limit:
if limit is not None and database_names.rowcount <= limit:
next_marker = None
return databases, next_marker
@ -529,7 +531,7 @@ class BaseMySqlAdmin(object):
self._associate_dbs(mysql_user)
next_marker = row['Marker']
users.append(mysql_user.serialize())
if result.rowcount <= limit:
if limit is not None and result.rowcount <= limit:
next_marker = None
LOG.debug("users = " + str(users))
@ -610,7 +612,7 @@ class BaseMySqlApp(object):
pwd = self.get_auth_password()
ENGINE = sqlalchemy.create_engine(
CONNECTION_STR_FORMAT % (ADMIN_USER_NAME,
urllib.quote(pwd.strip())),
urllib.parse.quote(pwd.strip())),
pool_recycle=120, echo=CONF.sql_query_logging,
listeners=[self.keep_alive_connection_cls()])
return ENGINE
@ -693,7 +695,7 @@ class BaseMySqlApp(object):
LOG.debug("Switching to the '%s' user now." % ADMIN_USER_NAME)
engine = sqlalchemy.create_engine(
CONNECTION_STR_FORMAT % (ADMIN_USER_NAME,
urllib.quote(admin_password)),
urllib.parse.quote(admin_password)),
echo=True)
with self.local_sql_client(engine) as client:
self._remove_anonymous_user(client)

View File

@ -626,6 +626,7 @@ class MySqlAdminTest(trove_testtools.TestCase):
" ORDER BY schema_name ASC LIMIT " + str(limit + 1) + ";"
)
with patch.object(self.mock_client, 'execute') as mock_execute:
mock_execute.return_value.rowcount = 0
self.mySqlAdmin.list_databases(limit)
self._assert_execute_call(expected, mock_execute)
@ -684,6 +685,7 @@ class MySqlAdminTest(trove_testtools.TestCase):
)
with patch.object(self.mock_client, 'execute') as mock_execute:
mock_execute.return_value.rowcount = 0
self.mySqlAdmin.list_users(limit)
self._assert_execute_call(expected, mock_execute)
@ -2941,7 +2943,7 @@ class VerticaAppTest(trove_testtools.TestCase):
self.assertEqual(0, vertica_system.shell_execute.call_count)
def test_vertica_write_config(self):
temp_file_handle = tempfile.NamedTemporaryFile(delete=False)
temp_file_handle = tempfile.NamedTemporaryFile("w", delete=False)
mock_mkstemp = MagicMock(return_value=(temp_file_handle))
mock_unlink = Mock(return_value=0)
self.app.write_config(config=self.test_config,
@ -2967,7 +2969,7 @@ class VerticaAppTest(trove_testtools.TestCase):
def test_vertica_error_in_write_config_verify_unlink(self):
mock_unlink = Mock(return_value=0)
temp_file_handle = tempfile.NamedTemporaryFile(delete=False)
temp_file_handle = tempfile.NamedTemporaryFile("w", delete=False)
mock_mkstemp = MagicMock(return_value=temp_file_handle)
with patch.object(vertica_system, 'shell_execute',