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
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
):
28 suite_name: Name of the test suite (e.g. base_unittests).
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(),
36 def GetGTestReturnCode(self
, device
):
38 ret_code
= 1 # Assume failure if we can't find it
39 ret_code_file
= tempfile
.NamedTemporaryFile()
41 if not device
.PullFile(
42 constants
.TEST_EXECUTABLE_DIR
+ '/' +
43 TestPackageExecutable
._TEST
_RUNNER
_RET
_VAL
_FILE
,
45 logging
.critical('Unable to pull gtest ret val file %s',
48 ret_code
= file(ret_code_file
.name
).read()
51 logging
.critical('Error reading gtest ret val file %s [%s]',
52 ret_code_file
.name
, ret_code
)
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
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
72 logging
.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: '
73 'No native coverage.')
75 except device_errors
.CommandFailedError
:
76 logging
.info('No external storage found: No native coverage.')
80 def ClearApplicationState(self
, device
):
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.
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
100 '%s LD_LIBRARY_PATH=%s/%s_deps %s/%s --gtest_filter=%s %s\n'
102 (constants
.TEST_EXECUTABLE_DIR
,
103 self
._AddNativeCoverageExports
(device
),
105 constants
.TEST_EXECUTABLE_DIR
,
107 constants
.TEST_EXECUTABLE_DIR
,
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([(
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())
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
)
129 'Failed to start binary:\n%s' % '\n'.join(output
))
130 return gtest_test_instance
.ParseGTestListTests(output
)
133 def SpawnTestProcess(self
, device
):
134 args
= ['adb', '-s', str(device
), 'shell', 'sh',
135 constants
.TEST_EXECUTABLE_DIR
+ '/chrome_test_runner.sh']
137 return pexpect
.spawn(args
[0], args
[1:], logfile
=sys
.stdout
)
140 def Install(self
, device
):
141 if self
.tool
.NeedsDebugInfo():
142 target_name
= self
.suite_path
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
:
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')])