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 7import logging 8import os.path 9import sys 10import traceback 11 12from pylib import constants 13from pylib.base import base_test_result 14from pylib.base import base_test_runner 15from pylib.linker import test_case 16 17 18# Name of the Android package to install for this to work. 19_PACKAGE_NAME = 'ChromiumLinkerTest' 20 21 22class LinkerExceptionTestResult(base_test_result.BaseTestResult): 23 """Test result corresponding to a python exception in a host-custom test.""" 24 25 def __init__(self, test_name, exc_info): 26 """Constructs a LinkerExceptionTestResult object. 27 28 Args: 29 test_name: name of the test which raised an exception. 30 exc_info: exception info, ostensibly from sys.exc_info(). 31 """ 32 exc_type, exc_value, exc_traceback = exc_info 33 trace_info = ''.join(traceback.format_exception(exc_type, exc_value, 34 exc_traceback)) 35 log_msg = 'Exception:\n' + trace_info 36 37 super(LinkerExceptionTestResult, self).__init__( 38 test_name, 39 base_test_result.ResultType.FAIL, 40 log="%s %s" % (exc_type, log_msg)) 41 42 43class LinkerTestRunner(base_test_runner.BaseTestRunner): 44 """Orchestrates running a set of linker tests. 45 46 Any Python exceptions in the tests are caught and translated into a failed 47 result, rather than being re-raised on the main thread. 48 """ 49 50 #override 51 def __init__(self, device, tool): 52 """Creates a new LinkerTestRunner. 53 54 Args: 55 device: Attached android device. 56 tool: Name of the Valgrind tool. 57 """ 58 super(LinkerTestRunner, self).__init__(device, tool) 59 60 #override 61 def InstallTestPackage(self): 62 apk_path = os.path.join( 63 constants.GetOutDirectory(), 'apks', '%s.apk' % _PACKAGE_NAME) 64 65 if not os.path.exists(apk_path): 66 raise Exception('%s not found, please build it' % apk_path) 67 68 self.device.Install(apk_path) 69 70 #override 71 def RunTest(self, test): 72 """Sets up and runs a test case. 73 74 Args: 75 test: An object which is ostensibly a subclass of LinkerTestCaseBase. 76 77 Returns: 78 A TestRunResults object which contains the result produced by the test 79 and, in the case of a failure, the test that should be retried. 80 """ 81 82 assert isinstance(test, test_case.LinkerTestCaseBase) 83 84 try: 85 results = test.Run(self.device) 86 except Exception: # pylint: disable=broad-except 87 logging.exception('Caught exception while trying to run test: ' + 88 test.tagged_name) 89 exc_info = sys.exc_info() 90 results = base_test_result.TestRunResults() 91 results.AddResult(LinkerExceptionTestResult( 92 test.tagged_name, exc_info)) 93 94 if not results.DidRunPass(): 95 return results, test 96 else: 97 return results, None 98