| 1 |
.. _overview: |
| 2 |
|
| 3 |
Overview |
| 4 |
======== |
| 5 |
|
| 6 |
Architecture |
| 7 |
------------ |
| 8 |
|
| 9 |
labgrid can be used in several ways: |
| 10 |
|
| 11 |
- on the command line to control individual embedded systems during development |
| 12 |
("board farm") |
| 13 |
- via a pytest plugin to automate testing of embedded systems |
| 14 |
- as a python library in other programs |
| 15 |
|
| 16 |
In the labgrid library, a controllable embedded system is represented as a |
| 17 |
:ref:`Target <usage-target>`. |
| 18 |
Targets normally have several :ref:`Resource <overview-resources>` and |
| 19 |
:any:`Driver <overview-drivers-protocols>` objects, which are used to store the |
| 20 |
board-specific information and to implement actions on different abstraction |
| 21 |
levels. |
| 22 |
For cases where a board needs to be transitioned to specific states (such as |
| 23 |
off, in bootloader, in Linux shell), a :ref:`Strategy <overview-strategies>` (a |
| 24 |
special kind of Driver) can be added to the Target. |
| 25 |
|
| 26 |
While labgrid comes with implementations for some resources, drivers and |
| 27 |
strategies, custom implementations for these can be registered at runtime. |
| 28 |
It is expected that for complex use-cases, the user would implement and |
| 29 |
register a custom Strategy and possibly some higher-level Drivers. |
| 30 |
|
| 31 |
.. _overview-resources: |
| 32 |
|
| 33 |
Resources |
| 34 |
~~~~~~~~~ |
| 35 |
|
| 36 |
*Resources* are passive and only store the information to access the |
| 37 |
corresponding part of the :ref:`Target <usage-target>`. |
| 38 |
Typical examples of resources are :any:`RawSerialPort`, :any:`NetworkPowerPort` |
| 39 |
and :any:`AndroidUSBFastboot`. |
| 40 |
|
| 41 |
An important type of :any:`Resources <Resource>` are :any:`ManagedResources <ManagedResource>`. |
| 42 |
While normal Resources are always considered available for use and have fixed |
| 43 |
properties (such as the ``/dev/ttyUSB0`` device name for a |
| 44 |
:any:`RawSerialPort`), the ManagedResources are used to represent interfaces |
| 45 |
which are discoverable in some way. |
| 46 |
They can appear/disappear at runtime and have different properties each time |
| 47 |
they are discovered. |
| 48 |
The most common examples of ManagedResources are the various USB resources |
| 49 |
discovered using udev, such as :any:`USBSerialPort`, :any:`IMXUSBLoader` or |
| 50 |
:any:`AndroidUSBFastboot` (see the :ref:`udev matching section <udev-matching>`). |
| 51 |
|
| 52 |
.. _overview-drivers-protocols: |
| 53 |
|
| 54 |
Drivers and Protocols |
| 55 |
~~~~~~~~~~~~~~~~~~~~~ |
| 56 |
|
| 57 |
A labgrid *Driver* uses one (or more) :any:`Resources <overview-resources>` |
| 58 |
and/or other, lower-level Drivers to perform a set of actions on a |
| 59 |
:ref:`Target <usage-target>`. |
| 60 |
For example, the :any:`NetworkPowerDriver` uses a :any:`NetworkPowerPort` |
| 61 |
resource to control the Target's power supply. |
| 62 |
In this case, the actions are "on", "off", "cycle" and "get". |
| 63 |
|
| 64 |
As another example, the :any:`ShellDriver` uses any driver implementing the |
| 65 |
:any:`ConsoleProtocol` (such as a :any:`SerialDriver`, see below). |
| 66 |
The ConsoleProtocol allows the ShellDriver to work with any specific method |
| 67 |
of accessing the board's console (locally via USB, over the network using a |
| 68 |
console server or even an external program). |
| 69 |
At the ConsoleProtocol level, characters are sent to and received from the |
| 70 |
target, but they are not yet interpreted as specific commands or their output. |
| 71 |
|
| 72 |
The ShellDriver implements the higher-level :any:`CommandProtocol`, providing |
| 73 |
actions such as "run" or "run_check". |
| 74 |
Internally, it interacts with the Linux shell on the target board. |
| 75 |
For example, it: |
| 76 |
|
| 77 |
- waits for the login prompt |
| 78 |
- enters user name and password |
| 79 |
- runs the requested shell command (delimited by marker strings) |
| 80 |
- parses the output |
| 81 |
- retrieves the exit status |
| 82 |
|
| 83 |
Other drivers, such as the :any:`SSHDriver`, also implement the |
| 84 |
CommandProtocol. |
| 85 |
This way, higher-level code (such as a test suite), can be independent of the |
| 86 |
concrete control method on a given board. |
| 87 |
|
| 88 |
Binding and Activation |
| 89 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 90 |
|
| 91 |
When a :ref:`Target <usage-target>` is configured, each driver is "bound" to |
| 92 |
the resources (or other drivers) required by it. |
| 93 |
Each :any:`Driver` class has a "bindings" attribute, which declares which |
| 94 |
:any:`Resources <Resource>` or Protocols it needs and under which name they should be |
| 95 |
available to the Driver instance. |
| 96 |
The binding resolution is handled by the Target during the initial |
| 97 |
configuration and results in a directed, acyclic graph of resources and |
| 98 |
drivers. |
| 99 |
During the lifetime of a Target, the bindings are considered static. |
| 100 |
|
| 101 |
In most non-trivial target configurations, some drivers are mutually exclusive. |
| 102 |
For example, a Target may have both a :any:`ShellDriver` and a :any:`BareboxDriver`. |
| 103 |
Both bind to a driver implementing the :any:`ConsoleProtocol` and provide the |
| 104 |
:any:`CommandProtocol`. |
| 105 |
Obviously, the board cannot be in the bootloader and in Linux at the same time, |
| 106 |
which is represented in labgrid via the :any:`BindingState` (``bound``/``active``). |
| 107 |
If, during activation of a driver, any other driver in its bindings is not |
| 108 |
active, they will be activated as well. |
| 109 |
|
| 110 |
Activating and deactivating Drivers is also used to handle :any:`ManagedResources <ManagedResource>` |
| 111 |
becoming available/unavailable at runtime. |
| 112 |
If some resources bound to by the activating drivers are currently unavailable, |
| 113 |
the Target will wait for them to appear (with a per resource timeout). |
| 114 |
A realistic sequence of activation might look like this: |
| 115 |
|
| 116 |
- enable power (:any:`PowerProtocol.on`) |
| 117 |
- activate the :any:`IMXUSBDriver` driver on the target (this will wait for the |
| 118 |
:any:`IMXUSBLoader` resource to be available) |
| 119 |
- load the bootloader (:any:`BootstrapProtocol.load`) |
| 120 |
- activate the :any:`AndroidFastbootDriver` driver on the target (this will |
| 121 |
wait for the :any:`AndroidUSBFastboot` resource to be available) |
| 122 |
- boot the kernel (:any:`AndroidFastbootDriver.boot`) |
| 123 |
- activate the :any:`ShellDriver` driver on the target (this will wait for the |
| 124 |
:any:`USBSerialPort` resource to be available and log in) |
| 125 |
|
| 126 |
Any ManagedResources which become unavailable at runtime will automatically |
| 127 |
deactivate the dependent drivers. |
| 128 |
|
| 129 |
Multiple Drivers and Names |
| 130 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 |
|
| 132 |
Each driver and resource can have an optional name. This parameter is required |
| 133 |
for all manual creations of drivers and resources. To manually bind to a |
| 134 |
specific driver set a binding mapping before creating the driver: |
| 135 |
|
| 136 |
.. doctest:: |
| 137 |
|
| 138 |
>>> from labgrid import Target |
| 139 |
>>> from labgrid.resource import SerialPort |
| 140 |
>>> from labgrid.driver import SerialDriver |
| 141 |
>>> |
| 142 |
>>> t = Target("Test") |
| 143 |
>>> SerialPort(t, "First") |
| 144 |
SerialPort(target=Target(name='Test', env=None), name='First', state=<BindingState.bound: 1>, avail=True, port=None, speed=115200) |
| 145 |
>>> SerialPort(t, "Second") |
| 146 |
SerialPort(target=Target(name='Test', env=None), name='Second', state=<BindingState.bound: 1>, avail=True, port=None, speed=115200) |
| 147 |
>>> t.set_binding_map({"port": "Second"}) |
| 148 |
>>> sd = SerialDriver(t, "Driver") |
| 149 |
>>> sd |
| 150 |
SerialDriver(target=Target(name='Test', env=None), name='Driver', state=<BindingState.bound: 1>, txdelay=0.0, txchunk=1, timeout=3.0) |
| 151 |
>>> sd.port |
| 152 |
SerialPort(target=Target(name='Test', env=None), name='Second', state=<BindingState.bound: 1>, avail=True, port=None, speed=115200) |
| 153 |
|
| 154 |
Priorities |
| 155 |
~~~~~~~~~~ |
| 156 |
Each driver supports a priorities class variable. |
| 157 |
This allows drivers which implement the same protocol to add a priority option |
| 158 |
to each of their protocols. |
| 159 |
This way a :any:`NetworkPowerDriver` can implement the :any:`ResetProtocol`, but if another |
| 160 |
ResetProtocol driver with a higher priority is available, it will be selected |
| 161 |
instead. |
| 162 |
|
| 163 |
.. note:: |
| 164 |
Priority resolution only takes place if you have multiple drivers |
| 165 |
which implement the same protocol and you are not fetching them by |
| 166 |
name. |
| 167 |
|
| 168 |
The target resolves the driver priority via the Method Resolution Order (MRO) |
| 169 |
of the driver's base classes. |
| 170 |
If a base class has a ``priorities`` dictionary which contains the requested |
| 171 |
Protocol as a key, that priority is used. |
| 172 |
Otherwise, ``0`` is returned as the default priority. |
| 173 |
|
| 174 |
To set the priority of a protocol for a driver, add a class variable with the |
| 175 |
name ``priorities``, e.g. |
| 176 |
|
| 177 |
.. testcode:: |
| 178 |
|
| 179 |
import attr |
| 180 |
from labgrid.driver import Driver |
| 181 |
from labgrid.protocol import PowerProtocol, ResetProtocol |
| 182 |
|
| 183 |
@attr.s |
| 184 |
class NetworkPowerDriver(Driver, PowerProtocol, ResetProtocol): |
| 185 |
priorities = {PowerProtocol: -10} |
| 186 |
|
| 187 |
.. _overview-strategies: |
| 188 |
|
| 189 |
Strategies |
| 190 |
~~~~~~~~~~ |
| 191 |
|
| 192 |
Especially when using labgrid from pytest, explicitly controlling the board's |
| 193 |
boot process can distract from the individual test case. |
| 194 |
Each :any:`Strategy` implements the board- or project-specific actions necessary to |
| 195 |
transition from one state to another. |
| 196 |
labgrid includes the :any:`BareboxStrategy` and the :any:`UBootStrategy`, which |
| 197 |
can be used as-is for simple cases or serve as an example for implementing a |
| 198 |
custom strategy. |
| 199 |
|
| 200 |
Strategies themselves are not activated/deactivated. |
| 201 |
Instead, they control the states of the other drivers explicitly and execute |
| 202 |
actions to bring the target into the requested state. |
| 203 |
|
| 204 |
See the strategy example (``examples/strategy``) and the included strategies in |
| 205 |
``labgrid/strategy`` for some more information. |
| 206 |
|
| 207 |
For more information on the reasons behind labgrid's architecture, see |
| 208 |
:doc:`design_decisions`. |
| 209 |
|
| 210 |
.. _remote-resources-and-places: |
| 211 |
|
| 212 |
Remote Resources and Places |
| 213 |
--------------------------- |
| 214 |
|
| 215 |
labgrid contains components for accessing resources which are not directly |
| 216 |
accessible on the local machine. |
| 217 |
The main parts of this are: |
| 218 |
|
| 219 |
labgrid-coordinator |
| 220 |
Clients and exporters connect to the coordinator to publish resources, manage |
| 221 |
place configuration and handle mutual exclusion. |
| 222 |
|
| 223 |
:ref:`labgrid-exporter` (CLI) |
| 224 |
Exports explicitly configured local resources to the coordinator and monitors |
| 225 |
these for changes in availability or parameters. |
| 226 |
|
| 227 |
:ref:`labgrid-client` (CLI) |
| 228 |
Configures places (consisting of exported resources) and allows command line |
| 229 |
access to some actions (such as power control, bootstrap, fastboot and the |
| 230 |
console). |
| 231 |
|
| 232 |
RemotePlace (managed resource) |
| 233 |
When used in a :ref:`Target <usage-target>`, the RemotePlace expands to the |
| 234 |
resources configured for the named places. |
| 235 |
|
| 236 |
These components communicate over `gRPC <https://grpc.io/>`_. The coordinator |
| 237 |
acts as a gRPC server to which client and exporter connect. |
| 238 |
|
| 239 |
The following sections describe the responsibilities of each component. See |
| 240 |
:ref:`remote-usage` for usage information. |
| 241 |
|
| 242 |
.. _overview-coordinator: |
| 243 |
|
| 244 |
Coordinator |
| 245 |
~~~~~~~~~~~ |
| 246 |
|
| 247 |
The *Coordinator* is implemented as a gRPC server and is started as a separate |
| 248 |
process. |
| 249 |
It provides separate RPC methods for the exporters and clients. |
| 250 |
|
| 251 |
The coordinator keeps a list of all resources for clients and |
| 252 |
notifies them of changes as they occur. |
| 253 |
The resource access from clients does not pass through the coordinator, but is |
| 254 |
instead done directly from client to exporter, avoiding the need to specify new |
| 255 |
interfaces for each resource type. |
| 256 |
|
| 257 |
The coordinator also manages the registry of "places". |
| 258 |
These are used to configure which resources belong together from the user's |
| 259 |
point of view. |
| 260 |
A *place* can be a generic rack location, where different boards are connected |
| 261 |
to a static set of interfaces (resources such as power, network, serial |
| 262 |
console, …). |
| 263 |
|
| 264 |
Alternatively, a place can also be created for a specific board, for example |
| 265 |
when special interfaces such as GPIO buttons need to be controlled and they are |
| 266 |
not available in the generic locations. |
| 267 |
|
| 268 |
Each place can have aliases to simplify accessing a specific board (which might |
| 269 |
be moved between generic places). |
| 270 |
It also has a comment, which is used to store a short description of the |
| 271 |
connected board. |
| 272 |
|
| 273 |
To support selecting a specific place from a group containing similar or |
| 274 |
identical hardware, key-value tags can be added to places and used for |
| 275 |
scheduling. |
| 276 |
|
| 277 |
Finally, a place is configured with one or more *resource matches*. |
| 278 |
A resource match pattern has the format ``<exporter>/<group>/<class>/<name>``, |
| 279 |
where each component may be replaced with the wildcard ``*``. |
| 280 |
The ``/<name>`` part is optional and can be left out to match all resources of a class. |
| 281 |
|
| 282 |
Some commonly used match patterns are: |
| 283 |
|
| 284 |
\*/1001/\* |
| 285 |
Matches all resources in groups named 1001 from all exporters. |
| 286 |
|
| 287 |
\*/1001/NetworkPowerPort |
| 288 |
Matches only the NetworkPowerPort resource in groups named 1001 from all |
| 289 |
exporters. |
| 290 |
This is useful to exclude a NetworkSerialPort in group 1001 in cases where |
| 291 |
the serial console is connected somewhere else (such as via USB on a |
| 292 |
different exporter). |
| 293 |
|
| 294 |
exporter1/hub1-port1/\* |
| 295 |
Matches all resources exported from exporter1 in the group hub1-port1. |
| 296 |
This is an easy way to match several USB resources related to the same board |
| 297 |
(such as a USB ROM-Loader interface, Android fastboot and a USB serial gadget |
| 298 |
in Linux). |
| 299 |
|
| 300 |
To avoid conflicting access to the same resources, a place must be *acquired* |
| 301 |
before it is used and the coordinator also keeps track of which user on which |
| 302 |
client host has currently acquired the place. |
| 303 |
The resource matches are only evaluated while a place is being acquired and cannot be |
| 304 |
changed until it is *released* again. |
| 305 |
|
| 306 |
.. _overview-exporter: |
| 307 |
|
| 308 |
Exporter |
| 309 |
~~~~~~~~ |
| 310 |
An exporter registers all its configured resources when it connects to the |
| 311 |
:any:`Coordinator` and updates the resource parameters when they change (such as |
| 312 |
(dis-)connection of USB devices). |
| 313 |
Internally, the exporter uses the normal :any:`Resource` (and |
| 314 |
:any:`ManagedResource`) classes as the rest of labgrid. |
| 315 |
By using ManagedResources, availability and parameters for resources such as |
| 316 |
USB serial ports are tracked and sent to the coordinator. |
| 317 |
|
| 318 |
For some specific resources (such as :any:`USBSerialPorts <USBSerialPort>`), |
| 319 |
the exporter uses external tools to allow access by clients (``ser2net`` in the |
| 320 |
serial port case). |
| 321 |
|
| 322 |
Resources which do not need explicit support in the exporter, are just |
| 323 |
published as declared in the configuration file. |
| 324 |
This is useful to register externally configured resources such as network |
| 325 |
power switches or serial port servers with a labgrid coordinator. |
| 326 |
|
| 327 |
.. note:: |
| 328 |
Users will require SSH access to the exporter to access services and command |
| 329 |
line utilities. You also have to ensure that users can access usb devices for |
| 330 |
i.e. imx-usb-loader. To test a SSH jump to a device over the exporter outside |
| 331 |
of labgrid, ``ssh -J EXPORTER USER@DEVICE`` can be used. |
| 332 |
|
| 333 |
.. _overview-client: |
| 334 |
|
| 335 |
Client |
| 336 |
~~~~~~ |
| 337 |
The client requests the current lists of resources and places from the |
| 338 |
coordinator when it connects to it and then registers for change events. |
| 339 |
Most of its functionality is exposed via the ``labgrid-client`` CLI tool. |
| 340 |
It is also used by the :ref:`RemotePlace <overview-remoteplace>` resource (see below). |
| 341 |
|
| 342 |
Besides viewing the list of :ref:`Resources <overview-resources>`, the client is used to configure and |
| 343 |
access :any:`places <Place>` on the coordinator. |
| 344 |
For more information on using the CLI, see the manual page for |
| 345 |
:ref:`labgrid-client`. |
| 346 |
|
| 347 |
.. _overview-remoteplace: |
| 348 |
|
| 349 |
RemotePlace |
| 350 |
~~~~~~~~~~~ |
| 351 |
To use the resources configured for a :any:`Place` to control the corresponding |
| 352 |
board (whether in pytest or directly with the labgrid library), the |
| 353 |
:any:`RemotePlace` resource should be used. |
| 354 |
When a RemotePlace is configured for a :any:`Target`, it will create a client |
| 355 |
connection to the coordinator, create additional resource objects for those |
| 356 |
configured for that place and keep them updated at runtime. |
| 357 |
|
| 358 |
The additional resource objects can be bound to by drivers as normal and the |
| 359 |
drivers do not need to be aware that they were provided by the coordinator. |
| 360 |
For resource types which do not have an existing, network-transparent protocol |
| 361 |
(such as USB ROM loaders or JTAG interfaces), the driver needs to be aware of |
| 362 |
the mapping done by the exporter. |
| 363 |
|
| 364 |
For generic USB resources, the exporter for example maps a |
| 365 |
:any:`AndroidUSBFastboot` resource to a :any:`RemoteAndroidUSBFastboot` resource and |
| 366 |
adds a hostname property which needs to be used by the client to connect to the |
| 367 |
exporter. |
| 368 |
To avoid the need for additional remote access protocols and authentication, |
| 369 |
labgrid currently expects that the hosts are accessible via SSH and that any |
| 370 |
file names refer to a shared filesystem (such as NFS or SMB). |
| 371 |
|
| 372 |
.. note:: |
| 373 |
Using SSH's session sharing (``ControlMaster auto``, ``ControlPersist``, …) |
| 374 |
makes RemotePlaces easy to use even for exporters with require passwords or |
| 375 |
more complex login procedures. |
| 376 |
|
| 377 |
For exporters which are not directly accessible via SSH, add the host to your |
| 378 |
.ssh/config file, with a ProxyCommand when need. |
| 379 |
|
| 380 |
.. _overview-proxy-mechanism: |
| 381 |
|
| 382 |
Proxy Mechanism |
| 383 |
~~~~~~~~~~~~~~~ |
| 384 |
|
| 385 |
Both client and exporter support the proxy mechanism which uses SSH to tunnel |
| 386 |
connections to a remote host. To enable and force proxy mode on the exporter use |
| 387 |
the :code:`-i` or :code:`--isolated` command line option. This indicates to clients that all |
| 388 |
connections to remote resources made available by this exporter need to be |
| 389 |
tunneled using a SSH connection. |
| 390 |
|
| 391 |
On the other hand, clients may need to access the remote coordinator |
| 392 |
infrastructure using a SSH tunnel. In this case the :code:`LG_PROXY` environment |
| 393 |
variable needs to be set to the remote host which should tunnel the connection |
| 394 |
to the coordinator. The client then forwards all network traffic - |
| 395 |
client-to-coordinator and client-to-exporter - through SSH, via their |
| 396 |
respective proxies. This means that with :code:`LG_PROXY` and |
| 397 |
:code:`LG_COORDINATOR` labgrid can be used fully remotely with only a SSH |
| 398 |
connection as a requirement. |
| 399 |
|
| 400 |
.. note:: |
| 401 |
Labgrid prefers to connect to an exporter-defined proxy over using the |
| 402 |
LG_PROXY variable. This means that a correct entry for the exporter needs to |
| 403 |
be set up in the ~/.ssh/config file. You can view exporter proxies with |
| 404 |
:code:`labgrid-client -v resources`. |
| 405 |
|
| 406 |
One remaining issue here is the forward of UDP connections, which is currently |
| 407 |
not possible. UDP connections are used by some of the power backends in the |
| 408 |
form of SNMP. |