3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
19 import functest.ci.generate_report as generate_report
20 import functest.ci.tier_builder as tb
21 import functest.core.TestCasesBase as TestCasesBase
22 import functest.utils.functest_logger as ft_logger
23 import functest.utils.functest_utils as ft_utils
24 import functest.utils.openstack_clean as os_clean
25 import functest.utils.openstack_snapshot as os_snapshot
26 import functest.utils.openstack_utils as os_utils
29 parser = argparse.ArgumentParser()
30 parser.add_argument("-t", "--test", dest="test", action='store',
31 help="Test case or tier (group of tests) to be executed. "
32 "It will run all the test if not specified.")
33 parser.add_argument("-n", "--noclean", help="Do not clean OpenStack resources"
34 " after running each test (default=false).",
36 parser.add_argument("-r", "--report", help="Push results to database "
37 "(default=false).", action="store_true")
38 args = parser.parse_args()
41 """ logging configuration """
42 logger = ft_logger.Logger("run_tests").getLogger()
45 """ global variables """
46 EXEC_SCRIPT = ("%s/ci/exec_test.sh" % ft_utils.FUNCTEST_REPO)
49 EXECUTED_TEST_CASES = []
51 # This will be the return code of this script. If any of the tests fails,
52 # this variable will change to -1
56 def print_separator(str, count=45):
58 for i in range(0, count - 1):
60 logger.info("%s" % line)
64 rc_file = os.getenv('creds')
65 if not os.path.isfile(rc_file):
66 logger.error("RC file %s does not exist..." % rc_file)
68 logger.debug("Sourcing the OpenStack RC file...")
69 os_utils.source_credentials(rc_file)
72 def generate_os_snapshot():
80 def update_test_info(test_name, result, duration):
81 for test in EXECUTED_TEST_CASES:
82 if test['test_name'] == test_name:
83 test.update({"result": result,
84 "duration": duration})
87 def get_run_dict_if_defined(testname):
89 dict = ft_utils.get_dict_by_test(testname)
91 logger.error("Cannot get {}'s config options".format(testname))
96 logger.exception("Cannot get {}'s config options".format(testname))
100 def run_test(test, tier_name):
101 global OVERALL_RESULT, EXECUTED_TEST_CASES
103 start = datetime.datetime.now()
104 test_name = test.get_name()
105 logger.info("\n") # blank line
107 logger.info("Running test case '%s'..." % test_name)
109 logger.debug("\n%s" % test)
112 generate_os_snapshot()
114 flags = (" -t %s" % (test_name))
118 result = TestCasesBase.TestCasesBase.EX_RUN_ERROR
119 run_dict = get_run_dict_if_defined(test_name)
122 module = importlib.import_module(run_dict['module'])
123 cls = getattr(module, run_dict['class'])
125 result = test_case.run()
126 if result == TestCasesBase.TestCasesBase.EX_OK and REPORT_FLAG:
127 result = test_case.push_to_db()
129 logger.exception("Cannot import module {}".format(
131 except AttributeError:
132 logger.exception("Cannot get class {}".format(
135 cmd = ("%s%s" % (EXEC_SCRIPT, flags))
136 logger.info("Executing command {} because {} "
137 "doesn't implement the new framework".format(
139 result = ft_utils.execute_command(cmd)
143 end = datetime.datetime.now()
144 duration = (end - start).seconds
145 duration_str = ("%02d:%02d" % divmod(duration, 60))
146 logger.info("Test execution time: %s" % duration_str)
149 logger.error("The test case '%s' failed. " % test_name)
153 if test.is_blocking():
154 if not args.test or args.test == "all":
155 logger.info("This test case is blocking. Aborting overall "
157 # if it is a single test we don't print the whole results table
158 update_test_info(test_name, result_str, duration_str)
159 generate_report.main(EXECUTED_TEST_CASES)
160 logger.info("Execution exit value: %s" % OVERALL_RESULT)
161 sys.exit(OVERALL_RESULT)
163 update_test_info(test_name, result_str, duration_str)
167 tier_name = tier.get_name()
168 tests = tier.get_tests()
169 if tests is None or len(tests) == 0:
170 logger.info("There are no supported test cases in this tier "
171 "for the given scenario")
173 logger.info("\n\n") # blank line
175 logger.info("Running tier '%s'" % tier_name)
177 logger.debug("\n%s" % tier)
179 run_test(test, tier_name)
183 global EXECUTED_TEST_CASES
185 BUILD_TAG = os.getenv('BUILD_TAG')
186 if BUILD_TAG is not None and re.search("daily", BUILD_TAG) is not None:
193 for tier in tiers.get_tiers():
194 if (len(tier.get_tests()) != 0 and
195 re.search(CI_LOOP, tier.get_ci_loop()) is not None):
196 tiers_to_run.append(tier)
197 summary += ("\n - %s:\n\t %s"
199 tier.get_test_names()))
201 logger.info("Tests to be executed:%s" % summary)
202 EXECUTED_TEST_CASES = generate_report.init(tiers_to_run)
203 for tier in tiers_to_run:
206 generate_report.main(EXECUTED_TEST_CASES)
213 CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
214 CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
216 file = ft_utils.get_testcases_file()
217 _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
227 if _tiers.get_tier(args.test):
228 run_tier(_tiers.get_tier(args.test))
230 elif _tiers.get_test(args.test):
231 run_test(_tiers.get_test(args.test), _tiers.get_tier(args.test))
233 elif args.test == "all":
237 logger.error("Unknown test case or tier '%s', or not supported by "
238 "the given scenario '%s'."
239 % (args.test, CI_SCENARIO))
240 logger.debug("Available tiers are:\n\n%s"
245 logger.info("Execution exit value: %s" % OVERALL_RESULT)
246 sys.exit(OVERALL_RESULT)
248 if __name__ == '__main__':