OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import os | 6 import os |
7 import re | 7 import re |
8 import subprocess | |
9 import sys | 8 import sys |
10 | 9 |
11 _logging = logging.getLogger() | 10 _logging = logging.getLogger() |
12 | 11 |
13 from mopy import android | 12 from mopy import test_util |
| 13 from mopy.background_app_group import BackgroundAppGroup |
14 from mopy.config import Config | 14 from mopy.config import Config |
15 from mopy.paths import Paths | 15 from mopy.paths import Paths |
16 from mopy.print_process_error import print_process_error | 16 from mopy.print_process_error import print_process_error |
17 | 17 |
18 | 18 |
19 def set_color(): | 19 def set_color(): |
20 """Run gtests with color if we're on a TTY (and we're not being told | 20 """Run gtests with color if we're on a TTY (and we're not being told |
21 explicitly what to do).""" | 21 explicitly what to do).""" |
22 if sys.stdout.isatty() and "GTEST_COLOR" not in os.environ: | 22 if sys.stdout.isatty() and "GTEST_COLOR" not in os.environ: |
23 _logging.debug("Setting GTEST_COLOR=yes") | 23 _logging.debug("Setting GTEST_COLOR=yes") |
24 os.environ["GTEST_COLOR"] = "yes" | 24 os.environ["GTEST_COLOR"] = "yes" |
25 | 25 |
| 26 def run_fixtures(config, apptest_dict, apptest, test_args, shell_args, |
| 27 launched_services): |
| 28 """Run the gtest fixtures in isolation.""" |
| 29 |
| 30 mojo_paths = Paths(config) |
| 31 |
| 32 # List the apptest fixtures so they can be run independently for isolation. |
| 33 # TODO(msw): Run some apptests without fixture isolation? |
| 34 fixtures = get_fixtures(config, shell_args, apptest) |
| 35 |
| 36 if not fixtures: |
| 37 return "Failed with no tests found." |
| 38 |
| 39 if any(not mojo_paths.IsValidAppUrl(url) for url in launched_services): |
| 40 return ("Failed with malformed launched-services: %r" % launched_services) |
| 41 |
| 42 apptest_result = "Succeeded" |
| 43 for fixture in fixtures: |
| 44 apptest_args = test_args + ["--gtest_filter=%s" % fixture] |
| 45 if launched_services: |
| 46 success = RunApptestInLauncher(config, mojo_paths, apptest, apptest_args, |
| 47 shell_args, launched_services) |
| 48 else: |
| 49 success = RunApptestInShell(config, apptest, apptest_args, shell_args) |
| 50 |
| 51 if not success: |
| 52 apptest_result = "Failed test(s) in %r" % apptest_dict |
| 53 |
| 54 return apptest_result |
| 55 |
26 | 56 |
27 def run_test(config, shell_args, apps_and_args=None, run_launcher=False): | 57 def run_test(config, shell_args, apps_and_args=None, run_launcher=False): |
28 """Runs a command line and checks the output for signs of gtest failure. | 58 """Runs a command line and checks the output for signs of gtest failure. |
29 | 59 |
30 Args: | 60 Args: |
31 config: The mopy.config.Config object for the build. | 61 config: The mopy.config.Config object for the build. |
32 shell_args: The arguments for mojo_shell. | 62 shell_args: The arguments for mojo_shell. |
33 apps_and_args: A Dict keyed by application URL associated to the | 63 apps_and_args: A Dict keyed by application URL associated to the |
34 application's specific arguments. | 64 application's specific arguments. |
35 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. | 65 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. |
36 """ | 66 """ |
37 apps_and_args = apps_and_args or {} | 67 apps_and_args = apps_and_args or {} |
38 output = _try_run_test(config, shell_args, apps_and_args, run_launcher) | 68 output = test_util.try_run_test(config, shell_args, apps_and_args, |
| 69 run_launcher) |
39 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". | 70 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". |
40 # The latter condition ensures failure on broken command lines or output. | 71 # The latter condition ensures failure on broken command lines or output. |
41 # Check output instead of exit codes because mojo_shell always exits with 0. | 72 # Check output instead of exit codes because mojo_shell always exits with 0. |
42 if (output is None or | 73 if (output is None or |
43 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): | 74 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): |
44 print "Failed test:" | 75 print "Failed test:" |
45 print_process_error( | 76 print_process_error( |
46 _build_command_line(config, shell_args, apps_and_args, run_launcher), | 77 test_util.build_command_line(config, shell_args, apps_and_args, |
| 78 run_launcher), |
47 output) | 79 output) |
48 return False | 80 return False |
49 _logging.debug("Succeeded with output:\n%s" % output) | 81 _logging.debug("Succeeded with output:\n%s" % output) |
50 return True | 82 return True |
51 | 83 |
52 | 84 |
53 def get_fixtures(config, shell_args, apptest): | 85 def get_fixtures(config, shell_args, apptest): |
54 """Returns the "Test.Fixture" list from an apptest using mojo_shell. | 86 """Returns the "Test.Fixture" list from an apptest using mojo_shell. |
55 | 87 |
56 Tests are listed by running the given apptest in mojo_shell and passing | 88 Tests are listed by running the given apptest in mojo_shell and passing |
57 --gtest_list_tests. The output is parsed and reformatted into a list like | 89 --gtest_list_tests. The output is parsed and reformatted into a list like |
58 [TestSuite.TestFixture, ... ] | 90 [TestSuite.TestFixture, ... ] |
59 An empty list is returned on failure, with errors logged. | 91 An empty list is returned on failure, with errors logged. |
60 | 92 |
61 Args: | 93 Args: |
62 config: The mopy.config.Config object for the build. | 94 config: The mopy.config.Config object for the build. |
63 apptest: The URL of the test application to run. | 95 apptest: The URL of the test application to run. |
64 """ | 96 """ |
65 try: | 97 try: |
66 apps_and_args = {apptest: ["--gtest_list_tests"]} | 98 apps_and_args = {apptest: ["--gtest_list_tests"]} |
67 list_output = _run_test(config, shell_args, apps_and_args) | 99 list_output = test_util.run_test(config, shell_args, apps_and_args) |
68 _logging.debug("Tests listed:\n%s" % list_output) | 100 _logging.debug("Tests listed:\n%s" % list_output) |
69 return _gtest_list_tests(list_output) | 101 return _gtest_list_tests(list_output) |
70 except Exception as e: | 102 except Exception as e: |
71 print "Failed to get test fixtures:" | 103 print "Failed to get test fixtures:" |
72 print_process_error( | 104 print_process_error( |
73 _build_command_line(config, shell_args, apps_and_args), e) | 105 test_util.build_command_line(config, shell_args, apps_and_args), e) |
74 return [] | 106 return [] |
75 | 107 |
76 | 108 |
77 def build_shell_arguments(shell_args, apps_and_args=None): | |
78 """Build the list of arguments for the shell. |shell_args| are the base | |
79 arguments, |apps_and_args| is a dictionary that associates each application to | |
80 its specific arguments|. Each app included will be run by the shell. | |
81 """ | |
82 result = shell_args[:] | |
83 if apps_and_args: | |
84 for (application, args) in apps_and_args.items(): | |
85 result += ["--args-for=%s %s" % (application, " ".join(args))] | |
86 result += apps_and_args.keys() | |
87 return result | |
88 | |
89 | |
90 def _get_shell_executable(config, run_launcher): | |
91 paths = Paths(config=config) | |
92 if config.target_os == Config.OS_ANDROID: | |
93 return os.path.join(paths.src_root, "mojo", "tools", | |
94 "android_mojo_shell.py") | |
95 elif run_launcher: | |
96 return paths.mojo_launcher_path | |
97 else: | |
98 return paths.mojo_shell_path | |
99 | |
100 | |
101 def _build_command_line(config, shell_args, apps_and_args, run_launcher=False): | |
102 executable = _get_shell_executable(config, run_launcher) | |
103 return "%s %s" % (executable, " ".join(["%r" % x for x in | |
104 build_shell_arguments( | |
105 shell_args, apps_and_args)])) | |
106 | |
107 | |
108 def _run_test_android(shell_args, apps_and_args): | |
109 """Run the given test on the single/default android device.""" | |
110 (r, w) = os.pipe() | |
111 with os.fdopen(r, "r") as rf: | |
112 with os.fdopen(w, "w") as wf: | |
113 arguments = build_shell_arguments(shell_args, apps_and_args) | |
114 android.StartShell(arguments, wf, wf.close) | |
115 return rf.read() | |
116 | |
117 | |
118 def _run_test(config, shell_args, apps_and_args, run_launcher=False): | |
119 """Run the given test, using mojo_launcher if |run_launcher| is True.""" | |
120 if (config.target_os == Config.OS_ANDROID): | |
121 return _run_test_android(shell_args, apps_and_args) | |
122 else: | |
123 executable = _get_shell_executable(config, run_launcher) | |
124 command = ([executable] + build_shell_arguments(shell_args, apps_and_args)) | |
125 return subprocess.check_output(command, stderr=subprocess.STDOUT) | |
126 | |
127 | |
128 def _try_run_test(config, shell_args, apps_and_args, run_launcher): | |
129 """Returns the output of a command line or an empty string on error.""" | |
130 command_line = _build_command_line(config, shell_args, apps_and_args, | |
131 run_launcher=run_launcher) | |
132 _logging.debug("Running command line: %s" % command_line) | |
133 try: | |
134 return _run_test(config, shell_args, apps_and_args, run_launcher) | |
135 except Exception as e: | |
136 print_process_error(command_line, e) | |
137 return None | |
138 | |
139 | |
140 def _gtest_list_tests(gtest_list_tests_output): | 109 def _gtest_list_tests(gtest_list_tests_output): |
141 """Returns a list of strings formatted as TestSuite.TestFixture from the | 110 """Returns a list of strings formatted as TestSuite.TestFixture from the |
142 output of running --gtest_list_tests on a GTEST application.""" | 111 output of running --gtest_list_tests on a GTEST application.""" |
143 | 112 |
144 # Remove log lines. | 113 # Remove log lines. |
145 gtest_list_tests_output = ( | 114 gtest_list_tests_output = ( |
146 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE)) | 115 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE)) |
147 | 116 |
148 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): | 117 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): |
149 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % | 118 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % |
150 gtest_list_tests_output) | 119 gtest_list_tests_output) |
151 | 120 |
152 output_lines = gtest_list_tests_output.split("\n") | 121 output_lines = gtest_list_tests_output.split("\n") |
153 | 122 |
154 test_list = [] | 123 test_list = [] |
155 for line in output_lines: | 124 for line in output_lines: |
156 if not line: | 125 if not line: |
157 continue | 126 continue |
158 if line[0] != " ": | 127 if line[0] != " ": |
159 suite = line.strip() | 128 suite = line.strip() |
160 continue | 129 continue |
161 test_list.append(suite + line.strip()) | 130 test_list.append(suite + line.strip()) |
162 | 131 |
163 return test_list | 132 return test_list |
| 133 |
| 134 |
| 135 def RunApptestInShell(config, application, application_args, shell_args): |
| 136 return run_test(config, shell_args, {application: application_args}) |
| 137 |
| 138 |
| 139 def RunApptestInLauncher(config, mojo_paths, application, application_args, |
| 140 shell_args, launched_services): |
| 141 with BackgroundAppGroup( |
| 142 mojo_paths, launched_services, |
| 143 test_util.build_shell_arguments(shell_args)) as apps: |
| 144 launcher_args = [ |
| 145 '--shell-path=' + apps.socket_path, |
| 146 '--app-url=' + application, |
| 147 '--app-path=' + mojo_paths.FileFromUrl(application), |
| 148 '--app-args=' + " ".join(application_args)] |
| 149 return run_test(config, launcher_args, run_launcher=True) |
OLD | NEW |