-
Notifications
You must be signed in to change notification settings - Fork 617
Adds emulator launch/shutdown capabilities. #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,127 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import datetime | ||
import logging | ||
import os | ||
import signal | ||
import subprocess | ||
import time | ||
|
||
_logger = logging.getLogger('fireci.emulator') | ||
|
||
EMULATOR_BINARY = 'emulator' | ||
ADB_BINARY = 'adb' | ||
EMULATOR_NAME = 'test' | ||
|
||
EMULATOR_FLAGS = ['-no-audio', '-no-window', '-skin', '768x1280'] | ||
|
||
|
||
# TODO(vkryachko): start/shutdown the emulator. | ||
class EmulatorHandler: | ||
""" | ||
Context manager that launches an android emulator for the duration of its execution. | ||
|
||
def __init__(self, artifacts_dir): | ||
As part of its run it: | ||
* Launches the emulator | ||
* Waits for it to boot | ||
* Starts logcat to store on-device logs | ||
* Produces stdout.log, stderr.log, logcat.log in the artifacts directory | ||
""" | ||
|
||
def __init__( | ||
self, | ||
artifacts_dir, | ||
*, | ||
name=EMULATOR_NAME, | ||
emulator_binary=EMULATOR_BINARY, | ||
adb_binary=ADB_BINARY, | ||
# for testing only | ||
emulator_stdin=None, | ||
wait_for_device_stdin=None, | ||
logcat_stdin=None): | ||
self._artifacts_dir = artifacts_dir | ||
|
||
log_dir = '{}_emulator'.format(name) | ||
self._stdout = self._open(log_dir, 'stdout.log') | ||
self._stderr = self._open(log_dir, 'stderr.log') | ||
self._adb_log = self._open(log_dir, 'logcat.log') | ||
self._name = name | ||
|
||
self._emulator_binary = emulator_binary | ||
self._adb_binary = adb_binary | ||
|
||
self._emulator_stdin = emulator_stdin | ||
self._wait_for_device_stdin = wait_for_device_stdin | ||
self._logcat_stdin = logcat_stdin | ||
|
||
def __enter__(self): | ||
_logger.debug('Pretend to start the emulator(TODO)') | ||
_logger.info('Starting avd "{}..."'.format(self._name)) | ||
self._process = subprocess.Popen( | ||
[self._emulator_binary, '-avd', self._name] + EMULATOR_FLAGS, | ||
env=os.environ, | ||
stdin=self._emulator_stdin, | ||
stdout=self._stdout, | ||
stderr=self._stderr) | ||
try: | ||
self._wait_for_boot(datetime.timedelta(minutes=5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably worth making 5 a constant on top of the file |
||
except: | ||
self._kill(self._process) | ||
self._close_files() | ||
raise | ||
|
||
self._logcat = subprocess.Popen( | ||
[self._adb_binary, 'logcat'], | ||
stdin=self._logcat_stdin, | ||
stdout=self._adb_log, | ||
) | ||
|
||
def __exit__(self, exception_type, exception_value, traceback): | ||
_logger.debug('Pretend to stop the emulator(TODO)') | ||
_logger.info('Shutting down avd "{}"...'.format(self._name)) | ||
self._kill(self._process) | ||
_logger.info('Avd "{}" shut down.'.format(self._name)) | ||
self._kill(self._logcat) | ||
self._close_files() | ||
|
||
def _open(self, dirname, filename): | ||
"""Opens a file in a given directory, creates directory if required.""" | ||
dirname = os.path.join(self._artifacts_dir, dirname) | ||
if (not os.path.exists(dirname)): | ||
os.makedirs(dirname) | ||
return open(os.path.join(dirname, filename), 'w') | ||
|
||
def _wait_for_boot(self, timeout: datetime.timedelta): | ||
_logger.info('Waiting for avd to boot...') | ||
wait = subprocess.Popen( | ||
[self._adb_binary, 'wait-for-device'], | ||
stdin=self._wait_for_device_stdin, | ||
stdout=self._stdout, | ||
stderr=self._stderr, | ||
) | ||
|
||
start = datetime.datetime.now() | ||
while self._process.poll() is None: | ||
wait_exitcode = wait.poll() | ||
if wait_exitcode is not None: | ||
if wait_exitcode == 0: | ||
_logger.info('Emulator booted successfully.') | ||
return | ||
raise RuntimeError("Waiting for emulator failed.") | ||
|
||
time.sleep(0.1) | ||
now = datetime.datetime.now() | ||
if now - start >= timeout: | ||
self._kill(wait, sig=signal.SIGKILL) | ||
raise RuntimeError("Emulator startup timed out.") | ||
|
||
self._kill(wait) | ||
raise RuntimeError( | ||
"Emulator failed to launch. See emulator logs for details.") | ||
|
||
def _kill(self, process, sig=signal.SIGTERM): | ||
process.send_signal(sig) | ||
process.wait() | ||
|
||
def _close_files(self): | ||
for f in (self._stdout, self._stderr, self._adb_log): | ||
if f is not None: | ||
f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import io | ||
import os | ||
import pathlib | ||
import threading | ||
import time | ||
import unittest | ||
|
||
from concurrent import futures | ||
|
||
from fireci.internal import _emulator_handler | ||
from fireci.emulator import EMULATOR_FLAGS | ||
from .fileutil import ( | ||
Artifact, | ||
create_artifacts, | ||
in_tempdir, | ||
with_env, | ||
) | ||
from . import scripts | ||
|
||
|
||
class ProcessChannel: | ||
"""Write-only communication channel with a process through its stdin.""" | ||
|
||
def __init__(self, fd): | ||
self._fd = fd | ||
|
||
def send(self, value, close=False): | ||
os.write(self._fd, bytes(str(value), encoding='utf8')) | ||
if close: | ||
os.close(self._fd) | ||
|
||
|
||
class EmulatorTests(unittest.TestCase): | ||
executor = futures.ThreadPoolExecutor(max_workers=1) | ||
|
||
@in_tempdir | ||
def test_emulator_when_not_requested_should_not_produce_logs(self): | ||
with _emulator_handler(False): | ||
pass | ||
self.assertFalse(os.listdir(os.getcwd())) | ||
|
||
@in_tempdir | ||
def test_emulator_when_emulator_fails_to_start_should_fail(self): | ||
create_artifacts( | ||
Artifact('emulator', content=scripts.waiting_for_status(), mode=0o744), | ||
Artifact('adb', content=scripts.waiting_for_status(), mode=0o744), | ||
) | ||
future, emulator, waiter, logcat = self._invoke_emulator( | ||
'./emulator', './adb') | ||
|
||
# emulator exits before wait-for-device | ||
emulator.send(0, close=True) | ||
|
||
with self.assertRaisesRegex(RuntimeError, 'Emulator failed to launch'): | ||
future.result(timeout=1) | ||
|
||
@in_tempdir | ||
def test_emulator_when_wait_for_device_fails_should_fail(self): | ||
create_artifacts( | ||
Artifact('emulator', content=scripts.waiting_for_status(), mode=0o744), | ||
Artifact('adb', content=scripts.waiting_for_status(), mode=0o744), | ||
) | ||
|
||
future, emulator, waiter, logcat = self._invoke_emulator( | ||
'./emulator', './adb') | ||
|
||
# wait-for-device fails | ||
waiter.send(1, close=True) | ||
|
||
with self.assertRaisesRegex(RuntimeError, 'Waiting for emulator failed'): | ||
future.result(timeout=1) | ||
|
||
@in_tempdir | ||
def test_emulator_when_startup_succeeds_should_produce_expected_outputs(self): | ||
create_artifacts( | ||
Artifact('emulator', content=scripts.waiting_for_status(), mode=0o744), | ||
Artifact('adb', content=scripts.waiting_for_status(), mode=0o744), | ||
) | ||
|
||
future, emulator, waiter, logcat = self._invoke_emulator( | ||
'./emulator', './adb') | ||
|
||
# wait-for-device succeeds | ||
waiter.send(0, close=True) | ||
|
||
future.result(timeout=1) | ||
|
||
path = pathlib.Path('_artifacts') / 'test_emulator' | ||
|
||
stdout = path / 'stdout.log' | ||
stderr = path / 'stderr.log' | ||
logcat = path / 'logcat.log' | ||
|
||
for p in (stdout, stderr, logcat): | ||
self.assertTrue(p.exists()) | ||
self.assertTrue(p.is_file()) | ||
|
||
# both emulator and wait-for-device write to stdout.log | ||
self._assert_file_contains( | ||
stdout, './emulator -avd test {}\n./adb wait-for-device\n'.format( | ||
' '.join(EMULATOR_FLAGS))) | ||
|
||
# emulator writes to stderr.log | ||
self._assert_file_contains(stderr, 'stderr\n' * 2) | ||
|
||
# logccat writes to logcat.log | ||
self._assert_file_contains(logcat, './adb logcat\n') | ||
|
||
def _assert_file_contains(self, path, expected_contents): | ||
with path.open() as f: | ||
c = f.read() | ||
self.assertEqual(c, expected_contents) | ||
|
||
def _invoke_emulator(self, emulator_binary, adb_binary): | ||
emulator_stdin, emulator_channel = os.pipe() | ||
wait_stdin, wait_channel = os.pipe() | ||
logcat_stdin, logcat_channel = os.pipe() | ||
|
||
def handler(): | ||
|
||
with _emulator_handler( | ||
True, | ||
'_artifacts', | ||
emulator_binary=emulator_binary, | ||
adb_binary=adb_binary, | ||
emulator_stdin=emulator_stdin, | ||
wait_for_device_stdin=wait_stdin, | ||
logcat_stdin=logcat_stdin, | ||
): | ||
time.sleep(0.1) | ||
|
||
future = self.executor.submit(handler) | ||
return future, ProcessChannel(emulator_channel), ProcessChannel( | ||
wait_channel), ProcessChannel(logcat_channel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the double quotes should probably not encompass ellipsis. Currently, this will look like this:
Starting avd "test...", while we'd probably want Starting avd "test"...