]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Use oslo function for parsing bool from env var
authorMaru Newby <marun@redhat.com>
Sat, 8 Nov 2014 17:20:37 +0000 (17:20 +0000)
committerMaru Newby <marun@redhat.com>
Tue, 2 Dec 2014 14:40:07 +0000 (14:40 +0000)
The tests were previously defining their own way of parsing booleans
from strings.  This change switches to using a function defined in
the oslo.utils.strutils module.

Implements: bp retargetable-functional-testing

Change-Id: Ic06de1ed67dd9cc762415d362a928a89da0ce2f0

neutron/tests/base.py
neutron/tests/functional/base.py
neutron/tests/unit/testlib_plugin.py

index b4bd93a4cdc101301f82ae422ea8ee63cb20d983..9e8ef18cb786fd483722f8f1685f041725143579 100644 (file)
@@ -27,6 +27,7 @@ import fixtures
 import mock
 from oslo.config import cfg
 from oslo.messaging import conffixture as messaging_conffixture
+from oslo.utils import strutils
 import testtools
 
 from neutron.common import config
@@ -37,7 +38,6 @@ from neutron.tests import post_mortem_debug
 
 CONF = cfg.CONF
 CONF.import_opt('state_path', 'neutron.common.config')
-TRUE_STRING = ['True', '1']
 LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
 
 ROOTDIR = os.path.dirname(__file__)
@@ -56,6 +56,11 @@ def fake_consume_in_threads(self):
     return []
 
 
+def bool_from_env(key, strict=False, default=False):
+    value = os.environ.get(key)
+    return strutils.bool_from_string(value, strict=strict, default=default)
+
+
 class BaseTestCase(testtools.TestCase):
 
     @staticmethod
@@ -78,11 +83,11 @@ class BaseTestCase(testtools.TestCase):
             self.addOnException(post_mortem_debug.get_exception_handler(
                 debugger))
 
-        if os.environ.get('OS_DEBUG') in TRUE_STRING:
+        if bool_from_env('OS_DEBUG'):
             _level = std_logging.DEBUG
         else:
             _level = std_logging.INFO
-        capture_logs = os.environ.get('OS_LOG_CAPTURE') in TRUE_STRING
+        capture_logs = bool_from_env('OS_LOG_CAPTURE')
         if not capture_logs:
             std_logging.basicConfig(format=LOG_FORMAT, level=_level)
         self.log_fixture = self.useFixture(
@@ -117,10 +122,10 @@ class BaseTestCase(testtools.TestCase):
         self.addCleanup(mock.patch.stopall)
         self.addCleanup(CONF.reset)
 
-        if os.environ.get('OS_STDOUT_CAPTURE') in TRUE_STRING:
+        if bool_from_env('OS_STDOUT_CAPTURE'):
             stdout = self.useFixture(fixtures.StringStream('stdout')).stream
             self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
-        if os.environ.get('OS_STDERR_CAPTURE') in TRUE_STRING:
+        if bool_from_env('OS_STDERR_CAPTURE'):
             stderr = self.useFixture(fixtures.StringStream('stderr')).stream
             self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
         self.useFixture(fixtures.MonkeyPatch(
index 7ca3237273506de51c3f91cc78c42252fca2ec30..7f14130be7540cff203e216cad623a9fc03e10e4 100644 (file)
@@ -49,11 +49,10 @@ class BaseSudoTestCase(base.BaseTestCase):
 
     def setUp(self):
         super(BaseSudoTestCase, self).setUp()
-        env = os.environ
-        self.sudo_enabled = env.get('OS_SUDO_TESTING') in base.TRUE_STRING
-        self.root_helper = env.get('OS_ROOTWRAP_CMD', SUDO_CMD)
+        self.sudo_enabled = base.bool_from_env('OS_SUDO_TESTING')
+        self.root_helper = os.environ.get('OS_ROOTWRAP_CMD', SUDO_CMD)
         self.fail_on_missing_deps = (
-            env.get('OS_FAIL_ON_MISSING_DEPS') in base.TRUE_STRING)
+            base.bool_from_env('OS_FAIL_ON_MISSING_DEPS'))
 
     def check_sudo_enabled(self):
         if not self.sudo_enabled:
index 2ace0e42031e0ee5dc34b360c2bb3d17b27dcb61..5b3150c81837a6929f74002b1a65f078b128e072 100644 (file)
@@ -14,7 +14,6 @@
 #    under the License.
 
 import gc
-import os
 import weakref
 
 import mock
@@ -42,7 +41,7 @@ class PluginSetupHelper(object):
         # configured to do so since calling gc.collect() after every
         # test increases test suite execution time by ~50%.
         check_plugin_deallocation = (
-            os.environ.get('OS_CHECK_PLUGIN_DEALLOCATION') in base.TRUE_STRING)
+            base.bool_from_env('OS_CHECK_PLUGIN_DEALLOCATION'))
         if check_plugin_deallocation:
             plugin = weakref.ref(nm._instance.plugin)