def get_build_path(): """ Return the random build path """ return ("coral_build_" + time_util.local_strftime(time_util.utcnow(), "%Y-%m-%d-%H_%M_%S-") + utils.random_word(8))
def run_test(log, workspace, only_test_names, first_test_names, local_host, reverse_order, start, stop, skip_basic, test_functs, args): """ Run test. If only is specified together with start/stop, start/stop option will be ignored. If only is specified together with reverse_order, reverse_order option will be ignored. Only/first tests can repeat tests, e.g. first=testA,testA would repeat testA for twice. :param skip_basic: Do not add basic test when it is not selected. :param test_functs: A list of function that has the argument types of: test_funct(log, test_workspace, *args) """ # pylint: disable=too-many-branches,too-many-locals # pylint: disable=too-many-statements,global-statement test_dict = {} for test_funct in test_functs: test_dict[test_funct.__name__] = test_funct if len(test_functs) == 0: log.cl_error("no test to run") return -1 if test_functs[0].__name__ != "basic": log.cl_error("the first test is not [basic]") return -1 basic_test = test_functs[0] # Reverse order won't change the order of first tests selected_tests = [] if first_test_names is not None: for test_name in first_test_names: if test_name not in test_dict: log.cl_error("first test [%s] does not exist", test_name) return -1 test_funct = test_dict[test_name] selected_tests.append(test_funct) if only_test_names is not None: for test_name in only_test_names: if test_name not in test_dict: log.cl_error("only test [%s] does not exist", test_name) return -1 test_funct = test_dict[test_name] selected_tests.append(test_funct) else: start_index = 0 if start is not None: if start not in test_dict: log.cl_error("start test [%s] does not exist", start) return -1 for test_funct in test_functs: if test_funct.__name__ == start: break start_index += 1 if start_index == len(test_functs): log.cl_error("failed to find the index of start test [%s]", start) return -1 stop_index = len(test_functs) - 1 if stop is not None: if stop not in test_dict: log.cl_error("stop test [%s] does not exist", stop) return -1 stop_index = 0 for test_funct in test_functs: if test_funct.__name__ == stop: break stop_index += 1 if stop_index == len(test_functs): log.cl_error("failed to find the index of start test [%s]", stop) return -1 if stop_index < start_index: log.cl_error("start test [%s] is behind stop test [%s]", start, stop) return -1 test_index = 0 for test_funct in test_functs: if test_index > stop_index: break if test_index >= start_index: selected_tests.append(test_funct) test_index += 1 if len(selected_tests) == 0: pass elif selected_tests[0].__name__ != "basic": if reverse_order: selected_tests.reverse() if not skip_basic: selected_tests.insert(0, basic_test) elif reverse_order: other_tests = selected_tests[1:] other_tests.reverse() if skip_basic: selected_tests = other_tests else: selected_tests = [basic_test] + other_tests if (len(selected_tests) > 0 and selected_tests[0].__name__ != "basic" and not skip_basic): selected_tests.insert(0, basic_test) table = prettytable.PrettyTable() table.field_names = ["Test name", "Result", "Duration"] exit_status = 0 for test_func in selected_tests: test_name = test_func.__name__ if exit_status: table.add_row([test_name, "Not Started", "0 seconds"]) continue test_workspace = ( workspace + "/" + time_util.local_strftime(time_util.utcnow(), "%Y-%m-%d-%H_%M_%S") + "-" + test_name + "-" + utils.random_word(8)) command = "mkdir -p %s" % test_workspace retval = local_host.sh_run(log, command) if retval.cr_exit_status: log.cl_error( "failed to run command [%s] on host [%s], " "ret = [%d], stdout = [%s], stderr = [%s]", command, local_host.sh_hostname, retval.cr_exit_status, retval.cr_stdout, retval.cr_stderr) table.add_row([test_name, "Not Started", "0 seconds"]) exit_status = -1 continue log.cl_info("starting test [%s]", test_name) start_time = time.time() ret = test_func(log, test_workspace, *args) duration_time = time.time() - start_time if ret < 0: log.cl_error("test [%s] failed, duration %f seconds", test_name, duration_time) table.add_row([test_name, "Failed", "%f seconds" % duration_time]) exit_status = -1 continue if ret == TEST_SKIPPED: log.cl_warning("test [%s] skipped, duration %f seconds", test_name, duration_time) table.add_row([test_name, "Skipped", "%f seconds" % duration_time]) else: log.cl_info("test [%s] passed, duration %f seconds", test_name, duration_time) table.add_row([test_name, "Passed", "%f seconds" % duration_time]) for test_funct in test_functs: if test_funct not in selected_tests: test_name = test_funct.__name__ table.add_row([test_name, "Excluded", "0"]) log.cl_stdout(table) return exit_status
def get_identity(): """ Return a unique identity based on time and random words """ return time_util.local_strftime( time_util.utcnow(), "%Y-%m-%d-%H_%M_%S-") + utils.random_word(8)