def DoRun(command, stdin_data):
    """
  Run the command, given stdin_data. Returns a return code (0 is good)
  and optionally a captured version of stdout, stderr from the run
  (if the global setting capture_output is true).
  """
    # Initialize stdout, stderr to indicate we have not captured
    # any of stdout or stderr.
    stdout = ''
    stderr = ''
    if not int(GlobalSettings['capture_output']):
        # We are only blurting out the stdout and stderr, not capturing it
        # for comparison, etc.
        assert (not GlobalSettings['stdout_golden']
                and not GlobalSettings['stderr_golden']
                and not GlobalSettings['log_golden']
                and not GlobalSettings['filter_regex']
                and not GlobalSettings['filter_inverse']
                and not GlobalSettings['filter_group_only']
                and not GlobalSettings['process_output_single']
                and not GlobalSettings['process_output_combined'])
        # If python ever changes popen.stdout.read() to not risk deadlock,
        # we could stream and capture, and use RunTestWithInputOutput instead.
        (total_time, exit_status,
         failed) = test_lib.RunTestWithInput(command, stdin_data)
        PrintTotalTime(total_time)
        if not CheckExitStatus(failed, GlobalSettings['exit_status'],
                               GlobalSettings['using_nacl_signal_handler'],
                               exit_status, None, None):
            Print(FailureMessage(total_time))
            return (1, stdout, stderr)
    else:
        (total_time, exit_status, failed, stdout,
         stderr) = test_lib.RunTestWithInputOutput(
             command, stdin_data, int(GlobalSettings['capture_stderr']))
        PrintTotalTime(total_time)
        # CheckExitStatus may spew stdout/stderr when there is an error.
        # Otherwise, we do not spew stdout/stderr in this case (capture_output).
        if not CheckExitStatus(failed, GlobalSettings['exit_status'],
                               GlobalSettings['using_nacl_signal_handler'],
                               exit_status, stdout, stderr):
            PrintStdStreams(stdout, stderr)
            Print(FailureMessage(total_time))
            return (1, stdout, stderr)
        if not CheckGoldenOutput(stdout, stderr):
            Print(FailureMessage(total_time))
            return (1, stdout, stderr)
        success, stdout, stderr = ProcessLogOutputSingle(stdout, stderr)
        if not success:
            Print(
                FailureMessage(total_time) + ' ProcessLogOutputSingle failed!')
            return (1, stdout, stderr)

    if not CheckTimeBounds(total_time):
        Print(FailureMessage(total_time))
        return (1, stdout, stderr)

    Print(SuccessMessage(total_time))
    return (0, stdout, stderr)
def ProcessLogOutputCombined(stdout, stderr):
    output_processor = GlobalSettings['process_output_combined']
    if output_processor is None:
        return True
    else:
        output_processor_cmd = DestringifyList(output_processor)
        all_output = stdout + stderr
        _, retcode, failed, new_stdout, new_stderr = \
            test_lib.RunTestWithInputOutput(output_processor_cmd, all_output)
        # Print the result, since we have done some processing.
        PrintStdStreams(new_stdout, new_stderr)
        if retcode != 0 or failed:
            return False
        else:
            return True
def ProcessLogOutputSingle(stdout, stderr):
    output_processor = GlobalSettings['process_output_single']
    if output_processor is None:
        return (True, stdout, stderr)
    else:
        output_processor_cmd = DestringifyList(output_processor)
        # Also, get the output from log_file to get NaClLog output in Windows.
        log_output = open(GlobalSettings['log_file']).read()
        # Assume the log processor does not care about the order of the lines.
        all_output = log_output + stdout + stderr
        _, retcode, failed, new_stdout, new_stderr = \
            test_lib.RunTestWithInputOutput(output_processor_cmd, all_output)
        # Print the result, since we have done some processing and we need
        # to have the processed data. However, if we intend to process it some
        # more later via process_output_combined, do not duplicate the data here.
        # Only print out the final result!
        if not GlobalSettings['process_output_combined']:
            PrintStdStreams(new_stdout, new_stderr)
        if retcode != 0 or failed:
            return (False, new_stdout, new_stderr)
        else:
            return (True, new_stdout, new_stderr)
示例#4
0
def main(argv):
    global GlobalReportStream
    command = ProcessOptions(argv)

    if GlobalSettings['report']:
        GlobalReportStream.append(open(GlobalSettings['report'], 'w'))

    if not GlobalSettings['name']:
        GlobalSettings['name'] = command[0]

    if GlobalSettings['osenv']:
        Banner('setting environment')
        # BUG(robertm): , is a legitimate character for an environment variable
        # value.
        env = GlobalSettings['osenv'].split(',')
        for e in env:
            # = is valid in val of an env
            eq_pos = e.find('=')
            key = e[:eq_pos]
            val = e[eq_pos + 1:]
            Print('[%s] = [%s]' % (key, val))
            os.putenv(key, val)

    if GlobalSettings['logout']:
        try:
            os.unlink(GlobalSettings['logout'])  # might not pre-exist
        except OSError:
            pass

    stdin_data = ''
    if GlobalSettings['stdin']:
        stdin_data = open(GlobalSettings['stdin'])

    run_under = GlobalSettings['run_under']
    if run_under:
        command = run_under.split(',') + command

    start_time = time.time()
    Banner('running %s' % str(command))
    # print the command in copy-and-pastable fashion
    print " ".join(command)
    _, exit_status, failed, stdout, stderr = test_lib.RunTestWithInputOutput(
        command, stdin_data)
    total_time = time.time() - start_time

    if not ExitStatusIsOK(GlobalSettings['exit_status'],
                          exit_status) or failed:
        if failed:
            Print('command failed')
        else:
            Print('command returned unexpected exit status %d' % exit_status)
        Banner('Stdout')
        Print(stdout)
        Banner('Stderr')
        Print(stderr)
        Print(FailureMessage())
        return -1

    if GlobalSettings['filter_validator']:
        stdout = test_lib.RegexpFilterLines(r'^(?!VALIDATOR)', stdout)

    for (stream, getter) in [
        ('stdout', lambda: stdout),
        ('stderr', lambda: stderr),
        ('log', lambda: open(GlobalSettings['logout']).read()),
    ]:
        golden = stream + '_golden'
        if GlobalSettings[golden]:
            golden_data = open(GlobalSettings[golden]).read()
            filt = stream + '_filter'
            actual = getter()
            if GlobalSettings[filt]:
                actual = test_lib.RegexpFilterLines(GlobalSettings[filt],
                                                    actual)
            if DifferentFromGolden(actual, golden_data, stream,
                                   FailureMessage()):
                return -1

    Print('Test %s took %f secs' % (GlobalSettings['name'], total_time))
    if GlobalSettings['time_error']:
        if total_time > GlobalSettings['time_error']:
            Print('ERROR: should have taken less than %f secs' %
                  (GlobalSettings['time_error']))
            Print(FailureMessage())
            return -1

    if GlobalSettings['time_warning']:
        if total_time > GlobalSettings['time_warning']:
            Print('WARNING: should have taken less than %f secs' %
                  (GlobalSettings['time_warning']))

    Print(SuccessMessage())
    return 0