Debian Codegraph @

doc/getting_started.rst

raw
1 Getting Started
2 ===============
3
4 This section of the manual contains introductory tutorials for installing
5 labgrid, running your first test and setting up the distributed infrastructure.
6 For an overview about the basic design and components of *labgrid*, read the
7 :ref:`overview` first.
8
9 Installation
10 ------------
11
12 Depending on your distribution you need some dependencies. On Debian these
13 usually are:
14
15 .. code-block:: bash
16
17 $ sudo apt install python3 python3-virtualenv python3-pip python3-setuptools virtualenv microcom
18
19
20 In many cases, the easiest way is to install labgrid into a virtualenv:
21
22 .. code-block:: bash
23
24 $ virtualenv -p python3 labgrid-venv
25 $ source labgrid-venv/bin/activate
26 labgrid-venv $ pip install --upgrade pip
27
28 Install Latest Release
29 ~~~~~~~~~~~~~~~~~~~~~~
30
31 .. code-block:: bash
32
33 labgrid-venv $ pip install labgrid
34
35 Install Development State
36 ~~~~~~~~~~~~~~~~~~~~~~~~~
37
38 Start by cloning the repository and installing labgrid:
39
40 .. code-block:: bash
41
42 labgrid-venv $ git clone https://github.com/labgrid-project/labgrid
43 labgrid-venv $ cd labgrid && pip install .
44
45 .. note::
46 If you are **not** installing via pip and intend to use Serial over IP
47 (RFC2217), it is highly recommended to uninstall pyserial after installation
48 and replace it with the pyserial version from the labgrid project:
49
50 https://github.com/labgrid-project/pyserial/tags
51
52 This pyserial version has two fixes for an Issue we found with Serial over IP
53 multiplexers. Additionally it reduces the Serial over IP traffic considerably
54 since the port is not reconfigured when labgrid changes the timeout (which is
55 done inside the library a lot).
56
57 Test Installation
58 ~~~~~~~~~~~~~~~~~
59
60 Test your installation by running:
61
62 .. code-block:: bash
63
64 labgrid-venv $ labgrid-client --help
65 usage: labgrid-client [-h] [-x ADDRESS] [-c CONFIG] [-p PLACE] [-d] COMMAND ...
66 ...
67
68 If the help for labgrid-client does not show up, open an `Issue
69 <https://github.com/labgrid-project/labgrid/issues>`_. If everything was
70 successful so far, proceed to the next section:
71
72 Optional Requirements
73 ~~~~~~~~~~~~~~~~~~~~~
74 labgrid provides optional features which are not included in the default
75 installation. An example for snmp support is:
76
77 .. code-block:: bash
78
79 labgrid-venv $ pip install ".[snmp]"
80
81 Onewire
82 +++++++
83 Onewire support requires the ``libow`` library with headers, installable on debian
84 via the ``libow-dev`` package. Use the ``onewire`` extra to install the correct
85 onewire library version in addition to the normal installation.
86
87 SNMP
88 ++++
89 SNMP support requires to additional packages, ``pysnmp`` and ``pysnmpmibs``. They
90 are included in the ``snmp`` extra.
91
92 Modbus
93 ++++++
94 Modbus support requires an additional package ``pyModbusTCP``. It is included in
95 the ``modbus`` extra.
96
97 ModbusRTU
98 +++++++++
99 Modbus support requires an additional package ``minimalmodbus``. It is included in
100 the ``modbusrtu`` extra.
101
102 Running Your First Test
103 -----------------------
104
105 Start by copying the initial example:
106
107 .. code-block:: bash
108
109 $ mkdir ../first_test/
110 $ cp examples/shell/* ../first_test/
111 $ cd ../first_test/
112
113 Connect your embedded board (raspberry pi, riotboard, …) to your computer and
114 adjust the ``port`` parameter of the ``RawSerialPort`` resource and ``username``
115 and ``password`` of the ShellDriver driver in the environment file ``local.yaml``:
116
117 .. code-block:: yaml
118
119 targets:
120 main:
121 resources:
122 RawSerialPort:
123 port: "/dev/ttyUSB0"
124 drivers:
125 ManualPowerDriver:
126 name: "example"
127 SerialDriver: {}
128 ShellDriver:
129 prompt: 'root@[\w-]+:[^ ]+ '
130 login_prompt: ' login: '
131 username: 'root'
132
133 For, now it is sufficient to know that labgrid tries to "bind" appropriate drivers
134 and resources to each other automatically if possible. The ``ShellDriver`` will use
135 the ``SerialDriver`` to connect to the ``RawSerialPort`` resource. If an automatic
136 binding is not possible due to ambiguity, the bindings can also be specified
137 explicitly.
138
139 More information about this and the environment configuration file in general can
140 be found :ref:`here <environment-configuration>`.
141
142 You can check which device name gets assigned to your USB-Serial converter by
143 unplugging the converter, running ``dmesg -w`` and plugging it back in. Boot up
144 your board (manually) and run your first test:
145
146 .. code-block:: bash
147
148 labgrid-venv $ pytest --lg-env local.yaml --capture=no test_shell.py
149
150 It should return successfully, in case it does not, open an `Issue
151 <https://github.com/labgrid-project/labgrid/issues>`_.
152
153 .. _remote-getting-started:
154
155 Setting Up the Distributed Infrastructure
156 -----------------------------------------
157
158 The labgrid :ref:`distributed infrastructure <remote-resources-and-places>`
159 consists of three components:
160
161 #. :ref:`overview-coordinator`
162 #. :ref:`overview-exporter`
163 #. :ref:`overview-client`
164
165 The system needs at least one coordinator and exporter, these can run on the
166 same machine. The client is used to access functionality provided by an
167 exporter. Over the course of this tutorial we will set up a coordinator and
168 exporter, and learn how to access the exporter via the client.
169
170 .. Attention::
171 Labgrid requires your user to be able to connect from the client machine via
172 ssh to the exporter machine _without_ a password prompt. This means that
173 public key authentication should be configured on all involved machines for
174 your user beforehand.
175
176 .. _remote-getting-started-coordinator:
177
178 Coordinator
179 ~~~~~~~~~~~
180
181 We can simply start the coordinator:
182
183 .. code-block:: bash
184
185 labgrid-venv $ labgrid-coordinator
186
187 Exporter
188 ~~~~~~~~
189
190 The exporter is responsible for exporting resources to the client. It is noted
191 that drivers are not handled by the exporter. In the distributed case, drivers
192 are managed by the client.
193
194 The exporter needs a configuration file written in YAML syntax, listing
195 the resources to be exported from the local machine.
196 The config file contains one or more named resource groups.
197 Each group contains one or more resource declarations and optionally a location
198 string.
199
200 For example, to export a ``USBSerialPort`` with ``ID_SERIAL_SHORT`` of
201 ``ID23421JLK``, the group name ``example-group`` and the location
202 ``example-location``:
203
204 .. code-block:: yaml
205
206 example-group:
207 location: example-location
208 USBSerialPort:
209 match:
210 ID_SERIAL_SHORT: ID23421JLK
211
212 .. note:: See the :ref:`udev matching section <udev-matching>` on how to
213 match ManagedResources and the
214 :ref:`resources sections <overview-resources>` for a description of
215 different resource types.
216
217 The exporter requires additional dependencies:
218
219 .. code-block:: bash
220
221 $ sudo apt install ser2net
222
223 It can now be started by running:
224
225 .. code-block:: bash
226
227 labgrid-venv $ labgrid-exporter configuration.yaml
228
229 Additional groups and resources can be added:
230
231 .. code-block:: yaml
232
233 example-group:
234 location: example-location
235 USBSerialPort:
236 match:
237 ID_SERIAL_SHORT: P-00-00682
238 speed: 115200
239 NetworkPowerPort:
240 model: netio
241 host: netio1
242 index: 3
243 example-group-2:
244 USBSerialPort:
245 match:
246 ID_SERIAL_SHORT: KSLAH2341J
247
248 More information about the exporter configuration file can be found
249 :ref:`here <exporter-configuration>`.
250
251 Restart the exporter to activate the new configuration.
252
253 .. Attention::
254 The :any:`ManagedFile` will create temporary uploads in the exporters
255 ``/var/cache/labgrid`` directory. This directory needs to be created manually
256 and should allow write access for users. The ``/contrib`` directory in the
257 labgrid-project contains a tmpfiles configuration example to automatically
258 create and clean the directory.
259 It is also highly recommended to enable ``fs.protected_regular=1`` and
260 ``fs.protected_fifos=1`` for kernels>=4.19, to protect the users from opening
261 files not owned by them in world writeable sticky directories.
262 For more information see `this kernel commit`_.
263
264 .. _`this kernel commit`: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=30aba6656f
265
266 Client
267 ~~~~~~
268
269 Finally we can test the client functionality, run:
270
271 .. code-block:: bash
272
273 labgrid-venv $ labgrid-client resources
274 kiwi/example-group/NetworkPowerPort
275 kiwi/example-group/NetworkSerialPort
276 kiwi/example-group-2/NetworkSerialPort
277
278 You can see the available resources listed by the coordinator. The groups
279 ``example-group`` and ``example-group-2`` should be available there.
280
281 To show more details on the exported resources, use ``-v`` (or ``-vv``):
282
283 .. code-block:: bash
284
285 labgrid-venv $ labgrid-client -v resources
286 Exporter 'kiwi':
287 Group 'example-group' (kiwi/example-group/*):
288 Resource 'NetworkPowerPort' (kiwi/example-group/NetworkPowerPort[/NetworkPowerPort]):
289 {'acquired': None,
290 'avail': True,
291 'cls': 'NetworkPowerPort',
292 'params': {'host': 'netio1', 'index': 3, 'model': 'netio'}}
293 ...
294
295 You can now add a place to the coordinator with:
296
297 .. code-block:: bash
298
299 labgrid-venv $ labgrid-client --place example-place create
300
301 And add resources to this place (``-p`` is short for ``--place``):
302
303 .. code-block:: bash
304
305 labgrid-venv $ labgrid-client -p example-place add-match */example-group/*
306
307 Which adds the previously defined resource from the exporter to the place.
308 To interact with this place, it needs to be acquired first, this is done by
309
310 .. code-block:: bash
311
312 labgrid-venv $ labgrid-client -p example-place acquire
313
314 Now we can connect to the serial console:
315
316 .. code-block:: bash
317
318 labgrid-venv $ labgrid-client -p example-place console
319
320 .. note:: Using remote connection requires ``microcom`` or ``telnet`` installed
321 on the host where the labgrid-client is called.
322
323 See :ref:`remote-usage` for some more advanced features.
324 For a complete reference have a look at the :doc:`labgrid-client(1) <man/client>`
325 man page.
326
327 Now, to connect drivers to the resources, you can configure an environment file,
328 which we call ``remote.yaml`` in this case:
329
330 .. code-block:: yaml
331
332 targets:
333 main:
334 resources:
335 RemotePlace:
336 name: myplace
337 drivers:
338 SerialDriver: {}
339 ShellDriver:
340 prompt: 'root@[\w-]+:[^ ]+ '
341 login_prompt: ' login: '
342 username: 'root'
343
344 The ``RemotePlace`` resource makes all resources available that are assigned to
345 to the place ``myplace`` on your coordinator.
346
347 Now, this environment file can be used interactively with the client:
348
349 .. code-block:: bash
350
351 labgrid-venv $ labgrid-client -c remote.yaml acquire
352 labgrid-venv $ labgrid-client -c remote.yaml console
353 labgrid-venv $ labgrid-client -c remote.yaml release
354
355 or directly with a test:
356
357 .. code-block:: bash
358
359 labgrid-venv $ labgrid-client -c remote.yaml acquire
360 labgrid-venv $ pytest --lg-env remote.yaml test_shell.py
361 labgrid-venv $ labgrid-client -c remote.yaml release
362
363 .. _remote-getting-started-systemd-files:
364
365 Systemd files
366 ~~~~~~~~~~~~~
367
368 Labgrid comes with several systemd files in :file:`contrib/systemd`:
369
370 - service files for coordinator and exporter
371 - tmpfiles.d file to regularly remove files uploaded to the exporter in
372 :file:`/var/cache/labgrid`
373 - sysusers.d file to create the ``labgrid`` user and group, enabling members of
374 the ``labgrid`` group to upload files to the exporter in :file:`/var/cache/labgrid`
375
376 Follow these instructions to install the systemd files on your machine(s):
377
378 #. Copy the service, tmpfiles.d and sysusers.d files to the respective
379 installation paths of your distribution.
380 #. Adapt the ``ExecStart`` paths of the service files to the respective Python
381 virtual environments of the coordinator and exporter.
382 #. If you use labgrid helpers (see :file:`helpers/`) with sudo on exporter,
383 replace ``DynamicUser`` with ``User`` and ``Group`` in the
384 :file:`labgrid-exporter.service`:
385
386 .. code-block::
387
388 User=labgrid
389 Group=labgrid
390
391 #. Adjust the ``SupplementaryGroups`` option in the
392 :file:`labgrid-exporter.service` file to your distribution so that the
393 exporter gains read and write access on TTY devices (for ``ser2net``); most
394 often, these groups are called ``dialout``, ``plugdev`` or ``tty``.
395 Depending on your udev configuration, you may need multiple groups.
396 #. Set the coordinator address the exporter should connect to by overriding the
397 exporter service file; i.e. execute ``systemctl edit
398 labgrid-exporter.service`` and add the following snippet:
399
400 .. code-block::
401
402 [Service]
403 Environment="LG_COORDINATOR=<your-host>[:<your-port>]"
404
405 #. Create the ``labgrid`` user and group:
406
407 .. code-block:: console
408
409 # systemd-sysusers
410
411 #. Reload the systemd manager configuration:
412
413 .. code-block:: console
414
415 # systemctl daemon-reload
416
417 #. Start the coordinator, if applicable:
418
419 .. code-block:: console
420
421 # systemctl start labgrid-coordinator
422
423 #. After creating the exporter configuration file referenced in the
424 ``ExecStart`` option of the :file:`labgrid-exporter.service` file, start the
425 exporter:
426
427 .. code-block:: console
428
429 # systemctl start labgrid-exporter
430
431 #. Optionally, for users being able to upload files to the exporter, add them
432 to the ``labgrid`` group on the exporter machine:
433
434 .. code-block:: console
435
436 # usermod -a -G labgrid <user>
437
438 Using a Strategy
439 ----------------
440
441 Strategies allow the labgrid library to automatically bring the board into a
442 defined state, e.g. boot through the bootloader into the Linux kernel and log in
443 to a shell. They have a few requirements:
444
445 - A driver implementing the ``PowerProtocol``, if no controllable infrastructure
446 is available a ``ManualPowerDriver`` can be used.
447 - A driver implementing the ``LinuxBootProtocol``, usually a specific driver for
448 the board's bootloader
449 - A driver implementing the ``CommandProtocol``, usually a ``ShellDriver`` with
450 a ``SerialDriver`` below it.
451
452 labgrid ships with two builtin strategies, ``BareboxStrategy`` and
453 ``UBootStrategy``. These can be used as a reference example for simple
454 strategies, more complex tests usually require the implementation of your own
455 strategies.
456
457 To use a strategy, add it and its dependencies to your configuration YAML,
458 retrieve it in your test and call the ``transition(status)`` function.
459 See the section about the various :ref:`shipped strategies <conf-strategies>`
460 for examples on this.
461
462 An example using the pytest plugin is provided under ``examples/strategy``.