| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 """Helper module for calling python-based tests.""" | 5 """Runs host-driven tests on a particular device.""" |
| 6 | |
| 7 | 6 |
| 8 import logging | 7 import logging |
| 9 import sys | 8 import sys |
| 10 import time | 9 import time |
| 11 import traceback | 10 import traceback |
| 12 | 11 |
| 13 from pylib.base import base_test_result | 12 from pylib.base import base_test_result |
| 13 from pylib.base import base_test_runner |
| 14 from pylib.instrumentation import test_result | 14 from pylib.instrumentation import test_result |
| 15 | 15 |
| 16 import test_case |
| 16 | 17 |
| 17 class PythonExceptionTestResult(test_result.InstrumentationTestResult): | 18 |
| 18 """Helper class for creating a test result from python exception.""" | 19 class HostDrivenExceptionTestResult(test_result.InstrumentationTestResult): |
| 20 """Test result corresponding to a python exception in a host-driven test.""" |
| 19 | 21 |
| 20 def __init__(self, test_name, start_date_ms, exc_info): | 22 def __init__(self, test_name, start_date_ms, exc_info): |
| 21 """Constructs an PythonExceptionTestResult object. | 23 """Constructs a HostDrivenExceptionTestResult object. |
| 22 | 24 |
| 23 Args: | 25 Args: |
| 24 test_name: name of the test which raised an exception. | 26 test_name: name of the test which raised an exception. |
| 25 start_date_ms: the starting time for the test. | 27 start_date_ms: the starting time for the test. |
| 26 exc_info: exception info, ostensibly from sys.exc_info(). | 28 exc_info: exception info, ostensibly from sys.exc_info(). |
| 27 """ | 29 """ |
| 28 exc_type, exc_value, exc_traceback = exc_info | 30 exc_type, exc_value, exc_traceback = exc_info |
| 29 trace_info = ''.join(traceback.format_exception(exc_type, exc_value, | 31 trace_info = ''.join(traceback.format_exception(exc_type, exc_value, |
| 30 exc_traceback)) | 32 exc_traceback)) |
| 31 log_msg = 'Exception:\n' + trace_info | 33 log_msg = 'Exception:\n' + trace_info |
| 32 duration_ms = (int(time.time()) * 1000) - start_date_ms | 34 duration_ms = (int(time.time()) * 1000) - start_date_ms |
| 33 | 35 |
| 34 super(PythonExceptionTestResult, self).__init__( | 36 super(HostDrivenExceptionTestResult, self).__init__( |
| 35 'PythonWrapper#' + test_name, | 37 test_name, |
| 36 base_test_result.ResultType.FAIL, | 38 base_test_result.ResultType.FAIL, |
| 37 start_date_ms, | 39 start_date_ms, |
| 38 duration_ms, | 40 duration_ms, |
| 39 log=str(exc_type) + ' ' + log_msg) | 41 log=str(exc_type) + ' ' + log_msg) |
| 40 | 42 |
| 41 | 43 |
| 42 def CallPythonTest(test, options): | 44 class HostDrivenTestRunner(base_test_runner.BaseTestRunner): |
| 43 """Invokes a test function and translates Python exceptions into test results. | 45 """Orchestrates running a set of host-driven tests. |
| 44 | 46 |
| 45 This method invokes SetUp()/TearDown() on the test. It is intended to be | 47 Any Python exceptions in the tests are caught and translated into a failed |
| 46 resilient to exceptions in SetUp(), the test itself, and TearDown(). Any | 48 result, rather than being re-raised on the main thread. |
| 47 Python exception means the test is marked as failed, and the test result will | |
| 48 contain information about the exception. | |
| 49 | |
| 50 If SetUp() raises an exception, the test is not run. | |
| 51 | |
| 52 If TearDown() raises an exception, the test is treated as a failure. However, | |
| 53 if the test itself raised an exception beforehand, that stack trace will take | |
| 54 precedence whether or not TearDown() also raised an exception. | |
| 55 | |
| 56 shard_index is not applicable in single-device scenarios, when test execution | |
| 57 is serial rather than parallel. Tests can use this to bring up servers with | |
| 58 unique port numbers, for example. See also python_test_sharder. | |
| 59 | |
| 60 Args: | |
| 61 test: an object which is ostensibly a subclass of PythonTestBase. | |
| 62 options: Options to use for setting up tests. | |
| 63 | |
| 64 Returns: | |
| 65 A TestRunResults object which contains any results produced by the test or, | |
| 66 in the case of a Python exception, the Python exception info. | |
| 67 """ | 49 """ |
| 68 | 50 |
| 69 start_date_ms = int(time.time()) * 1000 | 51 #override |
| 70 failed = False | 52 def __init__(self, device, shard_index, tool, build_type, push_deps, |
| 53 cleanup_test_files): |
| 54 """Creates a new HostDrivenTestRunner. |
| 71 | 55 |
| 72 try: | 56 Args: |
| 73 test.SetUp(options) | 57 device: Attached android device. |
| 74 except Exception: | 58 shard_index: Shard index. |
| 75 failed = True | 59 tool: Name of the Valgrind tool. |
| 76 logging.exception( | 60 build_type: 'Release' or 'Debug'. |
| 77 'Caught exception while trying to run SetUp() for test: ' + | 61 push_deps: If True, push all dependencies to the device. |
| 78 test.qualified_name) | 62 cleanup_test_files: Whether or not to cleanup test files on device. |
| 79 # Tests whose SetUp() method has failed are likely to fail, or at least | 63 """ |
| 80 # yield invalid results. | |
| 81 exc_info = sys.exc_info() | |
| 82 results = base_test_result.TestRunResults() | |
| 83 results.AddResult(PythonExceptionTestResult( | |
| 84 test.qualified_name, start_date_ms, exc_info)) | |
| 85 return results | |
| 86 | 64 |
| 87 try: | 65 super(HostDrivenTestRunner, self).__init__(device, tool, build_type, |
| 88 results = test.Run() | 66 push_deps, cleanup_test_files) |
| 89 except Exception: | |
| 90 # Setting this lets TearDown() avoid stomping on our stack trace from Run() | |
| 91 # should TearDown() also raise an exception. | |
| 92 failed = True | |
| 93 logging.exception('Caught exception while trying to run test: ' + | |
| 94 test.qualified_name) | |
| 95 exc_info = sys.exc_info() | |
| 96 results = base_test_result.TestRunResults() | |
| 97 results.AddResult(PythonExceptionTestResult( | |
| 98 test.qualified_name, start_date_ms, exc_info)) | |
| 99 | 67 |
| 100 try: | 68 # The shard index affords the ability to create unique port numbers (e.g. |
| 101 test.TearDown() | 69 # DEFAULT_PORT + shard_index) if the test so wishes. |
| 102 except Exception: | 70 self.shard_index = shard_index |
| 103 logging.exception( | 71 |
| 104 'Caught exception while trying run TearDown() for test: ' + | 72 #override |
| 105 test.qualified_name) | 73 def RunTest(self, test): |
| 106 if not failed: | 74 """Sets up and runs a test case. |
| 107 # Don't stomp the error during the test if TearDown blows up. This is a | 75 |
| 108 # trade-off: if the test fails, this will mask any problem with TearDown | 76 Args: |
| 109 # until the test is fixed. | 77 test: An object which is ostensibly a subclass of HostDrivenTestCase. |
| 78 |
| 79 Returns: |
| 80 A TestRunResults object which contains the result produced by the test |
| 81 and, in the case of a failure, the test that should be retried. |
| 82 """ |
| 83 |
| 84 assert isinstance(test, test_case.HostDrivenTestCase) |
| 85 |
| 86 start_date_ms = int(time.time()) * 1000 |
| 87 exception_raised = False |
| 88 |
| 89 try: |
| 90 test.SetUp(self.device, self.shard_index, self.build_type, |
| 91 self._push_deps, self._cleanup_test_files) |
| 92 except Exception: |
| 93 logging.exception( |
| 94 'Caught exception while trying to run SetUp() for test: ' + |
| 95 test.tagged_name) |
| 96 # Tests whose SetUp() method has failed are likely to fail, or at least |
| 97 # yield invalid results. |
| 110 exc_info = sys.exc_info() | 98 exc_info = sys.exc_info() |
| 111 results = base_test_result.TestRunResults() | 99 results = base_test_result.TestRunResults() |
| 112 results.AddResult(PythonExceptionTestResult( | 100 results.AddResult(HostDrivenExceptionTestResult( |
| 113 test.qualified_name, start_date_ms, exc_info)) | 101 test.tagged_name, start_date_ms, exc_info)) |
| 102 return results, test |
| 114 | 103 |
| 115 return results | 104 try: |
| 105 results = test.Run() |
| 106 except Exception: |
| 107 # Setting this lets TearDown() avoid stomping on our stack trace from |
| 108 # Run() should TearDown() also raise an exception. |
| 109 exception_raised = True |
| 110 logging.exception('Caught exception while trying to run test: ' + |
| 111 test.tagged_name) |
| 112 exc_info = sys.exc_info() |
| 113 results = base_test_result.TestRunResults() |
| 114 results.AddResult(HostDrivenExceptionTestResult( |
| 115 test.tagged_name, start_date_ms, exc_info)) |
| 116 |
| 117 try: |
| 118 test.TearDown() |
| 119 except Exception: |
| 120 logging.exception( |
| 121 'Caught exception while trying run TearDown() for test: ' + |
| 122 test.tagged_name) |
| 123 if not exception_raised: |
| 124 # Don't stomp the error during the test if TearDown blows up. This is a |
| 125 # trade-off: if the test fails, this will mask any problem with TearDown |
| 126 # until the test is fixed. |
| 127 exc_info = sys.exc_info() |
| 128 results = base_test_result.TestRunResults() |
| 129 results.AddResult(HostDrivenExceptionTestResult( |
| 130 test.tagged_name, start_date_ms, exc_info)) |
| 131 |
| 132 if not results.DidRunPass(): |
| 133 return results, test |
| 134 else: |
| 135 return results, None |
| OLD | NEW |