OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 |
| 6 import logging |
| 7 import os |
| 8 import re |
| 9 |
| 10 from pylib import constants |
| 11 from pylib import ports |
| 12 from pylib.base import base_test_result |
| 13 from pylib.base import test_run |
| 14 from pylib.device import device_errors |
| 15 from pylib.gtest import gtest_test_instance |
| 16 |
| 17 from pylib.local import local_test_server_spawner |
| 18 from pylib.local.device import local_device_environment |
| 19 from pylib.local.device import local_device_test_run |
| 20 from pylib.utils import apk_helper |
| 21 from pylib.utils import device_temp_file |
| 22 |
| 23 _COMMAND_LINE_FLAGS_SUPPORTED = True |
| 24 |
| 25 _EXTRA_COMMAND_LINE_FILE = ( |
| 26 'org.chromium.native_test.ChromeNativeTestActivity.CommandLineFile') |
| 27 _EXTRA_COMMAND_LINE_FLAGS = ( |
| 28 'org.chromium.native_test.ChromeNativeTestActivity.CommandLineFlags') |
| 29 |
| 30 # TODO(jbudorick): Remove these once we're no longer parsing stdout to generate |
| 31 # results. |
| 32 _RE_TEST_STATUS = re.compile( |
| 33 r'\[ +((?:RUN)|(?:FAILED)|(?:OK)) +\] ?([^ ]+)(?: \((\d+) ms\))?$') |
| 34 _RE_TEST_RUN_STATUS = re.compile( |
| 35 r'\[ +(PASSED|RUNNER_FAILED|CRASHED) \] ?[^ ]+') |
| 36 |
| 37 # TODO(jbudorick): Move this up to the test instance if the net test server is |
| 38 # handled outside of the APK for the remote_device environment. |
| 39 _SUITE_REQUIRES_TEST_SERVER_SPAWNER = [ |
| 40 'content_unittests', 'content_browsertests', 'net_unittests', 'unit_tests' |
| 41 ] |
| 42 |
| 43 class _ApkDelegate(object): |
| 44 def __init__(self, apk): |
| 45 self._apk = apk |
| 46 self._package = apk_helper.GetPackageName(self._apk) |
| 47 self._runner = apk_helper.GetInstrumentationName(self._apk) |
| 48 self._component = '%s/%s' % (self._package, self._runner) |
| 49 |
| 50 def Install(self, device): |
| 51 device.Install(self._apk) |
| 52 |
| 53 def RunWithFlags(self, device, flags, **kwargs): |
| 54 with device_temp_file.DeviceTempFile(device) as command_line_file: |
| 55 device.WriteFile(command_line_file.name, '_ %s' % flags) |
| 56 |
| 57 return device.StartInstrumentation( |
| 58 self._component, |
| 59 extras={_EXTRA_COMMAND_LINE_FILE: command_line_file.name}, |
| 60 raw=False, |
| 61 **kwargs) |
| 62 |
| 63 def Clear(self, device): |
| 64 device.ClearApplicationState(self._package) |
| 65 |
| 66 |
| 67 class _ExeDelegate(object): |
| 68 def __init__(self, exe, tr): |
| 69 self._exe_host_path = exe |
| 70 self._exe_file_name = os.path.split(exe)[-1] |
| 71 self._exe_device_path = '%s/%s' % ( |
| 72 constants.TEST_EXECUTABLE_DIR, self._exe_file_name) |
| 73 deps_host_path = self._exe_host_path + '_deps' |
| 74 if os.path.exists(deps_host_path): |
| 75 self._deps_host_path = deps_host_path |
| 76 self._deps_device_path = self._exe_device_path + '_deps' |
| 77 else: |
| 78 self._deps_host_path = None |
| 79 self._test_run = tr |
| 80 |
| 81 def Install(self, device): |
| 82 # TODO(jbudorick): Look into merging this with normal data deps pushing if |
| 83 # executables become supported on nonlocal environments. |
| 84 host_device_tuples = [(self._exe_host_path, self._exe_device_path)] |
| 85 if self._deps_host_path: |
| 86 host_device_tuples.append((self._deps_host_path, self._deps_device_path)) |
| 87 device.PushChangedFiles(host_device_tuples) |
| 88 |
| 89 def RunWithFlags(self, device, flags, **kwargs): |
| 90 cmd = [ |
| 91 self._test_run.GetTool(device).GetTestWrapper(), |
| 92 self._exe_device_path, |
| 93 flags, |
| 94 ] |
| 95 cwd = constants.TEST_EXECUTABLE_DIR |
| 96 |
| 97 env = { |
| 98 'LD_LIBRARY_PATH': |
| 99 '%s/%s_deps' % (constants.TEST_EXECUTABLE_DIR, self._exe_file_name), |
| 100 } |
| 101 try: |
| 102 gcov_strip_depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] |
| 103 external = device.GetExternalStoragePath() |
| 104 env['GCOV_PREFIX'] = '%s/gcov' % external |
| 105 env['GCOV_PREFIX_STRIP'] = gcov_strip_depth |
| 106 except (device_errors.CommandFailedError, KeyError): |
| 107 pass |
| 108 |
| 109 # TODO(jbudorick): Switch to just RunShellCommand once perezju@'s CL |
| 110 # for long shell commands lands. |
| 111 with device_temp_file.DeviceTempFile(device) as script_file: |
| 112 script_contents = ' '.join(cmd) |
| 113 logging.info('script contents: %r' % script_contents) |
| 114 device.WriteFile(script_file.name, script_contents) |
| 115 output = device.RunShellCommand(['sh', script_file.name], cwd=cwd, |
| 116 env=env, **kwargs) |
| 117 return output |
| 118 |
| 119 def Clear(self, device): |
| 120 try: |
| 121 device.KillAll(self._exe_file_name, blocking=True, timeout=30, retries=0) |
| 122 except device_errors.CommandFailedError: |
| 123 # Raised if there is no process with the given name, which in this case |
| 124 # is all we care about. |
| 125 pass |
| 126 |
| 127 |
| 128 class LocalDeviceGtestRun(local_device_test_run.LocalDeviceTestRun): |
| 129 |
| 130 def __init__(self, env, test_instance): |
| 131 assert isinstance(env, local_device_environment.LocalDeviceEnvironment) |
| 132 assert isinstance(test_instance, gtest_test_instance.GtestTestInstance) |
| 133 super(LocalDeviceGtestRun, self).__init__(env, test_instance) |
| 134 |
| 135 if self._test_instance.apk: |
| 136 self._delegate = _ApkDelegate(self._test_instance.apk) |
| 137 elif self._test_instance.exe: |
| 138 self._delegate = _ExeDelegate(self, self._test_instance.exe) |
| 139 |
| 140 self._servers = {} |
| 141 |
| 142 #override |
| 143 def TestPackage(self): |
| 144 return self._test_instance._suite |
| 145 |
| 146 #override |
| 147 def SetUp(self): |
| 148 |
| 149 def individual_device_set_up(dev, host_device_tuples): |
| 150 # Install test APK. |
| 151 self._delegate.Install(dev) |
| 152 |
| 153 # Push data dependencies. |
| 154 external_storage = dev.GetExternalStoragePath() |
| 155 host_device_tuples = [ |
| 156 (h, d if d is not None else external_storage) |
| 157 for h, d in host_device_tuples] |
| 158 dev.PushChangedFiles(host_device_tuples) |
| 159 |
| 160 self._servers[str(dev)] = [] |
| 161 if self.TestPackage() in _SUITE_REQUIRES_TEST_SERVER_SPAWNER: |
| 162 self._servers[str(dev)].append( |
| 163 local_test_server_spawner.LocalTestServerSpawner( |
| 164 ports.AllocateTestServerPort(), dev, self.GetTool(dev))) |
| 165 |
| 166 for s in self._servers[str(dev)]: |
| 167 s.SetUp() |
| 168 |
| 169 self._env.parallel_devices.pMap(individual_device_set_up, |
| 170 self._test_instance.GetDataDependencies()) |
| 171 |
| 172 #override |
| 173 def _ShouldShard(self): |
| 174 return True |
| 175 |
| 176 #override |
| 177 def _CreateShards(self, tests): |
| 178 device_count = len(self._env.devices) |
| 179 return [':'.join(tests[i::device_count]) |
| 180 for i in xrange(0, device_count)] |
| 181 |
| 182 #override |
| 183 def _GetTests(self): |
| 184 tests = self._delegate.RunWithFlags( |
| 185 self._env.devices[0], '--gtest_list_tests') |
| 186 tests = gtest_test_instance.ParseGTestListTests(tests) |
| 187 tests = self._test_instance.FilterTests(tests) |
| 188 return tests |
| 189 |
| 190 #override |
| 191 def _RunTest(self, device, test): |
| 192 # Run the test. |
| 193 output = self._delegate.RunWithFlags(device, '--gtest_filter=%s' % test, |
| 194 timeout=900, retries=0) |
| 195 for s in self._servers[str(device)]: |
| 196 s.Reset() |
| 197 self._delegate.Clear(device) |
| 198 |
| 199 # Parse the output. |
| 200 # TODO(jbudorick): Transition test scripts away from parsing stdout. |
| 201 results = [] |
| 202 for l in output: |
| 203 matcher = _RE_TEST_STATUS.match(l) |
| 204 if matcher: |
| 205 result_type = None |
| 206 if matcher.group(1) == 'OK': |
| 207 result_type = base_test_result.ResultType.PASS |
| 208 elif matcher.group(1) == 'FAILED': |
| 209 result_type = base_test_result.ResultType.FAIL |
| 210 |
| 211 if result_type: |
| 212 test_name = matcher.group(2) |
| 213 duration = matcher.group(3) if matcher.group(3) else 0 |
| 214 results.append(base_test_result.BaseTestResult( |
| 215 test_name, result_type, duration)) |
| 216 logging.info(l) |
| 217 return results |
| 218 |
| 219 #override |
| 220 def TearDown(self): |
| 221 def individual_device_tear_down(dev): |
| 222 for s in self._servers[str(dev)]: |
| 223 s.TearDown() |
| 224 |
| 225 self._env.parallel_devices.pMap(individual_device_tear_down) |
| 226 |
OLD | NEW |