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