Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions agents/autowebcompat-repro/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,61 @@ COPY --from=builder /opt/venv /opt/venv

CMD ["python", "-m", "hackbot_agents.autowebcompat_repro"]

FROM agent AS android-base

USER root

ENV ANDROID_HOME=/opt/android-sdk \
ANDROID_USER_HOME=/home/agent/.android \
ANDROID_AVD_HOME=/home/agent/.android/avd
ENV PATH="/opt/android-sdk/platform-tools:/opt/android-sdk/emulator:$PATH"

ARG TARGETARCH
RUN test "$TARGETARCH" = amd64 \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
libpulse0 libglu1-mesa \
&& rm -rf /var/lib/apt/lists/*

FROM android-base AS android-sdk

ARG ANDROID_API=37.0
ARG CMDLINE_TOOLS_VERSION=15859902

ENV PATH="/opt/android-sdk/cmdline-tools/latest/bin:$PATH"

RUN apt-get update \
&& apt-get install -y --no-install-recommends default-jre-headless curl unzip \
&& rm -rf /var/lib/apt/lists/*

RUN set -eux; \
mkdir -p "$ANDROID_HOME/cmdline-tools"; \
curl --fail --location --retry 5 --retry-delay 5 --retry-max-time 300 \
--connect-timeout 30 --max-time 180 \
-o /tmp/cmdline-tools.zip \
"https://dl.google.com/android/repository/commandlinetools-linux-${CMDLINE_TOOLS_VERSION}_latest.zip"; \
unzip -q /tmp/cmdline-tools.zip -d "$ANDROID_HOME/cmdline-tools"; \
mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest"; \
rm /tmp/cmdline-tools.zip; \
yes | sdkmanager --licenses > /dev/null; \
image="system-images;android-${ANDROID_API};google_apis;x86_64"; \
sdkmanager --install "platform-tools" "emulator" "$image"; \
mkdir -p "$ANDROID_AVD_HOME"; \
echo no | avdmanager create avd --force --name "webcompat" \
--device "pixel_9" --package "$image"; \
printf 'disk.dataPartition.size=12G\nhw.ramSize=8192\nhw.keyboard=no\n' \
>> "$ANDROID_AVD_HOME/webcompat.avd/config.ini"

FROM android-base AS android-agent

# Java and the SDK command-line tools are only needed to prepare the AVD.
COPY --from=android-sdk /opt/android-sdk/platform-tools /opt/android-sdk/platform-tools
COPY --from=android-sdk /opt/android-sdk/system-images /opt/android-sdk/system-images
COPY --from=android-sdk --chown=agent:agent /opt/android-sdk/emulator /opt/android-sdk/emulator
COPY --from=android-sdk --chown=agent:agent /home/agent/.android /home/agent/.android

USER agent

FROM base AS broker

RUN useradd --create-home --shell /bin/bash broker
Expand Down
12 changes: 12 additions & 0 deletions agents/autowebcompat-repro/compose.android.yml
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}"

Copy link
Copy Markdown
Contributor Author

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-exit

We discussed that on production we'll need to use Cloud Batch instead of Cloud Run, so this depends on #6638.

init: true
153 changes: 153 additions & 0 deletions libs/autowebcompat-tools/autowebcompat_tools/android_emulator.py
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()