blob: 6b3ddc3933f5b06ec596de0f37a3c1a62b11415c [file] [log] [blame]
Primiano Tucciac3fd3e2017-09-29 14:29:15 +01001#!/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
16import argparse
17import os
18import re
19import subprocess
20import sys
21
22
23""" Runs a test executable on Android.
24
25Takes care of pushing the extra shared libraries that might be required by
26some sanitizers. Propagates the test return code to the host, exiting with
270 only if the test execution succeeds on the device.
28"""
29
30ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31ADB_PATH = os.path.join(ROOT_DIR, 'buildtools/android_sdk/platform-tools/adb')
32
33
34def AdbCall(*args):
35 cmd = [ADB_PATH] + list(args)
36 print '> adb ' + ' '.join(args)
37 return subprocess.check_call(cmd)
38
39
40def Main():
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--no-cleanup', '-n', action='store_true')
43 parser.add_argument('out_dir', help='out/android/')
44 parser.add_argument('test_name', help='libtracing_unittests')
45 args = parser.parse_args()
46
47 test_bin = os.path.join(args.out_dir, args.test_name)
48 assert os.path.exists(test_bin)
49
50 print 'Waiting for device ...'
51 AdbCall('wait-for-device')
52 target_dir = '/data/local/tmp/' + args.test_name
53 AdbCall('shell', 'rm -rf "%s"; mkdir -p "%s"' % (2 * (target_dir,)))
54 AdbCall('push', test_bin, target_dir)
55
56 # LLVM sanitizers require to sideload a libclangrtXX.so on the device.
57 sanitizer_libs = os.path.join(args.out_dir, 'sanitizer_libs')
58 env = ''
59 if os.path.exists(sanitizer_libs):
60 AdbCall('push', sanitizer_libs, target_dir)
61 env = 'LD_LIBRARY_PATH="%s/sanitizer_libs" ' % (target_dir)
62 cmd = env + target_dir + '/' + args.test_name + '; echo TEST_RET_CODE=$?'
63 print cmd
64 test_output = subprocess.check_output([ADB_PATH, 'shell', cmd])
65 print test_output
66 retcode = re.search(r'^TEST_RET_CODE=(\d)', test_output, re.MULTILINE)
67 assert retcode, 'Could not find TEST_RET_CODE=N marker'
68 retcode = int(retcode.group(1))
69 if not args.no_cleanup:
70 AdbCall('shell', 'rm -rf "%s"' % target_dir)
71 return retcode
72
73
74if __name__ == '__main__':
75 sys.exit(Main())