| 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 | 5 import itertools |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import posixpath |
| 8 | 9 |
| 9 from pylib import constants | 10 from pylib import constants |
| 10 from pylib import ports | 11 from pylib import ports |
| 11 from pylib.base import test_run | 12 from pylib.base import test_run |
| 12 from pylib.device import device_errors | 13 from pylib.device import device_errors |
| 13 from pylib.gtest import gtest_test_instance | 14 from pylib.gtest import gtest_test_instance |
| 14 | 15 |
| 15 from pylib.local import local_test_server_spawner | 16 from pylib.local import local_test_server_spawner |
| 16 from pylib.local.device import local_device_environment | 17 from pylib.local.device import local_device_environment |
| 17 from pylib.local.device import local_device_test_run | 18 from pylib.local.device import local_device_test_run |
| 18 from pylib.utils import apk_helper | |
| 19 from pylib.utils import device_temp_file | 19 from pylib.utils import device_temp_file |
| 20 | 20 |
| 21 _COMMAND_LINE_FLAGS_SUPPORTED = True | 21 _COMMAND_LINE_FLAGS_SUPPORTED = True |
| 22 | 22 |
| 23 _EXTRA_COMMAND_LINE_FILE = ( | 23 _EXTRA_COMMAND_LINE_FILE = ( |
| 24 'org.chromium.native_test.NativeTestActivity.CommandLineFile') | 24 'org.chromium.native_test.NativeTestActivity.CommandLineFile') |
| 25 _EXTRA_COMMAND_LINE_FLAGS = ( | 25 _EXTRA_COMMAND_LINE_FLAGS = ( |
| 26 'org.chromium.native_test.NativeTestActivity.CommandLineFlags') | 26 'org.chromium.native_test.NativeTestActivity.CommandLineFlags') |
| 27 _EXTRA_NATIVE_TEST_ACTIVITY = ( | 27 _EXTRA_TEST_LIST = ( |
| 28 'org.chromium.native_test.NativeTestInstrumentationTestRunner' | 28 'org.chromium.native_test.NativeTestInstrumentationTestRunner' |
| 29 '.NativeTestActivity') | 29 '.TestList') |
| 30 | 30 |
| 31 _MAX_SHARD_SIZE = 256 | 31 _MAX_SHARD_SIZE = 256 |
| 32 | 32 |
| 33 # TODO(jbudorick): Move this up to the test instance if the net test server is | 33 # TODO(jbudorick): Move this up to the test instance if the net test server is |
| 34 # handled outside of the APK for the remote_device environment. | 34 # handled outside of the APK for the remote_device environment. |
| 35 _SUITE_REQUIRES_TEST_SERVER_SPAWNER = [ | 35 _SUITE_REQUIRES_TEST_SERVER_SPAWNER = [ |
| 36 'components_browsertests', 'content_unittests', 'content_browsertests', | 36 'components_browsertests', 'content_unittests', 'content_browsertests', |
| 37 'net_unittests', 'unit_tests' | 37 'net_unittests', 'unit_tests' |
| 38 ] | 38 ] |
| 39 | 39 |
| 40 # TODO(jbudorick): Move this inside _ApkDelegate once TestPackageApk is gone. |
| 41 def PullAppFilesImpl(device, package, files, directory): |
| 42 device_dir = device.GetApplicationDataDirectory(package) |
| 43 host_dir = os.path.join(directory, str(device)) |
| 44 for f in files: |
| 45 device_file = posixpath.join(device_dir, f) |
| 46 host_file = os.path.join(host_dir, *f.split(posixpath.sep)) |
| 47 host_file_base, ext = os.path.splitext(host_file) |
| 48 for i in itertools.count(): |
| 49 host_file = '%s_%d%s' % (host_file_base, i, ext) |
| 50 if not os.path.exists(host_file): |
| 51 break |
| 52 device.PullFile(device_file, host_file) |
| 53 |
| 40 class _ApkDelegate(object): | 54 class _ApkDelegate(object): |
| 41 def __init__(self, apk): | 55 def __init__(self, test_instance): |
| 42 self._apk = apk | 56 self._activity = test_instance.activity |
| 57 self._apk = test_instance.apk |
| 58 self._package = test_instance.package |
| 59 self._runner = test_instance.runner |
| 43 | 60 |
| 44 helper = apk_helper.ApkHelper(self._apk) | |
| 45 self._activity = helper.GetActivityName() | |
| 46 self._package = helper.GetPackageName() | |
| 47 self._runner = helper.GetInstrumentationName() | |
| 48 self._component = '%s/%s' % (self._package, self._runner) | 61 self._component = '%s/%s' % (self._package, self._runner) |
| 49 self._enable_test_server_spawner = False | 62 self._extras = test_instance.extras |
| 50 | 63 |
| 51 def Install(self, device): | 64 def Install(self, device): |
| 52 device.Install(self._apk) | 65 device.Install(self._apk) |
| 53 | 66 |
| 54 def RunWithFlags(self, device, flags, **kwargs): | 67 def Run(self, test, device, flags=None, **kwargs): |
| 68 extras = dict(self._extras) |
| 69 |
| 55 with device_temp_file.DeviceTempFile(device.adb) as command_line_file: | 70 with device_temp_file.DeviceTempFile(device.adb) as command_line_file: |
| 56 device.WriteFile(command_line_file.name, '_ %s' % flags) | 71 device.WriteFile(command_line_file.name, '_ %s' % flags if flags else '_') |
| 72 extras[_EXTRA_COMMAND_LINE_FILE] = command_line_file.name |
| 57 | 73 |
| 58 extras = { | 74 with device_temp_file.DeviceTempFile(device.adb) as test_list_file: |
| 59 _EXTRA_COMMAND_LINE_FILE: command_line_file.name, | 75 if test: |
| 60 _EXTRA_NATIVE_TEST_ACTIVITY: self._activity, | 76 device.WriteFile(test_list_file.name, '\n'.join(test)) |
| 61 } | 77 extras[_EXTRA_TEST_LIST] = test_list_file.name |
| 62 | 78 |
| 63 return device.StartInstrumentation( | 79 return device.StartInstrumentation( |
| 64 self._component, extras=extras, raw=False, **kwargs) | 80 self._component, extras=extras, raw=False, **kwargs) |
| 81 |
| 82 def PullAppFiles(self, device, files, directory): |
| 83 PullAppFilesImpl(device, self._package, files, directory) |
| 65 | 84 |
| 66 def Clear(self, device): | 85 def Clear(self, device): |
| 67 device.ClearApplicationState(self._package) | 86 device.ClearApplicationState(self._package) |
| 68 | 87 |
| 69 | 88 |
| 70 class _ExeDelegate(object): | 89 class _ExeDelegate(object): |
| 71 def __init__(self, exe, tr): | 90 def __init__(self, tr, exe): |
| 72 self._exe_host_path = exe | 91 self._exe_host_path = exe |
| 73 self._exe_file_name = os.path.split(exe)[-1] | 92 self._exe_file_name = os.path.split(exe)[-1] |
| 74 self._exe_device_path = '%s/%s' % ( | 93 self._exe_device_path = '%s/%s' % ( |
| 75 constants.TEST_EXECUTABLE_DIR, self._exe_file_name) | 94 constants.TEST_EXECUTABLE_DIR, self._exe_file_name) |
| 76 deps_host_path = self._exe_host_path + '_deps' | 95 deps_host_path = self._exe_host_path + '_deps' |
| 77 if os.path.exists(deps_host_path): | 96 if os.path.exists(deps_host_path): |
| 78 self._deps_host_path = deps_host_path | 97 self._deps_host_path = deps_host_path |
| 79 self._deps_device_path = self._exe_device_path + '_deps' | 98 self._deps_device_path = self._exe_device_path + '_deps' |
| 80 else: | 99 else: |
| 81 self._deps_host_path = None | 100 self._deps_host_path = None |
| 82 self._test_run = tr | 101 self._test_run = tr |
| 83 | 102 |
| 84 def Install(self, device): | 103 def Install(self, device): |
| 85 # TODO(jbudorick): Look into merging this with normal data deps pushing if | 104 # TODO(jbudorick): Look into merging this with normal data deps pushing if |
| 86 # executables become supported on nonlocal environments. | 105 # executables become supported on nonlocal environments. |
| 87 host_device_tuples = [(self._exe_host_path, self._exe_device_path)] | 106 host_device_tuples = [(self._exe_host_path, self._exe_device_path)] |
| 88 if self._deps_host_path: | 107 if self._deps_host_path: |
| 89 host_device_tuples.append((self._deps_host_path, self._deps_device_path)) | 108 host_device_tuples.append((self._deps_host_path, self._deps_device_path)) |
| 90 device.PushChangedFiles(host_device_tuples) | 109 device.PushChangedFiles(host_device_tuples) |
| 91 | 110 |
| 92 def RunWithFlags(self, device, flags, **kwargs): | 111 def Run(self, test, device, flags=None, **kwargs): |
| 93 cmd = [ | 112 cmd = [ |
| 94 self._test_run.GetTool(device).GetTestWrapper(), | 113 self._test_run.GetTool(device).GetTestWrapper(), |
| 95 self._exe_device_path, | 114 self._exe_device_path, |
| 96 flags, | |
| 97 ] | 115 ] |
| 116 if test: |
| 117 cmd.append('--gtest_filter=%s' % ':'.join(test)) |
| 118 if flags: |
| 119 cmd.append(flags) |
| 98 cwd = constants.TEST_EXECUTABLE_DIR | 120 cwd = constants.TEST_EXECUTABLE_DIR |
| 99 | 121 |
| 100 env = { | 122 env = { |
| 101 'LD_LIBRARY_PATH': | 123 'LD_LIBRARY_PATH': |
| 102 '%s/%s_deps' % (constants.TEST_EXECUTABLE_DIR, self._exe_file_name), | 124 '%s/%s_deps' % (constants.TEST_EXECUTABLE_DIR, self._exe_file_name), |
| 103 } | 125 } |
| 104 try: | 126 try: |
| 105 gcov_strip_depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] | 127 gcov_strip_depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] |
| 106 external = device.GetExternalStoragePath() | 128 external = device.GetExternalStoragePath() |
| 107 env['GCOV_PREFIX'] = '%s/gcov' % external | 129 env['GCOV_PREFIX'] = '%s/gcov' % external |
| 108 env['GCOV_PREFIX_STRIP'] = gcov_strip_depth | 130 env['GCOV_PREFIX_STRIP'] = gcov_strip_depth |
| 109 except (device_errors.CommandFailedError, KeyError): | 131 except (device_errors.CommandFailedError, KeyError): |
| 110 pass | 132 pass |
| 111 | 133 |
| 112 # TODO(jbudorick): Switch to just RunShellCommand once perezju@'s CL | 134 # TODO(jbudorick): Switch to just RunShellCommand once perezju@'s CL |
| 113 # for long shell commands lands. | 135 # for long shell commands lands. |
| 114 with device_temp_file.DeviceTempFile(device.adb) as script_file: | 136 with device_temp_file.DeviceTempFile(device.adb) as script_file: |
| 115 script_contents = ' '.join(cmd) | 137 script_contents = ' '.join(cmd) |
| 116 logging.info('script contents: %r' % script_contents) | 138 logging.info('script contents: %r' % script_contents) |
| 117 device.WriteFile(script_file.name, script_contents) | 139 device.WriteFile(script_file.name, script_contents) |
| 118 output = device.RunShellCommand(['sh', script_file.name], cwd=cwd, | 140 output = device.RunShellCommand(['sh', script_file.name], cwd=cwd, |
| 119 env=env, **kwargs) | 141 env=env, **kwargs) |
| 120 return output | 142 return output |
| 121 | 143 |
| 144 def PullAppFiles(self, device, files, directory): |
| 145 pass |
| 146 |
| 122 def Clear(self, device): | 147 def Clear(self, device): |
| 123 device.KillAll(self._exe_file_name, blocking=True, timeout=30, quiet=True) | 148 device.KillAll(self._exe_file_name, blocking=True, timeout=30, quiet=True) |
| 124 | 149 |
| 125 | 150 |
| 126 class LocalDeviceGtestRun(local_device_test_run.LocalDeviceTestRun): | 151 class LocalDeviceGtestRun(local_device_test_run.LocalDeviceTestRun): |
| 127 | 152 |
| 128 def __init__(self, env, test_instance): | 153 def __init__(self, env, test_instance): |
| 129 assert isinstance(env, local_device_environment.LocalDeviceEnvironment) | 154 assert isinstance(env, local_device_environment.LocalDeviceEnvironment) |
| 130 assert isinstance(test_instance, gtest_test_instance.GtestTestInstance) | 155 assert isinstance(test_instance, gtest_test_instance.GtestTestInstance) |
| 131 super(LocalDeviceGtestRun, self).__init__(env, test_instance) | 156 super(LocalDeviceGtestRun, self).__init__(env, test_instance) |
| 132 | 157 |
| 133 if self._test_instance.apk: | 158 if self._test_instance.apk: |
| 134 self._delegate = _ApkDelegate(self._test_instance.apk) | 159 self._delegate = _ApkDelegate(self._test_instance) |
| 135 elif self._test_instance.exe: | 160 elif self._test_instance.exe: |
| 136 self._delegate = _ExeDelegate(self, self._test_instance.exe) | 161 self._delegate = _ExeDelegate(self, self._test_instance.exe) |
| 137 | 162 |
| 138 self._servers = {} | 163 self._servers = {} |
| 139 | 164 |
| 140 #override | 165 #override |
| 141 def TestPackage(self): | 166 def TestPackage(self): |
| 142 return self._test_instance.suite | 167 return self._test_instance.suite |
| 143 | 168 |
| 144 #override | 169 #override |
| (...skipping 21 matching lines...) Expand all Loading... |
| 166 | 191 |
| 167 self._env.parallel_devices.pMap(individual_device_set_up, | 192 self._env.parallel_devices.pMap(individual_device_set_up, |
| 168 self._test_instance.GetDataDependencies()) | 193 self._test_instance.GetDataDependencies()) |
| 169 | 194 |
| 170 #override | 195 #override |
| 171 def _ShouldShard(self): | 196 def _ShouldShard(self): |
| 172 return True | 197 return True |
| 173 | 198 |
| 174 #override | 199 #override |
| 175 def _CreateShards(self, tests): | 200 def _CreateShards(self, tests): |
| 176 if self._test_instance.suite in gtest_test_instance.BROWSER_TEST_SUITES: | 201 device_count = len(self._env.devices) |
| 177 return tests | 202 shards = [] |
| 178 else: | 203 for i in xrange(0, device_count): |
| 179 device_count = len(self._env.devices) | 204 unbounded_shard = tests[i::device_count] |
| 180 shards = [] | 205 shards += [unbounded_shard[j:j+_MAX_SHARD_SIZE] |
| 181 for i in xrange(0, device_count): | 206 for j in xrange(0, len(unbounded_shard), _MAX_SHARD_SIZE)] |
| 182 unbounded_shard = tests[i::device_count] | 207 return shards |
| 183 shards += [unbounded_shard[j:j+_MAX_SHARD_SIZE] | |
| 184 for j in xrange(0, len(unbounded_shard), _MAX_SHARD_SIZE)] | |
| 185 return [':'.join(s) for s in shards] | |
| 186 | 208 |
| 187 #override | 209 #override |
| 188 def _GetTests(self): | 210 def _GetTests(self): |
| 189 tests = self._delegate.RunWithFlags( | 211 tests = self._delegate.Run( |
| 190 self._env.devices[0], '--gtest_list_tests') | 212 None, self._env.devices[0], flags='--gtest_list_tests') |
| 191 tests = gtest_test_instance.ParseGTestListTests(tests) | 213 tests = gtest_test_instance.ParseGTestListTests(tests) |
| 192 tests = self._test_instance.FilterTests(tests) | 214 tests = self._test_instance.FilterTests(tests) |
| 193 return tests | 215 return tests |
| 194 | 216 |
| 195 #override | 217 #override |
| 196 def _RunTest(self, device, test): | 218 def _RunTest(self, device, test): |
| 197 # Run the test. | 219 # Run the test. |
| 198 output = self._delegate.RunWithFlags( | 220 output = self._delegate.Run( |
| 199 device, '--gtest_filter=%s' % test, timeout=900, retries=0) | 221 test, device, timeout=900, retries=0) |
| 200 for s in self._servers[str(device)]: | 222 for s in self._servers[str(device)]: |
| 201 s.Reset() | 223 s.Reset() |
| 224 if self._test_instance.app_files: |
| 225 self._delegate.PullAppFiles(device, self._test_instance.app_files, |
| 226 self._test_instance.app_file_dir) |
| 202 self._delegate.Clear(device) | 227 self._delegate.Clear(device) |
| 203 | 228 |
| 204 # Parse the output. | 229 # Parse the output. |
| 205 # TODO(jbudorick): Transition test scripts away from parsing stdout. | 230 # TODO(jbudorick): Transition test scripts away from parsing stdout. |
| 206 results = self._test_instance.ParseGTestOutput(output) | 231 results = self._test_instance.ParseGTestOutput(output) |
| 207 return results | 232 return results |
| 208 | 233 |
| 209 #override | 234 #override |
| 210 def TearDown(self): | 235 def TearDown(self): |
| 211 def individual_device_tear_down(dev): | 236 def individual_device_tear_down(dev): |
| 212 for s in self._servers[str(dev)]: | 237 for s in self._servers[str(dev)]: |
| 213 s.TearDown() | 238 s.TearDown() |
| 214 | 239 |
| 215 self._env.parallel_devices.pMap(individual_device_tear_down) | 240 self._env.parallel_devices.pMap(individual_device_tear_down) |
| 216 | 241 |
| OLD | NEW |