blob: 205a96cc598f155a4bcf96afc5e3737aa8e2787b [file] [log] [blame]
Andrew Geissler517393d2023-01-13 08:55:19 -06001.. SPDX-License-Identifier: CC-BY-SA-2.0-UK
2
3Performing Automated Runtime Testing
4************************************
5
6The OpenEmbedded build system makes available a series of automated
7tests for images to verify runtime functionality. You can run these
8tests on either QEMU or actual target hardware. Tests are written in
9Python making use of the ``unittest`` module, and the majority of them
10run commands on the target system over SSH. This section describes how
11you set up the environment to use these tests, run available tests, and
12write and add your own tests.
13
14For information on the test and QA infrastructure available within the
15Yocto Project, see the ":ref:`ref-manual/release-process:testing and quality assurance`"
16section in the Yocto Project Reference Manual.
17
18Enabling Tests
19==============
20
21Depending on whether you are planning to run tests using QEMU or on the
22hardware, you have to take different steps to enable the tests. See the
23following subsections for information on how to enable both types of
24tests.
25
26Enabling Runtime Tests on QEMU
27------------------------------
28
29In order to run tests, you need to do the following:
30
31- *Set up to avoid interaction with sudo for networking:* To
32 accomplish this, you must do one of the following:
33
34 - Add ``NOPASSWD`` for your user in ``/etc/sudoers`` either for all
35 commands or just for ``runqemu-ifup``. You must provide the full
36 path as that can change if you are using multiple clones of the
37 source repository.
38
39 .. note::
40
41 On some distributions, you also need to comment out "Defaults
42 requiretty" in ``/etc/sudoers``.
43
44 - Manually configure a tap interface for your system.
45
46 - Run as root the script in ``scripts/runqemu-gen-tapdevs``, which
47 should generate a list of tap devices. This is the option
48 typically chosen for Autobuilder-type environments.
49
50 .. note::
51
52 - Be sure to use an absolute path when calling this script
53 with sudo.
54
55 - The package recipe ``qemu-helper-native`` is required to run
56 this script. Build the package using the following command::
57
58 $ bitbake qemu-helper-native
59
60- *Set the DISPLAY variable:* You need to set this variable so that
61 you have an X server available (e.g. start ``vncserver`` for a
62 headless machine).
63
64- *Be sure your host's firewall accepts incoming connections from
65 192.168.7.0/24:* Some of the tests (in particular DNF tests) start an
66 HTTP server on a random high number port, which is used to serve
67 files to the target. The DNF module serves
68 ``${WORKDIR}/oe-rootfs-repo`` so it can run DNF channel commands.
69 That means your host's firewall must accept incoming connections from
70 192.168.7.0/24, which is the default IP range used for tap devices by
71 ``runqemu``.
72
73- *Be sure your host has the correct packages installed:* Depending
74 your host's distribution, you need to have the following packages
75 installed:
76
77 - Ubuntu and Debian: ``sysstat`` and ``iproute2``
78
79 - openSUSE: ``sysstat`` and ``iproute2``
80
81 - Fedora: ``sysstat`` and ``iproute``
82
83 - CentOS: ``sysstat`` and ``iproute``
84
85Once you start running the tests, the following happens:
86
87#. A copy of the root filesystem is written to ``${WORKDIR}/testimage``.
88
89#. The image is booted under QEMU using the standard ``runqemu`` script.
90
91#. A default timeout of 500 seconds occurs to allow for the boot process
92 to reach the login prompt. You can change the timeout period by
93 setting
94 :term:`TEST_QEMUBOOT_TIMEOUT`
95 in the ``local.conf`` file.
96
97#. Once the boot process is reached and the login prompt appears, the
98 tests run. The full boot log is written to
99 ``${WORKDIR}/testimage/qemu_boot_log``.
100
101#. Each test module loads in the order found in :term:`TEST_SUITES`. You can
102 find the full output of the commands run over SSH in
103 ``${WORKDIR}/testimgage/ssh_target_log``.
104
105#. If no failures occur, the task running the tests ends successfully.
106 You can find the output from the ``unittest`` in the task log at
107 ``${WORKDIR}/temp/log.do_testimage``.
108
109Enabling Runtime Tests on Hardware
110----------------------------------
111
112The OpenEmbedded build system can run tests on real hardware, and for
113certain devices it can also deploy the image to be tested onto the
114device beforehand.
115
116For automated deployment, a "controller image" is installed onto the
117hardware once as part of setup. Then, each time tests are to be run, the
118following occurs:
119
120#. The controller image is booted into and used to write the image to be
121 tested to a second partition.
122
123#. The device is then rebooted using an external script that you need to
124 provide.
125
126#. The device boots into the image to be tested.
127
128When running tests (independent of whether the image has been deployed
129automatically or not), the device is expected to be connected to a
130network on a pre-determined IP address. You can either use static IP
131addresses written into the image, or set the image to use DHCP and have
132your DHCP server on the test network assign a known IP address based on
133the MAC address of the device.
134
135In order to run tests on hardware, you need to set :term:`TEST_TARGET` to an
136appropriate value. For QEMU, you do not have to change anything, the
137default value is "qemu". For running tests on hardware, the following
138options are available:
139
140- *"simpleremote":* Choose "simpleremote" if you are going to run tests
141 on a target system that is already running the image to be tested and
142 is available on the network. You can use "simpleremote" in
143 conjunction with either real hardware or an image running within a
144 separately started QEMU or any other virtual machine manager.
145
146- *"SystemdbootTarget":* Choose "SystemdbootTarget" if your hardware is
147 an EFI-based machine with ``systemd-boot`` as bootloader and
148 ``core-image-testmaster`` (or something similar) is installed. Also,
149 your hardware under test must be in a DHCP-enabled network that gives
150 it the same IP address for each reboot.
151
152 If you choose "SystemdbootTarget", there are additional requirements
153 and considerations. See the
154 ":ref:`dev-manual/runtime-testing:selecting systemdboottarget`" section, which
155 follows, for more information.
156
157- *"BeagleBoneTarget":* Choose "BeagleBoneTarget" if you are deploying
158 images and running tests on the BeagleBone "Black" or original
159 "White" hardware. For information on how to use these tests, see the
160 comments at the top of the BeagleBoneTarget
161 ``meta-yocto-bsp/lib/oeqa/controllers/beaglebonetarget.py`` file.
162
Andrew Geissler517393d2023-01-13 08:55:19 -0600163- *"GrubTarget":* Choose "GrubTarget" if you are deploying images and running
164 tests on any generic PC that boots using GRUB. For information on how
165 to use these tests, see the comments at the top of the GrubTarget
166 ``meta-yocto-bsp/lib/oeqa/controllers/grubtarget.py`` file.
167
168- *"your-target":* Create your own custom target if you want to run
169 tests when you are deploying images and running tests on a custom
170 machine within your BSP layer. To do this, you need to add a Python
171 unit that defines the target class under ``lib/oeqa/controllers/``
172 within your layer. You must also provide an empty ``__init__.py``.
173 For examples, see files in ``meta-yocto-bsp/lib/oeqa/controllers/``.
174
175Selecting SystemdbootTarget
176---------------------------
177
178If you did not set :term:`TEST_TARGET` to "SystemdbootTarget", then you do
179not need any information in this section. You can skip down to the
180":ref:`dev-manual/runtime-testing:running tests`" section.
181
182If you did set :term:`TEST_TARGET` to "SystemdbootTarget", you also need to
183perform a one-time setup of your controller image by doing the following:
184
185#. *Set EFI_PROVIDER:* Be sure that :term:`EFI_PROVIDER` is as follows::
186
187 EFI_PROVIDER = "systemd-boot"
188
189#. *Build the controller image:* Build the ``core-image-testmaster`` image.
190 The ``core-image-testmaster`` recipe is provided as an example for a
191 "controller" image and you can customize the image recipe as you would
192 any other recipe.
193
194 Here are the image recipe requirements:
195
196 - Inherits ``core-image`` so that kernel modules are installed.
197
198 - Installs normal linux utilities not BusyBox ones (e.g. ``bash``,
199 ``coreutils``, ``tar``, ``gzip``, and ``kmod``).
200
201 - Uses a custom :term:`Initramfs` image with a custom
202 installer. A normal image that you can install usually creates a
203 single root filesystem partition. This image uses another installer that
204 creates a specific partition layout. Not all Board Support
205 Packages (BSPs) can use an installer. For such cases, you need to
206 manually create the following partition layout on the target:
207
208 - First partition mounted under ``/boot``, labeled "boot".
209
210 - The main root filesystem partition where this image gets installed,
211 which is mounted under ``/``.
212
213 - Another partition labeled "testrootfs" where test images get
214 deployed.
215
216#. *Install image:* Install the image that you just built on the target
217 system.
218
219The final thing you need to do when setting :term:`TEST_TARGET` to
220"SystemdbootTarget" is to set up the test image:
221
222#. *Set up your local.conf file:* Make sure you have the following
223 statements in your ``local.conf`` file::
224
225 IMAGE_FSTYPES += "tar.gz"
Andrew Geissler20137392023-10-12 04:59:14 -0600226 IMAGE_CLASSES += "testimage"
Andrew Geissler517393d2023-01-13 08:55:19 -0600227 TEST_TARGET = "SystemdbootTarget"
228 TEST_TARGET_IP = "192.168.2.3"
229
230#. *Build your test image:* Use BitBake to build the image::
231
232 $ bitbake core-image-sato
233
234Power Control
235-------------
236
237For most hardware targets other than "simpleremote", you can control
238power:
239
240- You can use :term:`TEST_POWERCONTROL_CMD` together with
241 :term:`TEST_POWERCONTROL_EXTRA_ARGS` as a command that runs on the host
242 and does power cycling. The test code passes one argument to that
243 command: off, on or cycle (off then on). Here is an example that
244 could appear in your ``local.conf`` file::
245
246 TEST_POWERCONTROL_CMD = "powercontrol.exp test 10.11.12.1 nuc1"
247
248 In this example, the expect
249 script does the following:
250
251 .. code-block:: shell
252
253 ssh test@10.11.12.1 "pyctl nuc1 arg"
254
255 It then runs a Python script that controls power for a label called
256 ``nuc1``.
257
258 .. note::
259
260 You need to customize :term:`TEST_POWERCONTROL_CMD` and
261 :term:`TEST_POWERCONTROL_EXTRA_ARGS` for your own setup. The one requirement
262 is that it accepts "on", "off", and "cycle" as the last argument.
263
264- When no command is defined, it connects to the device over SSH and
265 uses the classic reboot command to reboot the device. Classic reboot
266 is fine as long as the machine actually reboots (i.e. the SSH test
267 has not failed). It is useful for scenarios where you have a simple
268 setup, typically with a single board, and where some manual
269 interaction is okay from time to time.
270
271If you have no hardware to automatically perform power control but still
272wish to experiment with automated hardware testing, you can use the
273``dialog-power-control`` script that shows a dialog prompting you to perform
274the required power action. This script requires either KDialog or Zenity
275to be installed. To use this script, set the
276:term:`TEST_POWERCONTROL_CMD`
277variable as follows::
278
279 TEST_POWERCONTROL_CMD = "${COREBASE}/scripts/contrib/dialog-power-control"
280
281Serial Console Connection
282-------------------------
283
284For test target classes requiring a serial console to interact with the
Patrick Williamsac13d5f2023-11-24 18:59:46 -0600285bootloader (e.g. BeagleBoneTarget and GrubTarget),
Andrew Geissler517393d2023-01-13 08:55:19 -0600286you need to specify a command to use to connect to the serial console of
287the target machine by using the
288:term:`TEST_SERIALCONTROL_CMD`
289variable and optionally the
290:term:`TEST_SERIALCONTROL_EXTRA_ARGS`
291variable.
292
293These cases could be a serial terminal program if the machine is
294connected to a local serial port, or a ``telnet`` or ``ssh`` command
295connecting to a remote console server. Regardless of the case, the
296command simply needs to connect to the serial console and forward that
297connection to standard input and output as any normal terminal program
298does. For example, to use the picocom terminal program on serial device
299``/dev/ttyUSB0`` at 115200bps, you would set the variable as follows::
300
301 TEST_SERIALCONTROL_CMD = "picocom /dev/ttyUSB0 -b 115200"
302
303For local
304devices where the serial port device disappears when the device reboots,
305an additional "serdevtry" wrapper script is provided. To use this
306wrapper, simply prefix the terminal command with
307``${COREBASE}/scripts/contrib/serdevtry``::
308
309 TEST_SERIALCONTROL_CMD = "${COREBASE}/scripts/contrib/serdevtry picocom -b 115200 /dev/ttyUSB0"
310
311Running Tests
312=============
313
314You can start the tests automatically or manually:
315
316- *Automatically running tests:* To run the tests automatically after the
317 OpenEmbedded build system successfully creates an image, first set the
318 :term:`TESTIMAGE_AUTO` variable to "1" in your ``local.conf`` file in the
319 :term:`Build Directory`::
320
321 TESTIMAGE_AUTO = "1"
322
323 Next, build your image. If the image successfully builds, the
324 tests run::
325
326 bitbake core-image-sato
327
328- *Manually running tests:* To manually run the tests, first globally
329 inherit the :ref:`ref-classes-testimage` class by editing your
330 ``local.conf`` file::
331
Andrew Geissler20137392023-10-12 04:59:14 -0600332 IMAGE_CLASSES += "testimage"
Andrew Geissler517393d2023-01-13 08:55:19 -0600333
334 Next, use BitBake to run the tests::
335
336 bitbake -c testimage image
337
338All test files reside in ``meta/lib/oeqa/runtime/cases`` in the
339:term:`Source Directory`. A test name maps
340directly to a Python module. Each test module may contain a number of
341individual tests. Tests are usually grouped together by the area tested
342(e.g tests for systemd reside in ``meta/lib/oeqa/runtime/cases/systemd.py``).
343
344You can add tests to any layer provided you place them in the proper
345area and you extend :term:`BBPATH` in
346the ``local.conf`` file as normal. Be sure that tests reside in
347``layer/lib/oeqa/runtime/cases``.
348
349.. note::
350
351 Be sure that module names do not collide with module names used in
352 the default set of test modules in ``meta/lib/oeqa/runtime/cases``.
353
354You can change the set of tests run by appending or overriding
355:term:`TEST_SUITES` variable in
356``local.conf``. Each name in :term:`TEST_SUITES` represents a required test
357for the image. Test modules named within :term:`TEST_SUITES` cannot be
358skipped even if a test is not suitable for an image (e.g. running the
359RPM tests on an image without ``rpm``). Appending "auto" to
360:term:`TEST_SUITES` causes the build system to try to run all tests that are
361suitable for the image (i.e. each test module may elect to skip itself).
362
363The order you list tests in :term:`TEST_SUITES` is important and influences
364test dependencies. Consequently, tests that depend on other tests should
365be added after the test on which they depend. For example, since the
366``ssh`` test depends on the ``ping`` test, "ssh" needs to come after
367"ping" in the list. The test class provides no re-ordering or dependency
368handling.
369
370.. note::
371
372 Each module can have multiple classes with multiple test methods.
373 And, Python ``unittest`` rules apply.
374
375Here are some things to keep in mind when running tests:
376
377- The default tests for the image are defined as::
378
379 DEFAULT_TEST_SUITES:pn-image = "ping ssh df connman syslog xorg scp vnc date rpm dnf dmesg"
380
381- Add your own test to the list of the by using the following::
382
383 TEST_SUITES:append = " mytest"
384
385- Run a specific list of tests as follows::
386
387 TEST_SUITES = "test1 test2 test3"
388
389 Remember, order is important. Be sure to place a test that is
390 dependent on another test later in the order.
391
392Exporting Tests
393===============
394
395You can export tests so that they can run independently of the build
396system. Exporting tests is required if you want to be able to hand the
397test execution off to a scheduler. You can only export tests that are
398defined in :term:`TEST_SUITES`.
399
400If your image is already built, make sure the following are set in your
401``local.conf`` file::
402
403 INHERIT += "testexport"
404 TEST_TARGET_IP = "IP-address-for-the-test-target"
405 TEST_SERVER_IP = "IP-address-for-the-test-server"
406
407You can then export the tests with the
408following BitBake command form::
409
410 $ bitbake image -c testexport
411
412Exporting the tests places them in the :term:`Build Directory` in
413``tmp/testexport/``\ image, which is controlled by the :term:`TEST_EXPORT_DIR`
414variable.
415
416You can now run the tests outside of the build environment::
417
418 $ cd tmp/testexport/image
419 $ ./runexported.py testdata.json
420
421Here is a complete example that shows IP addresses and uses the
422``core-image-sato`` image::
423
424 INHERIT += "testexport"
425 TEST_TARGET_IP = "192.168.7.2"
426 TEST_SERVER_IP = "192.168.7.1"
427
428Use BitBake to export the tests::
429
430 $ bitbake core-image-sato -c testexport
431
432Run the tests outside of
433the build environment using the following::
434
435 $ cd tmp/testexport/core-image-sato
436 $ ./runexported.py testdata.json
437
438Writing New Tests
439=================
440
441As mentioned previously, all new test files need to be in the proper
442place for the build system to find them. New tests for additional
443functionality outside of the core should be added to the layer that adds
444the functionality, in ``layer/lib/oeqa/runtime/cases`` (as long as
445:term:`BBPATH` is extended in the
446layer's ``layer.conf`` file as normal). Just remember the following:
447
448- Filenames need to map directly to test (module) names.
449
450- Do not use module names that collide with existing core tests.
451
452- Minimally, an empty ``__init__.py`` file must be present in the runtime
453 directory.
454
455To create a new test, start by copying an existing module (e.g.
456``syslog.py`` or ``gcc.py`` are good ones to use). Test modules can use
457code from ``meta/lib/oeqa/utils``, which are helper classes.
458
459.. note::
460
461 Structure shell commands such that you rely on them and they return a
462 single code for success. Be aware that sometimes you will need to
463 parse the output. See the ``df.py`` and ``date.py`` modules for examples.
464
465You will notice that all test classes inherit ``oeRuntimeTest``, which
466is found in ``meta/lib/oetest.py``. This base class offers some helper
467attributes, which are described in the following sections:
468
469Class Methods
470-------------
471
472Class methods are as follows:
473
474- *hasPackage(pkg):* Returns "True" if ``pkg`` is in the installed
475 package list of the image, which is based on the manifest file that
476 is generated during the :ref:`ref-tasks-rootfs` task.
477
478- *hasFeature(feature):* Returns "True" if the feature is in
479 :term:`IMAGE_FEATURES` or
480 :term:`DISTRO_FEATURES`.
481
482Class Attributes
483----------------
484
485Class attributes are as follows:
486
487- *pscmd:* Equals "ps -ef" if ``procps`` is installed in the image.
488 Otherwise, ``pscmd`` equals "ps" (busybox).
489
490- *tc:* The called test context, which gives access to the
491 following attributes:
492
493 - *d:* The BitBake datastore, which allows you to use stuff such
494 as ``oeRuntimeTest.tc.d.getVar("VIRTUAL-RUNTIME_init_manager")``.
495
496 - *testslist and testsrequired:* Used internally. The tests
497 do not need these.
498
499 - *filesdir:* The absolute path to
500 ``meta/lib/oeqa/runtime/files``, which contains helper files for
501 tests meant for copying on the target such as small files written
502 in C for compilation.
503
504 - *target:* The target controller object used to deploy and
505 start an image on a particular target (e.g. Qemu, SimpleRemote,
506 and SystemdbootTarget). Tests usually use the following:
507
508 - *ip:* The target's IP address.
509
510 - *server_ip:* The host's IP address, which is usually used
511 by the DNF test suite.
512
513 - *run(cmd, timeout=None):* The single, most used method.
514 This command is a wrapper for: ``ssh root@host "cmd"``. The
515 command returns a tuple: (status, output), which are what their
516 names imply - the return code of "cmd" and whatever output it
517 produces. The optional timeout argument represents the number
518 of seconds the test should wait for "cmd" to return. If the
519 argument is "None", the test uses the default instance's
520 timeout period, which is 300 seconds. If the argument is "0",
521 the test runs until the command returns.
522
523 - *copy_to(localpath, remotepath):*
524 ``scp localpath root@ip:remotepath``.
525
526 - *copy_from(remotepath, localpath):*
527 ``scp root@host:remotepath localpath``.
528
529Instance Attributes
530-------------------
531
532There is a single instance attribute, which is ``target``. The ``target``
533instance attribute is identical to the class attribute of the same name,
534which is described in the previous section. This attribute exists as
535both an instance and class attribute so tests can use
536``self.target.run(cmd)`` in instance methods instead of
537``oeRuntimeTest.tc.target.run(cmd)``.
538
539Installing Packages in the DUT Without the Package Manager
540==========================================================
541
542When a test requires a package built by BitBake, it is possible to
543install that package. Installing the package does not require a package
544manager be installed in the device under test (DUT). It does, however,
545require an SSH connection and the target must be using the
546``sshcontrol`` class.
547
548.. note::
549
550 This method uses ``scp`` to copy files from the host to the target, which
551 causes permissions and special attributes to be lost.
552
553A JSON file is used to define the packages needed by a test. This file
554must be in the same path as the file used to define the tests.
555Furthermore, the filename must map directly to the test module name with
556a ``.json`` extension.
557
558The JSON file must include an object with the test name as keys of an
559object or an array. This object (or array of objects) uses the following
560data:
561
562- "pkg" --- a mandatory string that is the name of the package to be
563 installed.
564
565- "rm" --- an optional boolean, which defaults to "false", that specifies
566 to remove the package after the test.
567
568- "extract" --- an optional boolean, which defaults to "false", that
569 specifies if the package must be extracted from the package format.
570 When set to "true", the package is not automatically installed into
571 the DUT.
572
573Following is an example JSON file that handles test "foo" installing
574package "bar" and test "foobar" installing packages "foo" and "bar".
575Once the test is complete, the packages are removed from the DUT::
576
577 {
578 "foo": {
579 "pkg": "bar"
580 },
581 "foobar": [
582 {
583 "pkg": "foo",
584 "rm": true
585 },
586 {
587 "pkg": "bar",
588 "rm": true
589 }
590 ]
591 }
592