OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 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 """Runs linker tests on a particular device.""" |
| 6 |
| 7 import logging |
| 8 import os.path |
| 9 import sys |
| 10 import time |
| 11 import traceback |
| 12 |
| 13 from pylib import constants |
| 14 from pylib.base import base_test_result |
| 15 from pylib.base import base_test_runner |
| 16 from pylib.utils import apk_helper |
| 17 |
| 18 import test_case |
| 19 |
| 20 |
| 21 # Name of the Android package to install for this to work. |
| 22 _PACKAGE_NAME = "ContentLinkerTest" |
| 23 |
| 24 class LinkerExceptionTestResult(base_test_result.BaseTestResult): |
| 25 """Test result corresponding to a python exception in a host-custom test.""" |
| 26 |
| 27 def __init__(self, test_name, exc_info): |
| 28 """Constructs a LinkerExceptionTestResult object. |
| 29 |
| 30 Args: |
| 31 test_name: name of the test which raised an exception. |
| 32 exc_info: exception info, ostensibly from sys.exc_info(). |
| 33 """ |
| 34 exc_type, exc_value, exc_traceback = exc_info |
| 35 trace_info = ''.join(traceback.format_exception(exc_type, exc_value, |
| 36 exc_traceback)) |
| 37 log_msg = 'Exception:\n' + trace_info |
| 38 |
| 39 super(LinkerExceptionTestResult, self).__init__( |
| 40 test_name, |
| 41 base_test_result.ResultType.FAIL, |
| 42 log=str(exc_type) + ' ' + log_msg) |
| 43 |
| 44 |
| 45 class LinkerTestRunner(base_test_runner.BaseTestRunner): |
| 46 """Orchestrates running a set of host-driven tests. |
| 47 |
| 48 Any Python exceptions in the tests are caught and translated into a failed |
| 49 result, rather than being re-raised on the main thread. |
| 50 """ |
| 51 |
| 52 #override |
| 53 def __init__(self, device, shard_index, tool, push_deps, |
| 54 cleanup_test_files): |
| 55 """Creates a new LinkerTestRunner. |
| 56 |
| 57 Args: |
| 58 device: Attached android device. |
| 59 shard_index: Shard index. |
| 60 tool: Name of the Valgrind tool. |
| 61 push_deps: If True, push all dependencies to the device. |
| 62 cleanup_test_files: Whether or not to cleanup test files on device. |
| 63 """ |
| 64 |
| 65 super(LinkerTestRunner, self).__init__(device, tool, push_deps, |
| 66 cleanup_test_files) |
| 67 |
| 68 # The shard index affords the ability to create unique port numbers (e.g. |
| 69 # DEFAULT_PORT + shard_index) if the test so wishes. |
| 70 self.shard_index = shard_index |
| 71 |
| 72 #override |
| 73 def InstallTestPackage(self): |
| 74 apk_path = os.path.join( |
| 75 constants.GetOutDirectory(), 'apks', '%s.apk' % _PACKAGE_NAME) |
| 76 |
| 77 if not os.path.exists(apk_path): |
| 78 raise Exception('%s not found, please build it' % apk_path) |
| 79 |
| 80 package_name = apk_helper.GetPackageName(apk_path) |
| 81 self.adb.ManagedInstall(apk_path, package_name) |
| 82 |
| 83 #override |
| 84 def RunTest(self, test): |
| 85 """Sets up and runs a test case. |
| 86 |
| 87 Args: |
| 88 test: An object which is ostensibly a subclass of LinkerTestCase. |
| 89 |
| 90 Returns: |
| 91 A TestRunResults object which contains the result produced by the test |
| 92 and, in the case of a failure, the test that should be retried. |
| 93 """ |
| 94 |
| 95 assert isinstance(test, test_case.LinkerTestCase) |
| 96 |
| 97 try: |
| 98 results = test.Run(self.device) |
| 99 except Exception: |
| 100 logging.exception('Caught exception while trying to run test: ' + |
| 101 test.tagged_name) |
| 102 exc_info = sys.exc_info() |
| 103 results = base_test_result.TestRunResults() |
| 104 results.AddResult(LinkerExceptionTestResult( |
| 105 test.tagged_name, exc_info)) |
| 106 |
| 107 if not results.DidRunPass(): |
| 108 return results, test |
| 109 else: |
| 110 return results, None |
OLD | NEW |