OLD | NEW |
| (Empty) |
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. | |
4 | |
5 | |
6 import logging | |
7 import os | |
8 import shutil | |
9 import sys | |
10 import tempfile | |
11 | |
12 import cmd_helper | |
13 import constants | |
14 from test_package import TestPackage | |
15 from pylib import pexpect | |
16 | |
17 | |
18 class TestPackageExecutable(TestPackage): | |
19 """A helper class for running stand-alone executables.""" | |
20 | |
21 _TEST_RUNNER_RET_VAL_FILE = 'gtest_retval' | |
22 | |
23 def __init__(self, adb, device, test_suite, timeout, | |
24 cleanup_test_files, tool, dump_debug_info, | |
25 symbols_dir=None): | |
26 """ | |
27 Args: | |
28 adb: ADB interface the tests are using. | |
29 device: Device to run the tests. | |
30 test_suite: A specific test suite to run, empty to run all. | |
31 timeout: Timeout for each test. | |
32 cleanup_test_files: Whether or not to cleanup test files on device. | |
33 tool: Name of the Valgrind tool. | |
34 dump_debug_info: A debug_info object. | |
35 symbols_dir: Directory to put the stripped binaries. | |
36 """ | |
37 TestPackage.__init__(self, adb, device, test_suite, timeout, | |
38 cleanup_test_files, tool, dump_debug_info) | |
39 self.symbols_dir = symbols_dir | |
40 | |
41 def _GetGTestReturnCode(self): | |
42 ret = None | |
43 ret_code = 1 # Assume failure if we can't find it | |
44 ret_code_file = tempfile.NamedTemporaryFile() | |
45 try: | |
46 if not self.adb.Adb().Pull( | |
47 constants.TEST_EXECUTABLE_DIR + '/' + | |
48 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE, | |
49 ret_code_file.name): | |
50 logging.critical('Unable to pull gtest ret val file %s', | |
51 ret_code_file.name) | |
52 raise ValueError | |
53 ret_code = file(ret_code_file.name).read() | |
54 ret = int(ret_code) | |
55 except ValueError: | |
56 logging.critical('Error reading gtest ret val file %s [%s]', | |
57 ret_code_file.name, ret_code) | |
58 ret = 1 | |
59 return ret | |
60 | |
61 def _AddNativeCoverageExports(self): | |
62 # export GCOV_PREFIX set the path for native coverage results | |
63 # export GCOV_PREFIX_STRIP indicates how many initial directory | |
64 # names to strip off the hardwired absolute paths. | |
65 # This value is calculated in buildbot.sh and | |
66 # depends on where the tree is built. | |
67 # Ex: /usr/local/google/code/chrome will become | |
68 # /code/chrome if GCOV_PREFIX_STRIP=3 | |
69 try: | |
70 depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] | |
71 except KeyError: | |
72 logging.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: ' | |
73 'No native coverage.') | |
74 return '' | |
75 export_string = ('export GCOV_PREFIX="%s/gcov"\n' % | |
76 self.adb.GetExternalStorage()) | |
77 export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth | |
78 return export_string | |
79 | |
80 def GetAllTests(self): | |
81 """Returns a list of all tests available in the test suite.""" | |
82 all_tests = self.adb.RunShellCommand( | |
83 '%s %s/%s --gtest_list_tests' % | |
84 (self.tool.GetTestWrapper(), | |
85 constants.TEST_EXECUTABLE_DIR, | |
86 self.test_suite_basename)) | |
87 return self._ParseGTestListTests(all_tests) | |
88 | |
89 def CreateTestRunnerScript(self, gtest_filter, test_arguments): | |
90 """Creates a test runner script and pushes to the device. | |
91 | |
92 Args: | |
93 gtest_filter: A gtest_filter flag. | |
94 test_arguments: Additional arguments to pass to the test binary. | |
95 """ | |
96 tool_wrapper = self.tool.GetTestWrapper() | |
97 sh_script_file = tempfile.NamedTemporaryFile() | |
98 # We need to capture the exit status from the script since adb shell won't | |
99 # propagate to us. | |
100 sh_script_file.write('cd %s\n' | |
101 '%s' | |
102 '%s %s/%s --gtest_filter=%s %s\n' | |
103 'echo $? > %s' % | |
104 (constants.TEST_EXECUTABLE_DIR, | |
105 self._AddNativeCoverageExports(), | |
106 tool_wrapper, constants.TEST_EXECUTABLE_DIR, | |
107 self.test_suite_basename, | |
108 gtest_filter, test_arguments, | |
109 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE)) | |
110 sh_script_file.flush() | |
111 cmd_helper.RunCmd(['chmod', '+x', sh_script_file.name]) | |
112 self.adb.PushIfNeeded( | |
113 sh_script_file.name, | |
114 constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh') | |
115 logging.info('Conents of the test runner script: ') | |
116 for line in open(sh_script_file.name).readlines(): | |
117 logging.info(' ' + line.rstrip()) | |
118 | |
119 def RunTestsAndListResults(self): | |
120 """Runs all the tests and checks for failures. | |
121 | |
122 Returns: | |
123 A TestResults object. | |
124 """ | |
125 args = ['adb', '-s', self.device, 'shell', 'sh', | |
126 constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh'] | |
127 logging.info(args) | |
128 p = pexpect.spawn(args[0], args[1:], logfile=sys.stdout) | |
129 return self._WatchTestOutput(p) | |
130 | |
131 def StripAndCopyExecutable(self): | |
132 """Strips and copies the executable to the device.""" | |
133 if self.tool.NeedsDebugInfo(): | |
134 target_name = self.test_suite | |
135 else: | |
136 target_name = self.test_suite + '_' + self.device + '_stripped' | |
137 should_strip = True | |
138 if os.path.isfile(target_name): | |
139 logging.info('Found target file %s' % target_name) | |
140 target_mtime = os.stat(target_name).st_mtime | |
141 source_mtime = os.stat(self.test_suite).st_mtime | |
142 if target_mtime > source_mtime: | |
143 logging.info('Target mtime (%d) is newer than source (%d), assuming ' | |
144 'no change.' % (target_mtime, source_mtime)) | |
145 should_strip = False | |
146 | |
147 if should_strip: | |
148 logging.info('Did not find up-to-date stripped binary. Generating a ' | |
149 'new one (%s).' % target_name) | |
150 # Whenever we generate a stripped binary, copy to the symbols dir. If we | |
151 # aren't stripping a new binary, assume it's there. | |
152 if self.symbols_dir: | |
153 if not os.path.exists(self.symbols_dir): | |
154 os.makedirs(self.symbols_dir) | |
155 shutil.copy(self.test_suite, self.symbols_dir) | |
156 strip = os.environ['STRIP'] | |
157 cmd_helper.RunCmd([strip, self.test_suite, '-o', target_name]) | |
158 test_binary = constants.TEST_EXECUTABLE_DIR + '/' + self.test_suite_basename | |
159 self.adb.PushIfNeeded(target_name, test_binary) | |
160 | |
161 def _GetTestSuiteBaseName(self): | |
162 """Returns the base name of the test suite.""" | |
163 return os.path.basename(self.test_suite) | |
OLD | NEW |