Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / build / android / pylib / gtest / test_package_exe.py
blobe607cad512aa90d06b7b60e698484f36fa634b0d
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."""
7 import logging
8 import os
9 import sys
10 import tempfile
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 import gtest_test_instance
17 from pylib.gtest.test_package import TestPackage
20 class TestPackageExecutable(TestPackage):
21 """A helper class for running stand-alone executables."""
23 _TEST_RUNNER_RET_VAL_FILE = 'gtest_retval'
25 def __init__(self, suite_name):
26 """
27 Args:
28 suite_name: Name of the test suite (e.g. base_unittests).
29 """
30 TestPackage.__init__(self, suite_name)
31 self.suite_path = os.path.join(constants.GetOutDirectory(), suite_name)
32 self._symbols_dir = os.path.join(constants.GetOutDirectory(),
33 'lib.target')
35 #override
36 def GetGTestReturnCode(self, device):
37 ret = None
38 ret_code = 1 # Assume failure if we can't find it
39 ret_code_file = tempfile.NamedTemporaryFile()
40 try:
41 if not device.PullFile(
42 constants.TEST_EXECUTABLE_DIR + '/' +
43 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE,
44 ret_code_file.name):
45 logging.critical('Unable to pull gtest ret val file %s',
46 ret_code_file.name)
47 raise ValueError
48 ret_code = file(ret_code_file.name).read()
49 ret = int(ret_code)
50 except ValueError:
51 logging.critical('Error reading gtest ret val file %s [%s]',
52 ret_code_file.name, ret_code)
53 ret = 1
54 return ret
56 @staticmethod
57 def _AddNativeCoverageExports(device):
58 # export GCOV_PREFIX set the path for native coverage results
59 # export GCOV_PREFIX_STRIP indicates how many initial directory
60 # names to strip off the hardwired absolute paths.
61 # This value is calculated in buildbot.sh and
62 # depends on where the tree is built.
63 # Ex: /usr/local/google/code/chrome will become
64 # /code/chrome if GCOV_PREFIX_STRIP=3
65 try:
66 depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP']
67 export_string = ('export GCOV_PREFIX="%s/gcov"\n' %
68 device.GetExternalStoragePath())
69 export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth
70 return export_string
71 except KeyError:
72 logging.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: '
73 'No native coverage.')
74 return ''
75 except device_errors.CommandFailedError:
76 logging.info('No external storage found: No native coverage.')
77 return ''
79 #override
80 def ClearApplicationState(self, device):
81 try:
82 # We don't expect the executable to be running, so we don't attempt
83 # to retry on failure.
84 device.KillAll(self.suite_name, blocking=True, timeout=30, retries=0)
85 except device_errors.CommandFailedError:
86 # KillAll raises an exception if it can't find a process with the given
87 # name. We only care that there is no process with the given name, so
88 # we can safely eat the exception.
89 pass
91 #override
92 def CreateCommandLineFileOnDevice(self, device, test_filter, test_arguments):
93 tool_wrapper = self.tool.GetTestWrapper()
94 sh_script_file = tempfile.NamedTemporaryFile()
95 # We need to capture the exit status from the script since adb shell won't
96 # propagate to us.
97 sh_script_file.write(
98 'cd %s\n'
99 '%s'
100 '%s LD_LIBRARY_PATH=%s/%s_deps %s/%s --gtest_filter=%s %s\n'
101 'echo $? > %s' %
102 (constants.TEST_EXECUTABLE_DIR,
103 self._AddNativeCoverageExports(device),
104 tool_wrapper,
105 constants.TEST_EXECUTABLE_DIR,
106 self.suite_name,
107 constants.TEST_EXECUTABLE_DIR,
108 self.suite_name,
109 test_filter, test_arguments,
110 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE))
111 sh_script_file.flush()
112 cmd_helper.RunCmd(['chmod', '+x', sh_script_file.name])
113 device.PushChangedFiles([(
114 sh_script_file.name,
115 constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh')])
116 logging.info('Conents of the test runner script: ')
117 for line in open(sh_script_file.name).readlines():
118 logging.info(' ' + line.rstrip())
120 #override
121 def GetAllTests(self, device):
122 cmd = '%s %s/%s --gtest_list_tests' % (self.tool.GetTestWrapper(),
123 constants.TEST_EXECUTABLE_DIR, self.suite_name)
124 lib_path = '%s/%s_deps' % (constants.TEST_EXECUTABLE_DIR, self.suite_name)
125 (exit_code, output) = device.old_interface.GetAndroidToolStatusAndOutput(
126 cmd, lib_path=lib_path)
127 if exit_code != 0:
128 raise Exception(
129 'Failed to start binary:\n%s' % '\n'.join(output))
130 return gtest_test_instance.ParseGTestListTests(output)
132 #override
133 def SpawnTestProcess(self, device):
134 args = ['adb', '-s', str(device), 'shell', 'sh',
135 constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh']
136 logging.info(args)
137 return pexpect.spawn(args[0], args[1:], logfile=sys.stdout)
139 #override
140 def Install(self, device):
141 if self.tool.NeedsDebugInfo():
142 target_name = self.suite_path
143 else:
144 target_name = self.suite_path + '_stripped'
145 if not os.path.isfile(target_name):
146 raise Exception('Did not find %s, build target %s' %
147 (target_name, self.suite_name + '_stripped'))
149 target_mtime = os.stat(target_name).st_mtime
150 source_mtime = os.stat(self.suite_path).st_mtime
151 if target_mtime < source_mtime:
152 raise Exception(
153 'stripped binary (%s, timestamp %d) older than '
154 'source binary (%s, timestamp %d), build target %s' %
155 (target_name, target_mtime, self.suite_path, source_mtime,
156 self.suite_name + '_stripped'))
158 test_binary_path = constants.TEST_EXECUTABLE_DIR + '/' + self.suite_name
159 device.PushChangedFiles([(target_name, test_binary_path)])
160 deps_path = self.suite_path + '_deps'
161 if os.path.isdir(deps_path):
162 device.PushChangedFiles([(deps_path, test_binary_path + '_deps')])