Commit f5ace001 authored by ppi's avatar ppi Committed by Commit bot

Add mojo apptest runner.

This patch copies apptest_runner from Mojo, stripping it down to parts that are
needed to run network service apptests on Linux. This allows us to run them in
a Chromium checkout.

BUG=450356

Review URL: https://codereview.chromium.org/874273002

Cr-Commit-Position: refs/heads/master@{#313834}
parent 168e275c
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""A test runner for gtest application tests."""
import argparse
import ast
import logging
import os
import sys
_logging = logging.getLogger()
import gtest
def main():
logging.basicConfig()
# Uncomment to debug:
#_logging.setLevel(logging.DEBUG)
parser = argparse.ArgumentParser(description='A test runner for gtest '
'application tests.')
parser.add_argument('apptest_list_file', type=file,
help='A file listing apptests to run.')
parser.add_argument('build_dir', type=str,
help='The build output directory.')
args = parser.parse_args()
apptest_list = ast.literal_eval(args.apptest_list_file.read())
_logging.debug("Test list: %s" % apptest_list)
gtest.set_color()
mojo_shell_path = os.path.join(args.build_dir, "mojo_shell")
exit_code = 0
for apptest_dict in apptest_list:
if apptest_dict.get("disabled"):
continue
apptest = apptest_dict["test"]
apptest_args = apptest_dict.get("test-args", [])
shell_args = apptest_dict.get("shell-args", [])
print "Running " + apptest + "...",
sys.stdout.flush()
# List the apptest fixtures so they can be run independently for isolation.
# TODO(msw): Run some apptests without fixture isolation?
fixtures = gtest.get_fixtures(mojo_shell_path, apptest)
if not fixtures:
print "Failed with no tests found."
exit_code = 1
continue
apptest_result = "Succeeded"
for fixture in fixtures:
args_for_apptest = " ".join(["--args-for=" + apptest,
"--gtest_filter=" + fixture] + apptest_args)
success = RunApptestInShell(mojo_shell_path, apptest,
shell_args + [args_for_apptest])
if not success:
apptest_result = "Failed test(s) in %r" % apptest_dict
exit_code = 1
print apptest_result
return exit_code
def RunApptestInShell(mojo_shell_path, apptest, shell_args):
return gtest.run_test([mojo_shell_path, apptest] + shell_args)
if __name__ == '__main__':
sys.exit(main())
# This file contains a list of Mojo gtest unit tests.
# This must be a valid python dictionary.
[
{
"test": "mojo:network_service_apptests",
},
]
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import re
import subprocess
import sys
_logging = logging.getLogger()
def _print_process_error(command_line, error):
"""Properly format an exception raised from a failed command execution."""
if command_line:
print 'Failed command: %r' % command_line
else:
print 'Failed command:'
print 72 * '-'
if hasattr(error, 'returncode'):
print ' with exit code %d' % error.returncode
print 72 * '-'
if hasattr(error, 'output'):
print error.output
else:
print error
print 72 * '-'
def set_color():
"""Run gtests with color if we're on a TTY (and we're not being told
explicitly what to do)."""
if sys.stdout.isatty() and 'GTEST_COLOR' not in os.environ:
_logging.debug("Setting GTEST_COLOR=yes")
os.environ['GTEST_COLOR'] = 'yes'
def _try_command_line(command_line):
"""Returns the output of a command line or an empty string on error."""
_logging.debug("Running command line: %s" % command_line)
try:
return subprocess.check_output(command_line, stderr=subprocess.STDOUT)
except Exception as e:
_print_process_error(command_line, e)
return None
def run_test(command_line):
"""Runs a command line and checks the output for signs of gtest failure."""
output = _try_command_line(command_line)
# Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]".
# The latter condition ensures failure on broken command lines or output.
# Check output instead of exit codes because mojo_shell always exits with 0.
if (output is None or
(output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)):
print "Failed test:"
_print_process_error(command_line, output)
return False
_logging.debug("Succeeded with output:\n%s" % output)
return True
def get_fixtures(mojo_shell, apptest):
"""Returns the "Test.Fixture" list from an apptest using mojo_shell.
Tests are listed by running the given apptest in mojo_shell and passing
--gtest_list_tests. The output is parsed and reformatted into a list like
[TestSuite.TestFixture, ... ]
An empty list is returned on failure, with errors logged.
"""
command = [mojo_shell,
"--args-for={0} --gtest_list_tests".format(apptest),
apptest]
try:
list_output = subprocess.check_output(command, stderr=subprocess.STDOUT)
_logging.debug("Tests listed:\n%s" % list_output)
return _gtest_list_tests(list_output)
except Exception as e:
print "Failed to get test fixtures:"
_print_process_error(command, e)
return []
def _gtest_list_tests(gtest_list_tests_output):
"""Returns a list of strings formatted as TestSuite.TestFixture from the
output of running --gtest_list_tests on a GTEST application."""
if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output):
raise Exception("Unrecognized --gtest_list_tests output:\n%s" %
gtest_list_tests_output)
output_lines = gtest_list_tests_output.split('\n')
test_list = []
for line in output_lines:
if not line:
continue
if line[0] != ' ':
suite = line.strip()
continue
test_list.append(suite + line.strip())
return test_list
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment