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