From 47935c2fe486bce6d154589229c989271ebd0f08 Mon Sep 17 00:00:00 2001 From: ashishk1994 Date: Thu, 29 Dec 2016 20:11:28 +0530 Subject: [PATCH] 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 ++++++++++++++--------- functest/ci/run_tests.py | 61 ++++++++++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 35 deletions(-) diff --git a/functest/ci/prepare_env.py b/functest/ci/prepare_env.py index 3df3a0e0..77bb14a8 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) diff --git a/functest/ci/run_tests.py b/functest/ci/run_tests.py index 7aac9d2c..722929d2 100755 --- a/functest/ci/run_tests.py +++ b/functest/ci/run_tests.py @@ -26,17 +26,6 @@ import functest.utils.openstack_snapshot as os_snapshot import functest.utils.openstack_utils as os_utils from functest.utils.constants import CONST -parser = argparse.ArgumentParser() -parser.add_argument("-t", "--test", dest="test", action='store', - help="Test case or tier (group of tests) to be executed. " - "It will run all the test if not specified.") -parser.add_argument("-n", "--noclean", help="Do not clean OpenStack resources" - " after running each test (default=false).", - action="store_true") -parser.add_argument("-r", "--report", help="Push results to database " - "(default=false).", action="store_true") -args = parser.parse_args() - """ logging configuration """ logger = ft_logger.Logger("run_tests").getLogger() @@ -49,6 +38,26 @@ EXEC_SCRIPT = ("%s/functest/ci/exec_test.sh" % CONST.dir_repo_functest) # this variable will change to -1 +class RunTestsParser(): + + def __init__(self): + self.parser = argparse.ArgumentParser() + self.parser.add_argument("-t", "--test", dest="test", action='store', + help="Test case or tier (group of tests) " + "to be executed. It will run all the test " + "if not specified.") + self.parser.add_argument("-n", "--noclean", help="Do not clean " + "OpenStack resources after running each " + "test (default=false).", + action="store_true") + self.parser.add_argument("-r", "--report", help="Push results to " + "database (default=false).", + action="store_true") + + def parse_args(self, argv=[]): + return vars(self.parser.parse_args(argv)) + + class GlobalVariables: EXECUTED_TEST_CASES = [] OVERALL_RESULT = 0 @@ -118,7 +127,7 @@ def get_run_dict_if_defined(testname): return None -def run_test(test, tier_name): +def run_test(test, tier_name, testcases=None): result_str = "PASS" start = datetime.datetime.now() test_name = test.get_name() @@ -174,7 +183,7 @@ def run_test(test, tier_name): result_str = "FAIL" if test.is_blocking(): - if not args.test or args.test == "all": + if not testcases or testcases == "all": logger.info("This test case is blocking. Aborting overall " "execution.") # if it is a single test we don't print the whole results table @@ -222,7 +231,7 @@ def run_all(tiers): generate_report.main(GlobalVariables.EXECUTED_TEST_CASES) -def main(): +def main(**kwargs): CI_INSTALLER_TYPE = CONST.INSTALLER_TYPE CI_SCENARIO = CONST.DEPLOY_SCENARIO @@ -230,27 +239,29 @@ def main(): file = CONST.functest_testcases_yaml _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file) - if args.noclean: + if kwargs['noclean']: GlobalVariables.CLEAN_FLAG = False - if args.report: + if kwargs['report']: GlobalVariables.REPORT_FLAG = True - if args.test: + if kwargs['test']: source_rc_file() - if _tiers.get_tier(args.test): - run_tier(_tiers.get_tier(args.test)) + if _tiers.get_tier(kwargs['test']): + run_tier(_tiers.get_tier(kwargs['test'])) - elif _tiers.get_test(args.test): - run_test(_tiers.get_test(args.test), _tiers.get_tier(args.test)) + elif _tiers.get_test(kwargs['test']): + run_test(_tiers.get_test(kwargs['test']), + _tiers.get_tier(kwargs['test']), + kwargs['test']) - elif args.test == "all": + elif kwargs['test'] == "all": run_all(_tiers) else: logger.error("Unknown test case or tier '%s', or not supported by " "the given scenario '%s'." - % (args.test, CI_SCENARIO)) + % (kwargs['test'], CI_SCENARIO)) logger.debug("Available tiers are:\n\n%s" % _tiers) else: @@ -261,4 +272,6 @@ def main(): if __name__ == '__main__': - main() + parser = RunTestsParser() + args = parser.parse_args(sys.argv[1:]) + main(**args) -- 2.16.6