]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Remove usage of locals() for formatting from cinder.tests.*
authorHaomai Wang <haomai@unitedstack.com>
Mon, 24 Jun 2013 06:03:31 +0000 (14:03 +0800)
committerHaomai Wang <haomai@unitedstack.com>
Mon, 24 Jun 2013 06:03:31 +0000 (14:03 +0800)
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
cinder/tests/integrated/api/client.py
cinder/tests/test_migrations.py

index 68aaf79646fe965e101983e8b3cf0dfbd790e2e3..f4da47d788ef1a037058d684cebbb0ab79a1d261 100644 (file)
@@ -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)
index 516a2a0e70536ec235f9ee16c82d760652bdbd8a..ac53006c8f75603073296b278cd88e57f64e865c 100644 (file)
@@ -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:
index 2683307ffe976c7c76273ee77bb6e06cea5264e4..edb0fed9f61dd302195d89edffbcf88ac108a694 100644 (file)
@@ -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')