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
3 # found in the LICENSE file.
9 from pylib
import constants
10 from pylib
import pexpect
11 from pylib
.base
import base_test_result
12 from pylib
.base
import base_test_runner
13 from pylib
.device
import device_errors
14 from pylib
.perf
import perf_control
17 def _TestSuiteRequiresMockTestServer(suite_name
):
18 """Returns True if the test suite requires mock test server."""
19 tests_require_net_test_server
= ['unit_tests', 'net_unittests',
21 'content_browsertests']
23 tests_require_net_test_server
)
25 def _TestSuiteRequiresHighPerfMode(suite_name
):
26 """Returns True if the test suite requires high performance mode."""
27 return 'perftests' in suite_name
29 class TestRunner(base_test_runner
.BaseTestRunner
):
30 def __init__(self
, test_options
, device
, test_package
):
31 """Single test suite attached to a single device.
34 test_options: A GTestOptions object.
35 device: Device to run the tests.
36 test_package: An instance of TestPackage class.
39 super(TestRunner
, self
).__init
__(device
, test_options
.tool
,
40 test_options
.push_deps
,
41 test_options
.cleanup_test_files
)
43 self
.test_package
= test_package
44 self
.test_package
.tool
= self
.tool
45 self
._test
_arguments
= test_options
.test_arguments
47 timeout
= test_options
.timeout
50 # On a VM (e.g. chromium buildbots), this timeout is way too small.
51 if os
.environ
.get('BUILDBOT_SLAVENAME'):
54 self
._timeout
= timeout
* self
.tool
.GetTimeoutScale()
55 if _TestSuiteRequiresHighPerfMode(self
.test_package
.suite_name
):
56 self
._perf
_controller
= perf_control
.PerfControl(self
.device
)
59 def InstallTestPackage(self
):
60 self
.test_package
.Install(self
.device
)
63 def PushDataDeps(self
):
64 self
.device
.WaitUntilFullyBooted(timeout
=20)
66 if os
.path
.exists(constants
.ISOLATE_DEPS_DIR
):
67 # TODO(frankf): linux_dumper_unittest_helper needs to be in the same dir
68 # as breakpad_unittests exe. Find a better way to do this.
69 if self
.test_package
.suite_name
== 'breakpad_unittests':
70 device_dir
= constants
.TEST_EXECUTABLE_DIR
72 device_dir
= self
.device
.GetExternalStoragePath()
73 self
.device
.PushChangedFiles(
74 [(os
.path
.join(constants
.ISOLATE_DEPS_DIR
, p
),
75 os
.path
.join(device_dir
, p
))
76 for p
in os
.listdir(constants
.ISOLATE_DEPS_DIR
)])
78 def _ParseTestOutput(self
, p
):
79 """Process the test output.
82 p: An instance of pexpect spawn class.
85 A TestRunResults object.
87 results
= base_test_result
.TestRunResults()
90 re_run
= re
.compile('\[ RUN \] ?(.*)\r\n')
91 re_fail
= re
.compile('\[ FAILED \] ?(.*)\r\n')
92 re_ok
= re
.compile('\[ OK \] ?(.*?) .*\r\n')
95 re_passed
= re
.compile('\[ PASSED \] ?(.*)\r\n')
96 re_runner_fail
= re
.compile('\[ RUNNER_FAILED \] ?(.*)\r\n')
97 # Signal handlers are installed before starting tests
98 # to output the CRASHED marker when a crash happens.
99 re_crash
= re
.compile('\[ CRASHED \](.*)\r\n')
104 full_test_name
= None
105 found
= p
.expect([re_run
, re_passed
, re_runner_fail
],
106 timeout
=self
._timeout
)
107 if found
== 1: # re_passed
109 elif found
== 2: # re_runner_fail
112 full_test_name
= p
.match
.group(1).replace('\r', '')
113 found
= p
.expect([re_ok
, re_fail
, re_crash
], timeout
=self
._timeout
)
114 log
= p
.before
.replace('\r', '')
115 if found
== 0: # re_ok
116 if full_test_name
== p
.match
.group(1).replace('\r', ''):
117 results
.AddResult(base_test_result
.BaseTestResult(
118 full_test_name
, base_test_result
.ResultType
.PASS
,
120 elif found
== 2: # re_crash
121 results
.AddResult(base_test_result
.BaseTestResult(
122 full_test_name
, base_test_result
.ResultType
.CRASH
,
126 results
.AddResult(base_test_result
.BaseTestResult(
127 full_test_name
, base_test_result
.ResultType
.FAIL
, log
=log
))
129 logging
.error('Test terminated - EOF')
130 # We're here because either the device went offline, or the test harness
131 # crashed without outputting the CRASHED marker (crbug.com/175538).
132 if not self
.device
.IsOnline():
133 raise device_errors
.DeviceUnreachableError(
134 'Device %s went offline.' % str(self
.device
))
136 results
.AddResult(base_test_result
.BaseTestResult(
137 full_test_name
, base_test_result
.ResultType
.CRASH
,
138 log
=p
.before
.replace('\r', '')))
139 except pexpect
.TIMEOUT
:
140 logging
.error('Test terminated after %d second timeout.',
143 results
.AddResult(base_test_result
.BaseTestResult(
144 full_test_name
, base_test_result
.ResultType
.TIMEOUT
,
145 log
=p
.before
.replace('\r', '')))
149 ret_code
= self
.test_package
.GetGTestReturnCode(self
.device
)
152 'gtest exit code: %d\npexpect.before: %s\npexpect.after: %s',
153 ret_code
, p
.before
, p
.after
)
158 def RunTest(self
, test
):
159 test_results
= base_test_result
.TestRunResults()
161 return test_results
, None
164 self
.test_package
.ClearApplicationState(self
.device
)
165 self
.test_package
.CreateCommandLineFileOnDevice(
166 self
.device
, test
, self
._test
_arguments
)
167 test_results
= self
._ParseTestOutput
(
168 self
.test_package
.SpawnTestProcess(self
.device
))
170 self
.CleanupSpawningServerState()
171 # Calculate unknown test results.
172 all_tests
= set(test
.split(':'))
173 all_tests_ran
= set([t
.GetName() for t
in test_results
.GetAll()])
174 unknown_tests
= all_tests
- all_tests_ran
175 test_results
.AddResults(
176 [base_test_result
.BaseTestResult(t
, base_test_result
.ResultType
.UNKNOWN
)
177 for t
in unknown_tests
])
178 retry
= ':'.join([t
.GetName() for t
in test_results
.GetNotPass()])
179 return test_results
, retry
183 """Sets up necessary test enviroment for the test suite."""
184 super(TestRunner
, self
).SetUp()
185 if _TestSuiteRequiresMockTestServer(self
.test_package
.suite_name
):
186 self
.LaunchChromeTestServerSpawner()
187 if _TestSuiteRequiresHighPerfMode(self
.test_package
.suite_name
):
188 self
._perf
_controller
.SetHighPerfMode()
189 self
.tool
.SetupEnvironment()
193 """Cleans up the test enviroment for the test suite."""
194 if _TestSuiteRequiresHighPerfMode(self
.test_package
.suite_name
):
195 self
._perf
_controller
.SetDefaultPerfMode()
196 self
.test_package
.ClearApplicationState(self
.device
)
197 self
.tool
.CleanUpEnvironment()
198 super(TestRunner
, self
).TearDown()