Add additional runner and env utils tests

Adds several tests to exercise more of runner and the env utils.

Makes several modifications to config.py, runner.py, and utils/env.py,
with the aim of making them more testable.

Change-Id: I708df9e5315f3dadd028c8107ac6416c707e90a0
Closes-Bug: #1634296
This commit is contained in:
Charles Neill 2016-10-19 21:06:55 -05:00
parent dabac296a7
commit 18781c37cd
5 changed files with 87 additions and 31 deletions

View File

@ -22,6 +22,7 @@ from syntribos.utils.file_utils import ExistingDirType
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
OPTS_REGISTERED = False
def handle_config_exception(exc):
@ -96,23 +97,26 @@ def list_opts():
def register_opts():
# CLI options
CONF.register_cli_opts(list_cli_opts())
# Syntribos options
CONF.register_group(syntribos_group)
CONF.register_cli_opts(list_syntribos_opts(), group=syntribos_group)
# Keystone options
CONF.register_group(user_group)
CONF.register_opts(list_user_opts(), group=user_group)
# Test options
CONF.register_group(test_group)
CONF.register_opts(list_test_opts(), group=test_group)
# Logger options
CONF.register_group(logger_group)
CONF.register_opts(list_logger_opts(), group=logger_group)
# Remote options
CONF.register_group(remote_group)
CONF.register_opts(list_remote_opts(), group=remote_group)
global OPTS_REGISTERED
if not OPTS_REGISTERED:
# CLI options
CONF.register_cli_opts(list_cli_opts())
# Syntribos options
CONF.register_group(syntribos_group)
CONF.register_cli_opts(list_syntribos_opts(), group=syntribos_group)
# Keystone options
CONF.register_group(user_group)
CONF.register_opts(list_user_opts(), group=user_group)
# Test options
CONF.register_group(test_group)
CONF.register_opts(list_test_opts(), group=test_group)
# Logger options
CONF.register_group(logger_group)
CONF.register_opts(list_logger_opts(), group=logger_group)
# Remote options
CONF.register_group(remote_group)
CONF.register_opts(list_remote_opts(), group=remote_group)
OPTS_REGISTERED = True
def list_cli_opts():

View File

@ -59,7 +59,6 @@ class Runner(object):
print("{test:<50}{desc}\r".format(
test=test, desc=test_description))
print("\n")
exit(0)
@classmethod
def load_modules(cls, package):
@ -119,16 +118,17 @@ class Runner(object):
return LOG
@classmethod
def setup_config(cls, use_file=False):
def setup_config(cls, use_file=False, argv=None):
"""Register CLI options & parse config file."""
if argv is None:
argv = sys.argv[1:]
try:
syntribos.config.register_opts()
if use_file:
CONF(
sys.argv[1:],
default_config_files=[ENV.get_default_conf_file()])
CONF(argv,
default_config_files=[ENV.get_default_conf_file()])
else:
CONF(sys.argv[1:], default_config_files=[])
CONF(argv, default_config_files=[])
except Exception as exc:
syntribos.config.handle_config_exception(exc)
@ -161,14 +161,16 @@ class Runner(object):
global result
cli.print_symbol()
cls.setup_config()
if CONF.sub_command.name == "init":
cls.setup_config()
ENV.initialize_syntribos_env()
exit(0)
elif CONF.sub_command.name == "list_tests":
cls.setup_config()
cls.list_tests()
exit(0)
if not ENV.is_syntribos_initialized():
print("Syntribos was not initialized. Please run the 'init' "
@ -176,6 +178,7 @@ class Runner(object):
"information about the installation process.")
exit(1)
cls.setup_config(use_file=True)
cls.setup_runtime_env()
decorator = unittest.runner._WritelnDecorator(cls.output)
@ -351,10 +354,11 @@ class Runner(object):
run_time = time.time() - template_start_time
LOG.debug("Run time: {} sec.".format(run_time))
num_tests = result.testsRun - result.testsRunSinceLastPrint
print("\nRan {num} test(s) in {time:.3f}s\n".format(
num=num_tests, time=run_time))
result.testsRunSinceLastPrint = result.testsRun
if hasattr(result, "testsRun"):
num_tests = result.testsRun - result.testsRunSinceLastPrint
print("\nRan {num} test(s) in {time:.3f}s\n".format(
num=num_tests, time=run_time))
result.testsRunSinceLastPrint = result.testsRun
except KeyboardInterrupt:
result.print_result(cls.start_time)

View File

@ -79,7 +79,7 @@ def get_syntribos_root():
def get_syntribos_path(*args):
return os.path.join(get_syntribos_root(), *args)
return os.path.abspath(os.path.join(get_syntribos_root(), *args))
def get_default_conf_file():
@ -182,8 +182,9 @@ def initialize_syntribos_env():
root_dir = get_venv_root() if is_venv() else get_user_home_root()
force = CONF.sub_command.force
if CONF.sub_command.custom_install_root:
root_dir = CONF.sub_command.custom_install_root
custom_root = CONF.sub_command.custom_install_root or ""
if custom_root:
root_dir = custom_root
elif CONF.sub_command.force:
pass
else:

View File

@ -13,11 +13,15 @@
# limitations under the License.
import os
import mock
import six
import testtools
import syntribos.config
import syntribos.utils.env as ENV
syntribos.config.register_opts()
class EnvUtilsUnittest(testtools.TestCase):
@ -29,3 +33,34 @@ class EnvUtilsUnittest(testtools.TestCase):
self.assertIsNot("", home_root)
self.assertIsNot("/", home_root)
self.assertTrue(os.path.isdir(home_dir))
def test_get_syntribos_root(self):
"""Check that we get something reasonable from get_syntribos_root."""
root = ENV.get_syntribos_root()
root_parent = os.path.abspath(os.path.join(root, ".."))
self.assertIsInstance(root, six.string_types)
self.assertIsNot("", root)
self.assertIsNot("/", root)
self.assertTrue(os.path.isdir(root_parent))
def test_get_syntribos_path(self):
"""Check that we get something reasonable from get_syntribos_path."""
root = ENV.get_syntribos_root()
self.assertIsInstance(root, six.string_types)
root_parent = os.path.abspath(os.path.join(root, ".."))
path_parent = ENV.get_syntribos_path("..")
self.assertEqual(root_parent, path_parent)
def test_get_log_dir_name(self):
"""Check that we get something reasonable from get_log_dir_name."""
log_dir = ENV.get_log_dir_name()
self.assertIsInstance(log_dir, six.string_types)
root_parent = os.path.abspath(os.path.join(log_dir, "..", ".."))
self.assertIsInstance(log_dir, six.string_types)
self.assertIsNot("", log_dir)
self.assertIsNot("/", log_dir)
self.assertTrue(os.path.isdir(root_parent))
@mock.patch("os.makedirs")
def test_create_env_dirs(self, makedirs):
ENV.create_env_dirs(ENV.get_syntribos_root())

View File

@ -120,3 +120,15 @@ class RunnerUnittest(testtools.TestCase):
"COMMAND_INJECTION_HEADERS", "COMMAND_INJECTION_PARAMS"]
loaded_tests = self.r.get_tests(["SQL", "COMMAND"], ["URL", "BODY"])
self._compare_tests(expected, loaded_tests)
def test_list_tests(self):
"""Check that we can list tests and exit successfully."""
self.r.list_tests()
def test_run_empty_tests(self):
"""Call Runner.run_given_tests with an empty list for sanity check."""
self.r.run_given_tests([], "", "")
def test_dry_run_empty_tests(self):
"""Call Runner.dry_run with empty list for sanity check."""
self.r.dry_run([], "", "", {})