From: Maru Newby Date: Sat, 8 Nov 2014 17:20:37 +0000 (+0000) Subject: Use oslo function for parsing bool from env var X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=b1b89a7394e23407010468c802337152d25d0463;p=openstack-build%2Fneutron-build.git Use oslo function for parsing bool from env var 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 --- diff --git a/neutron/tests/base.py b/neutron/tests/base.py index b4bd93a4c..9e8ef18cb 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -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( diff --git a/neutron/tests/functional/base.py b/neutron/tests/functional/base.py index 7ca323727..7f14130be 100644 --- a/neutron/tests/functional/base.py +++ b/neutron/tests/functional/base.py @@ -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: diff --git a/neutron/tests/unit/testlib_plugin.py b/neutron/tests/unit/testlib_plugin.py index 2ace0e420..5b3150c81 100644 --- a/neutron/tests/unit/testlib_plugin.py +++ b/neutron/tests/unit/testlib_plugin.py @@ -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)