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."""
13 from pylib
import cmd_helper
14 from pylib
import constants
15 from pylib
import pexpect
16 from pylib
.device
import device_errors
17 from pylib
.gtest
import gtest_test_instance
18 from pylib
.gtest
.test_package
import TestPackage
21 class TestPackageExecutable(TestPackage
):
22 """A helper class for running stand-alone executables."""
24 _TEST_RUNNER_RET_VAL_FILE
= 'gtest_retval'
26 def __init__(self
, suite_name
):
29 suite_name: Name of the test suite (e.g. base_unittests).
31 TestPackage
.__init
__(self
, suite_name
)
32 self
.suite_path
= os
.path
.join(constants
.GetOutDirectory(), suite_name
)
33 self
._symbols
_dir
= os
.path
.join(constants
.GetOutDirectory(),
37 def GetGTestReturnCode(self
, device
):
39 ret_code
= 1 # Assume failure if we can't find it
40 ret_code_file
= tempfile
.NamedTemporaryFile()
42 if not device
.PullFile(
43 constants
.TEST_EXECUTABLE_DIR
+ '/' +
44 TestPackageExecutable
._TEST
_RUNNER
_RET
_VAL
_FILE
,
46 logging
.critical('Unable to pull gtest ret val file %s',
49 ret_code
= file(ret_code_file
.name
).read()
52 logging
.critical('Error reading gtest ret val file %s [%s]',
53 ret_code_file
.name
, ret_code
)
58 def _AddNativeCoverageExports(device
):
59 # export GCOV_PREFIX set the path for native coverage results
60 # export GCOV_PREFIX_STRIP indicates how many initial directory
61 # names to strip off the hardwired absolute paths.
62 # This value is calculated in buildbot.sh and
63 # depends on where the tree is built.
64 # Ex: /usr/local/google/code/chrome will become
65 # /code/chrome if GCOV_PREFIX_STRIP=3
67 depth
= os
.environ
['NATIVE_COVERAGE_DEPTH_STRIP']
68 export_string
= ('export GCOV_PREFIX="%s/gcov"\n' %
69 device
.GetExternalStoragePath())
70 export_string
+= 'export GCOV_PREFIX_STRIP=%s\n' % depth
73 logging
.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: '
74 'No native coverage.')
76 except device_errors
.CommandFailedError
:
77 logging
.info('No external storage found: No native coverage.')
81 def ClearApplicationState(self
, device
):
82 device
.KillAll(self
.suite_name
, blocking
=True, timeout
=30, quiet
=True)
85 def CreateCommandLineFileOnDevice(self
, device
, test_filter
, test_arguments
):
86 tool_wrapper
= self
.tool
.GetTestWrapper()
87 sh_script_file
= tempfile
.NamedTemporaryFile()
88 # We need to capture the exit status from the script since adb shell won't
93 '%s LD_LIBRARY_PATH=%s/%s_deps %s/%s --gtest_filter=%s %s\n'
95 (constants
.TEST_EXECUTABLE_DIR
,
96 self
._AddNativeCoverageExports
(device
),
98 constants
.TEST_EXECUTABLE_DIR
,
100 constants
.TEST_EXECUTABLE_DIR
,
102 test_filter
, test_arguments
,
103 TestPackageExecutable
._TEST
_RUNNER
_RET
_VAL
_FILE
))
104 sh_script_file
.flush()
105 cmd_helper
.RunCmd(['chmod', '+x', sh_script_file
.name
])
106 device
.PushChangedFiles([(
108 constants
.TEST_EXECUTABLE_DIR
+ '/chrome_test_runner.sh')])
109 logging
.info('Conents of the test runner script: ')
110 for line
in open(sh_script_file
.name
).readlines():
111 logging
.info(' ' + line
.rstrip())
114 def GetAllTests(self
, device
):
115 lib_path
= posixpath
.join(
116 constants
.TEST_EXECUTABLE_DIR
, '%s_deps' % self
.suite_name
)
119 if self
.tool
.GetTestWrapper():
120 cmd
.append(self
.tool
.GetTestWrapper())
122 posixpath
.join(constants
.TEST_EXECUTABLE_DIR
, self
.suite_name
),
123 '--gtest_list_tests'])
125 output
= device
.RunShellCommand(
126 cmd
, check_return
=True, env
={'LD_LIBRARY_PATH': lib_path
})
127 return gtest_test_instance
.ParseGTestListTests(output
)
130 def SpawnTestProcess(self
, device
):
131 args
= ['adb', '-s', str(device
), 'shell', 'sh',
132 constants
.TEST_EXECUTABLE_DIR
+ '/chrome_test_runner.sh']
134 return pexpect
.spawn(args
[0], args
[1:], logfile
=sys
.stdout
)
137 def Install(self
, device
):
138 if self
.tool
.NeedsDebugInfo():
139 target_name
= self
.suite_path
141 target_name
= self
.suite_path
+ '_stripped'
142 if not os
.path
.isfile(target_name
):
143 raise Exception('Did not find %s, build target %s' %
144 (target_name
, self
.suite_name
+ '_stripped'))
146 target_mtime
= os
.stat(target_name
).st_mtime
147 source_mtime
= os
.stat(self
.suite_path
).st_mtime
148 if target_mtime
< source_mtime
:
150 'stripped binary (%s, timestamp %d) older than '
151 'source binary (%s, timestamp %d), build target %s' %
152 (target_name
, target_mtime
, self
.suite_path
, source_mtime
,
153 self
.suite_name
+ '_stripped'))
155 test_binary_path
= constants
.TEST_EXECUTABLE_DIR
+ '/' + self
.suite_name
156 device
.PushChangedFiles([(target_name
, test_binary_path
)])
157 deps_path
= self
.suite_path
+ '_deps'
158 if os
.path
.isdir(deps_path
):
159 device
.PushChangedFiles([(deps_path
, test_binary_path
+ '_deps')])