1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/build/android/pylib/test_package_executable.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,167 @@ 1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +# Use of this source code is governed by a BSD-style license that can be 1.6 +# found in the LICENSE file. 1.7 + 1.8 + 1.9 +import logging 1.10 +import os 1.11 +import shutil 1.12 +import sys 1.13 +import tempfile 1.14 + 1.15 +import cmd_helper 1.16 +import constants 1.17 +from test_package import TestPackage 1.18 +from pylib import pexpect 1.19 + 1.20 + 1.21 +class TestPackageExecutable(TestPackage): 1.22 + """A helper class for running stand-alone executables.""" 1.23 + 1.24 + _TEST_RUNNER_RET_VAL_FILE = 'gtest_retval' 1.25 + 1.26 + def __init__(self, adb, device, test_suite, timeout, rebaseline, 1.27 + performance_test, cleanup_test_files, tool, dump_debug_info, 1.28 + symbols_dir=None): 1.29 + """ 1.30 + Args: 1.31 + adb: ADB interface the tests are using. 1.32 + device: Device to run the tests. 1.33 + test_suite: A specific test suite to run, empty to run all. 1.34 + timeout: Timeout for each test. 1.35 + rebaseline: Whether or not to run tests in isolation and update the 1.36 + filter. 1.37 + performance_test: Whether or not performance test(s). 1.38 + cleanup_test_files: Whether or not to cleanup test files on device. 1.39 + tool: Name of the Valgrind tool. 1.40 + dump_debug_info: A debug_info object. 1.41 + symbols_dir: Directory to put the stripped binaries. 1.42 + """ 1.43 + TestPackage.__init__(self, adb, device, test_suite, timeout, 1.44 + rebaseline, performance_test, cleanup_test_files, 1.45 + tool, dump_debug_info) 1.46 + self.symbols_dir = symbols_dir 1.47 + 1.48 + def _GetGTestReturnCode(self): 1.49 + ret = None 1.50 + ret_code = 1 # Assume failure if we can't find it 1.51 + ret_code_file = tempfile.NamedTemporaryFile() 1.52 + try: 1.53 + if not self.adb.Adb().Pull( 1.54 + self.adb.GetExternalStorage() + '/' + 1.55 + TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE, 1.56 + ret_code_file.name): 1.57 + logging.critical('Unable to pull gtest ret val file %s', 1.58 + ret_code_file.name) 1.59 + raise ValueError 1.60 + ret_code = file(ret_code_file.name).read() 1.61 + ret = int(ret_code) 1.62 + except ValueError: 1.63 + logging.critical('Error reading gtest ret val file %s [%s]', 1.64 + ret_code_file.name, ret_code) 1.65 + ret = 1 1.66 + return ret 1.67 + 1.68 + def _AddNativeCoverageExports(self): 1.69 + # export GCOV_PREFIX set the path for native coverage results 1.70 + # export GCOV_PREFIX_STRIP indicates how many initial directory 1.71 + # names to strip off the hardwired absolute paths. 1.72 + # This value is calculated in buildbot.sh and 1.73 + # depends on where the tree is built. 1.74 + # Ex: /usr/local/google/code/chrome will become 1.75 + # /code/chrome if GCOV_PREFIX_STRIP=3 1.76 + try: 1.77 + depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] 1.78 + except KeyError: 1.79 + logging.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: ' 1.80 + 'No native coverage.') 1.81 + return '' 1.82 + export_string = ('export GCOV_PREFIX="%s/gcov"\n' % 1.83 + self.adb.GetExternalStorage()) 1.84 + export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth 1.85 + return export_string 1.86 + 1.87 + def GetAllTests(self): 1.88 + """Returns a list of all tests available in the test suite.""" 1.89 + all_tests = self.adb.RunShellCommand( 1.90 + '%s %s/%s --gtest_list_tests' % 1.91 + (self.tool.GetTestWrapper(), 1.92 + constants.TEST_EXECUTABLE_DIR, 1.93 + self.test_suite_basename)) 1.94 + return self._ParseGTestListTests(all_tests) 1.95 + 1.96 + def CreateTestRunnerScript(self, gtest_filter, test_arguments): 1.97 + """Creates a test runner script and pushes to the device. 1.98 + 1.99 + Args: 1.100 + gtest_filter: A gtest_filter flag. 1.101 + test_arguments: Additional arguments to pass to the test binary. 1.102 + """ 1.103 + tool_wrapper = self.tool.GetTestWrapper() 1.104 + sh_script_file = tempfile.NamedTemporaryFile() 1.105 + # We need to capture the exit status from the script since adb shell won't 1.106 + # propagate to us. 1.107 + sh_script_file.write('cd %s\n' 1.108 + '%s' 1.109 + '%s %s/%s --gtest_filter=%s %s\n' 1.110 + 'echo $? > %s' % 1.111 + (constants.TEST_EXECUTABLE_DIR, 1.112 + self._AddNativeCoverageExports(), 1.113 + tool_wrapper, constants.TEST_EXECUTABLE_DIR, 1.114 + self.test_suite_basename, 1.115 + gtest_filter, test_arguments, 1.116 + TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE)) 1.117 + sh_script_file.flush() 1.118 + cmd_helper.RunCmd(['chmod', '+x', sh_script_file.name]) 1.119 + self.adb.PushIfNeeded( 1.120 + sh_script_file.name, 1.121 + constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh') 1.122 + logging.info('Conents of the test runner script: ') 1.123 + for line in open(sh_script_file.name).readlines(): 1.124 + logging.info(' ' + line.rstrip()) 1.125 + 1.126 + def RunTestsAndListResults(self): 1.127 + """Runs all the tests and checks for failures. 1.128 + 1.129 + Returns: 1.130 + A TestResults object. 1.131 + """ 1.132 + args = ['adb', '-s', self.device, 'shell', 'sh', 1.133 + constants.TEST_EXECUTABLE_DIR + '/chrome_test_runner.sh'] 1.134 + logging.info(args) 1.135 + p = pexpect.spawn(args[0], args[1:], logfile=sys.stdout) 1.136 + return self._WatchTestOutput(p) 1.137 + 1.138 + def StripAndCopyExecutable(self): 1.139 + """Strips and copies the executable to the device.""" 1.140 + if self.tool.NeedsDebugInfo(): 1.141 + target_name = self.test_suite 1.142 + else: 1.143 + target_name = self.test_suite + '_' + self.device + '_stripped' 1.144 + should_strip = True 1.145 + if os.path.isfile(target_name): 1.146 + logging.info('Found target file %s' % target_name) 1.147 + target_mtime = os.stat(target_name).st_mtime 1.148 + source_mtime = os.stat(self.test_suite).st_mtime 1.149 + if target_mtime > source_mtime: 1.150 + logging.info('Target mtime (%d) is newer than source (%d), assuming ' 1.151 + 'no change.' % (target_mtime, source_mtime)) 1.152 + should_strip = False 1.153 + 1.154 + if should_strip: 1.155 + logging.info('Did not find up-to-date stripped binary. Generating a ' 1.156 + 'new one (%s).' % target_name) 1.157 + # Whenever we generate a stripped binary, copy to the symbols dir. If we 1.158 + # aren't stripping a new binary, assume it's there. 1.159 + if self.symbols_dir: 1.160 + if not os.path.exists(self.symbols_dir): 1.161 + os.makedirs(self.symbols_dir) 1.162 + shutil.copy(self.test_suite, self.symbols_dir) 1.163 + strip = os.environ['STRIP'] 1.164 + cmd_helper.RunCmd([strip, self.test_suite, '-o', target_name]) 1.165 + test_binary = constants.TEST_EXECUTABLE_DIR + '/' + self.test_suite_basename 1.166 + self.adb.PushIfNeeded(target_name, test_binary) 1.167 + 1.168 + def _GetTestSuiteBaseName(self): 1.169 + """Returns the base name of the test suite.""" 1.170 + return os.path.basename(self.test_suite)