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