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
This commit is contained in:
Maru Newby 2014-11-08 17:20:37 +00:00
parent 2e20452fe5
commit b1b89a7394
3 changed files with 14 additions and 11 deletions

View 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(

View 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:

View 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)