blob: a7d5e227c261eba1178251170e99a5c50ee99638 [file] [log] [blame]
# Copyright 2016 The Chromium OS 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 logging
import os
import re
import sys
import common
from autotest_lib.client.common_lib import error
from autotest_lib.server import utils
from autotest_lib.server.hosts import adb_host
from autotest_lib.utils import emulator_manager
OS_TYPE_EMULATED_BRILLO = 'emulated_brillo'
BOARD_DEFAULT = "brilloemulator_arm"
class EmulatedADBHost(adb_host.ADBHost):
"""Run an emulator as an ADB device preserving the API and assumptions of
Currently supported emulators:
* Brillo
* brilloemulator_arm
def _initialize(self, *args, **kwargs):
"""Intialize an emulator so that existing assumptions that the host is
always ready ar satisfied.
@param args: pass through to ADBHost
@param kwargs: pass through to ADBHost
super(EmulatedADBHost, self)._initialize(*args, **kwargs)
# Verify serial
m = re.match('emulator-(\d{4})', self.adb_serial)
if not m:
raise ValueError('Emulator serial must be in the format '
self.port = int( + 1
# Determine directory for images (needs to be persistent)
tmp_dir = self.teststation.get_tmp_dir()
self.imagedir = os.path.join(os.path.dirname(tmp_dir), self.adb_serial)'rm -rf %s' % tmp_dir)'mkdir -p %s' % self.imagedir)
# Boot the emulator, if not already booted, since ADBHost assumes the
# device is always available
self._emulator = emulator_manager.EmulatorManager(
self.imagedir, self.port,
def _start_emulator_if_not_started(self):
"""Boot or reboot the emulator if necessary.
If the emulator is not started boot the emulator. Otherwise leave it
alone. Ensure emulator is running and ready before returning.
host_os = self.get_os_type()
board = self.get_board()
# Check that images exist in imagedir
try:'test -f %s' % os.path.join(self.imagedir,
# Use default images
except error.GenericHostRunError:'cp %s/* %s/' % (
os.path.join('/usr/local/emulator_images', host_os, board),
if not self._emulator.find():
def get_os_type(self):
"""Determine the OS type from afe_host object or use the default if
no os label / no afe_host object.
@return: os type as str
info = self.host_info_store.get()
return info.os or OS_TYPE_DEFAULT
def get_board(self):
"""Determine the board from afe_host object or use the default if
no board label / no afe_host object.
@return: board as str
info = self.host_info_store.get()
return info.board or BOARD_DEFAULT
def check_host(host, timeout=10):
"""No dynamic host checking. Must be explicit.
@param host: ignored
@param timeout: ignored
@return: False
return False
def stage_emulator_artifact(self, build_url):
"""Download required build artifact from the given build_url to a
local directory in the machine running the emulator.
@param build_url: The url to use for downloading Android artifacts.
pattern: http://$devserv/static/branch/target/build_id
@return: Path to the directory contains image files.
build_info = self.get_build_info_from_build_url(build_url)
zipped_artifact = EMULATED_BRILLO_ARTIFACT_FORMAT % build_info
dtb_artifact = EMULATED_BRILLO_DTB_FORMAT % build_info
image_dir = self.teststation.get_tmp_dir()
self.download_file(build_url, zipped_artifact, image_dir,
self.download_file(build_url, dtb_artifact, image_dir,
return image_dir
except:'rm -rf %s' % image_dir)
def setup_brillo_emulator(self, build_url, build_local_path=None):
"""Install the Brillo DUT.
Following are the steps used here to provision an android device:
1. If build_local_path is not set, download the target_files zip, e.g.,, and unzip it.
2. Move the necessary images to a new directory.
3. Determine port for ADB from serial.
4. Use EmulatorManager to start the emulator.
@param build_url: The url to use for downloading Android artifacts.
pattern: http://$devserver:###/static/$build
@param build_local_path: The path to a local folder that contains the
image files needed to provision the device.
Note that the folder is in the machine running
adb command, rather than the drone.
@raises AndroidInstallError if any error occurs.
# If the build is not staged in local server yet, clean up the temp
# folder used to store image files after the provision is completed.
delete_build_folder = bool(not build_local_path)
# Download image files needed for provision to a local directory.
if not build_local_path:
build_local_path = self.stage_emulator_artifact(build_url)
# Create directory with required files'rm -rf %s && mkdir %s' % (self.imagedir,
self.imagedir))'mv %s %s' % (
os.path.join(build_local_path, 'IMAGES', 'system.img'),
os.path.join(self.imagedir, 'system.img')
))'mv %s %s' % (
os.path.join(build_local_path, 'IMAGES', 'userdata.img'),
os.path.join(self.imagedir, 'userdata.img')
))'mv %s %s' % (
os.path.join(build_local_path, 'BOOT', 'kernel'),
os.path.join(self.imagedir, 'kernel')
))'mv %s/*.dtb %s' % (build_local_path,
# Start the emulator
except Exception as e:
logging.error('Install Brillo build failed with error: %s', e)
# Re-raise the exception with type of AndroidInstallError.
raise (adb_host.AndroidInstallError, sys.exc_info()[1],
if delete_build_folder:'rm -rf %s' % build_local_path)'Successfully installed Android build staged at '
'%s.', build_url)
def machine_install(self, build_url=None, build_local_path=None, wipe=True,
flash_all=False, os_type=None):
"""Install the DUT.
@param build_url: The url to use for downloading Android artifacts.
pattern: http://$devserver:###/static/$build.
If build_url is set to None, the code may try
_parser.options.image to do the installation. If none
of them is set, machine_install will fail.
@param build_local_path: The path to a local directory that contains the
image files needed to provision the device.
@param wipe: No-op
@param flash_all: No-op
@param os_type: OS to install (overrides label).
@returns A tuple of (image_name, host_attributes). image_name is the
name of image installed, e.g.,
host_attributes is a dictionary of (attribute, value), which
can be saved to afe_host_attributes table in database. This
method returns a dictionary with a single entry of
`job_repo_url_[adb_serial]`: devserver_url, where devserver_url
is a url to the build staged on devserver.
os_type = os_type or self.get_os_type()
if not build_url and self._parser.options.image:
build_url, _ = self.stage_build_for_install(
self._parser.options.image, os_type=os_type)
build_url=build_url, build_local_path=build_local_path)
raise error.InstallError(
'Installation of os type %s is not supported.' %
return (build_url.split('static/')[-1],
{self.job_repo_url_attribute: build_url})
def repair(self):
"""No-op. No repair procedures for emulated devices.
def verify_software(self):
"""Verify commands are available on teststation.
@return: Bool - teststation has necessary software installed.
adb ='which adb')
qemu ='which qemu-system-arm')
unzip ='which unzip')
return bool(adb and qemu and unzip)
def fastboot_run(self, command, **kwargs):
"""No-op, emulators do not support fastboot.
@param command: command to not execute
@param kwargs: additional arguments to ignore
@return: empty CmdResult object
return utils.CmdResult()
def get_labels(self):
"""No-op, emulators do not have any detectable labels.
@return: empty list
return []
def get_platform(self):
"""@return: emulated_adb
return 'emulated_adb'