runtime-testing.rst 22 KB

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