| 1 |
Development |
| 2 |
============ |
| 3 |
The first step is to install labgrid into a local virtualenv. |
| 4 |
|
| 5 |
Installation |
| 6 |
------------ |
| 7 |
|
| 8 |
Clone the git repository: |
| 9 |
|
| 10 |
.. code-block:: bash |
| 11 |
|
| 12 |
git clone https://github.com/labgrid-project/labgrid && cd labgrid |
| 13 |
|
| 14 |
Create and activate a virtualenv for labgrid: |
| 15 |
|
| 16 |
.. code-block:: bash |
| 17 |
|
| 18 |
virtualenv -p python3 venv |
| 19 |
source venv/bin/activate |
| 20 |
pip install --upgrade pip |
| 21 |
|
| 22 |
Install required dependencies: |
| 23 |
|
| 24 |
.. code-block:: bash |
| 25 |
|
| 26 |
sudo apt install python3-dev libow-dev |
| 27 |
|
| 28 |
Install labgrid with development dependencies into the virtualenv in editable |
| 29 |
mode: |
| 30 |
|
| 31 |
.. code-block:: bash |
| 32 |
|
| 33 |
pip install -e ".[dev]" |
| 34 |
|
| 35 |
Tests can now be run via: |
| 36 |
|
| 37 |
.. code-block:: bash |
| 38 |
|
| 39 |
python -m pytest |
| 40 |
|
| 41 |
Writing a Driver |
| 42 |
---------------- |
| 43 |
|
| 44 |
To develop a new driver for labgrid, you need to decide which protocol to |
| 45 |
implement, or implement your own protocol. |
| 46 |
If you are unsure about a new protocol's API, just use the driver directly from |
| 47 |
the client code, as deciding on a good API will be much easier when another |
| 48 |
similar driver is added. |
| 49 |
|
| 50 |
labgrid uses the `attrs library <https://attrs.readthedocs.io>`_ for internal |
| 51 |
classes. |
| 52 |
First of all import attr, the protocol and the common driver class |
| 53 |
into your new driver file. |
| 54 |
|
| 55 |
.. testcode:: |
| 56 |
|
| 57 |
import attr |
| 58 |
|
| 59 |
from labgrid.driver import Driver |
| 60 |
from labgrid.protocol import ConsoleProtocol |
| 61 |
|
| 62 |
Next, define your new class and list the protocols as subclasses of the new |
| 63 |
driver class. |
| 64 |
Try to avoid subclassing existing other drivers, as this limits the flexibility |
| 65 |
provided by connecting drivers and resources on a given target at runtime. |
| 66 |
|
| 67 |
.. testcode:: |
| 68 |
|
| 69 |
import attr |
| 70 |
|
| 71 |
from labgrid.driver import Driver |
| 72 |
from labgrid.protocol import ConsoleProtocol |
| 73 |
|
| 74 |
@attr.s(eq=False) |
| 75 |
class ExampleDriver(Driver, ConsoleProtocol): |
| 76 |
pass |
| 77 |
|
| 78 |
The ConsoleExpectMixin is a mixin class to add expect functionality to any |
| 79 |
class supporting the :any:`ConsoleProtocol` and has to be the first item in the |
| 80 |
subclass list. |
| 81 |
Using the mixin class allows sharing common code, which would otherwise need to |
| 82 |
be added into multiple drivers. |
| 83 |
|
| 84 |
.. testcode:: |
| 85 |
|
| 86 |
import attr |
| 87 |
|
| 88 |
from labgrid.driver import Driver |
| 89 |
from labgrid.driver.consoleexpectmixin import ConsoleExpectMixin |
| 90 |
from labgrid.protocol import ConsoleProtocol |
| 91 |
|
| 92 |
@attr.s(eq=False) |
| 93 |
class ExampleDriver(ConsoleExpectMixin, Driver, ConsoleProtocol): |
| 94 |
pass |
| 95 |
|
| 96 |
Additionally the driver needs to be registered with the :any:`target_factory` |
| 97 |
and provide a bindings dictionary, so that the :any:`Target` can resolve |
| 98 |
dependencies on other drivers or resources. |
| 99 |
|
| 100 |
.. testsetup:: example-driver1 |
| 101 |
|
| 102 |
from labgrid.factory import target_factory |
| 103 |
target_factory.all_classes.pop('ExampleDriver', None) |
| 104 |
|
| 105 |
.. testcode:: example-driver1 |
| 106 |
|
| 107 |
import attr |
| 108 |
|
| 109 |
from labgrid.factory import target_factory |
| 110 |
from labgrid.driver import Driver |
| 111 |
from labgrid.driver.consoleexpectmixin import ConsoleExpectMixin |
| 112 |
from labgrid.protocol import ConsoleProtocol |
| 113 |
|
| 114 |
@target_factory.reg_driver |
| 115 |
@attr.s(eq=False) |
| 116 |
class ExampleDriver(ConsoleExpectMixin, Driver, ConsoleProtocol): |
| 117 |
bindings = { "port": "SerialPort" } |
| 118 |
|
| 119 |
The listed resource :code:`SerialPort` will be bound to :code:`self.port`, |
| 120 |
making it usable in the class. |
| 121 |
Checks are performed that the target which the driver binds to has a SerialPort, |
| 122 |
otherwise an error will be raised. |
| 123 |
|
| 124 |
If your driver can support alternative resources, you can use a set of classes |
| 125 |
instead of a single class:: |
| 126 |
|
| 127 |
bindings = { "port": {"SerialPort", "NetworkSerialPort"} } |
| 128 |
|
| 129 |
Optional bindings can be declared by including ``None`` in the set:: |
| 130 |
|
| 131 |
bindings = { "port": {"SerialPort", "NetworkSerialPort", None} } |
| 132 |
|
| 133 |
If you need to do something during instantiation, you need to add a |
| 134 |
:code:`__attrs_post_init__` method (instead of the usual :code:`__init__` used |
| 135 |
for non-attr-classes). |
| 136 |
The minimum requirement is a call to :code:`super().__attrs_post_init__()`. |
| 137 |
|
| 138 |
.. testsetup:: example-driver2 |
| 139 |
|
| 140 |
from labgrid.factory import target_factory |
| 141 |
target_factory.all_classes.pop('ExampleDriver', None) |
| 142 |
|
| 143 |
.. testcode:: example-driver2 |
| 144 |
|
| 145 |
import attr |
| 146 |
|
| 147 |
from labgrid.factory import target_factory |
| 148 |
from labgrid.driver import Driver |
| 149 |
from labgrid.driver.consoleexpectmixin import ConsoleExpectMixin |
| 150 |
from labgrid.protocol import ConsoleProtocol |
| 151 |
|
| 152 |
@target_factory.reg_driver |
| 153 |
@attr.s(eq=False) |
| 154 |
class ExampleDriver(ConsoleExpectMixin, Driver, ConsoleProtocol): |
| 155 |
bindings = { "port": "SerialPort" } |
| 156 |
|
| 157 |
def __attrs_post_init__(self): |
| 158 |
super().__attrs_post_init__() |
| 159 |
|
| 160 |
All that's left now is to implement the functionality described by the used |
| 161 |
protocol, by using the API of the bound drivers and resources. |
| 162 |
|
| 163 |
Writing a Resource |
| 164 |
------------------- |
| 165 |
|
| 166 |
To add a new resource to labgrid, we import attr into our new resource file. |
| 167 |
Additionally we need the :any:`target_factory` and the common ``Resource`` class. |
| 168 |
|
| 169 |
.. testcode:: |
| 170 |
|
| 171 |
import attr |
| 172 |
|
| 173 |
from labgrid.factory import target_factory |
| 174 |
from labgrid.resource import Resource |
| 175 |
|
| 176 |
Next we add our own resource with the :code:`Resource` parent class and |
| 177 |
register it with the :any:`target_factory`. |
| 178 |
|
| 179 |
.. testcode:: |
| 180 |
|
| 181 |
import attr |
| 182 |
|
| 183 |
from labgrid.factory import target_factory |
| 184 |
from labgrid.resource import Resource |
| 185 |
|
| 186 |
@target_factory.reg_resource |
| 187 |
@attr.s(eq=False) |
| 188 |
class ExampleResource(Resource): |
| 189 |
pass |
| 190 |
|
| 191 |
All that is left now is to add attributes via :code:`attr.ib()` member |
| 192 |
variables. |
| 193 |
|
| 194 |
.. testsetup:: example-resource |
| 195 |
|
| 196 |
from labgrid.factory import target_factory |
| 197 |
target_factory.all_classes.pop('ExampleResource', None) |
| 198 |
|
| 199 |
.. testcode:: example-resource |
| 200 |
|
| 201 |
import attr |
| 202 |
|
| 203 |
from labgrid.factory import target_factory |
| 204 |
from labgrid.resource import Resource |
| 205 |
|
| 206 |
@target_factory.reg_resource |
| 207 |
@attr.s(eq=False) |
| 208 |
class ExampleResource(Resource): |
| 209 |
examplevar1 = attr.ib() |
| 210 |
examplevar2 = attr.ib() |
| 211 |
|
| 212 |
The :code:`attr.ib()` style of member definition also supports defaults and |
| 213 |
validators, see the `attrs documentation <https://attrs.readthedocs.io/en/stable/>`_. |
| 214 |
|
| 215 |
Writing a Strategy |
| 216 |
------------------ |
| 217 |
|
| 218 |
labgrid offers only basic strategies, for complex use cases a customized |
| 219 |
strategy is required. |
| 220 |
Start by creating a strategy skeleton: |
| 221 |
|
| 222 |
.. testcode:: |
| 223 |
|
| 224 |
import enum |
| 225 |
|
| 226 |
import attr |
| 227 |
|
| 228 |
from labgrid.step import step |
| 229 |
from labgrid.strategy import Strategy, StrategyError, never_retry |
| 230 |
from labgrid.factory import target_factory |
| 231 |
|
| 232 |
class Status(enum.Enum): |
| 233 |
unknown = 0 |
| 234 |
|
| 235 |
@target_factory.reg_driver |
| 236 |
class MyStrategy(Strategy): |
| 237 |
bindings = { |
| 238 |
} |
| 239 |
|
| 240 |
status = attr.ib(default=Status.unknown) |
| 241 |
|
| 242 |
@never_retry |
| 243 |
@step() |
| 244 |
def transition(self, status, *, step): |
| 245 |
if not isinstance(status, Status): |
| 246 |
status = Status[status] |
| 247 |
if status == Status.unknown: |
| 248 |
raise StrategyError(f"can not transition to {status}") |
| 249 |
elif status == self.status: |
| 250 |
step.skip("nothing to do") |
| 251 |
return # nothing to do |
| 252 |
else: |
| 253 |
raise StrategyError( |
| 254 |
f"no transition found from {self.status} to {status}" |
| 255 |
) |
| 256 |
self.status = status |
| 257 |
|
| 258 |
|
| 259 |
The ``bindings`` variable needs to declare the drivers necessary for the |
| 260 |
strategy, usually one for power, bootloader and shell. |
| 261 |
It is possible to reference drivers via their protocol, e.g. |
| 262 |
``ConsoleProtocol``. |
| 263 |
Note that drivers which implement multiple protocols must not be referenced |
| 264 |
multiple times via different protocols. |
| 265 |
The ``Status`` class needs to be extended to cover the states of your strategy, |
| 266 |
then for each state an ``elif`` entry in the ``transition()`` method needs to be |
| 267 |
added. |
| 268 |
|
| 269 |
.. note:: |
| 270 |
Since infrastructure failures or broken strategies typically cannot recover, |
| 271 |
it makes little sense to continue operating with such a strategy after an |
| 272 |
error has occurred. |
| 273 |
To clearly mark a strategy as unusable after failure (and to avoid cascading |
| 274 |
errors in subsequent calls) the strategy's ``transition()`` method (and |
| 275 |
optionally its ``force()`` method) can be decorated with the |
| 276 |
``@never_retry`` decorator. |
| 277 |
This decorator causes the strategy to store the encountered exception in its |
| 278 |
``broken`` attribute and raise a ``StrategyError`` for the original and all |
| 279 |
subsequent calls to the decorated methods. |
| 280 |
|
| 281 |
Lets take a look at the builtin :ref:`BareboxStrategy <conf-bareboxstrategy>`. |
| 282 |
The Status enum for the BareboxStrategy: |
| 283 |
|
| 284 |
:: |
| 285 |
|
| 286 |
class Status(enum.Enum): |
| 287 |
unknown = 0 |
| 288 |
off = 1 |
| 289 |
barebox = 2 |
| 290 |
shell = 3 |
| 291 |
|
| 292 |
defines three custom states and the ``unknown`` state as the start point. |
| 293 |
These three states are handled in the transition function: |
| 294 |
|
| 295 |
:: |
| 296 |
|
| 297 |
elif status == Status.off: |
| 298 |
self.target.deactivate(self.barebox) |
| 299 |
self.target.deactivate(self.shell) |
| 300 |
self.target.activate(self.power) |
| 301 |
self.power.off() |
| 302 |
elif status == Status.barebox: |
| 303 |
self.transition(Status.off) |
| 304 |
# cycle power |
| 305 |
self.power.cycle() |
| 306 |
# interrupt barebox |
| 307 |
self.target.activate(self.barebox) |
| 308 |
elif status == Status.shell: |
| 309 |
# transition to barebox |
| 310 |
self.transition(Status.barebox) |
| 311 |
self.barebox.boot("") |
| 312 |
self.barebox.await_boot() |
| 313 |
self.target.activate(self.shell) |
| 314 |
|
| 315 |
Here, the ``barebox`` state simply cycles the board and activates the driver, |
| 316 |
while the ``shell`` state uses the barebox state to cycle the board and then boot |
| 317 |
the linux kernel. |
| 318 |
The ``off`` state switches the power off. |
| 319 |
|
| 320 |
Oftentimes it is also necessary to wait for specific resources to appear before |
| 321 |
a transition can be continued. The ``await_resources()`` function of the target |
| 322 |
implements this functionality, it expects a list of resources to wait for and |
| 323 |
optionally takes a timeout and whether the resource should be available or |
| 324 |
unavailable. |
| 325 |
|
| 326 |
|
| 327 |
Tips for Writing and Debugging Tests |
| 328 |
------------------------------------ |
| 329 |
|
| 330 |
Live-Reading Console Output |
| 331 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 332 |
|
| 333 |
When starting labgrid with ``--lg-log`` option, it will dump the input from the |
| 334 |
serial driver to a file in specified directory:: |
| 335 |
|
| 336 |
$ pytest [OPTIONS] --lg-log=logdir test-dir/ |
| 337 |
|
| 338 |
This can help understanding what happened and why it happened. |
| 339 |
However, when debugging tests, it might be more helpful to get a live |
| 340 |
impression of what is going on. |
| 341 |
For this, you can use ``tail -F`` to read the content written to the log file |
| 342 |
as if you would be connected to the device's serial console (except that it is |
| 343 |
read-only):: |
| 344 |
|
| 345 |
$ tail -F logdir/console_main # for the 'main' target |
| 346 |
|
| 347 |
For getting information about timing, the ``annotate-output`` command turned |
| 348 |
out to be quite helpful. |
| 349 |
On Debian it comes with the ``devscripts`` package and you can install it |
| 350 |
with:: |
| 351 |
|
| 352 |
$ sudo apt install devscripts |
| 353 |
|
| 354 |
To use it, run:: |
| 355 |
|
| 356 |
$ annotate-output tail -F logdir/console_main |
| 357 |
|
| 358 |
This will print your system time before each line, allowing you to both see |
| 359 |
relative delays between steps in your tests as well as absolute timing of |
| 360 |
things happening in your test environment. |
| 361 |
|
| 362 |
Dealing With Kernel Log Verbosity |
| 363 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 364 |
|
| 365 |
For testing your Linux system it can be quite annoying if the kernel outputs |
| 366 |
verbosely to the console you use for testing. |
| 367 |
Note that a too verbose kernel can break tests as kernel logs will pollute the |
| 368 |
expected command outputs making it unreadable for labgrid regular expressions. |
| 369 |
|
| 370 |
However, as the shell driver and most of the tests will depend on seeing |
| 371 |
console output of what is going on during boot, we cannot turn off kernel |
| 372 |
logging completely. |
| 373 |
|
| 374 |
.. note:: |
| 375 |
The labgrid ShellDriver itself attempts to disable console printing by |
| 376 |
calling ``dmesg -n 1`` as soon as having a logged-in shell. |
| 377 |
However, this may be too late for reliably capturing the initial login and |
| 378 |
shell prompt. |
| 379 |
|
| 380 |
A proper point in time for disabling kernel output to the console is when |
| 381 |
systemd starts. |
| 382 |
To achieve this, make use of the ``systemd-sysctl.service`` that uses |
| 383 |
``/etc/sysctl.d/`` to configure kernel parameters. |
| 384 |
This way, the kernel log level can be set to 'error' by the time of service |
| 385 |
execution with a config file like:: |
| 386 |
|
| 387 |
$ cat /etc/sysctl.d/20-quiet-printk.conf |
| 388 |
kernel.printk = 3 |
| 389 |
|
| 390 |
If the *initial* kernel logging is still too high, one could also reduce this. |
| 391 |
But note that for the standard configuration of the labgrid barebox and uboot |
| 392 |
drivers, we need to catch the ``Linux version ...`` line to detect we |
| 393 |
successfully left the bootloader (the ``bootstring`` attribute). |
| 394 |
This line is only printed when having at least kernel log level 6 (notice) |
| 395 |
enabled:: |
| 396 |
|
| 397 |
loglevel=6 |
| 398 |
|
| 399 |
Graph Strategies |
| 400 |
---------------- |
| 401 |
|
| 402 |
.. warning:: |
| 403 |
This feature is experimental and brings much complexity to your project. |
| 404 |
|
| 405 |
GraphStrategies are made for more complex strategies, with multiple, on each |
| 406 |
other depending, states. A GraphStrategy graph has to be a directed graph with |
| 407 |
one root state. |
| 408 |
|
| 409 |
Using a GraphStrategy makes only sense if you have board states that are |
| 410 |
reachable by different ways. In this case GraphStrategies reduce state |
| 411 |
duplication. |
| 412 |
|
| 413 |
Example |
| 414 |
~~~~~~~ |
| 415 |
|
| 416 |
``teststrategy.py``: |
| 417 |
|
| 418 |
.. code-block:: python |
| 419 |
:name: teststrategy.py |
| 420 |
|
| 421 |
from labgrid.strategy import GraphStrategy |
| 422 |
from labgrid.factory import target_factory |
| 423 |
|
| 424 |
@target_factory.reg_driver |
| 425 |
class TestStrategy(GraphStrategy): |
| 426 |
def state_unknown(self): |
| 427 |
pass |
| 428 |
|
| 429 |
@GraphStrategy.depends('unknown') |
| 430 |
def state_boot_via_nand(self): |
| 431 |
pass |
| 432 |
|
| 433 |
@GraphStrategy.depends('unknown') |
| 434 |
def state_boot_via_nfs(self): |
| 435 |
pass |
| 436 |
|
| 437 |
@GraphStrategy.depends('boot_via_nand', 'boot_via_nfs') |
| 438 |
def state_barebox(self): |
| 439 |
pass |
| 440 |
|
| 441 |
@GraphStrategy.depends('barebox') |
| 442 |
def state_linux_shell(self): |
| 443 |
pass |
| 444 |
|
| 445 |
``test.yaml``: |
| 446 |
|
| 447 |
.. code-block:: yaml |
| 448 |
:name: test.yaml |
| 449 |
|
| 450 |
targets: |
| 451 |
main: |
| 452 |
resources: {} |
| 453 |
drivers: |
| 454 |
TestStrategy: {} |
| 455 |
|
| 456 |
imports: |
| 457 |
- teststrategy.py |
| 458 |
|
| 459 |
The class can also render a graph as PNG (using GraphViz): |
| 460 |
|
| 461 |
.. doctest:: |
| 462 |
:skipif: shutil.which('dot') is None |
| 463 |
|
| 464 |
>>> from labgrid.environment import Environment |
| 465 |
>>> env = Environment('test.yaml') |
| 466 |
>>> strategy = env.get_target().get_driver('Strategy') |
| 467 |
>>> strategy.transition('barebox', via=['boot_via_nfs']) |
| 468 |
['unknown', 'boot_via_nfs', 'barebox'] |
| 469 |
>>> strategy.graph.render("teststrategy-via-nfs") |
| 470 |
'teststrategy-via-nfs.png' |
| 471 |
>>> strategy.transition('barebox', via=['boot_via_nand']) |
| 472 |
['unknown', 'boot_via_nand', 'barebox'] |
| 473 |
>>> strategy.graph.render("teststrategy-via-nand") |
| 474 |
'teststrategy-via-nand.png' |
| 475 |
|
| 476 |
.. figure:: res/graphstrategy-via-nfs.png |
| 477 |
|
| 478 |
TestStrategy transitioned to 'barebox' via 'boot_via_nfs' |
| 479 |
|
| 480 |
.. figure:: res/graphstrategy-via-nand.png |
| 481 |
|
| 482 |
TestStrategy transitioned to 'barebox' via 'boot_via_nand' |
| 483 |
|
| 484 |
State |
| 485 |
~~~~~ |
| 486 |
|
| 487 |
Every graph node describes a board state and how to reach it, A state |
| 488 |
has to be a class method following this prototype: |
| 489 |
``def state_$STATENAME(self):``. A state may not call ``transition()`` in its |
| 490 |
state definition. |
| 491 |
|
| 492 |
Dependency |
| 493 |
~~~~~~~~~~ |
| 494 |
|
| 495 |
Every state, but the root state, can depend on other States, If a state has |
| 496 |
multiple dependencies, not all of them, but one, have to be reached before |
| 497 |
running the current state. |
| 498 |
When no via is used during a transition the order of the given dependencies |
| 499 |
decides which one gets called, where the first one has the highest priority |
| 500 |
and the last one the lowest. |
| 501 |
Dependencies are represented by graph edges. |
| 502 |
|
| 503 |
Root State |
| 504 |
~~~~~~~~~~ |
| 505 |
|
| 506 |
Every GraphStrategy has to has to define exactly one root state. The root state |
| 507 |
defines the start of the graph and therefore the start of every transition. |
| 508 |
A state becomes a root state if it has no dependencies. |
| 509 |
|
| 510 |
Transition |
| 511 |
~~~~~~~~~~ |
| 512 |
|
| 513 |
A transition describes a path, or a part of a path, through a GraphStrategy |
| 514 |
graph. |
| 515 |
Every State in the graph has a auto generated default path starting from the |
| 516 |
root state. |
| 517 |
So using the given example, the GraphStrategy would call the states ``unknown``, |
| 518 |
``boot_via_nand``, ``barebox``, and ``linux_shell`` in this order if |
| 519 |
``transition('linux_shell')`` would be called. |
| 520 |
The GraphStrategy would prefer ``boot_via_nand`` over ``boot_via_nfs`` because |
| 521 |
``boot_via_nand`` is mentioned before ``boot_via_nfs`` in the dependencies of |
| 522 |
``barebox``. If you want to reach via ``boot_via_nfs`` the call would look like |
| 523 |
this: ``transition('linux_shell', via='boot_via_nfs')``. |
| 524 |
|
| 525 |
A transition can be incremental. If we trigger a transition with |
| 526 |
``transition('barebox')`` first, the states ``unknown``, ``boot_via_nand`` and |
| 527 |
``barebox`` will be called in this order. If we trigger a transition |
| 528 |
``transition('linux_shell')`` afterwards only ``linux_shell`` gets called. This |
| 529 |
happens because ``linux_shell`` is reachable from ``barebox`` and the Strategy |
| 530 |
holds state of the last walked path. |
| 531 |
But there is a catch! The second, incremental path must be *fully* incremental |
| 532 |
to the previous path! |
| 533 |
For example: Lets say we reached ``barebox`` via ``boot_via_nfs``, |
| 534 |
(``transition('barebox', via='boot_via_nfs')``). If we trigger |
| 535 |
``transition('linux_shell')`` afterwards the GraphStrategy would compare the last |
| 536 |
path ``'unknown', 'boot_via_nfs', 'barebox'`` with the default path to |
| 537 |
``linux_shell`` which would be |
| 538 |
``'unknown', 'boot_via_nand', 'barebox', 'linux_shell'``, and decides the path |
| 539 |
is not fully incremental and starts over by the root state. If we had given |
| 540 |
the second transition ``boot_via_nfs`` like in the first transition the paths |
| 541 |
had been incremental. |
| 542 |
|
| 543 |
.. _sshmanager: |
| 544 |
|
| 545 |
SSHManager |
| 546 |
---------- |
| 547 |
|
| 548 |
labgrid provides a SSHManager to allow connection reuse with control sockets. |
| 549 |
To use the SSHManager in your code, import it from :any:`labgrid.util.ssh`: |
| 550 |
|
| 551 |
.. doctest:: |
| 552 |
|
| 553 |
>>> from labgrid.util import sshmanager |
| 554 |
|
| 555 |
you can now request or remove port forwardings: |
| 556 |
|
| 557 |
.. testsetup:: sshmanager |
| 558 |
|
| 559 |
from labgrid.util import sshmanager |
| 560 |
sshmanager.get = Mock() |
| 561 |
|
| 562 |
.. doctest:: sshmanager |
| 563 |
|
| 564 |
>>> from labgrid.util import sshmanager |
| 565 |
>>> localport = sshmanager.request_forward('localhost', 'somehost', 3000) |
| 566 |
>>> sshmanager.remove_forward('localhost', 'somehost', 3000) |
| 567 |
|
| 568 |
or get and put files: |
| 569 |
|
| 570 |
.. doctest:: sshmanager |
| 571 |
|
| 572 |
>>> from labgrid.util import sshmanager |
| 573 |
>>> sshmanager.put_file('somehost', '/path/to/local/file', '/path/to/remote/file') |
| 574 |
|
| 575 |
.. note:: |
| 576 |
The SSHManager will reuse existing Control Sockets and set up a keepalive loop |
| 577 |
to prevent timeouts of the socket during tests. |
| 578 |
|
| 579 |
ManagedFile |
| 580 |
----------- |
| 581 |
While the :ref:`SSHManager <sshmanager>` exposes a lower level interface to use SSH Connections, |
| 582 |
the ManagedFile provides a higher level interface for file upload to another |
| 583 |
host. It is meant to be used in conjunction with a remote resource, and store |
| 584 |
the file on the remote host with the following pattern: |
| 585 |
|
| 586 |
.. code-block:: bash |
| 587 |
|
| 588 |
/tmp/labgrid-<username>/<sha256sum>/<filename> |
| 589 |
|
| 590 |
Additionally it provides ``get_remote_path()`` to retrieve the complete file path, |
| 591 |
to easily employ it for driver implementations. |
| 592 |
To use it in conjunction with a *Resource* and a file: |
| 593 |
|
| 594 |
.. testsetup:: managed-file |
| 595 |
|
| 596 |
import tempfile |
| 597 |
from labgrid.resource import Resource |
| 598 |
from labgrid import Target |
| 599 |
|
| 600 |
f = tempfile.NamedTemporaryFile() |
| 601 |
your_file = f.name |
| 602 |
your_resource = Resource(Target("main"), "example") |
| 603 |
|
| 604 |
.. doctest:: managed-file |
| 605 |
|
| 606 |
>>> from labgrid.util.managedfile import ManagedFile |
| 607 |
>>> mf = ManagedFile(your_file, your_resource) |
| 608 |
>>> mf.sync_to_resource() |
| 609 |
>>> path = mf.get_remote_path() |
| 610 |
|
| 611 |
Unless constructed with ``ManagedFile(..., detect_nfs=False)``, ManagedFile |
| 612 |
employs the following heuristic to check if a file is stored on a NFS share |
| 613 |
available both locally and remotely via the same path: |
| 614 |
|
| 615 |
- check if GNU coreutils stat(1) with option --format exists on local and |
| 616 |
remote system |
| 617 |
- check if inode number, total size and modification timestamp match |
| 618 |
on local and remote system |
| 619 |
|
| 620 |
If this is the case the actual file transfer in ``sync_to_resource`` is |
| 621 |
skipped. |
| 622 |
|
| 623 |
ProxyManager |
| 624 |
------------ |
| 625 |
The proxymanager is used to open connections across proxies via an attribute in |
| 626 |
the resource. This allows gated testing networks by always using the exporter as |
| 627 |
an SSH gateway to proxy the connections using SSH Forwarding. Currently this is |
| 628 |
used in the :any:`SerialDriver` for proxy connections. |
| 629 |
|
| 630 |
Usage: |
| 631 |
|
| 632 |
.. testsetup:: proxy-manager |
| 633 |
|
| 634 |
from labgrid.resource import Resource |
| 635 |
from labgrid import Target |
| 636 |
|
| 637 |
your_resource = Resource(Target("main"), "example") |
| 638 |
your_resource.host = "localhost" |
| 639 |
|
| 640 |
.. doctest:: proxy-manager |
| 641 |
|
| 642 |
>>> from labgrid.util.proxy import proxymanager |
| 643 |
>>> host, port = proxymanager.get_host_and_port(your_resource) |
| 644 |
|
| 645 |
|
| 646 |
.. _contributing: |
| 647 |
|
| 648 |
Contributing |
| 649 |
------------ |
| 650 |
|
| 651 |
Thank you for thinking about contributing to labgrid! |
| 652 |
Some different backgrounds and use-cases are essential for making labgrid work |
| 653 |
well for all users. |
| 654 |
|
| 655 |
The following should help you with submitting your changes, but don't let these |
| 656 |
guidelines keep you from opening a pull request. |
| 657 |
If in doubt, we'd prefer to see the code earlier as a work-in-progress PR and |
| 658 |
help you with the submission process. |
| 659 |
|
| 660 |
Workflow |
| 661 |
~~~~~~~~ |
| 662 |
|
| 663 |
- Changes should be submitted via a `GitHub pull request |
| 664 |
<https://github.com/labgrid-project/labgrid/pulls>`_. |
| 665 |
- Try to limit each commit to a single conceptual change. |
| 666 |
- Add a signed-of-by line to your commits according to the *Developer's |
| 667 |
Certificate of Origin* (see below). |
| 668 |
- Check that the tests still work before submitting the pull request. Also |
| 669 |
check the CI's feedback on the pull request after submission. |
| 670 |
- When adding new drivers or resources, please also add the corresponding |
| 671 |
documentation and test code. |
| 672 |
- If your change affects backward compatibility, describe the necessary changes |
| 673 |
in the commit message and update the examples where needed. |
| 674 |
|
| 675 |
Code |
| 676 |
~~~~ |
| 677 |
|
| 678 |
- Follow the :pep:`8` style. |
| 679 |
- Use attr.ib attributes for public attributes of your drivers and resources. |
| 680 |
- Use `isort <https://pypi.python.org/pypi/isort>`_ to sort the import |
| 681 |
statements. |
| 682 |
|
| 683 |
Documentation |
| 684 |
~~~~~~~~~~~~~ |
| 685 |
|
| 686 |
- Use `semantic linefeeds |
| 687 |
<http://rhodesmill.org/brandon/2012/one-sentence-per-line/>`_ in .rst files. |
| 688 |
|
| 689 |
Building the documentation |
| 690 |
++++++++++++++++++++++++++ |
| 691 |
|
| 692 |
When contributing to documentation it's practical to be able to build it also locally. |
| 693 |
|
| 694 |
.. code-block:: bash |
| 695 |
|
| 696 |
virtualenv -p python3 labgrid-venv |
| 697 |
source labgrid-venv/bin/activate |
| 698 |
pip install --upgrade pip |
| 699 |
|
| 700 |
git clone https://github.com/labgrid-project/labgrid.git |
| 701 |
cd labgrid |
| 702 |
pip install -e ".[dev]" |
| 703 |
cd doc |
| 704 |
make html |
| 705 |
|
| 706 |
Once the build is done you can see the results with ``firefox .build/html/index.html``. |
| 707 |
|
| 708 |
If for whatever the reason you need to rebuild everything from scratch, use ``make SPHINXOPTS="-a -E" html``. |
| 709 |
|
| 710 |
Run Tests |
| 711 |
~~~~~~~~~ |
| 712 |
|
| 713 |
.. code-block:: bash |
| 714 |
|
| 715 |
$ tox -r |
| 716 |
|
| 717 |
Developer's Certificate of Origin |
| 718 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 719 |
|
| 720 |
labgrid uses the `Developer's Certificate of Origin 1.1 |
| 721 |
<https://developercertificate.org/>`_ with the same `process |
| 722 |
<https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin>`_ |
| 723 |
as used for the Linux kernel: |
| 724 |
|
| 725 |
Developer's Certificate of Origin 1.1 |
| 726 |
|
| 727 |
By making a contribution to this project, I certify that: |
| 728 |
|
| 729 |
(a) The contribution was created in whole or in part by me and I |
| 730 |
have the right to submit it under the open source license |
| 731 |
indicated in the file; or |
| 732 |
|
| 733 |
(b) The contribution is based upon previous work that, to the best |
| 734 |
of my knowledge, is covered under an appropriate open source |
| 735 |
license and I have the right under that license to submit that |
| 736 |
work with modifications, whether created in whole or in part |
| 737 |
by me, under the same open source license (unless I am |
| 738 |
permitted to submit under a different license), as indicated |
| 739 |
in the file; or |
| 740 |
|
| 741 |
(c) The contribution was provided directly to me by some other |
| 742 |
person who certified (a), (b) or (c) and I have not modified |
| 743 |
it. |
| 744 |
|
| 745 |
(d) I understand and agree that this project and the contribution |
| 746 |
are public and that a record of the contribution (including all |
| 747 |
personal information I submit with it, including my sign-off) is |
| 748 |
maintained indefinitely and may be redistributed consistent with |
| 749 |
this project or the open source license(s) involved. |
| 750 |
|
| 751 |
Then you just add a line (using ``git commit -s``) saying: |
| 752 |
|
| 753 |
Signed-off-by: Random J Developer <[email protected]> |
| 754 |
|
| 755 |
using your real name (sorry, no pseudonyms or anonymous contributions). |
| 756 |
|
| 757 |
.. _ideas: |
| 758 |
|
| 759 |
Ideas |
| 760 |
----- |
| 761 |
|
| 762 |
.. please keep these sorted alphabetically |
| 763 |
|
| 764 |
Driver Preemption |
| 765 |
~~~~~~~~~~~~~~~~~ |
| 766 |
|
| 767 |
To allow better handling of unexpected reboots or crashes, inactive Drivers |
| 768 |
could register callbacks on their providers (for example the BareboxDriver it's |
| 769 |
ConsoleProtocol). |
| 770 |
These callbacks would look for indications that the Target has changed state |
| 771 |
unexpectedly (by looking for the bootloader startup messages, in this case). |
| 772 |
The inactive Driver could then cause a preemption and would be activated. |
| 773 |
The current caller of the originally active driver would be notified via an |
| 774 |
exception. |
| 775 |
|
| 776 |
Step Tracing |
| 777 |
~~~~~~~~~~~~ |
| 778 |
|
| 779 |
The Step infrastructure already collects timing and nesting information on |
| 780 |
executed commands, but is currently only used in the pytest plugin or via the |
| 781 |
standalone StepLogger (or deprecated StepReporter). |
| 782 |
By writing these events to a file (or sqlite database) as a trace, we can |
| 783 |
collect data over multiple runs for later analysis. |
| 784 |
This would become more useful by passing recognized events (stack traces, |
| 785 |
crashes, ...) and benchmark results via the Step infrastructure. |
| 786 |
|
| 787 |
CommandProtocol Support for Background Processes |
| 788 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 789 |
|
| 790 |
Currently the CommandProtocol does not support long running |
| 791 |
processes well. |
| 792 |
An implementation should start a new process, |
| 793 |
return a handle and forbid running other processes in the foreground. |
| 794 |
The handle can be used to retrieve output from a command. |