Hash :
d988db39
Author :
Date :
2022-04-22T11:14:44
Fall back to adb on PATH if platform-tools not present.
Log adb --version. Example:
INFO:root:adb --version: Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
Installed as /{snip}/third_party/android_sdk/public/platform-tools/adb
When not found -> "Installed as /usr/bin/adb"
Bug: angleproject:6854
Change-Id: I3c19c4eda2211480f2c93b7d14f72f5641b3bfc2
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3602821
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Roman Lavrov <romanl@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
# Copyright 2022 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import contextlib
import functools
import glob
import json
import logging
import os
import pathlib
import posixpath
import random
import subprocess
import tarfile
import tempfile
import time
import angle_path_util
def _ApkPath(suite_name):
return os.path.join('%s_apk' % suite_name, '%s-debug.apk' % suite_name)
def ApkFileExists(suite_name):
return os.path.exists(_ApkPath(suite_name))
def _Run(cmd):
logging.debug('Executing command: %s', cmd)
startupinfo = None
if hasattr(subprocess, 'STARTUPINFO'):
# Prevent console window popping up on Windows
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
output = subprocess.check_output(cmd, startupinfo=startupinfo)
return output
@functools.lru_cache()
def _FindAdb():
platform_tools = (
pathlib.Path(angle_path_util.ANGLE_ROOT_DIR) / 'third_party' / 'android_sdk' / 'public' /
'platform-tools')
if platform_tools.exists():
adb = str(platform_tools / 'adb')
else:
adb = 'adb'
adb_info = subprocess.check_output([adb, '--version']).decode()
logging.info('adb --version: %s', adb_info)
return adb
class Adb(object):
def __init__(self):
self._adb_path = _FindAdb()
def Run(self, args):
return _Run([self._adb_path] + args)
def Shell(self, cmd):
return _Run([self._adb_path, 'shell', cmd])
def _GetAdbRoot(adb):
adb.Run(['root'])
for _ in range(20):
time.sleep(0.5)
try:
id_out = adb.Shell(adb_path, 'id').decode('ascii')
if 'uid=0(root)' in id_out:
return
except Exception:
continue
raise Exception("adb root failed")
def _ReadDeviceFile(adb, device_path):
out_wc = adb.Shell('cat %s | wc -c' % device_path)
expected_size = int(out_wc.decode('ascii').strip())
out = adb.Run(['exec-out', 'cat %s' % device_path])
assert len(out) == expected_size, 'exec-out mismatch'
return out
def _RemoveDeviceFile(adb, device_path):
adb.Shell('rm -f ' + device_path + ' || true') # ignore errors
def _AddRestrictedTracesJson(adb):
adb.Shell('mkdir -p /sdcard/chromium_tests_root/')
def add(tar, fn):
assert (fn.startswith('../../'))
tar.add(fn, arcname=fn.replace('../../', ''))
with _TempLocalFile() as tempfile_path:
with tarfile.open(tempfile_path, 'w', format=tarfile.GNU_FORMAT) as tar:
for f in glob.glob('../../src/tests/restricted_traces/*/*.json', recursive=True):
add(tar, f)
add(tar, '../../src/tests/restricted_traces/restricted_traces.json')
adb.Run(['push', tempfile_path, '/sdcard/chromium_tests_root/t.tar'])
adb.Shell('r=/sdcard/chromium_tests_root; tar -xf $r/t.tar -C $r/ && rm $r/t.tar')
def PrepareTestSuite(adb, suite_name):
apk_path = _ApkPath(suite_name)
logging.info('Installing apk path=%s size=%s' % (apk_path, os.path.getsize(apk_path)))
adb.Run(['install', '-r', '-d', apk_path])
permissions = [
'android.permission.CAMERA', 'android.permission.CHANGE_CONFIGURATION',
'android.permission.READ_EXTERNAL_STORAGE', 'android.permission.RECORD_AUDIO',
'android.permission.WRITE_EXTERNAL_STORAGE'
]
adb.Shell('p=com.android.angle.test;'
'for q in %s;do pm grant "$p" "$q";done;' % ' '.join(permissions))
if suite_name == 'angle_perftests':
_AddRestrictedTracesJson(adb)
def PrepareRestrictedTraces(adb, traces):
start = time.time()
total_size = 0
for trace in traces:
path_from_root = 'src/tests/restricted_traces/' + trace + '/' + trace + '.angledata.gz'
local_path = '../../' + path_from_root
total_size += os.path.getsize(local_path)
adb.Run(['push', local_path, '/sdcard/chromium_tests_root/' + path_from_root])
logging.info('Pushed %d trace files (%.1fMB) in %.1fs', len(traces), total_size / 1e6,
time.time() - start)
def _RandomHex():
return hex(random.randint(0, 2**64))[2:]
@contextlib.contextmanager
def _TempDeviceDir(adb):
path = '/sdcard/Download/temp_dir-%s' % _RandomHex()
adb.Shell('mkdir -p ' + path)
try:
yield path
finally:
adb.Shell('rm -rf ' + path)
@contextlib.contextmanager
def _TempDeviceFile(adb):
path = '/sdcard/Download/temp_file-%s' % _RandomHex()
try:
yield path
finally:
adb.Shell('rm -f ' + path)
@contextlib.contextmanager
def _TempLocalFile():
fd, path = tempfile.mkstemp()
os.close(fd)
try:
yield path
finally:
os.remove(path)
def _RunInstrumentation(adb, flags):
with _TempDeviceFile(adb) as temp_device_file:
cmd = ' '.join([
'p=com.android.angle.test;',
'ntr=org.chromium.native_test.NativeTestInstrumentationTestRunner;',
'am instrument -w',
'-e $ntr.NativeTestActivity "$p".AngleUnitTestActivity',
'-e $ntr.ShardNanoTimeout 2400000000000',
'-e org.chromium.native_test.NativeTest.CommandLineFlags "%s"' % ' '.join(flags),
'-e $ntr.StdoutFile ' + temp_device_file,
'"$p"/org.chromium.build.gtest_apk.NativeTestInstrumentationTestRunner',
])
adb.Shell(cmd)
return _ReadDeviceFile(adb, temp_device_file)
def AngleSystemInfo(adb, args):
PrepareTestSuite(adb, 'angle_system_info_test')
with _TempDeviceDir(adb) as temp_dir:
_RunInstrumentation(adb, args + ['--render-test-output-dir=' + temp_dir])
output_file = posixpath.join(temp_dir, 'angle_system_info.json')
return json.loads(_ReadDeviceFile(adb, output_file))
def ListTests(adb):
out_lines = _RunInstrumentation(adb, ["--list-tests"]).decode('ascii').split('\n')
start = out_lines.index('Tests list:')
end = out_lines.index('End tests list.')
return out_lines[start + 1:end]
def _PullDir(adb, device_dir, local_dir):
files = adb.Shell('ls -1 %s' % device_dir).decode('ascii').split('\n')
for f in files:
f = f.strip()
if f:
adb.Run(['pull', posixpath.join(device_dir, f), posixpath.join(local_dir, f)])
def RunTests(adb, test_suite, args, stdoutfile, output_dir):
with _TempDeviceDir(adb) as temp_dir:
output = _RunInstrumentation(adb, args + ['--render-test-output-dir=' + temp_dir])
with open(stdoutfile, 'wb') as f:
f.write(output)
logging.info(output.decode())
_PullDir(adb, temp_dir, output_dir)