From 47935c2fe486bce6d154589229c989271ebd0f08 Mon Sep 17 00:00:00 2001 From: ashishk1994 Date: Thu, 29 Dec 2016 20:11:28 +0530 Subject: Refactoring of args and parser variable in ci/run_tests, prepare_env Can't import run_tests in test_run_tests.py (new unit test file). It is throwing error: nosetests: error: unrecognized arguments It is because of declaration of parser and args which are global in file, So to allow safe import of this file, we need to declare it into __main__ branch. Change-Id: Ie11ebcfd8a1b8e692bd8bf4260a54f752b67fd5e Signed-off-by: ashishk1994 --- functest/ci/prepare_env.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'functest/ci/prepare_env.py') diff --git a/functest/ci/prepare_env.py b/functest/ci/prepare_env.py index 3df3a0e0e..77bb14a88 100755 --- a/functest/ci/prepare_env.py +++ b/functest/ci/prepare_env.py @@ -29,12 +29,6 @@ import functest.utils.openstack_utils as os_utils from functest.utils.constants import CONST actions = ['start', 'check'] -parser = argparse.ArgumentParser() -parser.add_argument("action", help="Possible actions are: " - "'{d[0]}|{d[1]}' ".format(d=actions)) -parser.add_argument("-d", "--debug", help="Debug mode", action="store_true") -args = parser.parse_args() - """ logging configuration """ logger = ft_logger.Logger("prepare_env").getLogger() @@ -48,6 +42,19 @@ with open(CONFIG_PATCH_PATH) as f: functest_patch_yaml = yaml.safe_load(f) +class PrepareEnvParser(): + + def __init__(self): + self.parser = argparse.ArgumentParser() + self.parser.add_argument("action", help="Possible actions are: " + "'{d[0]}|{d[1]}' ".format(d=actions)) + self.parser.add_argument("-d", "--debug", help="Debug mode", + action="store_true") + + def parse_args(self, argv=[]): + return vars(self.parser.parse_args(argv)) + + def print_separator(): logger.info("==============================================") @@ -270,12 +277,12 @@ def check_environment(): logger.info("Functest environment installed.") -def main(): - if not (args.action in actions): +def main(**kwargs): + if not (kwargs['action'] in actions): logger.error('Argument not valid.') sys.exit() - if args.action == "start": + if kwargs['action'] == "start": logger.info("######### Preparing Functest environment #########\n") check_env_variables() create_directories() @@ -289,11 +296,13 @@ def main(): check_environment() - if args.action == "check": + if kwargs['action'] == "check": check_environment() exit(0) if __name__ == '__main__': - main() + parser = PrepareEnvParser() + args = parser.parse_args(sys.argv[1:]) + main(**args) -- cgit 1.2.3-korg