Chromium Code Reviews| 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 import copy | |
| 8 import logging | |
| 9 import multiprocessing | |
| 10 | |
| 11 from pylib.base import base_test_result | |
| 12 from pylib.base import base_test_runner | |
| 13 from pylib.base import sharded_tests_queue | |
| 14 from pylib.instrumentation import test_result | |
| 15 | |
| 16 import test_base | |
| 6 | 17 |
| 7 | 18 |
| 8 import logging | 19 class HostDrivenExceptionTestResult(test_result.InstrumentationTestResult): |
| 9 import sys | 20 """A test result from an exception in a host-driven test.""" |
| 10 import time | |
| 11 import traceback | |
| 12 | |
| 13 from pylib.base import base_test_result | |
| 14 from pylib.instrumentation import test_result | |
| 15 | |
| 16 | |
| 17 class PythonExceptionTestResult(test_result.InstrumentationTestResult): | |
| 18 """Helper class for creating a test result from python exception.""" | |
| 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 an 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 'HostDrivenWrapper#' + 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): |
|
frankf
2013/07/24 01:04:55
By deriving from BaseTestRunner, you're inheriting
gkanwar1
2013/07/24 17:38:04
Yes, I took a look at the BaseTestRunner before ma
| |
| 43 """Invokes a test function and translates Python exceptions into test results. | 45 """Thin wrapper around a list of HostDrivenTestBase instances. |
| 44 | 46 |
| 45 This method invokes SetUp()/TearDown() on the test. It is intended to be | 47 This is meant to be a long-lived object which can run multiple host-driven |
| 46 resilient to exceptions in SetUp(), the test itself, and TearDown(). Any | 48 tests within its lifetime. Tests will receive the device_id and shard_index. |
| 47 Python exception means the test is marked as failed, and the test result will | |
| 48 contain information about the exception. | |
| 49 | 49 |
| 50 If SetUp() raises an exception, the test is not run. | 50 The shard index affords the ability to create unique port numbers (e.g. |
| 51 | 51 DEFAULT_PORT + shard_index) if the test so wishes. |
| 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 """ | 52 """ |
| 68 | 53 |
| 69 start_date_ms = int(time.time()) * 1000 | 54 #override |
| 70 failed = False | 55 def __init__(self, device, shard_index, tool, build_type, push_deps, |
| 56 cleanup_test_files): | |
| 57 """Create a new HostDrivenTestRunner | |
| 71 | 58 |
| 72 try: | 59 Args: |
| 73 test.SetUp(options) | 60 device: Attached android device. |
| 74 except Exception: | 61 shard_index: Shard index. |
| 75 failed = True | 62 tool: Name of the Valgrind tool. |
| 76 logging.exception( | 63 build_type: 'Release' or 'Debug'. |
| 77 'Caught exception while trying to run SetUp() for test: ' + | 64 push_deps: If True, push all dependencies to the device. |
| 78 test.qualified_name) | 65 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 | 66 """ |
| 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 | 67 |
| 87 try: | 68 super(HostDrivenTestRunner, self).__init__(device, tool, build_type, |
| 69 push_deps, cleanup_test_files) | |
| 70 self.shard_index = shard_index | |
| 71 | |
| 72 #override | |
| 73 def RunTest(self, test): | |
| 74 """Sets up and runs a test case. | |
| 75 | |
| 76 Args: | |
| 77 test: An object which is ostensibly a subclass of HostDrivenTestBase. | |
| 78 | |
| 79 Returns: | |
| 80 A TestRunResults object which contains any results produced by the test | |
| 81 or, in the case of a Python exception, the Python exception info, and | |
| 82 which tests to retry or None. | |
| 83 """ | |
| 84 | |
| 85 assert(isinstance(test, test_base.HostDrivenTestBase)) | |
| 86 test.SetUp(self.device, self.shard_index, self.build_type, self._push_deps, | |
| 87 self._cleanup_test_files) | |
| 88 results = test.Run() | 88 results = test.Run() |
| 89 except Exception: | 89 test.TearDown() |
|
frankf
2013/07/24 01:04:55
There's no exception handling here. Think about th
gkanwar1
2013/07/24 17:38:04
Ah, I removed the try/excepts because we're using
| |
| 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 | 90 |
| 100 try: | 91 if not results.DidRunPass(): |
| 101 test.TearDown() | 92 return results, test |
| 102 except Exception: | 93 else: |
| 103 logging.exception( | 94 return results, None |
| 104 'Caught exception while trying run TearDown() for test: ' + | |
| 105 test.qualified_name) | |
| 106 if not failed: | |
| 107 # Don't stomp the error during the test if TearDown blows up. This is a | |
| 108 # trade-off: if the test fails, this will mask any problem with TearDown | |
| 109 # until the test is fixed. | |
| 110 exc_info = sys.exc_info() | |
| 111 results = base_test_result.TestRunResults() | |
| 112 results.AddResult(PythonExceptionTestResult( | |
| 113 test.qualified_name, start_date_ms, exc_info)) | |
| 114 | |
| 115 return results | |
| OLD | NEW |