Skip to content

Commit 4a2298b

Browse files
Andrew Lambcopybara-github
Andrew Lamb
authored andcommitted
Add subclasses of LocalDeviceEnvironment for ethernet connections.
- Subclasses call `adb connect` to set up device network connections. Specific subclasses provide the hostname for an environment, e.g. based off the Swarming bot id. Change-Id: Ia80ff695513c5ac1ec62f202eb0edfc545912fd5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6109218 Reviewed-by: Haiyang Pan <hypan@google.com> Reviewed-by: Andrew Grieve <agrieve@chromium.org> Auto-Submit: Andrew Lamb <andrewlamb@chromium.org> Commit-Queue: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/main@{#1399872} NOKEYCHECK=True GitOrigin-RevId: adb42e799e65b6378f683f663b7a48fa4420596d
1 parent 43a1bdc commit 4a2298b

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

android/pylib/base/environment_factory.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5+
import os
56

67
from pylib import constants
78
from pylib.local.device import local_device_environment
9+
from pylib.local.device import skylab_environment
10+
811
from pylib.local.machine import local_machine_environment
912

1013
try:
@@ -26,6 +29,18 @@ def CreateEnvironment(args, output_manager, error_func):
2629
raise RuntimeError('error_func must call exit inside.')
2730
return local_emulator_environment.LocalEmulatorEnvironment(
2831
args, output_manager, error_func)
32+
33+
if args.connect_over_ethernet:
34+
swarming_server = os.environ.get('SWARMING_SERVER', '')
35+
if skylab_environment.SWARMING_SERVER not in swarming_server:
36+
error_func(
37+
'connect-over-ethernet is only supported for tasks running on '
38+
f'{skylab_environment.SWARMING_SERVER}. '
39+
f'SWARMING_SERVER={swarming_server}')
40+
raise RuntimeError('error_func must call exit inside.')
41+
return skylab_environment.SkylabEnvironment(args, output_manager,
42+
error_func)
43+
2944
return local_device_environment.LocalDeviceEnvironment(
3045
args, output_manager, error_func)
3146
return local_machine_environment.LocalMachineEnvironment(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2024 The Chromium Authors
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import logging
6+
7+
from devil.android.sdk import adb_wrapper
8+
9+
from pylib.local.device import local_device_environment
10+
11+
12+
class LocalDeviceEthernetEnvironment(
13+
local_device_environment.LocalDeviceEnvironment):
14+
"""
15+
A subclass of LocalDeviceEnvironment for devices connected over ethernet.
16+
17+
This class cannot be instantiated. Subclasses should implement the
18+
GetDeviceHostname method, as this is specific to each environment.
19+
"""
20+
21+
def __init__(self, args, output_manager, error_func):
22+
super().__init__(args, output_manager, error_func)
23+
hostname = self.GetDeviceHostname()
24+
logging.info('connecting to %s', hostname)
25+
adb_wrapper.AdbWrapper.Connect(hostname)
26+
27+
def GetDeviceHostname(self):
28+
raise NotImplementedError
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2024 The Chromium Authors
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import os
6+
7+
from pylib.local.device import local_device_ethernet_environment
8+
9+
SWARMING_SERVER = 'chromeos-swarming.appspot.com'
10+
11+
12+
class SkylabEnvironment(
13+
local_device_ethernet_environment.LocalDeviceEthernetEnvironment):
14+
"""
15+
A subclass of LocalDeviceEthernetEnvironment for Skylab devices.
16+
"""
17+
18+
def GetDeviceHostname(self):
19+
"""Return the hostname based on the bot id.
20+
21+
Strips the first component of the bot id, e.g. 'cros-clank1' -> 'clank1'.
22+
23+
Gets the bot id from the SWARMING_BOT_ID envvar, see
24+
https://chromium.googlesource.com/infra/luci/luci-py/+/HEAD/appengine/swarming/doc/Magic-Values.md#bot-environment-variables.
25+
"""
26+
bot_id = os.environ.get('SWARMING_BOT_ID')
27+
if not bot_id:
28+
raise ValueError(
29+
"device_arg is 'swarming' but SWARMING_BOT_ID is not set")
30+
31+
return bot_id[bot_id.index("-") + 1:]

android/test_runner.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ def AddDeviceOptions(parser):
360360
'to the id of the main user on device. Only use when the main user is a '
361361
'secondary user, e.g. Android Automotive OS.')
362362

363+
parser.add_argument(
364+
'--connect-over-ethernet',
365+
action='store_true',
366+
help='Connect to devices over the network using "adb connect". Only '
367+
'supported when running on chromeos-swarming')
368+
363369

364370
def AddEmulatorOptions(parser):
365371
"""Adds emulator-specific options to |parser|."""

android/test_runner.pydeps

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ pylib/junit/junit_test_instance.py
181181
pylib/local/__init__.py
182182
pylib/local/device/__init__.py
183183
pylib/local/device/local_device_environment.py
184+
pylib/local/device/local_device_ethernet_environment.py
184185
pylib/local/device/local_device_gtest_run.py
185186
pylib/local/device/local_device_instrumentation_test_run.py
186187
pylib/local/device/local_device_monkey_test_run.py
187188
pylib/local/device/local_device_test_run.py
189+
pylib/local/device/skylab_environment.py
188190
pylib/local/emulator/__init__.py
189191
pylib/local/emulator/avd.py
190192
pylib/local/emulator/ini.py

0 commit comments

Comments
 (0)