Ejemplo n.º 1
0
def run_pytest(known_args, extra_args):
    """
    Triggers test discovery and execution through pytest
    :param known_args: Arguments recognized by the parser and handled here
        - output_path: the location to save XML results
    :param extra_args: Additional arguments, passed directly to pytest
    :raises: CalledProcessError if pytest detects failures (returning a non-zero return code)
    :raises: SubprocessTimeoutException if pytest times out
    """
    timeout_sec = 1200  # 20 minutes
    if known_args.module_timeout:
        timeout_sec = int(known_args.module_timeout)
        print 'Setting module timeout to {0}'.format(known_args.module_timeout)
    else:
        print 'No timeout set, using default of 1200 seconds (20 minutes).'

    xunit_command = _get_xunit_command(known_args.output_path)
    argument_call = [
        sys.executable, "-B", "-m", "pytest", "--cache-clear", "-c",
        "lmbr_test_pytest.ini", xunit_command
    ]
    argument_call.extend(extra_args)
    log.info("Invoking pytest")
    log.info(argument_call)

    try:
        # raise on failure
        return_code = subprocess_with_timeout(argument_call, timeout_sec)
        if return_code != 0:
            raise subprocess.CalledProcessError(return_code, argument_call)
    except SubprocessTimeoutException as ste:
        log.error("Pytest timed out after {} seconds.".format(timeout_sec))
        raise ste
Ejemplo n.º 2
0
def run_pytest(known_args, extra_args):
    """
    Triggers test discovery and execution through pytest
    :param known_args: Arguments recognized by the parser and handled here
    :param extra_args: Additional arguments, passed directly to pytest
    :raises: CalledProcessError if pytest detects failures (returning a non-zero return code)
    :raises: SubprocessTimeoutException if pytest fails to return within timeout
    """
    lg.setup_logging(level=known_args.verbosity)
    timeout_sec = known_args.module_timeout
    xunit_flags = _get_xunit_flags(known_args.output_path)
    argument_call = [
        sys.executable, "-B", "-m", "pytest", "--cache-clear", "-c",
        "lmbr_test_pytest.ini"
    ]
    argument_call.extend(xunit_flags)
    argument_call.extend(extra_args)
    log.info(
        "Invoking pytest with a timeout of {} seconds".format(timeout_sec))
    log.info(argument_call)

    try:
        return_code = subprocess_with_timeout(argument_call, timeout_sec)
    except SubprocessTimeoutException as ste:
        log.error(
            "Pytest execution timed out after {} seconds".format(timeout_sec))

    if return_code != 0:
        log.error("Pytest tests failed with exit code: {}".format(return_code))
        # raise on failure
        raise subprocess.CalledProcessError(return_code, argument_call)
Ejemplo n.º 3
0
def run_cmd_in_subprocess(cmd, cwd=None, timeout=None):
    """
    Run a command on Windows in a subprocess
    :type cmd: list of arguments to run, first should be the executable
    :type cwd: directory to execute from, or null
    :type timeout: time out on the subprocess in seconds
    :return: return code from process
    """

    # https://docs.python.org/2/library/subprocess.html?highlight=subprocess#subprocess.Popen
    # "If cwd is not None, the child's current directory will be changed to cwd before it
    # is executed. Note that this directory is not considered when searching the executable,
    # so you can't specify the program's path relative to cwd."
    if not os.path.isabs(cmd[0]):
        cmd = (os.path.join(cwd, cmd[0]), ) + cmd[1:]

    return subprocess_with_timeout(cmd, timeout, cwd)
Ejemplo n.º 4
0
def _get_xunit_flags(output_path):
    timestamp = clean_timestamp(datetime.now().isoformat())
    current_folder = os.path.abspath(output_path)

    output_folder = os.path.join(
        current_folder,  # Absolute path where lmbr_test command is invoked OR user defined path if using --output-path arg in cmd.
        timestamp,  # Timestamp folder name in YYYY_MM_DDTHH_MM_SSSSS format.
        ARTIFACT_FOLDER)
    results_file = os.path.join(output_folder, RESULT_XML_FILENAME)

    log.info("Setting results folder to {}".format(output_folder))

    #Linux
    if sys.platform == "linux" or sys.platform == "linux2":
        lmbr_test_path = os.path.join(os.getcwd(), "lmbr_test.sh")
    #OS X
    elif sys.platform == "darwin":
        lmbr_test_path = os.path.join(os.getcwd(), "lmbr_test.sh")
    #Windows
    elif sys.platform == "win32":
        lmbr_test_path = os.path.join(os.getcwd(), "lmbr_test.cmd")

    #replacing double backslash with single forward slash.
    #python subprocess has a bug where it can't recognize file if path contains double backslash.
    lmbr_test_path = lmbr_test_path.replace(os.sep, '/')
    argument_call = [lmbr_test_path, "pysetup", "check", "ly_test_tools"]
    process = subprocess_with_timeout(argument_call, 120)

    #LyTestTools is installed if process returns 0.
    if process == 0:
        return [
            "--junitxml={}".format(results_file),
            "--output-path={}".format(output_folder)
        ]
    else:
        return [
            "--junitxml={}".format(results_file),
            "--logs_path={}".format(output_folder)
        ]