Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS 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 | # repohooks/pre-upload.py currently does not run pylint. But for developers who |
| 6 | # want to check their code manually we disable several harmless pylint warnings |
| 7 | # which just distract from more serious remaining issues. |
| 8 | # |
| 9 | # The instance variables _host and _install_paths are not defined in __init__(). |
| 10 | # pylint: disable=attribute-defined-outside-init |
| 11 | # |
| 12 | # Many short variable names don't follow the naming convention. |
| 13 | # pylint: disable=invalid-name |
| 14 | # |
| 15 | # _parse_result() and _dir_size() don't access self and could be functions. |
| 16 | # pylint: disable=no-self-use |
| 17 | # |
| 18 | # _ChromeLogin and _TradefedLogCollector have no public methods. |
| 19 | # pylint: disable=too-few-public-methods |
| 20 | |
| 21 | import contextlib |
| 22 | import errno |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 23 | import glob |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 24 | import hashlib |
Ilja H. Friedel | 4686377 | 2017-01-25 00:53:44 -0800 | [diff] [blame] | 25 | import lockfile |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 26 | import logging |
| 27 | import os |
| 28 | import pipes |
| 29 | import random |
| 30 | import re |
| 31 | import shutil |
| 32 | import stat |
| 33 | import tempfile |
| 34 | import urlparse |
| 35 | |
| 36 | from autotest_lib.client.bin import utils as client_utils |
Luis Hector Chavez | 554c6f8 | 2017-01-27 14:21:40 -0800 | [diff] [blame] | 37 | from autotest_lib.client.common_lib import base_utils |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 38 | from autotest_lib.client.common_lib import error |
| 39 | from autotest_lib.client.common_lib.cros import dev_server |
| 40 | from autotest_lib.server import afe_utils |
| 41 | from autotest_lib.server import autotest |
| 42 | from autotest_lib.server import test |
| 43 | from autotest_lib.server import utils |
| 44 | from autotest_lib.site_utils import lxc |
| 45 | |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 46 | |
| 47 | _SDK_TOOLS_DIR = ('gs://chromeos-arc-images/builds/' |
Ilja H. Friedel | 5c46f52 | 2016-12-07 20:24:00 -0800 | [diff] [blame] | 48 | 'git_mnc-dr-arc-dev-linux-static_sdk_tools/3554341') |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 49 | _SDK_TOOLS_FILES = ['aapt'] |
| 50 | # To stabilize adb behavior, we use dynamically linked adb. |
| 51 | _ADB_DIR = ('gs://chromeos-arc-images/builds/' |
Ilja H. Friedel | 5c46f52 | 2016-12-07 20:24:00 -0800 | [diff] [blame] | 52 | 'git_mnc-dr-arc-dev-linux-cheets_arm-user/3554341') |
Ilja H. Friedel | 9463990 | 2017-01-18 00:42:44 -0800 | [diff] [blame] | 53 | # TODO(ihf): Make this the path below as it seems to work locally. |
| 54 | # 'git_mnc-dr-arc-dev-linux-static_sdk_tools/3554341') |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 55 | _ADB_FILES = ['adb'] |
| 56 | |
| 57 | _ADB_POLLING_INTERVAL_SECONDS = 1 |
| 58 | _ADB_READY_TIMEOUT_SECONDS = 60 |
| 59 | _ANDROID_ADB_KEYS_PATH = '/data/misc/adb/adb_keys' |
| 60 | |
| 61 | _ARC_POLLING_INTERVAL_SECONDS = 1 |
| 62 | _ARC_READY_TIMEOUT_SECONDS = 60 |
| 63 | |
| 64 | _TRADEFED_PREFIX = 'autotest-tradefed-install_' |
| 65 | _TRADEFED_CACHE_LOCAL = '/tmp/autotest-tradefed-cache' |
| 66 | _TRADEFED_CACHE_CONTAINER = '/usr/local/autotest/results/shared/cache' |
| 67 | _TRADEFED_CACHE_CONTAINER_LOCK = '/usr/local/autotest/results/shared/lock' |
| 68 | |
| 69 | # According to dshi a drone has 500GB of disk space. It is ok for now to use |
| 70 | # 10GB of disk space, as no more than 10 tests should run in parallel. |
| 71 | # TODO(ihf): Investigate tighter cache size. |
| 72 | _TRADEFED_CACHE_MAX_SIZE = (10 * 1024 * 1024 * 1024) |
| 73 | |
| 74 | |
| 75 | class _ChromeLogin(object): |
| 76 | """Context manager to handle Chrome login state.""" |
| 77 | |
| 78 | def __init__(self, host): |
| 79 | self._host = host |
| 80 | |
| 81 | def __enter__(self): |
| 82 | """Logs in to the Chrome.""" |
| 83 | logging.info('Ensure Android is running...') |
| 84 | autotest.Autotest(self._host).run_test('cheets_CTSHelper', |
| 85 | check_client_result=True) |
| 86 | |
| 87 | def __exit__(self, exc_type, exc_value, traceback): |
| 88 | """On exit, to wipe out all the login state, reboot the machine. |
| 89 | |
| 90 | @param exc_type: Exception type if an exception is raised from the |
| 91 | with-block. |
| 92 | @param exc_value: Exception instance if an exception is raised from |
| 93 | the with-block. |
| 94 | @param traceback: Stack trace info if an exception is raised from |
| 95 | the with-block. |
| 96 | @return None, indicating not to ignore an exception from the with-block |
| 97 | if raised. |
| 98 | """ |
| 99 | logging.info('Rebooting...') |
| 100 | try: |
| 101 | self._host.reboot() |
| 102 | except Exception: |
| 103 | if exc_type is None: |
| 104 | raise |
| 105 | # If an exception is raise from the with-block, just record the |
| 106 | # exception for the rebooting to avoid ignoring the original |
| 107 | # exception. |
| 108 | logging.exception('Rebooting failed.') |
| 109 | |
| 110 | |
| 111 | @contextlib.contextmanager |
| 112 | def lock(filename): |
| 113 | """Prevents other autotest/tradefed instances from accessing cache.""" |
| 114 | filelock = lockfile.FileLock(filename) |
| 115 | # It is tempting just to call filelock.acquire(3600). But the implementation |
| 116 | # has very poor temporal granularity (timeout/10), which is unsuitable for |
| 117 | # our needs. See /usr/lib64/python2.7/site-packages/lockfile/ |
Ilja H. Friedel | d2410cc | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 118 | attempts = 0 |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 119 | while not filelock.i_am_locking(): |
| 120 | try: |
Ilja H. Friedel | d2410cc | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 121 | attempts += 1 |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 122 | logging.info('Waiting for cache lock...') |
| 123 | filelock.acquire(random.randint(1, 5)) |
| 124 | except (lockfile.AlreadyLocked, lockfile.LockTimeout): |
Ilja H. Friedel | d2410cc | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 125 | if attempts > 1000: |
| 126 | # Normally we should aqcuire the lock in a few seconds. Once we |
| 127 | # wait on the order of hours either the dev server IO is |
| 128 | # overloaded or a lock didn't get cleaned up. Take one for the |
| 129 | # team, break the lock and report a failure. This should fix |
| 130 | # the lock for following tests. If the failure affects more than |
| 131 | # one job look for a deadlock or dev server overload. |
| 132 | logging.error('Permanent lock failure. Trying to break lock.') |
| 133 | filelock.break_lock() |
| 134 | raise error.TestFail('Error: permanent cache lock failure.') |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 135 | else: |
Ilja H. Friedel | d2410cc | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 136 | logging.info('Acquired cache lock after %d attempts.', attempts) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 137 | try: |
| 138 | yield |
| 139 | finally: |
| 140 | filelock.release() |
| 141 | logging.info('Released cache lock.') |
| 142 | |
| 143 | |
Luis Hector Chavez | 554c6f8 | 2017-01-27 14:21:40 -0800 | [diff] [blame] | 144 | @contextlib.contextmanager |
| 145 | def adb_keepalive(target, extra_paths): |
| 146 | """A context manager that keeps the adb connection alive. |
| 147 | |
| 148 | AdbKeepalive will spin off a new process that will continuously poll for |
| 149 | adb's connected state, and will attempt to reconnect if it ever goes down. |
| 150 | This is the only way we can currently recover safely from (intentional) |
| 151 | reboots. |
| 152 | |
| 153 | @param target: the hostname and port of the DUT. |
| 154 | @param extra_paths: any additional components to the PATH environment |
| 155 | variable. |
| 156 | """ |
| 157 | from autotest_lib.client.common_lib.cros import adb_keepalive as module |
| 158 | # |__file__| returns the absolute path of the compiled bytecode of the |
| 159 | # module. We want to run the original .py file, so we need to change the |
| 160 | # extension back. |
| 161 | script_filename = module.__file__.replace('.pyc', '.py') |
| 162 | job = base_utils.BgJob([script_filename, target], |
| 163 | nickname='adb_keepalive', stderr_level=logging.DEBUG, |
| 164 | stdout_tee=base_utils.TEE_TO_LOGS, |
| 165 | stderr_tee=base_utils.TEE_TO_LOGS, |
| 166 | extra_paths=extra_paths) |
| 167 | |
| 168 | try: |
| 169 | yield |
| 170 | finally: |
| 171 | # The adb_keepalive.py script runs forever until SIGTERM is sent. |
| 172 | base_utils.nuke_subprocess(job.sp) |
| 173 | base_utils.join_bg_jobs([job]) |
| 174 | |
| 175 | |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 176 | class TradefedTest(test.test): |
| 177 | """Base class to prepare DUT to run tests via tradefed.""" |
| 178 | version = 1 |
| 179 | |
| 180 | def initialize(self, host=None): |
| 181 | """Sets up the tools and binary bundles for the test.""" |
| 182 | logging.info('Hostname: %s', host.hostname) |
| 183 | self._host = host |
| 184 | self._install_paths = [] |
| 185 | # Tests in the lab run within individual lxc container instances. |
| 186 | if utils.is_in_container(): |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 187 | cache_root = _TRADEFED_CACHE_CONTAINER |
| 188 | else: |
| 189 | cache_root = _TRADEFED_CACHE_LOCAL |
Ilja H. Friedel | 9463990 | 2017-01-18 00:42:44 -0800 | [diff] [blame] | 190 | # Quick sanity check and spew of java version installed on the server. |
| 191 | utils.run('java', args=('-version',), ignore_status=False, verbose=True, |
| 192 | stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 193 | # The content of the cache survives across jobs. |
| 194 | self._safe_makedirs(cache_root) |
| 195 | self._tradefed_cache = os.path.join(cache_root, 'cache') |
| 196 | self._tradefed_cache_lock = os.path.join(cache_root, 'lock') |
| 197 | # The content of the install location does not survive across jobs and |
| 198 | # is isolated (by using a unique path)_against other autotest instances. |
| 199 | # This is not needed for the lab, but if somebody wants to run multiple |
| 200 | # TradedefTest instance. |
| 201 | self._tradefed_install = tempfile.mkdtemp(prefix=_TRADEFED_PREFIX) |
| 202 | # Under lxc the cache is shared between multiple autotest/tradefed |
| 203 | # instances. We need to synchronize access to it. All binaries are |
| 204 | # installed through the (shared) cache into the local (unshared) |
| 205 | # lxc/autotest instance storage. |
| 206 | # If clearing the cache it must happen before all downloads. |
| 207 | self._clear_download_cache_if_needed() |
| 208 | # Set permissions (rwxr-xr-x) to the executable binaries. |
| 209 | permission = (stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH |
| 210 | | stat.S_IXOTH) |
| 211 | self._install_files(_ADB_DIR, _ADB_FILES, permission) |
| 212 | self._install_files(_SDK_TOOLS_DIR, _SDK_TOOLS_FILES, permission) |
| 213 | |
| 214 | def cleanup(self): |
| 215 | """Cleans up any dirtied state.""" |
| 216 | # Kill any lingering adb servers. |
| 217 | self._run('adb', verbose=True, args=('kill-server',)) |
| 218 | logging.info('Cleaning up %s.', self._tradefed_install) |
| 219 | shutil.rmtree(self._tradefed_install) |
| 220 | |
| 221 | def _login_chrome(self): |
| 222 | """Returns Chrome log-in context manager. |
| 223 | |
| 224 | Please see also cheets_CTSHelper for details about how this works. |
| 225 | """ |
| 226 | return _ChromeLogin(self._host) |
| 227 | |
Luis Hector Chavez | 554c6f8 | 2017-01-27 14:21:40 -0800 | [diff] [blame] | 228 | def _get_adb_target(self): |
| 229 | return '{}:{}'.format(self._host.hostname, self._host.port) |
| 230 | |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 231 | def _try_adb_connect(self): |
| 232 | """Attempts to connect to adb on the DUT. |
| 233 | |
| 234 | @return boolean indicating if adb connected successfully. |
| 235 | """ |
| 236 | # This may fail return failure due to a race condition in adb connect |
| 237 | # (b/29370989). If adb is already connected, this command will |
| 238 | # immediately return success. |
Luis Hector Chavez | 554c6f8 | 2017-01-27 14:21:40 -0800 | [diff] [blame] | 239 | hostport = self._get_adb_target() |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 240 | result = self._run( |
| 241 | 'adb', |
| 242 | args=('connect', hostport), |
| 243 | verbose=True, |
| 244 | ignore_status=True) |
| 245 | logging.info('adb connect {}:\n{}'.format(hostport, result.stdout)) |
| 246 | if result.exit_status != 0: |
| 247 | return False |
| 248 | |
| 249 | result = self._run('adb', args=('devices',)) |
| 250 | logging.info('adb devices:\n' + result.stdout) |
| 251 | if not re.search( |
| 252 | r'{}\s+(device|unauthorized)'.format(re.escape(hostport)), |
| 253 | result.stdout): |
| 254 | return False |
| 255 | |
| 256 | # Actually test the connection with an adb command as there can be |
| 257 | # a race between detecting the connected device and actually being |
| 258 | # able to run a commmand with authenticated adb. |
| 259 | result = self._run('adb', args=('shell', 'exit'), ignore_status=True) |
| 260 | return result.exit_status == 0 |
| 261 | |
| 262 | def _android_shell(self, command): |
| 263 | """Run a command remotely on the device in an android shell |
| 264 | |
| 265 | This function is strictly for internal use only, as commands do not run |
| 266 | in a fully consistent Android environment. Prefer adb shell instead. |
| 267 | """ |
| 268 | self._host.run('android-sh -c ' + pipes.quote(command)) |
| 269 | |
| 270 | def _write_android_file(self, filename, data): |
| 271 | """Writes a file to a location relative to the android container. |
| 272 | |
| 273 | This is an internal function used to bootstrap adb. |
| 274 | Tests should use adb push to write files. |
| 275 | """ |
| 276 | android_cmd = 'echo %s > %s' % (pipes.quote(data), |
| 277 | pipes.quote(filename)) |
| 278 | self._android_shell(android_cmd) |
| 279 | |
| 280 | def _connect_adb(self): |
| 281 | """Sets up ADB connection to the ARC container.""" |
| 282 | logging.info('Setting up adb connection.') |
| 283 | # Generate and push keys for adb. |
| 284 | # TODO(elijahtaylor): Extract this code to arc_common and de-duplicate |
| 285 | # code in arc.py on the client side tests. |
| 286 | key_path = os.path.join(self.tmpdir, 'test_key') |
| 287 | pubkey_path = key_path + '.pub' |
| 288 | self._run('adb', verbose=True, args=('keygen', pipes.quote(key_path))) |
| 289 | with open(pubkey_path, 'r') as f: |
| 290 | self._write_android_file(_ANDROID_ADB_KEYS_PATH, f.read()) |
| 291 | self._android_shell('restorecon ' + pipes.quote(_ANDROID_ADB_KEYS_PATH)) |
| 292 | os.environ['ADB_VENDOR_KEYS'] = key_path |
| 293 | |
| 294 | # Kill existing adb server to ensure that the env var is picked up. |
| 295 | self._run('adb', verbose=True, args=('kill-server',)) |
| 296 | |
| 297 | # This starts adbd. |
| 298 | self._android_shell('setprop sys.usb.config mtp,adb') |
| 299 | |
Luis Hector Chavez | 554c6f8 | 2017-01-27 14:21:40 -0800 | [diff] [blame] | 300 | # Also let it be automatically started upon reboot. |
| 301 | self._android_shell('setprop persist.sys.usb.config mtp,adb') |
| 302 | |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 303 | # adbd may take some time to come up. Repeatedly try to connect to adb. |
| 304 | utils.poll_for_condition(lambda: self._try_adb_connect(), |
Ilja H. Friedel | 6d5ca8f | 2016-10-26 22:35:36 -0700 | [diff] [blame] | 305 | exception=error.TestFail( |
| 306 | 'Error: Failed to set up adb connection'), |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 307 | timeout=_ADB_READY_TIMEOUT_SECONDS, |
| 308 | sleep_interval=_ADB_POLLING_INTERVAL_SECONDS) |
| 309 | |
| 310 | logging.info('Successfully setup adb connection.') |
| 311 | |
| 312 | def _wait_for_arc_boot(self): |
| 313 | """Wait until ARC is fully booted. |
| 314 | |
| 315 | Tests for the presence of the intent helper app to determine whether ARC |
| 316 | has finished booting. |
| 317 | """ |
| 318 | def intent_helper_running(): |
Kazuhiro Inaba | f2c4705 | 2017-01-26 09:18:51 +0900 | [diff] [blame] | 319 | result = self._run('adb', args=('shell', 'pgrep', '-f', |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 320 | 'org.chromium.arc.intent_helper')) |
| 321 | return bool(result.stdout) |
| 322 | utils.poll_for_condition( |
| 323 | intent_helper_running, |
Ilja H. Friedel | 6d5ca8f | 2016-10-26 22:35:36 -0700 | [diff] [blame] | 324 | exception=error.TestFail( |
| 325 | 'Error: Timed out waiting for intent helper.'), |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 326 | timeout=_ARC_READY_TIMEOUT_SECONDS, |
| 327 | sleep_interval=_ARC_POLLING_INTERVAL_SECONDS) |
| 328 | |
| 329 | def _disable_adb_install_dialog(self): |
| 330 | """Disables a dialog shown on adb install execution. |
| 331 | |
| 332 | By default, on adb install execution, "Allow Google to regularly check |
| 333 | device activity ... " dialog is shown. It requires manual user action |
| 334 | so that tests are blocked at the point. |
| 335 | This method disables it. |
| 336 | """ |
| 337 | logging.info('Disabling the adb install dialog.') |
| 338 | result = self._run( |
| 339 | 'adb', |
| 340 | verbose=True, |
| 341 | args=( |
| 342 | 'shell', |
| 343 | 'settings', |
| 344 | 'put', |
| 345 | 'global', |
| 346 | 'verifier_verify_adb_installs', |
| 347 | '0')) |
| 348 | logging.info('Disable adb dialog: %s', result.stdout) |
| 349 | |
| 350 | def _ready_arc(self): |
| 351 | """Ready ARC and adb for running tests via tradefed.""" |
| 352 | self._connect_adb() |
| 353 | self._disable_adb_install_dialog() |
| 354 | self._wait_for_arc_boot() |
| 355 | |
| 356 | def _safe_makedirs(self, path): |
| 357 | """Creates a directory at |path| and its ancestors. |
| 358 | |
| 359 | Unlike os.makedirs(), ignore errors even if directories exist. |
| 360 | """ |
| 361 | try: |
| 362 | os.makedirs(path) |
| 363 | except OSError as e: |
| 364 | if not (e.errno == errno.EEXIST and os.path.isdir(path)): |
| 365 | raise |
| 366 | |
| 367 | def _unzip(self, filename): |
| 368 | """Unzip the file. |
| 369 | |
| 370 | The destination directory name will be the stem of filename. |
| 371 | E.g., _unzip('foo/bar/baz.zip') will create directory at |
| 372 | 'foo/bar/baz', and then will inflate zip's content under the directory. |
| 373 | If here is already a directory at the stem, that directory will be used. |
| 374 | |
| 375 | @param filename: Path to the zip archive. |
| 376 | @return Path to the inflated directory. |
| 377 | """ |
| 378 | destination = os.path.splitext(filename)[0] |
| 379 | if os.path.isdir(destination): |
| 380 | return destination |
| 381 | self._safe_makedirs(destination) |
| 382 | utils.run('unzip', args=('-d', destination, filename)) |
| 383 | return destination |
| 384 | |
| 385 | def _dir_size(self, directory): |
| 386 | """Compute recursive size in bytes of directory.""" |
| 387 | size = 0 |
| 388 | for root, _, files in os.walk(directory): |
| 389 | size += sum(os.path.getsize(os.path.join(root, name)) |
| 390 | for name in files) |
| 391 | return size |
| 392 | |
| 393 | def _clear_download_cache_if_needed(self): |
| 394 | """Invalidates cache to prevent it from growing too large.""" |
| 395 | # If the cache is large enough to hold a working set, we can simply |
| 396 | # delete everything without thrashing. |
| 397 | # TODO(ihf): Investigate strategies like LRU. |
| 398 | with lock(self._tradefed_cache_lock): |
| 399 | size = self._dir_size(self._tradefed_cache) |
| 400 | if size > _TRADEFED_CACHE_MAX_SIZE: |
| 401 | logging.info('Current cache size=%d got too large. Clearing %s.' |
| 402 | , size, self._tradefed_cache) |
| 403 | shutil.rmtree(self._tradefed_cache) |
| 404 | self._safe_makedirs(self._tradefed_cache) |
| 405 | else: |
| 406 | logging.info('Current cache size=%d of %s.', size, |
| 407 | self._tradefed_cache) |
| 408 | |
| 409 | def _download_to_cache(self, uri): |
| 410 | """Downloads the uri from the storage server. |
| 411 | |
| 412 | It always checks the cache for available binaries first and skips |
| 413 | download if binaries are already in cache. |
| 414 | |
| 415 | The caller of this function is responsible for holding the cache lock. |
| 416 | |
| 417 | @param uri: The Google Storage or dl.google.com uri. |
| 418 | @return Path to the downloaded object, name. |
| 419 | """ |
| 420 | # Split uri into 3 pieces for use by gsutil and also by wget. |
| 421 | parsed = urlparse.urlparse(uri) |
| 422 | filename = os.path.basename(parsed.path) |
| 423 | # We are hashing the uri instead of the binary. This is acceptable, as |
| 424 | # the uris are supposed to contain version information and an object is |
| 425 | # not supposed to be changed once created. |
| 426 | output_dir = os.path.join(self._tradefed_cache, |
| 427 | hashlib.md5(uri).hexdigest()) |
| 428 | output = os.path.join(output_dir, filename) |
| 429 | # Check for existence of file. |
| 430 | if os.path.exists(output): |
| 431 | logging.info('Skipping download of %s, reusing %s.', uri, output) |
| 432 | return output |
| 433 | self._safe_makedirs(output_dir) |
| 434 | |
| 435 | if parsed.scheme not in ['gs', 'http', 'https']: |
Ilja H. Friedel | 6d5ca8f | 2016-10-26 22:35:36 -0700 | [diff] [blame] | 436 | raise error.TestFail('Error: Unknown download scheme %s' % |
| 437 | parsed.scheme) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 438 | if parsed.scheme in ['http', 'https']: |
| 439 | logging.info('Using wget to download %s to %s.', uri, output_dir) |
| 440 | # We are downloading 1 file at a time, hence using -O over -P. |
| 441 | # We also limit the rate to 20MBytes/s |
| 442 | utils.run( |
| 443 | 'wget', |
| 444 | args=( |
| 445 | '--report-speed=bits', |
| 446 | '--limit-rate=20M', |
| 447 | '-O', |
| 448 | output, |
| 449 | uri), |
| 450 | verbose=True) |
| 451 | return output |
| 452 | |
| 453 | if not client_utils.is_moblab(): |
| 454 | # If the machine can access to the storage server directly, |
| 455 | # defer to "gsutil" for downloading. |
| 456 | logging.info('Host %s not in lab. Downloading %s directly to %s.', |
| 457 | self._host.hostname, uri, output) |
| 458 | # b/17445576: gsutil rsync of individual files is not implemented. |
| 459 | utils.run('gsutil', args=('cp', uri, output), verbose=True) |
| 460 | return output |
| 461 | |
| 462 | # We are in the moblab. Because the machine cannot access the storage |
| 463 | # server directly, use dev server to proxy. |
| 464 | logging.info('Host %s is in lab. Downloading %s by staging to %s.', |
| 465 | self._host.hostname, uri, output) |
| 466 | |
| 467 | dirname = os.path.dirname(parsed.path) |
| 468 | archive_url = '%s://%s%s' % (parsed.scheme, parsed.netloc, dirname) |
| 469 | |
| 470 | # First, request the devserver to download files into the lab network. |
| 471 | # TODO(ihf): Switch stage_artifacts to honor rsync. Then we don't have |
| 472 | # to shuffle files inside of tarballs. |
| 473 | build = afe_utils.get_build(self._host) |
| 474 | ds = dev_server.ImageServer.resolve(build) |
| 475 | ds.stage_artifacts(build, files=[filename], archive_url=archive_url) |
| 476 | |
| 477 | # Then download files from the dev server. |
| 478 | # TODO(ihf): use rsync instead of wget. Are there 3 machines involved? |
| 479 | # Itself, dev_server plus DUT? Or is there just no rsync in moblab? |
| 480 | ds_src = '/'.join([ds.url(), 'static', dirname, filename]) |
| 481 | logging.info('dev_server URL: %s', ds_src) |
| 482 | # Calls into DUT to pull uri from dev_server. |
| 483 | utils.run( |
| 484 | 'wget', |
| 485 | args=( |
| 486 | '--report-speed=bits', |
| 487 | '--limit-rate=20M', |
| 488 | '-O', |
Ilja H. Friedel | b83646b | 2016-10-18 13:02:59 -0700 | [diff] [blame] | 489 | output, |
| 490 | ds_src), |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 491 | verbose=True) |
| 492 | return output |
| 493 | |
| 494 | def _instance_copy(self, cache_path): |
| 495 | """Makes a copy of a file from the (shared) cache to a wholy owned |
| 496 | local instance. Also copies one level of cache directoy (MD5 named). |
| 497 | """ |
| 498 | filename = os.path.basename(cache_path) |
| 499 | dirname = os.path.basename(os.path.dirname(cache_path)) |
| 500 | instance_dir = os.path.join(self._tradefed_install, dirname) |
| 501 | # Make sure destination directory is named the same. |
| 502 | self._safe_makedirs(instance_dir) |
| 503 | instance_path = os.path.join(instance_dir, filename) |
| 504 | shutil.copyfile(cache_path, instance_path) |
| 505 | return instance_path |
| 506 | |
| 507 | def _install_bundle(self, gs_uri): |
| 508 | """Downloads a zip file, installs it and returns the local path.""" |
| 509 | if not gs_uri.endswith('.zip'): |
Ilja H. Friedel | 6d5ca8f | 2016-10-26 22:35:36 -0700 | [diff] [blame] | 510 | raise error.TestFail('Error: Not a .zip file %s.', gs_uri) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 511 | # Atomic write through of file. |
| 512 | with lock(self._tradefed_cache_lock): |
| 513 | cache_path = self._download_to_cache(gs_uri) |
| 514 | local = self._instance_copy(cache_path) |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 515 | |
| 516 | unzipped = self._unzip(local) |
| 517 | self._abi = 'x86' if 'x86-x86' in unzipped else 'arm' |
| 518 | return unzipped |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 519 | |
| 520 | def _install_files(self, gs_dir, files, permission): |
| 521 | """Installs binary tools.""" |
| 522 | for filename in files: |
| 523 | gs_uri = os.path.join(gs_dir, filename) |
| 524 | # Atomic write through of file. |
| 525 | with lock(self._tradefed_cache_lock): |
| 526 | cache_path = self._download_to_cache(gs_uri) |
| 527 | local = self._instance_copy(cache_path) |
| 528 | os.chmod(local, permission) |
| 529 | # Keep track of PATH. |
| 530 | self._install_paths.append(os.path.dirname(local)) |
| 531 | |
| 532 | def _run(self, *args, **kwargs): |
| 533 | """Executes the given command line. |
| 534 | |
| 535 | To support SDK tools, such as adb or aapt, this adds _install_paths |
| 536 | to the extra_paths. Before invoking this, ensure _install_files() has |
| 537 | been called. |
| 538 | """ |
| 539 | kwargs['extra_paths'] = ( |
| 540 | kwargs.get('extra_paths', []) + self._install_paths) |
| 541 | return utils.run(*args, **kwargs) |
| 542 | |
| 543 | def _parse_tradefed_datetime(self, result, summary=None): |
| 544 | """Get the tradefed provided result ID consisting of a datetime stamp. |
| 545 | |
| 546 | Unfortunately we are unable to tell tradefed where to store the results. |
| 547 | In the lab we have multiple instances of tradefed running in parallel |
| 548 | writing results and logs to the same base directory. This function |
| 549 | finds the identifier which tradefed used during the current run and |
| 550 | returns it for further processing of result files. |
| 551 | |
| 552 | @param result: The result object from utils.run. |
| 553 | @param summary: Test result summary from runs so far. |
| 554 | @return datetime_id: The result ID chosen by tradefed. |
| 555 | Example: '2016.07.14_00.34.50'. |
| 556 | """ |
| 557 | # This string is show for both 'run' and 'continue' after all tests. |
| 558 | match = re.search(r': XML test result file generated at (\S+). Passed', |
| 559 | result.stdout) |
| 560 | if not (match and match.group(1)): |
| 561 | # TODO(ihf): Find out if we ever recover something interesting in |
| 562 | # this case. Otherwise delete it. |
| 563 | # Try harder to find the remains. This string shows before all |
| 564 | # tests but only with 'run', not 'continue'. |
| 565 | logging.warning('XML test result file incomplete?') |
| 566 | match = re.search(r': Created result dir (\S+)', result.stdout) |
| 567 | if not (match and match.group(1)): |
| 568 | error_msg = 'Test did not complete due to Chrome or ARC crash.' |
| 569 | if summary: |
| 570 | error_msg += (' Test summary from previous runs: %s' |
| 571 | % summary) |
Ilja H. Friedel | 6d5ca8f | 2016-10-26 22:35:36 -0700 | [diff] [blame] | 572 | raise error.TestFail(error_msg) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 573 | datetime_id = match.group(1) |
| 574 | logging.info('Tradefed identified results and logs with %s.', |
| 575 | datetime_id) |
| 576 | return datetime_id |
| 577 | |
Rohit Makasana | 99116d3 | 2016-10-17 19:32:04 -0700 | [diff] [blame] | 578 | def _parse_result(self, result, waivers=None): |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 579 | """Check the result from the tradefed output. |
| 580 | |
| 581 | This extracts the test pass/fail/executed list from the output of |
| 582 | tradefed. It is up to the caller to handle inconsistencies. |
| 583 | |
| 584 | @param result: The result object from utils.run. |
Rohit Makasana | 99116d3 | 2016-10-17 19:32:04 -0700 | [diff] [blame] | 585 | @param waivers: a set() of tests which are permitted to fail. |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 586 | """ |
| 587 | # Parse the stdout to extract test status. In particular step over |
| 588 | # similar output for each ABI and just look at the final summary. |
| 589 | match = re.search(r'(XML test result file generated at (\S+). ' |
| 590 | r'Passed (\d+), Failed (\d+), Not Executed (\d+))', |
| 591 | result.stdout) |
| 592 | if not match: |
| 593 | raise error.Test('Test log does not contain a summary.') |
| 594 | |
| 595 | passed = int(match.group(3)) |
| 596 | failed = int(match.group(4)) |
| 597 | not_executed = int(match.group(5)) |
| 598 | match = re.search(r'(Start test run of (\d+) packages, containing ' |
| 599 | r'(\d+(?:,\d+)?) tests)', result.stdout) |
| 600 | if match and match.group(3): |
| 601 | tests = int(match.group(3).replace(',', '')) |
| 602 | else: |
| 603 | # Unfortunately this happens. Assume it made no other mistakes. |
| 604 | logging.warning('Tradefed forgot to print number of tests.') |
| 605 | tests = passed + failed + not_executed |
Rohit Makasana | 99116d3 | 2016-10-17 19:32:04 -0700 | [diff] [blame] | 606 | # TODO(rohitbm): make failure parsing more robust by extracting the list |
| 607 | # of failing tests instead of searching in the result blob. As well as |
| 608 | # only parse for waivers for the running ABI. |
| 609 | if waivers: |
| 610 | for testname in waivers: |
David Haddock | 1671233 | 2016-11-03 14:35:23 -0700 | [diff] [blame] | 611 | # TODO(dhaddock): Find a more robust way to apply waivers. |
| 612 | fail_count = result.stdout.count(testname + ' FAIL') |
| 613 | if fail_count: |
| 614 | if fail_count > 2: |
| 615 | raise error.TestFail('Error: There are too many ' |
| 616 | 'failures found in the output to ' |
| 617 | 'be valid for applying waivers. ' |
| 618 | 'Please check output.') |
| 619 | failed -= fail_count |
Rohit Makasana | 99116d3 | 2016-10-17 19:32:04 -0700 | [diff] [blame] | 620 | # To maintain total count consistency. |
David Haddock | 1671233 | 2016-11-03 14:35:23 -0700 | [diff] [blame] | 621 | passed += fail_count |
| 622 | logging.info('Waived failure for %s %d time(s)', |
| 623 | testname, fail_count) |
Rohit Makasana | 99116d3 | 2016-10-17 19:32:04 -0700 | [diff] [blame] | 624 | logging.info('tests=%d, passed=%d, failed=%d, not_executed=%d', |
| 625 | tests, passed, failed, not_executed) |
David Haddock | 1671233 | 2016-11-03 14:35:23 -0700 | [diff] [blame] | 626 | if failed < 0: |
| 627 | raise error.TestFail('Error: Internal waiver book keeping has ' |
| 628 | 'become inconsistent.') |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 629 | return (tests, passed, failed, not_executed) |
| 630 | |
| 631 | def _collect_logs(self, repository, datetime, destination): |
| 632 | """Collects the tradefed logs. |
| 633 | |
| 634 | It is legal to collect the same logs multiple times. This is normal |
| 635 | after 'tradefed continue' updates existing logs with new results. |
| 636 | |
| 637 | @param repository: Full path to tradefeds output on disk. |
| 638 | @param datetime: The identifier which tradefed assigned to the run. |
| 639 | Currently this looks like '2016.07.14_00.34.50'. |
| 640 | @param destination: Autotest result directory (destination of logs). |
| 641 | """ |
| 642 | logging.info('Collecting tradefed testResult.xml and logs to %s.', |
| 643 | destination) |
| 644 | repository_results = os.path.join(repository, 'results') |
| 645 | repository_logs = os.path.join(repository, 'logs') |
| 646 | # Because other tools rely on the currently chosen Google storage paths |
| 647 | # we need to keep destination_results in |
| 648 | # cheets_CTS.*/results/android-cts/2016.mm.dd_hh.mm.ss(/|.zip) |
| 649 | # and destination_logs in |
| 650 | # cheets_CTS.*/results/android-cts/logs/2016.mm.dd_hh.mm.ss/ |
| 651 | destination_results = destination |
Ilja H. Friedel | b83646b | 2016-10-18 13:02:59 -0700 | [diff] [blame] | 652 | destination_results_datetime = os.path.join(destination_results, |
| 653 | datetime) |
Ilja H. Friedel | bee84a7 | 2016-09-28 15:57:06 -0700 | [diff] [blame] | 654 | destination_results_datetime_zip = destination_results_datetime + '.zip' |
| 655 | destination_logs = os.path.join(destination, 'logs') |
| 656 | destination_logs_datetime = os.path.join(destination_logs, datetime) |
| 657 | # We may have collected the same logs before, clean old versions. |
| 658 | if os.path.exists(destination_results_datetime_zip): |
| 659 | os.remove(destination_results_datetime_zip) |
| 660 | if os.path.exists(destination_results_datetime): |
| 661 | shutil.rmtree(destination_results_datetime) |
| 662 | if os.path.exists(destination_logs_datetime): |
| 663 | shutil.rmtree(destination_logs_datetime) |
| 664 | shutil.copytree( |
| 665 | os.path.join(repository_results, datetime), |
| 666 | destination_results_datetime) |
| 667 | # Copying the zip file has to happen after the tree so the destination |
| 668 | # directory is available. |
| 669 | shutil.copy( |
| 670 | os.path.join(repository_results, datetime) + '.zip', |
| 671 | destination_results_datetime_zip) |
| 672 | shutil.copytree( |
| 673 | os.path.join(repository_logs, datetime), |
| 674 | destination_logs_datetime) |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 675 | |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 676 | def _get_expected_failures(self, directory): |
| 677 | """Return a list of expected failures. |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 678 | |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 679 | @return: a list of expected failures. |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 680 | """ |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 681 | logging.info('Loading expected failures from %s.', directory) |
| 682 | expected_fail_dir = os.path.join(self.bindir, directory) |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 683 | expected_fail_files = glob.glob(expected_fail_dir + '/*.' + self._abi) |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 684 | expected_failures = set() |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 685 | for expected_fail_file in expected_fail_files: |
| 686 | try: |
| 687 | file_path = os.path.join(expected_fail_dir, expected_fail_file) |
| 688 | with open(file_path) as f: |
| 689 | lines = set(f.read().splitlines()) |
| 690 | logging.info('Loaded %d expected failures from %s', |
| 691 | len(lines), expected_fail_file) |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 692 | expected_failures |= lines |
David Haddock | b9a362b | 2016-10-28 16:19:12 -0700 | [diff] [blame] | 693 | except IOError as e: |
| 694 | logging.error('Error loading %s (%s).', file_path, e.strerror) |
Rohit Makasana | 7756690 | 2016-11-01 15:34:27 -0700 | [diff] [blame] | 695 | logging.info('Finished loading expected failures: %s', expected_failures) |
| 696 | return expected_failures |