Repository navigation
Add emulator and Android browsers to autowebcompat tools #7495
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
Open
Open
Changes from all commits
Commits
Show all changes
2 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
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,12 @@ | ||
| # Use with compose.yml on an x86_64 Linux host with /dev/kvm. | ||
| # Set KVM_GID to the numeric group owning /dev/kvm on the host. | ||
| services: | ||
| autowebcompat-repro-agent: | ||
| platform: linux/amd64 | ||
| build: | ||
| target: android-agent | ||
| devices: | ||
| - /dev/kvm:/dev/kvm | ||
| group_add: | ||
| - "${KVM_GID:?Set KVM_GID to the group ID owning /dev/kvm}" | ||
| init: true | ||
153 changes: 153 additions & 0 deletions
153
libs/autowebcompat-tools/autowebcompat_tools/android_emulator.py
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,153 @@ | ||
| import logging | ||
| import subprocess | ||
| import time | ||
| from collections.abc import Iterator | ||
| from contextlib import contextmanager | ||
| from functools import cache | ||
| from pathlib import Path | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| AVD_NAME = "webcompat" | ||
| EMULATOR_LOG = Path("/tmp/android-emulator.log") | ||
| DEVICE_SERIAL = "emulator-5554" | ||
| BOOT_TIMEOUT = 300 | ||
| ADB_TIMEOUT = 120 | ||
|
|
||
|
|
||
| class AndroidError(Exception): | ||
| """Raised when the emulator or a device operation fails.""" | ||
|
|
||
|
|
||
| def adb(*args: str, check: bool = True, timeout: int = ADB_TIMEOUT) -> str: | ||
| proc = subprocess.run( | ||
| ["adb", "-s", DEVICE_SERIAL, *args], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=timeout, | ||
| ) | ||
| if check and proc.returncode != 0: | ||
| raise AndroidError( | ||
| f"adb {' '.join(args)} failed ({proc.returncode}): " | ||
| f"{proc.stderr.strip() or proc.stdout.strip()}" | ||
| ) | ||
| return proc.stdout | ||
|
|
||
|
|
||
| def getprop(name: str) -> str: | ||
| return adb("shell", "getprop", name, check=False).strip() | ||
|
|
||
|
|
||
| def install_apk(apk: Path) -> None: | ||
| """Install an APK, allowing replacement and downgrades.""" | ||
| logger.info("installing %s", apk.name) | ||
| adb("install", "-r", "-d", str(apk), timeout=600) | ||
|
|
||
|
|
||
| @cache | ||
| def launcher_activity(package: str) -> str: | ||
| """Resolve a package's launcher component; ``am start -p`` can't resolve Fenix's.""" | ||
| output = adb( | ||
| "shell", | ||
| "cmd", | ||
| "package", | ||
| "query-activities", | ||
| "--components", | ||
| "-a", | ||
| "android.intent.action.MAIN", | ||
| "-c", | ||
| "android.intent.category.LAUNCHER", | ||
| "-p", | ||
| package, | ||
| ) | ||
| for line in output.split(): | ||
| if line.startswith(f"{package}/"): | ||
| return line | ||
| raise AndroidError(f"No launcher activity found for {package}: {output.strip()}") | ||
|
|
||
|
|
||
| def am_start(*args: str) -> None: | ||
| """Start an activity and wait until it has launched.""" | ||
| output = adb("shell", "am", "start", "-W", *args) | ||
| if "Error:" in output or "Error type" in output: | ||
| raise AndroidError(f"am start {' '.join(args)} failed: {output.strip()}") | ||
|
|
||
|
|
||
| def start_app(package: str, url: str | None = None) -> None: | ||
| """Launch an app or bring it to the foreground, optionally at a URL.""" | ||
| intent = ( | ||
| ["-a", "android.intent.action.VIEW", "-d", url] | ||
| if url is not None | ||
| else [ | ||
| "-a", | ||
| "android.intent.action.MAIN", | ||
| "-c", | ||
| "android.intent.category.LAUNCHER", | ||
| ] | ||
| ) | ||
| am_start("-n", launcher_activity(package), *intent) | ||
|
|
||
|
|
||
| def wait_for_boot(process: subprocess.Popen, timeout: int = BOOT_TIMEOUT) -> None: | ||
| """Block until the device reports a completed boot.""" | ||
| deadline = time.monotonic() + timeout | ||
| while time.monotonic() < deadline: | ||
| if process.poll() is not None: | ||
| raise AndroidError( | ||
| f"emulator exited with {process.returncode} before booting" | ||
| ) | ||
| if getprop("sys.boot_completed") == "1": | ||
| logger.info("emulator booted") | ||
| return | ||
| time.sleep(5) | ||
| raise AndroidError( | ||
| f"emulator did not boot within {timeout}s; adb devices: " | ||
| f"{adb('devices', check=False, timeout=10).strip()}" | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def emulator(logcat: Path | None = None) -> Iterator[None]: | ||
| """Boot the baked AVD for the duration of the block.""" | ||
| # Create adb's key before boot so the emulator trusts it; otherwise the | ||
| # device shows as "unauthorized". | ||
| subprocess.run(["adb", "start-server"], check=True, capture_output=True, timeout=30) | ||
| cmd = [ | ||
| "emulator", | ||
| "-avd", | ||
| AVD_NAME, | ||
| "-no-boot-anim", | ||
| "-no-snapshot", | ||
| "-accel", | ||
| "on", | ||
| "-gpu", | ||
| "swiftshader", | ||
| "-camera-back", | ||
| "emulated", | ||
| "-camera-front", | ||
| "emulated", | ||
| ] | ||
| logger.info("starting emulator %s", AVD_NAME) | ||
| with EMULATOR_LOG.open("w") as log: | ||
| process = subprocess.Popen(cmd, stdout=log, stderr=subprocess.STDOUT) | ||
| try: | ||
| try: | ||
| wait_for_boot(process) | ||
| except Exception: | ||
| logger.error( | ||
| "Emulator log tail:\n%s", | ||
| EMULATOR_LOG.read_text(errors="replace")[-4000:], | ||
| ) | ||
| raise | ||
| yield | ||
| finally: | ||
| if logcat is not None and process.poll() is None: | ||
| try: | ||
| logcat.write_text(adb("logcat", "-d", check=False, timeout=60)) | ||
| except (subprocess.SubprocessError, OSError) as e: | ||
| logger.warning("Could not save logcat: %s", e) | ||
| process.terminate() | ||
| try: | ||
| process.wait(timeout=30) | ||
| except subprocess.TimeoutExpired: | ||
| process.kill() | ||
| process.wait() |
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.
I tested this in a gcp vm with nested virtualization enabled:
KVM_GID=$(stat -c %g /dev/kvm) BUG_ID=2085742 RUN_ID=2085742-android-1 docker compose -f agents/autowebcompat-repro/compose.yml -f agents/autowebcompat-repro/compose.android.yml up --build --force-recreate --abort-on-container-exitWe discussed that on production we'll need to use Cloud Batch instead of Cloud Run, so this depends on #6638.