From 729a08a019837bf4a5cbce5eee46a0a6e8e12acd Mon Sep 17 00:00:00 2001 From: Haomai Wang Date: Mon, 24 Jun 2013 14:03:31 +0800 Subject: [PATCH] Remove usage of locals() for formatting from cinder.tests.* Using of locals() for formatting string is a nasty thing because: 1) It is not so clear as using explicit dicts 2) It could produce hidden errors during refactoring 3) Changing name of variable causes change in message 4) Creating a lot of unused variables Fix bug 1171936 Change-Id: I67aa5bff650b55484ca76adf5be8555f72a4c426 --- cinder/tests/fake_utils.py | 5 ++--- cinder/tests/integrated/api/client.py | 15 ++++++++------- cinder/tests/test_migrations.py | 22 +++++++++++++--------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cinder/tests/fake_utils.py b/cinder/tests/fake_utils.py index 68aaf7964..f4da47d78 100644 --- a/cinder/tests/fake_utils.py +++ b/cinder/tests/fake_utils.py @@ -96,10 +96,9 @@ def fake_execute(*cmd_parts, **kwargs): LOG.debug(_('Faked command raised an exception %s'), e) raise - stdout = reply[0] - stderr = reply[1] LOG.debug(_("Reply to faked command is stdout='%(stdout)s' " - "stderr='%(stderr)s'") % locals()) + "stderr='%(stderr)s'") % {'stdout': reply[0], + 'stderr': reply[1]}) # Replicate the sleep call in the real function greenthread.sleep(0) diff --git a/cinder/tests/integrated/api/client.py b/cinder/tests/integrated/api/client.py index 516a2a0e7..ac53006c8 100644 --- a/cinder/tests/integrated/api/client.py +++ b/cinder/tests/integrated/api/client.py @@ -31,11 +31,9 @@ class OpenStackApiException(Exception): message = 'Unspecified error' if response: - _status = response.status - _body = response.read() - message = _('%(message)s\nStatus Code: %(_status)s\n' - 'Body: %(_body)s') % locals() + 'Body: %(_body)s') % {'_status': response.status, + '_body': response.read()} super(OpenStackApiException, self).__init__(message) @@ -101,7 +99,8 @@ class TestOpenStackClient(object): relative_url = parsed_url.path if parsed_url.query: relative_url = relative_url + "?" + parsed_url.query - LOG.info(_("Doing %(method)s on %(relative_url)s") % locals()) + LOG.info(_("Doing %(method)s on %(relative_url)s"), + {'method': method, 'relative_url': relative_url}) if body: LOG.info(_("Body: %s") % body) @@ -121,7 +120,8 @@ class TestOpenStackClient(object): headers=headers) http_status = response.status - LOG.debug(_("%(auth_uri)s => code %(http_status)s") % locals()) + LOG.debug(_("%(auth_uri)s => code %(http_status)s"), + {'auth_uri': auth_uri, 'http_status': http_status}) if http_status == 401: raise OpenStackApiAuthenticationException(response=response) @@ -147,7 +147,8 @@ class TestOpenStackClient(object): response = self.request(full_uri, **kwargs) http_status = response.status - LOG.debug(_("%(relative_uri)s => code %(http_status)s") % locals()) + LOG.debug(_("%(relative_uri)s => code %(http_status)s"), + {'relative_uri': relative_uri, 'http_status': http_status}) if check_response_status: if http_status not in check_response_status: diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 2683307ff..edb0fed9f 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -54,8 +54,9 @@ def _get_connect_string(backend, if backend == "postgres": backend = "postgresql+psycopg2" - return ("%(backend)s://%(user)s:%(passwd)s@localhost/%(database)s" - % locals()) + return ("%(backend)s://%(user)s:%(passwd)s@localhost/%(database)s", + {'backend': backend, 'user': user, 'passwd': passwd, + 'database': database}) def _is_mysql_avail(**kwargs): @@ -186,10 +187,11 @@ class TestMigrations(test.TestCase): if len(auth_pieces) > 1: if auth_pieces[1].strip(): password = "-p\"%s\"" % auth_pieces[1] - sql = ("drop database if exists %(database)s; " - "create database %(database)s;") % locals() + sql = ("drop database if exists %(database)s; create database " + "%(database)s;") % {'database': database} cmd = ("mysql -u \"%(user)s\" %(password)s -h %(host)s " - "-e \"%(sql)s\"") % locals() + "-e \"%(sql)s\"") % {'user': user, 'password': password, + 'host': host, 'sql': sql} execute_cmd(cmd) elif conn_string.startswith('postgresql'): database = conn_pieces.path.strip('/') @@ -212,11 +214,13 @@ class TestMigrations(test.TestCase): # operations there is a special database template1. sqlcmd = ("psql -w -U %(user)s -h %(host)s -c" " '%(sql)s' -d template1") - sql = ("drop database if exists %(database)s;") % locals() - droptable = sqlcmd % locals() + sql = ("drop database if exists %(database)s;") % {'database': + database} + droptable = sqlcmd % {'user': user, 'host': host, 'sql': sql} execute_cmd(droptable) - sql = ("create database %(database)s;") % locals() - createtable = sqlcmd % locals() + sql = ("create database %(database)s;") % {'database': + database} + createtable = sqlcmd % {'user': user, 'host': host, 'sql': sql} execute_cmd(createtable) os.unsetenv('PGPASSWORD') os.unsetenv('PGUSER') -- 2.45.2