OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 |
| 7 import logging |
| 8 import os |
| 9 import pexpect |
| 10 import shutil |
| 11 import sys |
| 12 import tempfile |
| 13 |
| 14 import cmd_helper |
| 15 from test_package import TestPackage |
| 16 |
| 17 |
| 18 class TestPackageExecutable(TestPackage): |
| 19 """A helper class for running stand-alone executables. |
| 20 |
| 21 Args: |
| 22 adb: ADB interface the tests are using. |
| 23 device: Device to run the tests. |
| 24 test_suite: A specific test suite to run, empty to run all. |
| 25 timeout: Timeout for each test. |
| 26 rebaseline: Whether or not to run tests in isolation and update the filter. |
| 27 performance_test: Whether or not performance test(s). |
| 28 cleanup_test_files: Whether or not to cleanup test files on device. |
| 29 tool: Name of the Valgrind tool. |
| 30 dump_debug_info: A debug_info object. |
| 31 symbols_dir: Directory to put the stripped binaries. |
| 32 """ |
| 33 |
| 34 _TEST_RUNNER_RET_VAL_FILE = '/data/local/tmp/gtest_retval' |
| 35 |
| 36 def __init__(self, adb, device, test_suite, timeout, rebaseline, |
| 37 performance_test, cleanup_test_files, tool, dump_debug_info, |
| 38 symbols_dir=None): |
| 39 TestPackage.__init__(self, adb, device, test_suite, timeout, |
| 40 rebaseline, performance_test, cleanup_test_files, |
| 41 tool, dump_debug_info) |
| 42 self.symbols_dir = symbols_dir |
| 43 |
| 44 def _GetGTestReturnCode(self): |
| 45 ret = None |
| 46 ret_code_file = tempfile.NamedTemporaryFile() |
| 47 try: |
| 48 if not self.adb.Adb().Pull( |
| 49 TestPackageExecutable._TEST_RUNNER_RET_VAL_FILE, ret_code_file.name): |
| 50 logging.critical('Unable to pull gtest ret val file %s', |
| 51 ret_code_file.name) |
| 52 raise ValueError |
| 53 ret_code = file(ret_code_file.name).read() |
| 54 ret = int(ret_code) |
| 55 except ValueError: |
| 56 logging.critical('Error reading gtest ret val file %s [%s]', |
| 57 ret_code_file.name, ret_code) |
| 58 ret = 1 |
| 59 return ret |
| 60 |
| 61 def _AddNativeCoverageExports(self): |
| 62 # export GCOV_PREFIX set the path for native coverage results |
| 63 # export GCOV_PREFIX_STRIP indicates how many initial directory |
| 64 # names to strip off the hardwired absolute paths. |
| 65 # This value is calculated in buildbot.sh and |
| 66 # depends on where the tree is built. |
| 67 # Ex: /usr/local/google/code/chrome will become |
| 68 # /code/chrome if GCOV_PREFIX_STRIP=3 |
| 69 try: |
| 70 depth = os.environ['NATIVE_COVERAGE_DEPTH_STRIP'] |
| 71 except KeyError: |
| 72 logging.info('NATIVE_COVERAGE_DEPTH_STRIP is not defined: ' |
| 73 'No native coverage.') |
| 74 return '' |
| 75 export_string = 'export GCOV_PREFIX="/data/local/gcov"\n' |
| 76 export_string += 'export GCOV_PREFIX_STRIP=%s\n' % depth |
| 77 return export_string |
| 78 |
| 79 def GetAllTests(self): |
| 80 """Returns a list of all tests available in the test suite.""" |
| 81 all_tests = self.adb.RunShellCommand( |
| 82 '/data/local/%s --gtest_list_tests' % self.test_suite_basename) |
| 83 return self._ParseGTestListTests(all_tests) |
| 84 |
| 85 def CreateTestRunnerScript(self, gtest_filter, test_arguments): |
| 86 """Creates a test runner script and pushes to the device. |
| 87 |
| 88 Args: |
| 89 gtest_filter: A gtest_filter flag. |
| 90 test_arguments: Additional arguments to pass to the test binary. |
| 91 """ |
| 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 |
| 95 # propagate to us. |
| 96 sh_script_file.write('cd /data/local\n' |
| 97 '%s' |
| 98 '%s /data/local/%s --gtest_filter=%s %s\n' |
| 99 'echo $? > %s' % |
| 100 (self._AddNativeCoverageExports(), |
| 101 tool_wrapper, self.test_suite_basename, |
| 102 gtest_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 self.adb.PushIfNeeded(sh_script_file.name, |
| 107 '/data/local/chrome_test_runner.sh') |
| 108 |
| 109 def RunTestsAndListResults(self): |
| 110 """Runs all the tests and checks for failures. |
| 111 |
| 112 Returns: |
| 113 A TestResults object. |
| 114 """ |
| 115 args = ['adb', '-s', self.device, 'shell', 'sh', |
| 116 '/data/local/chrome_test_runner.sh'] |
| 117 logging.info(args) |
| 118 p = pexpect.spawn(args[0], args[1:], logfile=sys.stdout) |
| 119 return self._WatchTestOutput(p) |
| 120 |
| 121 def StripAndCopyExecutable(self): |
| 122 """Strips and copies the executable to the device.""" |
| 123 if self.tool.NeedsDebugInfo(): |
| 124 target_name = self.test_suite |
| 125 elif self.test_suite_basename == 'webkit_unit_tests': |
| 126 # webkit_unit_tests has been stripped in build step. |
| 127 target_name = self.test_suite |
| 128 else: |
| 129 target_name = self.test_suite + '_' + self.device + '_stripped' |
| 130 should_strip = True |
| 131 if os.path.isfile(target_name): |
| 132 logging.info('Found target file %s' % target_name) |
| 133 target_mtime = os.stat(target_name).st_mtime |
| 134 source_mtime = os.stat(self.test_suite).st_mtime |
| 135 if target_mtime > source_mtime: |
| 136 logging.info('Target mtime (%d) is newer than source (%d), assuming ' |
| 137 'no change.' % (target_mtime, source_mtime)) |
| 138 should_strip = False |
| 139 |
| 140 if should_strip: |
| 141 logging.info('Did not find up-to-date stripped binary. Generating a ' |
| 142 'new one (%s).' % target_name) |
| 143 # Whenever we generate a stripped binary, copy to the symbols dir. If we |
| 144 # aren't stripping a new binary, assume it's there. |
| 145 if self.symbols_dir: |
| 146 if not os.path.exists(self.symbols_dir): |
| 147 os.makedirs(self.symbols_dir) |
| 148 shutil.copy(self.test_suite, self.symbols_dir) |
| 149 strip = os.environ['STRIP'] |
| 150 cmd_helper.RunCmd([strip, self.test_suite, '-o', target_name]) |
| 151 test_binary = '/data/local/' + self.test_suite_basename |
| 152 self.adb.PushIfNeeded(target_name, test_binary) |
OLD | NEW |