| 1 |
Usage |
| 2 |
===== |
| 3 |
|
| 4 |
.. _remote-usage: |
| 5 |
|
| 6 |
Remote Access |
| 7 |
------------- |
| 8 |
|
| 9 |
As described in :ref:`remote-resources-and-places`, one of labgrid's main |
| 10 |
features is granting access to boards connected to other hosts transparent for |
| 11 |
the client. |
| 12 |
To get started with remote access, take a look at |
| 13 |
:ref:`remote-getting-started`. |
| 14 |
|
| 15 |
Place Scheduling |
| 16 |
~~~~~~~~~~~~~~~~ |
| 17 |
|
| 18 |
When sharing places between developers or with CI jobs, it soon becomes |
| 19 |
necessary to manage who can access which places. |
| 20 |
Developers often just need any place which has one of a group of identical |
| 21 |
devices, while CI jobs should wait until the necessary place is free instead of |
| 22 |
failing. |
| 23 |
|
| 24 |
To support these use-cases, the coordinator has support for reserving places by |
| 25 |
using a tag filter and an optional priority. |
| 26 |
First, the places have to be tagged with the relevant key-value pairs: |
| 27 |
|
| 28 |
.. code-block:: bash |
| 29 |
|
| 30 |
$ labgrid-client -p board-1 set-tags board=imx6-foo |
| 31 |
$ labgrid-client -p board-2 set-tags board=imx6-foo |
| 32 |
$ labgrid-client -p board-3 set-tags board=imx8m-bar |
| 33 |
$ labgrid-client -v places |
| 34 |
Place 'board-1': |
| 35 |
tags: bar=baz, board=imx6-foo, jlu=2, rcz=1 |
| 36 |
matches: |
| 37 |
rl-test/Testport1/NetworkSerialPort |
| 38 |
… |
| 39 |
Place 'board-2': |
| 40 |
tags: board=imx6-foo |
| 41 |
matches: |
| 42 |
rl-test/Testport2/NetworkSerialPort |
| 43 |
… |
| 44 |
Place 'board-3': |
| 45 |
tags: board=imx8m-bar |
| 46 |
matches: |
| 47 |
rl-test/Testport3/NetworkSerialPort |
| 48 |
… |
| 49 |
|
| 50 |
Now, if you want to access any ``imx6-foo`` board, you could find that all are |
| 51 |
already in use by someone else: |
| 52 |
|
| 53 |
.. code-block:: bash |
| 54 |
|
| 55 |
$ labgrid-client who |
| 56 |
User Host Place Changed |
| 57 |
rcz dude board-1 2019-08-06 12:14:38.446201 |
| 58 |
jenkins worker1 board-2 2019-08-06 12:52:44.762131 |
| 59 |
|
| 60 |
In this case, you can create a reservation. |
| 61 |
You can specify any custom tags as part of the filter, as well as |
| 62 |
``name=<place-name>`` to select only a specific place (even if it has no custom |
| 63 |
tags). |
| 64 |
|
| 65 |
.. code-block:: bash |
| 66 |
|
| 67 |
$ labgrid-client reserve board=imx6-foo |
| 68 |
Reservation 'SP37P5OQRU': |
| 69 |
owner: rettich/jlu |
| 70 |
token: SP37P5OQRU |
| 71 |
state: waiting |
| 72 |
filters: |
| 73 |
main: board=imx6-foo |
| 74 |
created: 2019-08-06 12:56:49.779982 |
| 75 |
timeout: 2019-08-06 12:57:49.779983 |
| 76 |
|
| 77 |
As soon as any matching place becomes free, the reservation state will change |
| 78 |
from ``waiting`` to ``allocated``. |
| 79 |
Then, you can use the reservation token prefixed by ``+`` to refer to the |
| 80 |
allocated place for locking and usage. |
| 81 |
While a place is allocated for a reservation, only the owner of the reservation |
| 82 |
can lock that place. |
| 83 |
|
| 84 |
|
| 85 |
.. code-block:: bash |
| 86 |
|
| 87 |
$ labgrid-client wait SP37P5OQRU |
| 88 |
owner: rettich/jlu |
| 89 |
token: SP37P5OQRU |
| 90 |
state: waiting |
| 91 |
filters: |
| 92 |
main: board=imx6-foo |
| 93 |
created: 2019-08-06 12:56:49.779982 |
| 94 |
timeout: 2019-08-06 12:58:14.900621 |
| 95 |
… |
| 96 |
owner: rettich/jlu |
| 97 |
token: SP37P5OQRU |
| 98 |
state: allocated |
| 99 |
filters: |
| 100 |
main: board=imx6-foo |
| 101 |
allocations: |
| 102 |
main: board-2 |
| 103 |
created: 2019-08-06 12:56:49.779982 |
| 104 |
timeout: 2019-08-06 12:58:46.145851 |
| 105 |
$ labgrid-client -p +SP37P5OQRU lock |
| 106 |
acquired place board-2 |
| 107 |
$ labgrid-client reservations |
| 108 |
Reservation 'SP37P5OQRU': |
| 109 |
owner: rettich/jlu |
| 110 |
token: SP37P5OQRU |
| 111 |
state: acquired |
| 112 |
filters: |
| 113 |
main: board=imx6-foo |
| 114 |
allocations: |
| 115 |
main: board-2 |
| 116 |
created: 2019-08-06 12:56:49.779982 |
| 117 |
timeout: 2019-08-06 12:59:11.840780 |
| 118 |
$ labgrid-client -p +SP37P5OQRU console |
| 119 |
|
| 120 |
When using reservation in a CI job or to save some typing, the ``labgrid-client |
| 121 |
reserve`` command supports a ``--shell`` command to print code for evaluating |
| 122 |
in the shell. |
| 123 |
This sets the ``LG_TOKEN`` environment variable, which is then automatically |
| 124 |
used by ``wait`` and expanded via ``-p +``. |
| 125 |
|
| 126 |
.. code-block:: bash |
| 127 |
|
| 128 |
$ eval `labgrid-client reserve --shell board=imx6-foo` |
| 129 |
$ echo $LG_TOKEN |
| 130 |
ZDMZJZNLBF |
| 131 |
$ labgrid-client wait |
| 132 |
owner: rettich/jlu |
| 133 |
token: ZDMZJZNLBF |
| 134 |
state: waiting |
| 135 |
filters: |
| 136 |
main: board=imx6-foo |
| 137 |
created: 2019-08-06 13:05:30.987072 |
| 138 |
timeout: 2019-08-06 13:06:44.629736 |
| 139 |
… |
| 140 |
owner: rettich/jlu |
| 141 |
token: ZDMZJZNLBF |
| 142 |
state: allocated |
| 143 |
filters: |
| 144 |
main: board=imx6-foo |
| 145 |
allocations: |
| 146 |
main: board-1 |
| 147 |
created: 2019-08-06 13:05:30.987072 |
| 148 |
timeout: 2019-08-06 13:06:56.196684 |
| 149 |
$ labgrid-client -p + lock |
| 150 |
acquired place board-1 |
| 151 |
$ labgrid-client -p + show |
| 152 |
Place 'board-1': |
| 153 |
tags: bar=baz, board=imx6-foo, jlu=2, rcz=1 |
| 154 |
matches: |
| 155 |
rettich/Testport1/NetworkSerialPort |
| 156 |
acquired: rettich/jlu |
| 157 |
acquired resources: |
| 158 |
created: 2019-07-29 16:11:52.006269 |
| 159 |
changed: 2019-08-06 13:06:09.667682 |
| 160 |
reservation: ZDMZJZNLBF |
| 161 |
|
| 162 |
Finally, to avoid calling the ``wait`` command explicitly, you can add |
| 163 |
``--wait`` to the ``reserve`` command, so it waits until the reservation is |
| 164 |
allocated before returning. |
| 165 |
|
| 166 |
A reservation will time out after a short time, if it is neither refreshed nor |
| 167 |
used by locked places. |
| 168 |
|
| 169 |
Library |
| 170 |
------- |
| 171 |
labgrid can be used directly as a Python library, without the infrastructure |
| 172 |
provided by the pytest plugin. |
| 173 |
|
| 174 |
The labgrid library provides two ways to configure targets with resources and |
| 175 |
drivers: either create the :any:`Target` directly or use :any:`Environment` to |
| 176 |
load a configuration file. |
| 177 |
|
| 178 |
.. note:: |
| 179 |
On exit of your script/application, labgrid will call ``cleanup()`` on the |
| 180 |
targets using the python atexit module. |
| 181 |
|
| 182 |
.. _usage-target: |
| 183 |
|
| 184 |
Targets |
| 185 |
~~~~~~~ |
| 186 |
|
| 187 |
.. note:: |
| 188 |
In most cases it is easier to :ref:`use a complete environment from a YAML |
| 189 |
file <usage_environments>` instead of manually creating and activating objects. |
| 190 |
Nevertheless, we explain this in the following to clarify the underlying concepts, |
| 191 |
and how to work with targets on a lower level, e.g. in strategies. |
| 192 |
|
| 193 |
At the lower level, a :any:`Target` can be created directly: |
| 194 |
|
| 195 |
.. doctest:: |
| 196 |
|
| 197 |
>>> from labgrid import Target |
| 198 |
>>> t = Target('example') |
| 199 |
|
| 200 |
Next, any required :any:`Resource` objects can be created, which each represent |
| 201 |
a piece of hardware to be used with labgrid: |
| 202 |
|
| 203 |
.. doctest:: |
| 204 |
|
| 205 |
>>> from labgrid.resource import RawSerialPort |
| 206 |
>>> rsp = RawSerialPort(t, name=None, port='/dev/ttyUSB0') |
| 207 |
|
| 208 |
.. note:: |
| 209 |
Since we support multiple drivers of the same type, resources and drivers |
| 210 |
have a required ``name`` attribute. If you don't use multiple drivers of the |
| 211 |
same type, you can set the name to ``None``. |
| 212 |
|
| 213 |
Further on, a :any:`Driver` encapsulates logic how to work with resources. |
| 214 |
Drivers need to be created on the :any:`Target`: |
| 215 |
|
| 216 |
.. doctest:: |
| 217 |
|
| 218 |
>>> from labgrid.driver import SerialDriver |
| 219 |
>>> sd = SerialDriver(t, name=None) |
| 220 |
|
| 221 |
As the :any:`SerialDriver` declares a binding to a :any:`SerialPort`, the target binds it |
| 222 |
to the resource object created above: |
| 223 |
|
| 224 |
.. doctest:: |
| 225 |
|
| 226 |
>>> sd.port |
| 227 |
RawSerialPort(target=Target(name='example', env=None), name=None, state=<BindingState.bound: 1>, avail=True, port='/dev/ttyUSB0', speed=115200) |
| 228 |
>>> sd.port is rsp |
| 229 |
True |
| 230 |
|
| 231 |
Driver Activation |
| 232 |
^^^^^^^^^^^^^^^^^ |
| 233 |
Before a bound driver can be used, it needs to be activated. |
| 234 |
During activation, the driver makes sure that all hardware represented by the |
| 235 |
resources it is bound to can be used, and, if necessary, it acquires the |
| 236 |
underlying hardware on the OS level. |
| 237 |
For example, activating a :any:`SerialDriver` makes sure that the hardware |
| 238 |
represented by its bound :any:`RawSerialPort` object (e.g. something like |
| 239 |
``/dev/ttyUSB0``) is available, and that it can only be used by labgrid and not |
| 240 |
by other applications while the :any:`SerialDriver` is activated. |
| 241 |
|
| 242 |
If we use a car analogy here, binding is the process of screwing the car parts |
| 243 |
together, and activation is igniting the engine. |
| 244 |
|
| 245 |
After activation, we can use the driver to do our work: |
| 246 |
|
| 247 |
.. testsetup:: driver-activation |
| 248 |
|
| 249 |
from labgrid.resource import RawSerialPort |
| 250 |
from labgrid.driver import SerialDriver |
| 251 |
from labgrid import Target |
| 252 |
|
| 253 |
t = Target('example') |
| 254 |
rsp = RawSerialPort(t, name=None, port='/dev/ttyUSB0') |
| 255 |
sd = SerialDriver(t, name=None) |
| 256 |
sd.serial.open = Mock() |
| 257 |
sd.serial.write = Mock(return_value=4) |
| 258 |
|
| 259 |
.. doctest:: driver-activation |
| 260 |
|
| 261 |
>>> t.activate(sd) |
| 262 |
>>> sd.write(b'test') |
| 263 |
4 |
| 264 |
|
| 265 |
If an underlying hardware resource is not available (or not available after a |
| 266 |
certain timeout, depending on the driver), the activation step will raise an |
| 267 |
exception, e.g.:: |
| 268 |
|
| 269 |
>>> t.activate(sd) |
| 270 |
Traceback (most recent call last): |
| 271 |
File "/usr/lib/python3.8/site-packages/serial/serialposix.py", line 288, in open |
| 272 |
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) |
| 273 |
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyUSB0' |
| 274 |
|
| 275 |
Active drivers can be accessed by class (any :any:`Driver <labgrid.driver>` or |
| 276 |
:any:`Protocol <labgrid.protocol>`) using some syntactic sugar: |
| 277 |
|
| 278 |
.. doctest:: |
| 279 |
|
| 280 |
>>> from labgrid import Target |
| 281 |
>>> from labgrid.driver.fake import FakeConsoleDriver |
| 282 |
>>> target = Target('main') |
| 283 |
>>> console = FakeConsoleDriver(target, 'console') |
| 284 |
>>> target.activate(console) |
| 285 |
>>> target[FakeConsoleDriver] |
| 286 |
FakeConsoleDriver(target=Target(name='main', env=None), name='console', state=<BindingState.active: 2>, txdelay=0.0, txchunk=1) |
| 287 |
>>> target[FakeConsoleDriver, 'console'] |
| 288 |
FakeConsoleDriver(target=Target(name='main', env=None), name='console', state=<BindingState.active: 2>, txdelay=0.0, txchunk=1) |
| 289 |
|
| 290 |
Driver Deactivation |
| 291 |
^^^^^^^^^^^^^^^^^^^ |
| 292 |
Driver deactivation works in a similar manner: |
| 293 |
|
| 294 |
.. testsetup:: driver-deactivation |
| 295 |
|
| 296 |
from labgrid import Target |
| 297 |
from labgrid.driver.fake import FakeConsoleDriver |
| 298 |
target = Target('main') |
| 299 |
console = FakeConsoleDriver(target, 'console') |
| 300 |
target.activate(console) |
| 301 |
|
| 302 |
.. doctest:: driver-deactivation |
| 303 |
|
| 304 |
>>> target.deactivate(console) |
| 305 |
[FakeConsoleDriver(target=Target(name='main', env=None), name='console', state=<BindingState.bound: 1>, txdelay=0.0, txchunk=1)] |
| 306 |
|
| 307 |
Drivers need to be deactivated in the following cases: |
| 308 |
|
| 309 |
* Some drivers have internal logic depending on the state of the target. |
| 310 |
For example, the :any:`ShellDriver` remembers whether it has already logged |
| 311 |
in to the shell. |
| 312 |
If the target reboots, e.g. through a hardware watchdog timeout, |
| 313 |
a power cycle, or by issuing a ``reboot`` command on the shell, |
| 314 |
the ShellDriver's internal state becomes outdated, |
| 315 |
and the ShellDriver needs to be deactivated and re-activated. |
| 316 |
|
| 317 |
* One of the driver's bound resources is required by another driver which is to |
| 318 |
be activated. |
| 319 |
For example, the :any:`ShellDriver` and the :any:`BareboxDriver` both |
| 320 |
require access to a :any:`SerialPort` resource. |
| 321 |
If both drivers are bound to the same resource object, labgrid will |
| 322 |
automatically deactivate the BareboxDriver when activating the ShellDriver. |
| 323 |
|
| 324 |
Target Cleanup |
| 325 |
^^^^^^^^^^^^^^ |
| 326 |
After you are done with the target, optionally call the cleanup method on your |
| 327 |
target. While labgrid registers an ``atexit`` handler to cleanup targets, this has |
| 328 |
the advantage that exceptions can be handled by your application: |
| 329 |
|
| 330 |
.. testsetup:: target-cleanup |
| 331 |
|
| 332 |
from labgrid import Target |
| 333 |
target = Target('main') |
| 334 |
|
| 335 |
.. doctest:: target-cleanup |
| 336 |
|
| 337 |
>>> try: |
| 338 |
... target.cleanup() |
| 339 |
... except Exception as e: |
| 340 |
... pass # your code here |
| 341 |
|
| 342 |
.. _usage_environments: |
| 343 |
|
| 344 |
Environments |
| 345 |
~~~~~~~~~~~~ |
| 346 |
In practice, it is often useful to separate the :ref:`Target <usage-target>` configuration from the |
| 347 |
code which needs to control the board (such as a test case or installation |
| 348 |
script). |
| 349 |
For this use-case, labgrid can construct targets from a configuration file in |
| 350 |
YAML format: |
| 351 |
|
| 352 |
.. code-block:: yaml |
| 353 |
:name: example-env.yaml |
| 354 |
|
| 355 |
targets: |
| 356 |
example: |
| 357 |
resources: |
| 358 |
RawSerialPort: |
| 359 |
port: '/dev/ttyUSB0' |
| 360 |
drivers: |
| 361 |
SerialDriver: {} |
| 362 |
|
| 363 |
To parse this configuration file, use the :any:`Environment` class: |
| 364 |
|
| 365 |
.. doctest:: |
| 366 |
|
| 367 |
>>> from labgrid import Environment |
| 368 |
>>> env = Environment('example-env.yaml') |
| 369 |
|
| 370 |
Using :any:`Environment.get_target`, the configured targets can be retrieved |
| 371 |
by name. |
| 372 |
Without an argument, ``get_target()`` would default to 'main': |
| 373 |
|
| 374 |
.. doctest:: |
| 375 |
|
| 376 |
>>> t = env.get_target('example') |
| 377 |
|
| 378 |
To access the target's console, the correct driver object can be found by using |
| 379 |
:any:`Target.get_driver`: |
| 380 |
|
| 381 |
.. testsetup:: get-driver |
| 382 |
|
| 383 |
from labgrid import Environment |
| 384 |
|
| 385 |
env = Environment('example-env.yaml') |
| 386 |
t = env.get_target('example') |
| 387 |
|
| 388 |
s = t.get_driver('SerialDriver', activate=False) |
| 389 |
s.serial.open = Mock() |
| 390 |
s.serial.write = Mock(return_value=4) |
| 391 |
|
| 392 |
.. doctest:: get-driver |
| 393 |
|
| 394 |
>>> cp = t.get_driver('ConsoleProtocol') |
| 395 |
>>> cp |
| 396 |
SerialDriver(target=Target(name='example', env=Environment(config_file='example-env.yaml')), name=None, state=<BindingState.active: 2>, txdelay=0.0, txchunk=1, timeout=3.0) |
| 397 |
>>> cp.write(b'test') |
| 398 |
4 |
| 399 |
|
| 400 |
When using the ``get_driver`` method, the driver is automatically activated. |
| 401 |
The driver activation will also wait for unavailable resources when needed. |
| 402 |
|
| 403 |
For more information on the environment configuration files and the usage of |
| 404 |
multiple drivers, see :ref:`configuration:Environment Configuration`. |
| 405 |
|
| 406 |
pytest Plugin |
| 407 |
------------- |
| 408 |
labgrid includes a `pytest <http://pytest.org>`_ plugin to simplify writing tests which |
| 409 |
involve embedded boards. |
| 410 |
The plugin is configured by providing an environment config file |
| 411 |
(via the --lg-env pytest option, or the LG_ENV environment variable) |
| 412 |
and automatically creates the targets described in the environment. |
| 413 |
|
| 414 |
These `pytest fixtures <http://docs.pytest.org/en/latest/fixture.html>`_ are provided: |
| 415 |
|
| 416 |
env (session scope) |
| 417 |
Used to access the :any:`Environment` object created from the configuration |
| 418 |
file. |
| 419 |
This is mostly used for defining custom fixtures at the test suite level. |
| 420 |
|
| 421 |
target (session scope) |
| 422 |
Used to access the 'main' :any:`Target` defined in the configuration file. |
| 423 |
|
| 424 |
strategy (session scope) |
| 425 |
Used to access the :any:`Strategy` configured in the 'main' :any:`Target`. |
| 426 |
If the Strategy enters broken state, all subsequent tests requesting it via |
| 427 |
this fixture will be skipped. |
| 428 |
See also :any:`never_retry` for an easy way to mark strategies broken on |
| 429 |
error. |
| 430 |
|
| 431 |
Command-Line Options |
| 432 |
~~~~~~~~~~~~~~~~~~~~ |
| 433 |
The pytest plugin also supports the verbosity argument of pytest: |
| 434 |
|
| 435 |
- ``-vv``: activates the step reporting feature, showing function parameters and/or results |
| 436 |
- ``-vvv``: activates debug logging |
| 437 |
|
| 438 |
This allows debugging during the writing of tests and inspection during test runs. |
| 439 |
|
| 440 |
Other labgrid-related pytest plugin options are: |
| 441 |
|
| 442 |
``--lg-env=LG_ENV`` |
| 443 |
Specify a labgrid environment config file. |
| 444 |
This is equivalent to labgrid-client's ``-c``/``--config``. |
| 445 |
|
| 446 |
``--lg-coordinator=COORDINATOR_ADDRESS`` |
| 447 |
Specify labgrid coordinator gRPC address as ``HOST[:PORT]``. |
| 448 |
Defaults to ``127.0.0.1:20408``. |
| 449 |
This is equivalent to labgrid-client's ``-x``/``--coordinator``. |
| 450 |
|
| 451 |
``--lg-log=[path to logfiles]`` |
| 452 |
Path to store console log file. |
| 453 |
If option is specified without path the current working directory is used. |
| 454 |
|
| 455 |
``--lg-colored-steps`` |
| 456 |
Previously enabled the ColoredStepReporter, which has been removed with the |
| 457 |
StepLogger introduction. |
| 458 |
Kept for compatibility reasons without effect. |
| 459 |
|
| 460 |
``--lg-initial-state=STATE_NAME`` |
| 461 |
Sets the Strategy's initial state. |
| 462 |
This is useful during development if the board is known to be in a defined |
| 463 |
state already. |
| 464 |
The Strategy used must implement the ``force()`` method. |
| 465 |
See the shipped :any:`ShellStrategy` for an example. |
| 466 |
|
| 467 |
``pytest --help`` shows these options in a separate *labgrid* section. |
| 468 |
|
| 469 |
Environment Variables |
| 470 |
~~~~~~~~~~~~~~~~~~~~~ |
| 471 |
|
| 472 |
LG_ENV |
| 473 |
^^^^^^ |
| 474 |
Behaves like ``LG_ENV`` for :doc:`labgrid-client <man/client>`. |
| 475 |
|
| 476 |
LG_PROXY |
| 477 |
^^^^^^^^ |
| 478 |
Specifies a SSH proxy host to be used for port forwards to access the |
| 479 |
coordinator. Network resources made available by the exporter will prefer their |
| 480 |
own proxy, and only fallback to LG_PROXY. |
| 481 |
|
| 482 |
See also :ref:`overview-proxy-mechanism`. |
| 483 |
|
| 484 |
|
| 485 |
Writing and Running Tests |
| 486 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 487 |
|
| 488 |
Getting Started: A Minimal Test |
| 489 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 490 |
As a minimal example, we have a target connected via a USB serial converter |
| 491 |
('/dev/ttyUSB0') and booted to the Linux shell. |
| 492 |
The following environment config file (``shell-example.yaml``) describes how to |
| 493 |
access this board: |
| 494 |
|
| 495 |
.. code-block:: yaml |
| 496 |
:name: shell-example.yaml |
| 497 |
|
| 498 |
targets: |
| 499 |
main: |
| 500 |
resources: |
| 501 |
RawSerialPort: |
| 502 |
port: '/dev/ttyUSB0' |
| 503 |
drivers: |
| 504 |
SerialDriver: {} |
| 505 |
ShellDriver: |
| 506 |
prompt: 'root@[\w-]+:[^ ]+ ' |
| 507 |
login_prompt: ' login: ' |
| 508 |
username: 'root' |
| 509 |
|
| 510 |
We then add the following test in a file called ``test_example.py``: |
| 511 |
|
| 512 |
.. code-block:: python |
| 513 |
:name: test_shell.py |
| 514 |
|
| 515 |
def test_echo(target): |
| 516 |
command = target.get_driver('CommandProtocol') |
| 517 |
result = command.run_check('echo OK') |
| 518 |
assert 'OK' in result |
| 519 |
|
| 520 |
To run this test, we simply execute pytest in the same directory with the |
| 521 |
environment config: |
| 522 |
|
| 523 |
.. testsetup:: pytest-example |
| 524 |
|
| 525 |
from labgrid.driver import SerialDriver, ShellDriver |
| 526 |
|
| 527 |
patch('serial.Serial').start() |
| 528 |
patch.object( |
| 529 |
SerialDriver, |
| 530 |
'_read', |
| 531 |
Mock(return_value=b'root@example:~ ') |
| 532 |
).start() |
| 533 |
patch.object( |
| 534 |
ShellDriver, |
| 535 |
'_run', |
| 536 |
Mock(return_value=(['OK'], [], 0)) |
| 537 |
).start() |
| 538 |
|
| 539 |
.. testcode:: pytest-example |
| 540 |
:hide: |
| 541 |
|
| 542 |
import pytest |
| 543 |
|
| 544 |
plugins = ['labgrid.pytestplugin'] |
| 545 |
pytest.main(['--lg-env', 'shell-example.yaml', 'test_shell.py'], plugins) |
| 546 |
|
| 547 |
.. testoutput:: pytest-example |
| 548 |
:hide: |
| 549 |
|
| 550 |
... 1 passed... |
| 551 |
|
| 552 |
.. code-block:: bash |
| 553 |
|
| 554 |
$ pytest --lg-env shell-example.yaml --verbose |
| 555 |
============================= test session starts ============================== |
| 556 |
platform linux -- Python 3.5.3, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 |
| 557 |
… |
| 558 |
collected 1 items |
| 559 |
|
| 560 |
test_example.py::test_echo PASSED |
| 561 |
=========================== 1 passed in 0.51 seconds =========================== |
| 562 |
|
| 563 |
pytest has automatically found the test case and executed it on the target. |
| 564 |
|
| 565 |
Reusing Setup Code with Custom Fixtures |
| 566 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 567 |
When writing many test cases which use the same driver, we can get rid of some |
| 568 |
common code by wrapping the :any:`CommandProtocol` in a fixture. |
| 569 |
As pytest always executes the ``conftest.py`` file in the test suite directory, |
| 570 |
we can define additional fixtures there: |
| 571 |
|
| 572 |
.. code-block:: python |
| 573 |
:name: conftest_fixture.py |
| 574 |
|
| 575 |
import pytest |
| 576 |
|
| 577 |
@pytest.fixture(scope='session') |
| 578 |
def command(target): |
| 579 |
return target.get_driver('CommandProtocol') |
| 580 |
|
| 581 |
With this fixture, we can simplify the ``test_example.py`` file to: |
| 582 |
|
| 583 |
.. code-block:: python |
| 584 |
:name: test_custom_fixture.py |
| 585 |
|
| 586 |
def test_echo(command): |
| 587 |
result = command.run_check('echo OK') |
| 588 |
assert 'OK' in result |
| 589 |
|
| 590 |
.. testcode:: pytest-example |
| 591 |
:hide: |
| 592 |
|
| 593 |
import pytest |
| 594 |
|
| 595 |
plugins = ['labgrid.pytestplugin', 'conftest_fixture'] |
| 596 |
pytest.main(['--lg-env', 'shell-example.yaml', 'test_custom_fixture.py'], plugins) |
| 597 |
|
| 598 |
.. testoutput:: pytest-example |
| 599 |
:hide: |
| 600 |
|
| 601 |
... 1 passed... |
| 602 |
|
| 603 |
Managing Target States with Strategy Fixtures |
| 604 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 605 |
When using a :any:`Strategy` to transition the target between states, it is |
| 606 |
useful to define a function scope fixture per state in ``conftest.py``: |
| 607 |
|
| 608 |
.. code-block:: python |
| 609 |
|
| 610 |
import pytest |
| 611 |
|
| 612 |
@pytest.fixture(scope='function') |
| 613 |
def switch_off(strategy, capsys): |
| 614 |
with capsys.disabled(): |
| 615 |
strategy.transition('off') |
| 616 |
|
| 617 |
@pytest.fixture(scope='function') |
| 618 |
def bootloader_command(target, strategy, capsys): |
| 619 |
with capsys.disabled(): |
| 620 |
strategy.transition('barebox') |
| 621 |
return target.get_active_driver('CommandProtocol') |
| 622 |
|
| 623 |
@pytest.fixture(scope='function') |
| 624 |
def shell_command(target, strategy, capsys): |
| 625 |
with capsys.disabled(): |
| 626 |
strategy.transition('shell') |
| 627 |
return target.get_active_driver('CommandProtocol') |
| 628 |
|
| 629 |
.. note:: |
| 630 |
The ``capsys.disabled()`` context manager is only needed when using the |
| 631 |
:any:`ManualPowerDriver`, as it will not be able to access the console |
| 632 |
otherwise. |
| 633 |
See the corresponding `pytest documentation for details |
| 634 |
<http://doc.pytest.org/en/latest/capture.html#accessing-captured-output-from-a-test-function>`_. |
| 635 |
|
| 636 |
With the fixtures defined above, switching between bootloader and Linux shells |
| 637 |
is easy: |
| 638 |
|
| 639 |
.. code-block:: python |
| 640 |
|
| 641 |
def test_barebox_initial(bootloader_command): |
| 642 |
stdout = bootloader_command.run_check('version') |
| 643 |
assert 'barebox' in '\n'.join(stdout) |
| 644 |
|
| 645 |
def test_shell(shell_command): |
| 646 |
stdout = shell_command.run_check('cat /proc/version') |
| 647 |
assert 'Linux' in stdout[0] |
| 648 |
|
| 649 |
def test_barebox_after_reboot(bootloader_command): |
| 650 |
bootloader_command.run_check('true') |
| 651 |
|
| 652 |
.. note:: |
| 653 |
The ``bootloader_command`` and ``shell_command`` fixtures use |
| 654 |
:any:`Target.get_active_driver` to get the currently active :any:`CommandProtocol` |
| 655 |
driver (either :any:`BareboxDriver` or :any:`ShellDriver`). |
| 656 |
Activation and deactivation of drivers is handled by the |
| 657 |
:ref:`BareboxStrategy <conf-bareboxstrategy>` in this example. |
| 658 |
|
| 659 |
The :ref:`Strategy <overview-strategies>` needs additional drivers to control the target. |
| 660 |
Adapt the following environment config file (``strategy-example.yaml``) to your |
| 661 |
setup: |
| 662 |
|
| 663 |
.. code-block:: yaml |
| 664 |
|
| 665 |
targets: |
| 666 |
main: |
| 667 |
resources: |
| 668 |
RawSerialPort: |
| 669 |
port: '/dev/ttyUSB0' |
| 670 |
drivers: |
| 671 |
ManualPowerDriver: |
| 672 |
name: 'example-board' |
| 673 |
SerialDriver: {} |
| 674 |
BareboxDriver: |
| 675 |
prompt: 'barebox@[^:]+:[^ ]+ ' |
| 676 |
ShellDriver: |
| 677 |
prompt: 'root@[\w-]+:[^ ]+ ' |
| 678 |
login_prompt: ' login: ' |
| 679 |
username: 'root' |
| 680 |
BareboxStrategy: {} |
| 681 |
|
| 682 |
For this example, you should get a report similar to this: |
| 683 |
|
| 684 |
.. code-block:: bash |
| 685 |
|
| 686 |
$ pytest --lg-env strategy-example.yaml -v --capture=no |
| 687 |
============================= test session starts ============================== |
| 688 |
platform linux -- Python 3.5.3, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 |
| 689 |
… |
| 690 |
collected 3 items |
| 691 |
|
| 692 |
test_strategy.py::test_barebox_initial |
| 693 |
main: CYCLE the target example-board and press enter |
| 694 |
PASSED |
| 695 |
test_strategy.py::test_shell PASSED |
| 696 |
test_strategy.py::test_barebox_after_reboot |
| 697 |
main: CYCLE the target example-board and press enter |
| 698 |
PASSED |
| 699 |
|
| 700 |
========================== 3 passed in 29.77 seconds =========================== |
| 701 |
|
| 702 |
.. _usage_pytestplugin_mark_lg_feature: |
| 703 |
|
| 704 |
@pytest.mark.lg_feature() |
| 705 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 706 |
labgrid supports :ref:`environment-configuration-feature-flags` in the |
| 707 |
:ref:`environment-configuration`. |
| 708 |
Adding a ``@pytest.mark.lg_feature()`` decorator to a test ensures it is only |
| 709 |
executed if the desired feature is set, either under the target or global |
| 710 |
``features:`` keys. |
| 711 |
|
| 712 |
.. code-block:: python |
| 713 |
|
| 714 |
import pytest |
| 715 |
|
| 716 |
@pytest.mark.lg_feature("camera") |
| 717 |
def test_camera(target): |
| 718 |
pass |
| 719 |
|
| 720 |
In case the feature is unavailable, pytest will record the missing feature |
| 721 |
as the skip reason. |
| 722 |
|
| 723 |
Tests requiring multiple features are also possible: |
| 724 |
|
| 725 |
.. code-block:: python |
| 726 |
|
| 727 |
import pytest |
| 728 |
|
| 729 |
@pytest.mark.lg_feature(["camera", "console"]) |
| 730 |
def test_camera(target): |
| 731 |
pass |
| 732 |
|
| 733 |
@pytest.mark.lg_xfail_feature() |
| 734 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 735 |
labgrid supports :ref:`environment-configuration-feature-flags` in the |
| 736 |
:ref:`environment-configuration`. |
| 737 |
pytest supports the ``xfail`` marker, see |
| 738 |
`pytest.mark.xfail() <https://docs.pytest.org/en/stable/reference/reference.html#pytest-mark-xfail>`_. |
| 739 |
|
| 740 |
When having more specific features, tests can be marked as ``xfail`` for a |
| 741 |
particular feature. |
| 742 |
|
| 743 |
Imagine two targets have ``camera`` feature flags. |
| 744 |
One of them has the additional ``special-camera-2000`` feature flag. |
| 745 |
The other has the additional ``special-camera-3000`` feature flag. |
| 746 |
Due to a known bug on ``special-camera-3000``, the test is expected to |
| 747 |
fail. |
| 748 |
The test can be marked as ``xfail`` for that feature: |
| 749 |
|
| 750 |
.. code-block:: python |
| 751 |
|
| 752 |
import pytest |
| 753 |
|
| 754 |
@pytest.mark.lg_feature("camera"]) |
| 755 |
@pytest.mark.lg_xfail_feature( |
| 756 |
"special-camera-3000", |
| 757 |
reason="known bug xy on special-camera-3000", |
| 758 |
raises=AssertionError, |
| 759 |
strict=True, |
| 760 |
) |
| 761 |
def test_camera(target): |
| 762 |
pass |
| 763 |
|
| 764 |
Features under the target and global ``features:`` keys are considered. |
| 765 |
|
| 766 |
``@pytest.mark.lg_xfail_feature(feature, **kwargs)``: |
| 767 |
- ``feature`` (str) - Feature that should mark the test as ``xfail``, passed |
| 768 |
as boolean ``condition=`` to ``pytest.mark.xfail()``. |
| 769 |
- ``**kwargs`` - All kw-only args are passed to ``pytest.mark.xfail()``. |
| 770 |
|
| 771 |
Test Reports |
| 772 |
~~~~~~~~~~~~ |
| 773 |
|
| 774 |
pytest-html |
| 775 |
^^^^^^^^^^^ |
| 776 |
With the `pytest-html plugin <https://pypi.python.org/pypi/pytest-html>`_, the |
| 777 |
test results can be converted directly to a single-page HTML report: |
| 778 |
|
| 779 |
.. code-block:: bash |
| 780 |
|
| 781 |
$ pip install pytest-html |
| 782 |
$ pytest --lg-env shell-example.yaml --html=report.html |
| 783 |
|
| 784 |
JUnit XML |
| 785 |
^^^^^^^^^ |
| 786 |
JUnit XML reports can be generated directly by pytest and are especially useful for |
| 787 |
use in CI systems such as `Jenkins <https://jenkins.io/>`_ with the `JUnit |
| 788 |
Plugin <https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Plugin>`_. |
| 789 |
|
| 790 |
They can also be converted to other formats, such as HTML with `junit2html tool |
| 791 |
<https://pypi.python.org/pypi/junit2html>`_: |
| 792 |
|
| 793 |
.. code-block:: bash |
| 794 |
|
| 795 |
$ pip install junit2html |
| 796 |
$ pytest --lg-env shell-example.yaml --junit-xml=report.xml |
| 797 |
$ junit2html report.xml |
| 798 |
|
| 799 |
|
| 800 |
labgrid adds additional xml properties to a test run, these are: |
| 801 |
|
| 802 |
- ENV_CONFIG: Name of the configuration file |
| 803 |
- TARGETS: List of target names |
| 804 |
- TARGET_{NAME}_REMOTE: optional, if the target uses a RemotePlace |
| 805 |
resource, its name is recorded here |
| 806 |
- PATH_{NAME}: optional, labgrid records the name and path |
| 807 |
- PATH_{NAME}_GIT_COMMIT: optional, labgrid tries to record git sha1 values for every |
| 808 |
path |
| 809 |
- IMAGE_{NAME}: optional, labgrid records the name and path to the image |
| 810 |
- IMAGE_{NAME}_GIT_COMMIT: optional, labgrid tries to record git sha1 values for every |
| 811 |
image |
| 812 |
|
| 813 |
Command-Line |
| 814 |
------------ |
| 815 |
|
| 816 |
labgrid contains some command line tools which are used for remote access to |
| 817 |
resources. |
| 818 |
See :doc:`man/client`, :doc:`man/device-config` and :doc:`man/exporter` for |
| 819 |
more information. |
| 820 |
|
| 821 |
Advanced CLI features |
| 822 |
~~~~~~~~~~~~~~~~~~~~~ |
| 823 |
|
| 824 |
This section of the manual describes advanced features that are supported by the |
| 825 |
labgrid client CLI. |
| 826 |
|
| 827 |
Sharing a place with a co-worker |
| 828 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 829 |
|
| 830 |
Labgrid client allows multiple people to access the same shared place, |
| 831 |
even though locks can only be acquired by one person. To allow a coworker to use |
| 832 |
a place use the ``allow`` command of labgrid client in conjunction with the |
| 833 |
coworkers ``user`` and ``hostname``. As an example, with a places named |
| 834 |
``example``, a user name ``john`` and a host named ``sirius``, the command looks |
| 835 |
like this: |
| 836 |
|
| 837 |
.. code-block:: bash |
| 838 |
|
| 839 |
$ labgrid-client -p example allow sirius/john |
| 840 |
|
| 841 |
To remove the allow it is currently necessary to unlock and lock the place. |