Exemplo n.º 1
0
def sync_iso_dir(log, workspace, host, iso_pattern, dest_iso_dir,
                 use_cp=False):
    """
    Sync the files in the iso to the directory
    """
    # pylint: disable=too-many-branches
    # Just to check ssh to local host works well
    retval = host.sh_run(log, "true", timeout=60)
    if retval.cr_exit_status:
        log.cl_error("failed to ssh to local host [%s] using user [%s], "
                     "stdout = [%s], stderr = [%s]",
                     host.sh_hostname,
                     host.sh_login_name,
                     retval.cr_stdout,
                     retval.cr_stderr)
        return -1

    if not iso_pattern.startswith("/"):
        iso_pattern = os.getcwd() + "/" + iso_pattern
    fnames = host.sh_resolve_path(log, iso_pattern, quiet=True)
    if fnames is None or len(fnames) == 0:
        log.cl_error("failed to find ISO with pattern [%s] on local host [%s]",
                     iso_pattern, host.sh_hostname)
        return -1
    if len(fnames) > 1:
        log.cl_info("multiple ISOs found by pattern [%s] on local host [%s]",
                    iso_pattern, host.sh_hostname)
        return -1
    iso_path = fnames[0]

    is_dir = host.sh_path_isdir(log, iso_path)
    if is_dir < 0:
        log.cl_error("failed to check whether path [%s] is dir or not on host [%s]",
                     iso_path, host.sh_hostname)
        return -1
    if is_dir == 0:
        iso_dir = workspace + "/mnt/" + utils.random_word(8)
        command = ("mkdir -p %s && mount -o loop %s %s" %
                   (iso_dir, iso_path, iso_dir))
        retval = 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,
                         host.sh_hostname,
                         retval.cr_exit_status,
                         retval.cr_stdout,
                         retval.cr_stderr)
            return -1
    else:
        iso_dir = iso_path

    ret = 0
    log.cl_info("syncing ISO to [%s] on host [%s]",
                dest_iso_dir, host.sh_hostname)
    cmds = ["mkdir -p %s" % dest_iso_dir]
    if use_cp:
        cmds.append("rm -fr %s/*" % (dest_iso_dir))
        cmds.append("cp -a %s/* %s" % (iso_dir, dest_iso_dir))
    else:
        cmds.append("rsync --delete -a %s/ %s" % (iso_dir, dest_iso_dir))
    for command in cmds:
        retval = 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,
                         host.sh_hostname,
                         retval.cr_exit_status,
                         retval.cr_stdout,
                         retval.cr_stderr)
            ret = -1
            break

    if iso_dir != iso_path:
        cmds = ["umount %s" % iso_dir,
                "rmdir %s" % iso_dir]
        for command in cmds:
            retval = 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,
                             host.sh_hostname,
                             retval.cr_exit_status,
                             retval.cr_stdout,
                             retval.cr_stderr)
                ret = -1
    return ret
Exemplo n.º 2
0
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 run basic test.
    :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)

    if skip_basic:
        former_selected_tests = selected_tests
        selected_tests = []
        for selected_test in former_selected_tests:
            if selected_test.__name__ == "basic":
                continue
            selected_tests.append(selected_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
Exemplo n.º 3
0
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))
Exemplo n.º 4
0
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)