runtime-testing.rst 22 KB

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