blob: 56ff3c6ee6479f58d81f86df6b31bcc1a3559387 [file] [log] [blame]
Ang Li93420002016-05-10 19:11:44 -07001#!/usr/bin/env python3.4
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from builtins import str
18from builtins import open
19
Ang Li7f0e1c72016-06-14 11:23:49 -070020import logging
Ang Li93420002016-05-10 19:11:44 -070021import os
22import time
23import traceback
Sahil Jain06dd6a22016-06-24 13:47:37 -070024import threading
25import socket
Ang Li93420002016-05-10 19:11:44 -070026
Keun Soo Yime34754e2016-08-15 11:25:15 -070027from vts.runners.host import keys
Ang Li93420002016-05-10 19:11:44 -070028from vts.runners.host import logger as vts_logger
29from vts.runners.host import signals
30from vts.runners.host import utils
Ang Li7f0e1c72016-06-14 11:23:49 -070031from vts.utils.python.controllers import adb
32from vts.utils.python.controllers import event_dispatcher
33from vts.utils.python.controllers import fastboot
Ang Li6c303fc2016-09-22 13:19:33 -070034from vts.utils.python.controllers import sl4a_client
Sahil Jain06dd6a22016-06-24 13:47:37 -070035from vts.runners.host.tcp_client import vts_tcp_client
Ang Li7f0e1c72016-06-14 11:23:49 -070036from vts.utils.python.mirror import hal_mirror
Keun Soo Yim63d67512016-07-01 17:13:47 -070037from vts.utils.python.mirror import shell_mirror
Keun Soo Yim39bc0b92016-07-06 18:27:15 -070038from vts.utils.python.mirror import lib_mirror
Sahil Jain06dd6a22016-06-24 13:47:37 -070039from vts.runners.host import errors
40import subprocess
Ang Li93420002016-05-10 19:11:44 -070041
42VTS_CONTROLLER_CONFIG_NAME = "AndroidDevice"
43VTS_CONTROLLER_REFERENCE_NAME = "android_devices"
44
45ANDROID_DEVICE_PICK_ALL_TOKEN = "*"
46# Key name for adb logcat extra params in config file.
47ANDROID_DEVICE_ADB_LOGCAT_PARAM_KEY = "adb_logcat_param"
48ANDROID_DEVICE_EMPTY_CONFIG_MSG = "Configuration is empty, abort!"
49ANDROID_DEVICE_NOT_LIST_CONFIG_MSG = "Configuration should be a list, abort!"
50
Keun Soo Yime34754e2016-08-15 11:25:15 -070051ANDROID_PRODUCT_TYPE_UNKNOWN = "unknown"
52
Sahil Jain06dd6a22016-06-24 13:47:37 -070053# Target-side directory where the VTS binaries are uploaded
54DEFAULT_AGENT_BASE_DIR = "/data/local/tmp"
55# Time for which the current is put on sleep when the client is unable to
56# make a connection.
57THREAD_SLEEP_TIME = 1
58# Max number of attempts that the client can make to connect to the agent
59MAX_AGENT_CONNECT_RETRIES = 10
60
Ang Li93420002016-05-10 19:11:44 -070061class AndroidDeviceError(signals.ControllerError):
62 pass
63
Ang Lie2139f12016-05-12 17:39:06 -070064
Ang Li6c303fc2016-09-22 13:19:33 -070065def create(configs):
Ang Li53bb72b2016-07-19 18:29:37 -070066 """Creates AndroidDevice controller objects.
67
68 Args:
69 configs: A list of dicts, each representing a configuration for an
70 Android device.
71
72 Returns:
73 A list of AndroidDevice objects.
74 """
Ang Li93420002016-05-10 19:11:44 -070075 if not configs:
76 raise AndroidDeviceError(ANDROID_DEVICE_EMPTY_CONFIG_MSG)
77 elif configs == ANDROID_DEVICE_PICK_ALL_TOKEN:
Ang Li7f0e1c72016-06-14 11:23:49 -070078 ads = get_all_instances()
Ang Li93420002016-05-10 19:11:44 -070079 elif not isinstance(configs, list):
80 raise AndroidDeviceError(ANDROID_DEVICE_NOT_LIST_CONFIG_MSG)
81 elif isinstance(configs[0], str):
82 # Configs is a list of serials.
Ang Li7f0e1c72016-06-14 11:23:49 -070083 ads = get_instances(configs)
Ang Li93420002016-05-10 19:11:44 -070084 else:
85 # Configs is a list of dicts.
Ang Li7f0e1c72016-06-14 11:23:49 -070086 ads = get_instances_with_configs(configs)
Ang Li93420002016-05-10 19:11:44 -070087 connected_ads = list_adb_devices()
88 for ad in ads:
89 if ad.serial not in connected_ads:
Ang Li53bb72b2016-07-19 18:29:37 -070090 raise DoesNotExistError(("Android device %s is specified in config"
91 " but is not attached.") % ad.serial)
Ang Li6c303fc2016-09-22 13:19:33 -070092 _startServicesOnAds(ads)
Ang Li93420002016-05-10 19:11:44 -070093 return ads
94
Ang Lie2139f12016-05-12 17:39:06 -070095
Ang Li93420002016-05-10 19:11:44 -070096def destroy(ads):
Ang Li53bb72b2016-07-19 18:29:37 -070097 """Cleans up AndroidDevice objects.
98
99 Args:
100 ads: A list of AndroidDevice objects.
101 """
Ang Li93420002016-05-10 19:11:44 -0700102 for ad in ads:
Ang Li53bb72b2016-07-19 18:29:37 -0700103 try:
104 ad.cleanUp()
105 except:
106 ad.log.exception("Failed to clean up properly.")
107
108
Ang Li6c303fc2016-09-22 13:19:33 -0700109def _startServicesOnAds(ads):
Ang Li53bb72b2016-07-19 18:29:37 -0700110 """Starts long running services on multiple AndroidDevice objects.
111
112 If any one AndroidDevice object fails to start services, cleans up all
113 existing AndroidDevice objects and their services.
114
115 Args:
116 ads: A list of AndroidDevice objects whose services to start.
117 """
118 running_ads = []
119 for ad in ads:
120 running_ads.append(ad)
121 try:
Ang Li6c303fc2016-09-22 13:19:33 -0700122 ad.startServices()
Ang Li53bb72b2016-07-19 18:29:37 -0700123 except:
124 ad.log.exception("Failed to start some services, abort!")
125 destroy(running_ads)
126 raise
Ang Li93420002016-05-10 19:11:44 -0700127
Ang Lie2139f12016-05-12 17:39:06 -0700128
Ang Li93420002016-05-10 19:11:44 -0700129def _parse_device_list(device_list_str, key):
130 """Parses a byte string representing a list of devices. The string is
131 generated by calling either adb or fastboot.
132
133 Args:
134 device_list_str: Output of adb or fastboot.
135 key: The token that signifies a device in device_list_str.
136
137 Returns:
138 A list of android device serial numbers.
139 """
140 clean_lines = str(device_list_str, 'utf-8').strip().split('\n')
141 results = []
142 for line in clean_lines:
143 tokens = line.strip().split('\t')
144 if len(tokens) == 2 and tokens[1] == key:
145 results.append(tokens[0])
146 return results
147
Ang Lie2139f12016-05-12 17:39:06 -0700148
Ang Li93420002016-05-10 19:11:44 -0700149def list_adb_devices():
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700150 """List all target devices connected to the host and detected by adb.
Ang Li93420002016-05-10 19:11:44 -0700151
152 Returns:
153 A list of android device serials. Empty if there's none.
154 """
155 out = adb.AdbProxy().devices()
156 return _parse_device_list(out, "device")
157
Ang Lie2139f12016-05-12 17:39:06 -0700158
Ang Li93420002016-05-10 19:11:44 -0700159def list_fastboot_devices():
160 """List all android devices connected to the computer that are in in
161 fastboot mode. These are detected by fastboot.
162
163 Returns:
164 A list of android device serials. Empty if there's none.
165 """
166 out = fastboot.FastbootProxy().devices()
167 return _parse_device_list(out, "fastboot")
168
Ang Lie2139f12016-05-12 17:39:06 -0700169
Ang Li7f0e1c72016-06-14 11:23:49 -0700170def get_instances(serials):
Ang Li93420002016-05-10 19:11:44 -0700171 """Create AndroidDevice instances from a list of serials.
172
173 Args:
174 serials: A list of android device serials.
Ang Li93420002016-05-10 19:11:44 -0700175
176 Returns:
177 A list of AndroidDevice objects.
178 """
179 results = []
180 for s in serials:
Ang Li7f0e1c72016-06-14 11:23:49 -0700181 results.append(AndroidDevice(s))
Ang Li93420002016-05-10 19:11:44 -0700182 return results
183
Ang Lie2139f12016-05-12 17:39:06 -0700184
Ang Li7f0e1c72016-06-14 11:23:49 -0700185def get_instances_with_configs(configs):
Ang Li93420002016-05-10 19:11:44 -0700186 """Create AndroidDevice instances from a list of json configs.
187
188 Each config should have the required key-value pair "serial".
189
190 Args:
191 configs: A list of dicts each representing the configuration of one
192 android device.
Ang Li93420002016-05-10 19:11:44 -0700193
194 Returns:
195 A list of AndroidDevice objects.
196 """
197 results = []
198 for c in configs:
199 try:
Keun Soo Yime34754e2016-08-15 11:25:15 -0700200 serial = c.pop(keys.ConfigKeys.IKEY_SERIAL)
Ang Li93420002016-05-10 19:11:44 -0700201 except KeyError:
Keun Soo Yime34754e2016-08-15 11:25:15 -0700202 raise AndroidDeviceError(
203 ('Required value %s is missing in '
204 'AndroidDevice config %s.') % (keys.ConfigKeys.IKEY_SERIAL,
205 c))
206 try:
207 product_type = c.pop(keys.ConfigKeys.IKEY_PRODUCT_TYPE)
208 except KeyError:
209 logging.error(
210 'Required value %s is missing in '
211 'AndroidDevice config %s.',
212 keys.ConfigKeys.IKEY_PRODUCT_TYPE, c)
213 product_type = ANDROID_PRODUCT_TYPE_UNKNOWN
214
215 ad = AndroidDevice(serial, product_type)
Ang Li93420002016-05-10 19:11:44 -0700216 ad.loadConfig(c)
217 results.append(ad)
218 return results
219
Ang Lie2139f12016-05-12 17:39:06 -0700220
Ang Li7f0e1c72016-06-14 11:23:49 -0700221def get_all_instances(include_fastboot=False):
Ang Li93420002016-05-10 19:11:44 -0700222 """Create AndroidDevice instances for all attached android devices.
223
224 Args:
225 include_fastboot: Whether to include devices in bootloader mode or not.
Ang Li93420002016-05-10 19:11:44 -0700226
227 Returns:
228 A list of AndroidDevice objects each representing an android device
229 attached to the computer.
230 """
231 if include_fastboot:
232 serial_list = list_adb_devices() + list_fastboot_devices()
Ang Li7f0e1c72016-06-14 11:23:49 -0700233 return get_instances(serial_list)
234 return get_instances(list_adb_devices())
Ang Li93420002016-05-10 19:11:44 -0700235
Ang Lie2139f12016-05-12 17:39:06 -0700236
Ang Li93420002016-05-10 19:11:44 -0700237def filter_devices(ads, func):
238 """Finds the AndroidDevice instances from a list that match certain
239 conditions.
240
241 Args:
242 ads: A list of AndroidDevice instances.
243 func: A function that takes an AndroidDevice object and returns True
244 if the device satisfies the filter condition.
245
246 Returns:
247 A list of AndroidDevice instances that satisfy the filter condition.
248 """
249 results = []
250 for ad in ads:
251 if func(ad):
252 results.append(ad)
253 return results
254
Ang Lie2139f12016-05-12 17:39:06 -0700255
Ang Li93420002016-05-10 19:11:44 -0700256def get_device(ads, **kwargs):
257 """Finds a unique AndroidDevice instance from a list that has specific
258 attributes of certain values.
259
260 Example:
261 get_device(android_devices, label="foo", phone_number="1234567890")
262 get_device(android_devices, model="angler")
263
264 Args:
265 ads: A list of AndroidDevice instances.
266 kwargs: keyword arguments used to filter AndroidDevice instances.
267
268 Returns:
269 The target AndroidDevice instance.
270
271 Raises:
272 AndroidDeviceError is raised if none or more than one device is
273 matched.
274 """
Ang Lie2139f12016-05-12 17:39:06 -0700275
Ang Li93420002016-05-10 19:11:44 -0700276 def _get_device_filter(ad):
277 for k, v in kwargs.items():
278 if not hasattr(ad, k):
279 return False
280 elif getattr(ad, k) != v:
281 return False
282 return True
Ang Lie2139f12016-05-12 17:39:06 -0700283
Ang Li93420002016-05-10 19:11:44 -0700284 filtered = filter_devices(ads, _get_device_filter)
285 if not filtered:
286 raise AndroidDeviceError(("Could not find a target device that matches"
287 " condition: %s.") % kwargs)
288 elif len(filtered) == 1:
289 return filtered[0]
290 else:
291 serials = [ad.serial for ad in filtered]
292 raise AndroidDeviceError("More than one device matched: %s" % serials)
293
Ang Lie2139f12016-05-12 17:39:06 -0700294
Ang Li93420002016-05-10 19:11:44 -0700295def takeBugReports(ads, test_name, begin_time):
296 """Takes bug reports on a list of android devices.
297
298 If you want to take a bug report, call this function with a list of
299 android_device objects in on_fail. But reports will be taken on all the
300 devices in the list concurrently. Bug report takes a relative long
301 time to take, so use this cautiously.
302
303 Args:
304 ads: A list of AndroidDevice instances.
305 test_name: Name of the test case that triggered this bug report.
306 begin_time: Logline format timestamp taken when the test started.
307 """
308 begin_time = vts_logger.normalizeLogLineTimestamp(begin_time)
Ang Lie2139f12016-05-12 17:39:06 -0700309
Ang Li93420002016-05-10 19:11:44 -0700310 def take_br(test_name, begin_time, ad):
311 ad.takeBugReport(test_name, begin_time)
Ang Lie2139f12016-05-12 17:39:06 -0700312
Ang Li93420002016-05-10 19:11:44 -0700313 args = [(test_name, begin_time, ad) for ad in ads]
314 utils.concurrent_exec(take_br, args)
315
Ang Lie2139f12016-05-12 17:39:06 -0700316
Ang Li7f0e1c72016-06-14 11:23:49 -0700317class AndroidDevice(object):
Ang Li93420002016-05-10 19:11:44 -0700318 """Class representing an android device.
319
Ang Li53bb72b2016-07-19 18:29:37 -0700320 Each object of this class represents one Android device. The object holds
321 handles to adb, fastboot, and various RPC clients.
Ang Li93420002016-05-10 19:11:44 -0700322
323 Attributes:
Keun Soo Yime34754e2016-08-15 11:25:15 -0700324 serial: A string that's the serial number of the Android device.
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700325 device_command_port: int, the port number used on the Android device
326 for adb port forwarding (for command-response sessions).
327 device_callback_port: int, the port number used on the Android device
328 for adb port reverse forwarding (for callback sessions).
Ang Lie014a8b2016-06-28 18:24:52 -0700329 log: A logger project with a device-specific prefix for each line -
330 [AndroidDevice|<serial>]
Ang Li93420002016-05-10 19:11:44 -0700331 log_path: A string that is the path where all logs collected on this
332 android device should be stored.
333 adb_logcat_process: A process that collects the adb logcat.
334 adb_logcat_file_path: A string that's the full path to the adb logcat
335 file collected, if any.
Ang Lie014a8b2016-06-28 18:24:52 -0700336 vts_agent_process: A process that runs the HAL agent.
Ang Li93420002016-05-10 19:11:44 -0700337 adb: An AdbProxy object used for interacting with the device via adb.
338 fastboot: A FastbootProxy object used for interacting with the device
339 via fastboot.
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700340 host_command_port: the host-side port for runner to agent sessions
341 (to send commands and receive responses).
342 host_callback_port: the host-side port for agent to runner sessions
343 (to get callbacks from agent).
Ang Li53bb72b2016-07-19 18:29:37 -0700344 hal: HalMirror, in charge of all communications with the HAL layer.
345 lib: LibMirror, in charge of all communications with static and shared
346 native libs.
347 shell: ShellMirror, in charge of all communications with shell.
Keun Soo Yime34754e2016-08-15 11:25:15 -0700348 _product_type: A string, the device product type (e.g., bullhead) if
349 known, ANDROID_PRODUCT_TYPE_UNKNOWN otherwise.
Ang Li93420002016-05-10 19:11:44 -0700350 """
351
Keun Soo Yime34754e2016-08-15 11:25:15 -0700352 def __init__(self, serial="", product_type=ANDROID_PRODUCT_TYPE_UNKNOWN,
353 device_callback_port=5010):
Ang Li93420002016-05-10 19:11:44 -0700354 self.serial = serial
Keun Soo Yime34754e2016-08-15 11:25:15 -0700355 self._product_type = product_type
Keun Soo Yimf5d0bca2016-07-30 23:59:26 -0700356 self.device_command_port = None
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700357 self.device_callback_port = device_callback_port
Ang Lie014a8b2016-06-28 18:24:52 -0700358 self.log = AndroidDeviceLoggerAdapter(logging.getLogger(),
359 {"serial": self.serial})
360 base_log_path = getattr(logging, "log_path", "/tmp/logs/")
Ang Li7f0e1c72016-06-14 11:23:49 -0700361 self.log_path = os.path.join(base_log_path, "AndroidDevice%s" % serial)
Ang Li93420002016-05-10 19:11:44 -0700362 self.adb_logcat_process = None
363 self.adb_logcat_file_path = None
Ang Lie014a8b2016-06-28 18:24:52 -0700364 self.vts_agent_process = None
Ang Li93420002016-05-10 19:11:44 -0700365 self.adb = adb.AdbProxy(serial)
366 self.fastboot = fastboot.FastbootProxy(serial)
367 if not self.isBootloaderMode:
368 self.rootAdb()
Ang Li6c303fc2016-09-22 13:19:33 -0700369 self.host_command_port = None
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700370 self.host_callback_port = adb.get_available_host_port()
Ang Lie014a8b2016-06-28 18:24:52 -0700371 self.adb.reverse_tcp_forward(self.device_callback_port,
372 self.host_callback_port)
Ang Li53bb72b2016-07-19 18:29:37 -0700373 self.hal = None
Keun Soo Yimf5d0bca2016-07-30 23:59:26 -0700374 self.lib = None
375 self.shell = None
Ang Li6c303fc2016-09-22 13:19:33 -0700376 self.sl4a_host_port = None
377 # TODO: figure out a good way to detect which port is available
378 # on the target side, instead of hard coding a port number.
379 self.sl4a_target_port = 8082
Ang Li93420002016-05-10 19:11:44 -0700380
381 def __del__(self):
Ang Li53bb72b2016-07-19 18:29:37 -0700382 self.cleanUp()
383
384 def cleanUp(self):
385 """Cleans up the AndroidDevice object and releases any resources it
386 claimed.
387 """
388 self.stopServices()
Keun Soo Yimcbd8c052016-06-20 15:10:58 -0700389 if self.host_command_port:
390 self.adb.forward("--remove tcp:%s" % self.host_command_port)
Ang Li6c303fc2016-09-22 13:19:33 -0700391 self.host_command_port = None
392 if self.sl4a_host_port:
393 self.adb.forward("--remove tcp:%s" % self.sl4a_host_port)
394 self.sl4a_host_port = None
Ang Li93420002016-05-10 19:11:44 -0700395
396 @property
397 def isBootloaderMode(self):
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700398 """True if the device is in bootloader mode."""
Ang Li93420002016-05-10 19:11:44 -0700399 return self.serial in list_fastboot_devices()
400
401 @property
402 def isAdbRoot(self):
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700403 """True if adb is running as root for this device."""
404 id_str = self.adb.shell("id -u").decode("utf-8")
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700405 return "root" in id_str
Ang Li93420002016-05-10 19:11:44 -0700406
407 @property
Tri Voe1a5a852016-10-20 19:59:45 -0700408 def verityEnabled(self):
409 """True if verity is enabled for this device."""
410 try:
411 self.adb.shell('getprop | grep partition.system.verified').decode("utf-8")
412 except adb.AdbError:
413 # If verity is disabled, there is no property 'partition.system.verified'
414 return False
415 return True
416
417 @property
Ang Li93420002016-05-10 19:11:44 -0700418 def model(self):
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700419 """The Android code name for the device."""
Ang Li93420002016-05-10 19:11:44 -0700420 # If device is in bootloader mode, get mode name from fastboot.
421 if self.isBootloaderMode:
422 out = self.fastboot.getvar("product").strip()
423 # "out" is never empty because of the "total time" message fastboot
424 # writes to stderr.
425 lines = out.decode("utf-8").split('\n', 1)
426 if lines:
427 tokens = lines[0].split(' ')
428 if len(tokens) > 1:
429 return tokens[1].lower()
430 return None
431 out = self.adb.shell('getprop | grep ro.build.product')
432 model = out.decode("utf-8").strip().split('[')[-1][:-1].lower()
433 if model == "sprout":
434 return model
435 else:
436 out = self.adb.shell('getprop | grep ro.product.name')
437 model = out.decode("utf-8").strip().split('[')[-1][:-1].lower()
438 return model
439
440 @property
Tri Vo921bfa02016-09-20 10:25:23 -0700441 def cpu_abi(self):
442 """CPU ABI (Application Binary Interface) of the device."""
443 out = self.adb.shell('getprop | grep "\[ro.product.cpu.abi\]"')
444 if not out:
445 return "unknown"
446
447 cpu_abi = out.decode("utf-8").strip().split('[')[-1][:-1].lower()
448 return cpu_abi
449
450 @property
Tri Vof07f1292016-09-21 17:27:46 -0700451 def is64Bit(self):
452 """True if device is 64 bit."""
453 out = self.adb.shell('uname -m')
454 return "64" in out
455
456 @property
Ang Li93420002016-05-10 19:11:44 -0700457 def isAdbLogcatOn(self):
458 """Whether there is an ongoing adb logcat collection.
459 """
460 if self.adb_logcat_process:
461 return True
462 return False
463
464 def loadConfig(self, config):
465 """Add attributes to the AndroidDevice object based on json config.
466
467 Args:
468 config: A dictionary representing the configs.
469
470 Raises:
471 AndroidDeviceError is raised if the config is trying to overwrite
472 an existing attribute.
473 """
474 for k, v in config.items():
475 if hasattr(self, k):
Ang Li7f0e1c72016-06-14 11:23:49 -0700476 raise AndroidDeviceError(
477 "Attempting to set existing attribute %s on %s" %
478 (k, self.serial))
Ang Li93420002016-05-10 19:11:44 -0700479 setattr(self, k, v)
480
481 def rootAdb(self):
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700482 """Changes adb to root mode for this device."""
Ang Li93420002016-05-10 19:11:44 -0700483 if not self.isAdbRoot:
Keun Soo Yimc2bc9f82016-07-16 15:36:36 -0700484 try:
485 self.adb.root()
Keun Soo Yima5a0dc22016-07-16 17:56:19 -0700486 self.adb.wait_for_device()
487 self.adb.remount()
Keun Soo Yim731e9172016-07-16 17:48:39 -0700488 self.adb.wait_for_device()
489 except adb.AdbError as e:
490 # adb wait-for-device is not always possible in the lab
Keun Soo Yima5a0dc22016-07-16 17:56:19 -0700491 # continue with an assumption it's done by the harness.
Keun Soo Yim731e9172016-07-16 17:48:39 -0700492 logging.exception(e)
Ang Li93420002016-05-10 19:11:44 -0700493
Ang Li93420002016-05-10 19:11:44 -0700494 def startAdbLogcat(self):
495 """Starts a standing adb logcat collection in separate subprocesses and
496 save the logcat in a file.
497 """
498 if self.isAdbLogcatOn:
Ang Li7f0e1c72016-06-14 11:23:49 -0700499 raise AndroidDeviceError(("Android device %s already has an adb "
Ang Lie2139f12016-05-12 17:39:06 -0700500 "logcat thread going on. Cannot start "
Ang Li7f0e1c72016-06-14 11:23:49 -0700501 "another one.") % self.serial)
Ang Li7f0e1c72016-06-14 11:23:49 -0700502 f_name = "adblog,%s,%s.txt" % (self.model, self.serial)
Ang Li93420002016-05-10 19:11:44 -0700503 utils.create_dir(self.log_path)
504 logcat_file_path = os.path.join(self.log_path, f_name)
505 try:
506 extra_params = self.adb_logcat_param
507 except AttributeError:
508 extra_params = "-b all"
Ang Li7f0e1c72016-06-14 11:23:49 -0700509 cmd = "adb -s %s logcat -v threadtime %s >> %s" % (
Ang Li93420002016-05-10 19:11:44 -0700510 self.serial, extra_params, logcat_file_path)
511 self.adb_logcat_process = utils.start_standing_subprocess(cmd)
512 self.adb_logcat_file_path = logcat_file_path
513
514 def stopAdbLogcat(self):
515 """Stops the adb logcat collection subprocess.
516 """
517 if not self.isAdbLogcatOn:
Ang Li7f0e1c72016-06-14 11:23:49 -0700518 raise AndroidDeviceError(
519 "Android device %s does not have an ongoing adb logcat collection."
520 % self.serial)
Ang Li93420002016-05-10 19:11:44 -0700521 utils.stop_standing_subprocess(self.adb_logcat_process)
522 self.adb_logcat_process = None
523
524 def takeBugReport(self, test_name, begin_time):
525 """Takes a bug report on the device and stores it in a file.
526
527 Args:
528 test_name: Name of the test case that triggered this bug report.
529 begin_time: Logline format timestamp taken when the test started.
530 """
531 br_path = os.path.join(self.log_path, "BugReports")
532 utils.create_dir(br_path)
Ang Li7f0e1c72016-06-14 11:23:49 -0700533 base_name = ",%s,%s.txt" % (begin_time, self.serial)
Ang Li93420002016-05-10 19:11:44 -0700534 test_name_len = utils.MAX_FILENAME_LEN - len(base_name)
535 out_name = test_name[:test_name_len] + base_name
536 full_out_path = os.path.join(br_path, out_name.replace(' ', '\ '))
537 self.log.info("Taking bugreport for %s on %s", test_name, self.serial)
Ang Li7f0e1c72016-06-14 11:23:49 -0700538 self.adb.bugreport(" > %s" % full_out_path)
Ang Li93420002016-05-10 19:11:44 -0700539 self.log.info("Bugreport for %s taken at %s", test_name, full_out_path)
540
Ang Li93420002016-05-10 19:11:44 -0700541 @utils.timeout(15 * 60)
542 def waitForBootCompletion(self):
543 """Waits for Android framework to broadcast ACTION_BOOT_COMPLETED.
544
545 This function times out after 15 minutes.
546 """
Keun Soo Yim731e9172016-07-16 17:48:39 -0700547 try:
548 self.adb.wait_for_device()
549 except adb.AdbError as e:
550 # adb wait-for-device is not always possible in the lab
551 logging.exception(e)
Tri Vo175de122016-10-28 13:36:57 -0700552 while not self.hasBooted():
Ang Li93420002016-05-10 19:11:44 -0700553 time.sleep(5)
554
Tri Vo175de122016-10-28 13:36:57 -0700555 def hasBooted(self):
556 """Checks whether the device has booted.
557
558 Returns:
559 True if booted, False otherwise.
560 """
561 try:
562 out = self.adb.shell("getprop sys.boot_completed")
563 completed = out.decode('utf-8').strip()
564 if completed == '1':
565 return True
566 except adb.AdbError:
567 # adb shell calls may fail during certain period of booting
568 # process, which is normal. Ignoring these errors.
569 return False
570
Ang Li93420002016-05-10 19:11:44 -0700571 def reboot(self):
Ang Li7f0e1c72016-06-14 11:23:49 -0700572 """Reboots the device and wait for device to complete booting.
Ang Li93420002016-05-10 19:11:44 -0700573
574 This is probably going to print some error messages in console. Only
575 use if there's no other option.
576
Ang Li93420002016-05-10 19:11:44 -0700577 Raises:
578 AndroidDeviceError is raised if waiting for completion timed
579 out.
580 """
581 if self.isBootloaderMode:
582 self.fastboot.reboot()
583 return
584 has_adb_log = self.isAdbLogcatOn
Ang Lie014a8b2016-06-28 18:24:52 -0700585 has_vts_agent = True if self.vts_agent_process else False
Ang Li93420002016-05-10 19:11:44 -0700586 if has_adb_log:
587 self.stopAdbLogcat()
Ang Lie014a8b2016-06-28 18:24:52 -0700588 if has_vts_agent:
589 self.stopVtsAgent()
Ang Li93420002016-05-10 19:11:44 -0700590 self.adb.reboot()
591 self.waitForBootCompletion()
592 self.rootAdb()
Ang Li93420002016-05-10 19:11:44 -0700593 if has_adb_log:
594 self.startAdbLogcat()
Ang Lie014a8b2016-06-28 18:24:52 -0700595 if has_vts_agent:
596 self.startVtsAgent()
Sahil Jain06dd6a22016-06-24 13:47:37 -0700597
Ang Li6c303fc2016-09-22 13:19:33 -0700598 def startServices(self):
Ang Li53bb72b2016-07-19 18:29:37 -0700599 """Starts long running services on the android device.
600
601 1. Start adb logcat capture.
Ang Li6c303fc2016-09-22 13:19:33 -0700602 2. Start VtsAgent and create HalMirror unless disabled in config.
603 3. If enabled in config, start sl4a service and create sl4a clients.
Ang Li53bb72b2016-07-19 18:29:37 -0700604 """
Ang Li6c303fc2016-09-22 13:19:33 -0700605 enable_vts_agent = getattr(self, "enable_vts_agent", True)
606 enable_sl4a = getattr(self, "enable_sl4a", False)
Ang Li53bb72b2016-07-19 18:29:37 -0700607 try:
608 self.startAdbLogcat()
609 except:
610 self.log.exception("Failed to start adb logcat!")
611 raise
Ang Li6c303fc2016-09-22 13:19:33 -0700612 if enable_vts_agent:
Keun Soo Yim4231b6e2016-07-31 19:01:05 -0700613 self.startVtsAgent()
614 self.device_command_port = int(
615 self.adb.shell("cat /data/local/tmp/vts_tcp_server_port"))
616 logging.info("device_command_port: %s", self.device_command_port)
Ang Li6c303fc2016-09-22 13:19:33 -0700617 if not self.host_command_port:
618 self.host_command_port = adb.get_available_host_port()
Keun Soo Yim4231b6e2016-07-31 19:01:05 -0700619 self.adb.tcp_forward(self.host_command_port, self.device_command_port)
620 self.hal = hal_mirror.HalMirror(self.host_command_port,
621 self.host_callback_port)
622 self.lib = lib_mirror.LibMirror(self.host_command_port)
623 self.shell = shell_mirror.ShellMirror(self.host_command_port)
Ang Li6c303fc2016-09-22 13:19:33 -0700624 if enable_sl4a:
625 self.startSl4aClient()
Ang Li53bb72b2016-07-19 18:29:37 -0700626
627 def stopServices(self):
628 """Stops long running services on the android device.
629 """
630 if self.adb_logcat_process:
631 self.stopAdbLogcat()
632 self.stopVtsAgent()
633 if self.hal:
634 self.hal.CleanUp()
635
Ang Lie014a8b2016-06-28 18:24:52 -0700636 def startVtsAgent(self):
637 """Start HAL agent on the AndroidDevice.
Sahil Jain06dd6a22016-06-24 13:47:37 -0700638
639 This function starts the target side native agent and is persisted
Ang Lie014a8b2016-06-28 18:24:52 -0700640 throughout the test run.
Sahil Jain06dd6a22016-06-24 13:47:37 -0700641 """
Ang Li6c303fc2016-09-22 13:19:33 -0700642 self.log.info("Starting VTS agent")
Ang Lie014a8b2016-06-28 18:24:52 -0700643 if self.vts_agent_process:
644 raise AndroidDeviceError("HAL agent is already running on %s." %
645 self.serial)
Keun Soo Yima066dd52016-07-01 15:18:28 -0700646
647 cleanup_commands = [
Keun Soo Yim02da0272016-07-19 07:56:38 -0700648 "rm -f /data/local/tmp/vts_driver_*",
Ang Li53bb72b2016-07-19 18:29:37 -0700649 "rm -f /data/local/tmp/vts_agent_callback*"
650 ]
Keun Soo Yim2eecdfa2016-07-29 11:48:37 -0700651 kill_commands = ["killall vts_hal_agent32", "killall vts_hal_agent64",
652 "killall fuzzer32", "killall fuzzer64",
653 "killall vts_shell_driver32",
Yuexi Ma674e59d2016-07-19 15:39:54 -0700654 "killall vts_shell_driver64"]
Keun Soo Yima066dd52016-07-01 15:18:28 -0700655 cleanup_commands.extend(kill_commands)
Keun Soo Yimbf8f7b42016-07-13 13:58:28 -0700656 chmod_commands = [
Keun Soo Yim2eecdfa2016-07-29 11:48:37 -0700657 "chmod 755 %s/32/vts_hal_agent32" % DEFAULT_AGENT_BASE_DIR,
658 "chmod 755 %s/64/vts_hal_agent64" % DEFAULT_AGENT_BASE_DIR,
Keun Soo Yimbf8f7b42016-07-13 13:58:28 -0700659 "chmod 755 %s/32/fuzzer32" % DEFAULT_AGENT_BASE_DIR,
Keun Soo Yim02da0272016-07-19 07:56:38 -0700660 "chmod 755 %s/64/fuzzer64" % DEFAULT_AGENT_BASE_DIR,
661 "chmod 755 %s/32/vts_shell_driver32" % DEFAULT_AGENT_BASE_DIR,
Ang Li53bb72b2016-07-19 18:29:37 -0700662 "chmod 755 %s/64/vts_shell_driver64" % DEFAULT_AGENT_BASE_DIR
663 ]
Keun Soo Yimbf8f7b42016-07-13 13:58:28 -0700664 cleanup_commands.extend(chmod_commands)
Keun Soo Yima066dd52016-07-01 15:18:28 -0700665 for cmd in cleanup_commands:
Sahil Jain06dd6a22016-06-24 13:47:37 -0700666 try:
667 self.adb.shell(cmd)
Keun Soo Yimab7fb062016-07-13 14:47:19 -0700668 except adb.AdbError as e:
669 self.log.warning(
Ang Li53bb72b2016-07-19 18:29:37 -0700670 "A command to setup the env to start the VTS Agent failed %s",
671 e)
Ang Lie014a8b2016-06-28 18:24:52 -0700672 vts_agent_log_path = os.path.join(self.log_path, "vts_agent.log")
Tri Vo843b57f2016-09-24 14:51:38 -0700673
674 bits = ['64', '32'] if self.is64Bit else ['32']
675 for bitness in bits:
Keun Soo Yimd2aa5b82016-07-30 20:12:19 -0700676 cmd = (
677 'adb -s {s} shell LD_LIBRARY_PATH={path}/{bitness} '
678 '{path}/{bitness}/vts_hal_agent{bitness}'
679 ' {path}/32/fuzzer32 {path}/64/fuzzer64 {path}/spec'
680 ' {path}/32/vts_shell_driver32 {path}/64/vts_shell_driver64 >> {log}'
681 ).format(s=self.serial,
682 bitness=bitness,
683 path=DEFAULT_AGENT_BASE_DIR,
684 log=vts_agent_log_path)
685 try:
686 self.vts_agent_process = utils.start_standing_subprocess(
687 cmd, check_health_delay=1)
688 except utils.VTSUtilsError as e:
689 logging.exception(e)
690 with open(vts_agent_log_path, 'r') as log_file:
691 logging.error("VTS agent output:\n")
692 logging.error(log_file.read())
693 # one common cause is that 64-bit executable is not supported
694 # in low API level devices.
695 if bitness == '32':
696 raise
697 else:
698 logging.error('retrying using a 32-bit binary.')
Sahil Jain06dd6a22016-06-24 13:47:37 -0700699
Ang Lie014a8b2016-06-28 18:24:52 -0700700 def stopVtsAgent(self):
701 """Stop the HAL agent running on the AndroidDevice.
Sahil Jain06dd6a22016-06-24 13:47:37 -0700702 """
Ang Lie014a8b2016-06-28 18:24:52 -0700703 if self.vts_agent_process:
704 utils.stop_standing_subprocess(self.vts_agent_process)
705 self.vts_agent_process = None
Sahil Jain06dd6a22016-06-24 13:47:37 -0700706
Keun Soo Yime34754e2016-08-15 11:25:15 -0700707 @property
708 def product_type(self):
709 """Gets the product type name."""
710 return self._product_type
711
Ang Li6c303fc2016-09-22 13:19:33 -0700712 # Code for using SL4A client
713 def startSl4aClient(self, handle_event=True):
714 """Create an sl4a connection to the device.
715
716 Return the connection handler 'droid'. By default, another connection
717 on the same session is made for EventDispatcher, and the dispatcher is
718 returned to the caller as well.
719 If sl4a server is not started on the device, try to start it.
720
721 Args:
722 handle_event: True if this droid session will need to handle
723 events.
724 """
725 self._sl4a_sessions = {}
726 self._sl4a_event_dispatchers = {}
727 if not self.sl4a_host_port or not adb.is_port_available(self.sl4a_host_port):
728 self.sl4a_host_port = adb.get_available_host_port()
729 self.adb.tcp_forward(self.sl4a_host_port, self.sl4a_target_port)
730 try:
731 droid = self._createNewSl4aSession()
732 except sl4a_client.Error:
733 sl4a_client.start_sl4a(self.adb)
734 droid = self._createNewSl4aSession()
735 self.sl4a = droid
736 if handle_event:
737 ed = self._getSl4aEventDispatcher(droid)
738 self.sl4a_event = ed
739
740 def _getSl4aEventDispatcher(self, droid):
741 """Return an EventDispatcher for an sl4a session
742
743 Args:
744 droid: Session to create EventDispatcher for.
745
746 Returns:
747 ed: An EventDispatcher for specified session.
748 """
749 # TODO (angli): Move service-specific start/stop functions out of
750 # android_device, including VTS Agent, SL4A, and any other
751 # target-side services.
752 ed_key = self.serial + str(droid.uid)
753 if ed_key in self._sl4a_event_dispatchers:
754 if self._sl4a_event_dispatchers[ed_key] is None:
755 raise AndroidDeviceError("EventDispatcher Key Empty")
756 self.log.debug("Returning existing key %s for event dispatcher!",
757 ed_key)
758 return self._sl4a_event_dispatchers[ed_key]
759 event_droid = self._addNewConnectionToSl4aSession(droid.uid)
760 ed = event_dispatcher.EventDispatcher(event_droid)
761 self._sl4a_event_dispatchers[ed_key] = ed
762 return ed
763
764 def _createNewSl4aSession(self):
765 """Start a new session in sl4a.
766
767 Also caches the droid in a dict with its uid being the key.
768
769 Returns:
770 An Android object used to communicate with sl4a on the android
771 device.
772
773 Raises:
774 sl4a_client.Error: Something is wrong with sl4a and it returned an
775 existing uid to a new session.
776 """
777 droid = sl4a_client.Sl4aClient(port=self.sl4a_host_port)
778 droid.open()
779 if droid.uid in self._sl4a_sessions:
780 raise sl4a_client.Error(
781 "SL4A returned an existing uid for a new session. Abort.")
782 self._sl4a_sessions[droid.uid] = [droid]
783 return droid
784
785 def _addNewConnectionToSl4aSession(self, session_id):
786 """Create a new connection to an existing sl4a session.
787
788 Args:
789 session_id: UID of the sl4a session to add connection to.
790
791 Returns:
792 An Android object used to communicate with sl4a on the android
793 device.
794
795 Raises:
796 DoesNotExistError: Raised if the session it's trying to connect to
797 does not exist.
798 """
799 if session_id not in self._sl4a_sessions:
800 raise DoesNotExistError("Session %d doesn't exist." % session_id)
801 droid = sl4a_client.Sl4aClient(port=self.sl4a_host_port, uid=session_id)
802 droid.open(cmd=sl4a_client.Sl4aCommand.CONTINUE)
803 return droid
804
805 def _terminateSl4aSession(self, session_id):
806 """Terminate a session in sl4a.
807
808 Send terminate signal to sl4a server; stop dispatcher associated with
809 the session. Clear corresponding droids and dispatchers from cache.
810
811 Args:
812 session_id: UID of the sl4a session to terminate.
813 """
814 if self._sl4a_sessions and (session_id in self._sl4a_sessions):
815 for droid in self._sl4a_sessions[session_id]:
816 droid.closeSl4aSession()
817 droid.close()
818 del self._sl4a_sessions[session_id]
819 ed_key = self.serial + str(session_id)
820 if ed_key in self._sl4a_event_dispatchers:
821 self._sl4a_event_dispatchers[ed_key].clean_up()
822 del self._sl4a_event_dispatchers[ed_key]
823
824 def _terminateAllSl4aSessions(self):
825 """Terminate all sl4a sessions on the AndroidDevice instance.
826
827 Terminate all sessions and clear caches.
828 """
829 if self._sl4a_sessions:
830 session_ids = list(self._sl4a_sessions.keys())
831 for session_id in session_ids:
832 try:
833 self._terminateSl4aSession(session_id)
834 except:
835 self.log.exception("Failed to terminate session %d.",
836 session_id)
837 if self.sl4a_host_port:
838 self.adb.forward("--remove tcp:%d" % self.sl4a_host_port)
839 self.sl4a_host_port = None
840
Sahil Jain06dd6a22016-06-24 13:47:37 -0700841
Ang Lie014a8b2016-06-28 18:24:52 -0700842class AndroidDeviceLoggerAdapter(logging.LoggerAdapter):
843 """A wrapper class that attaches a prefix to all log lines from an
844 AndroidDevice object.
845 """
846
847 def process(self, msg, kwargs):
848 """Process every log message written via the wrapped logger object.
849
850 We are adding the prefix "[AndroidDevice|<serial>]" to all log lines.
851
852 Args:
853 msg: string, the original log message.
854 kwargs: dict, the key value pairs that can be used to modify the
855 original log message.
856 """
857 msg = "[AndroidDevice|%s] %s" % (self.extra["serial"], msg)
858 return (msg, kwargs)