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
18 import functest.ci.generate_report as generate_report
19 import functest.ci.tier_builder as tb
20 import functest.utils.functest_logger as ft_logger
21 import functest.utils.functest_utils as ft_utils
22 import functest.utils.openstack_clean as os_clean
23 import functest.utils.openstack_snapshot as os_snapshot
24 import functest.utils.openstack_utils as os_utils
25 from functest.testcases.Controllers.ODL.OpenDaylightTesting import ODLTestCases
27 parser = argparse.ArgumentParser()
28 parser.add_argument("-t", "--test", dest="test", action='store',
29 help="Test case or tier (group of tests) to be executed. "
30 "It will run all the test if not specified.")
31 parser.add_argument("-n", "--noclean", help="Do not clean OpenStack resources"
32 " after running each test (default=false).",
34 parser.add_argument("-r", "--report", help="Push results to database "
35 "(default=false).", action="store_true")
36 args = parser.parse_args()
39 """ logging configuration """
40 logger = ft_logger.Logger("run_tests").getLogger()
43 """ global variables """
44 EXEC_SCRIPT = ("%s/ci/exec_test.sh" % ft_utils.FUNCTEST_REPO)
47 EXECUTED_TEST_CASES = []
49 # This will be the return code of this script. If any of the tests fails,
50 # this variable will change to -1
54 def print_separator(str, count=45):
56 for i in range(0, count - 1):
58 logger.info("%s" % line)
62 rc_file = os.getenv('creds')
63 if not os.path.isfile(rc_file):
64 logger.error("RC file %s does not exist..." % rc_file)
66 logger.debug("Sourcing the OpenStack RC file...")
67 os_utils.source_credentials(rc_file)
70 def generate_os_snapshot():
78 def update_test_info(test_name, result, duration):
79 for test in EXECUTED_TEST_CASES:
80 if test['test_name'] == test_name:
81 test.update({"result": result,
82 "duration": duration})
85 def run_test(test, tier_name):
86 global OVERALL_RESULT, EXECUTED_TEST_CASES
88 start = datetime.datetime.now()
89 test_name = test.get_name()
90 logger.info("\n") # blank line
92 logger.info("Running test case '%s'..." % test_name)
94 logger.debug("\n%s" % test)
97 generate_os_snapshot()
99 flags = (" -t %s" % (test_name))
103 if test_name == 'odl':
104 result = ODLTestCases.functest_run()
105 if result and REPORT_FLAG:
106 result = ODLTestCases.push_to_db()
109 cmd = ("%s%s" % (EXEC_SCRIPT, flags))
110 logger.debug("Executing command '%s'" % cmd)
111 result = ft_utils.execute_command(cmd, exit_on_error=False)
115 end = datetime.datetime.now()
116 duration = (end - start).seconds
117 duration_str = ("%02d:%02d" % divmod(duration, 60))
118 logger.info("Test execution time: %s" % duration_str)
121 logger.error("The test case '%s' failed. " % test_name)
125 if test.is_blocking():
126 if not args.test or args.test == "all":
127 logger.info("This test case is blocking. Aborting overall "
129 # if it is a single test we don't print the whole results table
130 update_test_info(test_name, result_str, duration_str)
131 generate_report.main(EXECUTED_TEST_CASES)
132 logger.info("Execution exit value: %s" % OVERALL_RESULT)
133 sys.exit(OVERALL_RESULT)
135 update_test_info(test_name, result_str, duration_str)
139 tier_name = tier.get_name()
140 tests = tier.get_tests()
141 if tests is None or len(tests) == 0:
142 logger.info("There are no supported test cases in this tier "
143 "for the given scenario")
145 logger.info("\n\n") # blank line
147 logger.info("Running tier '%s'" % tier_name)
149 logger.debug("\n%s" % tier)
151 run_test(test, tier_name)
155 global EXECUTED_TEST_CASES
157 BUILD_TAG = os.getenv('BUILD_TAG')
158 if BUILD_TAG is not None and re.search("daily", BUILD_TAG) is not None:
165 for tier in tiers.get_tiers():
166 if (len(tier.get_tests()) != 0 and
167 re.search(CI_LOOP, tier.get_ci_loop()) is not None):
168 tiers_to_run.append(tier)
169 summary += ("\n - %s:\n\t %s"
171 tier.get_test_names()))
173 logger.info("Tests to be executed:%s" % summary)
174 EXECUTED_TEST_CASES = generate_report.init(tiers_to_run)
175 for tier in tiers_to_run:
178 generate_report.main(EXECUTED_TEST_CASES)
185 CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
186 CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
188 file = ft_utils.get_testcases_file()
189 _tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, file)
199 if _tiers.get_tier(args.test):
200 run_tier(_tiers.get_tier(args.test))
202 elif _tiers.get_test(args.test):
203 run_test(_tiers.get_test(args.test), _tiers.get_tier(args.test))
205 elif args.test == "all":
209 logger.error("Unknown test case or tier '%s', or not supported by "
210 "the given scenario '%s'."
211 % (args.test, CI_SCENARIO))
212 logger.debug("Available tiers are:\n\n%s"
217 logger.info("Execution exit value: %s" % OVERALL_RESULT)
218 sys.exit(OVERALL_RESULT)
220 if __name__ == '__main__':