Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2017 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import argparse |
| 17 | import os |
| 18 | import re |
Hector Dearman | 0d30587 | 2017-10-18 16:18:07 +0100 | [diff] [blame] | 19 | import functools |
| 20 | import logging |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 21 | import subprocess |
| 22 | import sys |
Primiano Tucci | c6259a9 | 2017-11-10 13:51:55 +0000 | [diff] [blame] | 23 | import tempfile |
Hector Dearman | 273823c | 2017-10-13 18:12:27 +0100 | [diff] [blame] | 24 | import time |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | """ Runs a test executable on Android. |
| 28 | |
| 29 | Takes care of pushing the extra shared libraries that might be required by |
| 30 | some sanitizers. Propagates the test return code to the host, exiting with |
| 31 | 0 only if the test execution succeeds on the device. |
| 32 | """ |
| 33 | |
| 34 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 35 | ADB_PATH = os.path.join(ROOT_DIR, 'buildtools/android_sdk/platform-tools/adb') |
| 36 | |
| 37 | |
Hector Dearman | 0d30587 | 2017-10-18 16:18:07 +0100 | [diff] [blame] | 38 | def RetryOn(exc_type=(), returns_falsy=False, retries=5): |
| 39 | """Decorator to retry a function in case of errors or falsy values. |
| 40 | |
| 41 | Implements exponential backoff between retries. |
| 42 | |
| 43 | Args: |
| 44 | exc_type: Type of exceptions to catch and retry on. May also pass a tuple |
| 45 | of exceptions to catch and retry on any of them. Defaults to catching no |
| 46 | exceptions at all. |
| 47 | returns_falsy: If True then the function will be retried until it stops |
| 48 | returning a "falsy" value (e.g. None, False, 0, [], etc.). If equal to |
| 49 | 'raise' and the function keeps returning falsy values after all retries, |
| 50 | then the decorator will raise a ValueError. |
| 51 | retries: Max number of retry attempts. After exhausting that number of |
| 52 | attempts the function will be called with no safeguards: any exceptions |
| 53 | will be raised and falsy values returned to the caller (except when |
| 54 | returns_falsy='raise'). |
| 55 | """ |
| 56 | def Decorator(f): |
| 57 | @functools.wraps(f) |
| 58 | def Wrapper(*args, **kwargs): |
| 59 | wait = 1 |
| 60 | this_retries = kwargs.pop('retries', retries) |
| 61 | for _ in range(this_retries): |
| 62 | retry_reason = None |
| 63 | try: |
| 64 | value = f(*args, **kwargs) |
| 65 | except exc_type as exc: |
| 66 | retry_reason = 'raised %s' % type(exc).__name__ |
| 67 | if retry_reason is None: |
| 68 | if returns_falsy and not value: |
| 69 | retry_reason = 'returned %r' % value |
| 70 | else: |
| 71 | return value # Success! |
| 72 | print('{} {}, will retry in {} second{} ...'.format( |
| 73 | f.__name__, retry_reason, wait, '' if wait == 1 else 's')) |
| 74 | time.sleep(wait) |
| 75 | wait *= 2 |
| 76 | value = f(*args, **kwargs) # Last try to run with no safeguards. |
| 77 | if returns_falsy == 'raise' and not value: |
| 78 | raise ValueError('%s returned %r' % (f.__name__, value)) |
| 79 | return value |
| 80 | return Wrapper |
| 81 | return Decorator |
| 82 | |
| 83 | |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 84 | def AdbCall(*args): |
| 85 | cmd = [ADB_PATH] + list(args) |
| 86 | print '> adb ' + ' '.join(args) |
| 87 | return subprocess.check_call(cmd) |
| 88 | |
| 89 | |
Hector Dearman | 273823c | 2017-10-13 18:12:27 +0100 | [diff] [blame] | 90 | def GetProp(prop): |
| 91 | cmd = [ADB_PATH, 'shell', 'getprop', prop] |
| 92 | print '> adb ' + ' '.join(cmd) |
| 93 | output = subprocess.check_output(cmd) |
| 94 | lines = output.splitlines() |
Hector Dearman | 0d30587 | 2017-10-18 16:18:07 +0100 | [diff] [blame] | 95 | assert len(lines) == 1, 'Expected output to have one line: {}'.format(output) |
Hector Dearman | 273823c | 2017-10-13 18:12:27 +0100 | [diff] [blame] | 96 | print lines[0] |
| 97 | return lines[0] |
| 98 | |
| 99 | |
Hector Dearman | 0d30587 | 2017-10-18 16:18:07 +0100 | [diff] [blame] | 100 | @RetryOn([subprocess.CalledProcessError], returns_falsy=True, retries=10) |
| 101 | def WaitForBootCompletion(): |
Hector Dearman | 273823c | 2017-10-13 18:12:27 +0100 | [diff] [blame] | 102 | return GetProp('sys.boot_completed') == '1' |
| 103 | |
| 104 | |
Primiano Tucci | c6259a9 | 2017-11-10 13:51:55 +0000 | [diff] [blame] | 105 | def EnumerateDataDeps(): |
Primiano Tucci | 7a40e4d | 2017-12-06 09:51:09 +0000 | [diff] [blame] | 106 | with open(os.path.join(ROOT_DIR, 'tools', 'test_data.txt')) as f: |
Primiano Tucci | c6259a9 | 2017-11-10 13:51:55 +0000 | [diff] [blame] | 107 | lines = f.readlines() |
| 108 | for line in (line.strip() for line in lines if not line.startswith('#')): |
| 109 | assert os.path.exists(line), line |
| 110 | yield line |
| 111 | |
| 112 | |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 113 | def Main(): |
| 114 | parser = argparse.ArgumentParser() |
| 115 | parser.add_argument('--no-cleanup', '-n', action='store_true') |
Primiano Tucci | 480c68b | 2017-12-19 09:18:00 +0100 | [diff] [blame] | 116 | parser.add_argument('--no-data-deps', '-x', action='store_true') |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 117 | parser.add_argument('out_dir', help='out/android/') |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 118 | parser.add_argument('test_name', help='perfetto_unittests') |
Sami Kyostila | 4473d9f | 2017-11-24 17:45:24 +0000 | [diff] [blame] | 119 | parser.add_argument('cmd_args', nargs=argparse.REMAINDER) |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 120 | args = parser.parse_args() |
| 121 | |
| 122 | test_bin = os.path.join(args.out_dir, args.test_name) |
| 123 | assert os.path.exists(test_bin) |
| 124 | |
| 125 | print 'Waiting for device ...' |
| 126 | AdbCall('wait-for-device') |
Lalit Maganti | 367fcd5 | 2018-02-05 16:06:13 +0000 | [diff] [blame^] | 127 | # WaitForBootCompletion() |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 128 | AdbCall('root') |
| 129 | AdbCall('wait-for-device') |
Hector Dearman | 273823c | 2017-10-13 18:12:27 +0100 | [diff] [blame] | 130 | |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 131 | target_dir = '/data/local/tmp/' + args.test_name |
| 132 | AdbCall('shell', 'rm -rf "%s"; mkdir -p "%s"' % (2 * (target_dir,))) |
| 133 | AdbCall('push', test_bin, target_dir) |
| 134 | |
Primiano Tucci | 480c68b | 2017-12-19 09:18:00 +0100 | [diff] [blame] | 135 | if not args.no_data_deps: |
| 136 | for dep in EnumerateDataDeps(): |
| 137 | AdbCall('push', os.path.join(ROOT_DIR, dep), target_dir + '/' + dep) |
Primiano Tucci | c6259a9 | 2017-11-10 13:51:55 +0000 | [diff] [blame] | 138 | |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 139 | # LLVM sanitizers require to sideload a libclangrtXX.so on the device. |
| 140 | sanitizer_libs = os.path.join(args.out_dir, 'sanitizer_libs') |
| 141 | env = '' |
| 142 | if os.path.exists(sanitizer_libs): |
| 143 | AdbCall('push', sanitizer_libs, target_dir) |
Hector Dearman | 72f697c | 2017-11-13 11:06:34 +0000 | [diff] [blame] | 144 | env = 'LD_LIBRARY_PATH="%s/sanitizer_libs" ' % (target_dir) |
| 145 | cmd = 'cd %s;' % target_dir; |
Sami Kyostila | 4473d9f | 2017-11-24 17:45:24 +0000 | [diff] [blame] | 146 | cmd += env + './%s' % args.test_name |
| 147 | if args.cmd_args: |
| 148 | cmd += ' ' + ' '.join(args.cmd_args) |
| 149 | cmd += ';echo -e "\\nTEST_RET_CODE=$?"' |
Primiano Tucci | ac3fd3e | 2017-09-29 14:29:15 +0100 | [diff] [blame] | 150 | print cmd |
| 151 | test_output = subprocess.check_output([ADB_PATH, 'shell', cmd]) |
| 152 | print test_output |
| 153 | retcode = re.search(r'^TEST_RET_CODE=(\d)', test_output, re.MULTILINE) |
| 154 | assert retcode, 'Could not find TEST_RET_CODE=N marker' |
| 155 | retcode = int(retcode.group(1)) |
| 156 | if not args.no_cleanup: |
| 157 | AdbCall('shell', 'rm -rf "%s"' % target_dir) |
| 158 | return retcode |
| 159 | |
| 160 | |
| 161 | if __name__ == '__main__': |
| 162 | sys.exit(Main()) |