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.
5 """Defines TestPackageExecutable to help run stand-alone executables."""
12 from pylib import cmd_helper
13 from pylib import constants
14 from pylib import pexpect
15 from pylib.device import device_errors
16 from pylib.gtest.test_package import TestPackage
19 class TestPackageExecutable(TestPackage):
20 """A helper class for running stand-alone executables."""
22 _TEST_RUNNER_RET_VAL_FILE = 'gtest_retval'
24 def __init__(self, suite_name):
27 suite_name: Name of the test suite (e.g. base_unittests).
29 TestPackage.__init__(self, suite_name)
30 self.suite_path = os.path.join(constants.GetOutDirectory(), suite_name)
31 self._symbols_dir = os.path.join(constants.GetOutDirectory(),
35 def GetGTestReturnCode(self, device):
37 ret_code = 1 # Assume failure if we can't find it
38 ret_code_file = tempfile.NamedTemporaryFile()
40 if not device.PullFile(
41 constants.TEST_EXECUTABLE_DIR + '/' +
42 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE,
44 logging.critical('Unable to pull gtest ret val file %s',
47 ret_code = file(ret_code_file.name).read()
50 logging.critical('Error reading gtest ret val file %s [%s]',
51 ret_code_file.name, ret_code)
56 def _AddNativeCoverageExports(device):
57 # export GCOV_PREFIX set the path for native coverage results
58 # export GCOV_PREFIX_STRIP indicates how many initial directory
59 # names to strip off the hardwired absolute paths.
60 # This value is calculated in buildbot.sh and
61 # depends on where the tree is built.
62 # Ex: /usr/local/google/code/chrome will become
63 # /code/chrome if GCOV_PREFIX_STRIP=3
65 depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP']
66 export_string = ('export GCOV_PREFIX="%s/gcov"\n' %
67 device.GetExternalStoragePath())
68 export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth
71 logging.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: '
72 'No native coverage.')
74 except device_errors.CommandFailedError:
75 logging.info('No external storage found: No native coverage.')
79 def ClearApplicationState(self, device):
81 # We don't expect the executable to be running, so we don't attempt
82 # to retry on failure.
83 device.KillAll(self.suite_name, blocking=True, timeout=30, retries=0)
84 except device_errors.CommandFailedError:
85 # KillAll raises an exception if it can't find a process with the given
86 # name. We only care that there is no process with the given name, so
87 # we can safely eat the exception.
91 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
92 tool_wrapper = self.tool.GetTestWrapper()
93 sh_script_file = tempfile.NamedTemporaryFile()
94 # We need to capture the exit status from the script since adb shell won't
99 '%s LD_LIBRARY_PATH=%s/%s_deps %s/%s --gtest_filter=%s %s\n'
101 (constants.TEST_EXECUTABLE_DIR,
102 self._AddNativeCoverageExports(device),
104 constants.TEST_EXECUTABLE_DIR,
106 constants.TEST_EXECUTABLE_DIR,
108 test_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 device.PushChangedFiles([(
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())
120 def GetAllTests(self, device):
121 cmd = '%s %s/%s --gtest_list_tests' % (self.tool.GetTestWrapper(),
122 constants.TEST_EXECUTABLE_DIR, self.suite_name)
123 lib_path = '%s/%s_deps' % (constants.TEST_EXECUTABLE_DIR, self.suite_name)
124 (exit_code, output) = device.old_interface.GetAndroidToolStatusAndOutput(
125 cmd, lib_path=lib_path)
128 'Failed to start binary:\n%s' % '\n'.join(output))
129 return self._ParseGTestListTests(output)
132 def SpawnTestProcess(self, device):
133 args = ['adb', '-s', str(device), 'shell', 'sh',
134 constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh']
136 return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
139 def Install(self, device):
140 if self.tool.NeedsDebugInfo():
141 target_name = self.suite_path
143 target_name = self.suite_path + '_stripped'
144 if not os.path.isfile(target_name):
145 raise Exception('Did not find %s, build target %s' %
146 (target_name, self.suite_name + '_stripped'))
148 target_mtime = os.stat(target_name).st_mtime
149 source_mtime = os.stat(self.suite_path).st_mtime
150 if target_mtime < source_mtime:
152 'stripped binary (%s, timestamp %d) older than '
153 'source binary (%s, timestamp %d), build target %s' %
154 (target_name, target_mtime, self.suite_path, source_mtime,
155 self.suite_name + '_stripped'))
157 test_binary_path = constants.TEST_EXECUTABLE_DIR + '/' + self.suite_name
158 device.PushChangedFiles([(target_name, test_binary_path)])
159 deps_path = self.suite_path + '_deps'
160 if os.path.isdir(deps_path):
161 device.PushChangedFiles([(deps_path, test_binary_path + '_deps')])