2
0

testing.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. ===============
  2. Testing in QEMU
  3. ===============
  4. This document describes the testing infrastructure in QEMU.
  5. Testing with "make check"
  6. =========================
  7. The "make check" testing family includes most of the C based tests in QEMU. For
  8. a quick help, run ``make check-help`` from the source tree.
  9. The usual way to run these tests is:
  10. .. code::
  11. make check
  12. which includes QAPI schema tests, unit tests, and QTests. Different sub-types
  13. of "make check" tests will be explained below.
  14. Before running tests, it is best to build QEMU programs first. Some tests
  15. expect the executables to exist and will fail with obscure messages if they
  16. cannot find them.
  17. Unit tests
  18. ----------
  19. Unit tests, which can be invoked with ``make check-unit``, are simple C tests
  20. that typically link to individual QEMU object files and exercise them by
  21. calling exported functions.
  22. If you are writing new code in QEMU, consider adding a unit test, especially
  23. for utility modules that are relatively stateless or have few dependencies. To
  24. add a new unit test:
  25. 1. Create a new source file. For example, ``tests/foo-test.c``.
  26. 2. Write the test. Normally you would include the header file which exports
  27. the module API, then verify the interface behaves as expected from your
  28. test. The test code should be organized with the glib testing framework.
  29. Copying and modifying an existing test is usually a good idea.
  30. 3. Add the test to ``tests/Makefile.include``. First, name the unit test
  31. program and add it to ``$(check-unit-y)``; then add a rule to build the
  32. executable. For example:
  33. .. code::
  34. check-unit-y += tests/foo-test$(EXESUF)
  35. tests/foo-test$(EXESUF): tests/foo-test.o $(test-util-obj-y)
  36. ...
  37. Since unit tests don't require environment variables, the simplest way to debug
  38. a unit test failure is often directly invoking it or even running it under
  39. ``gdb``. However there can still be differences in behavior between ``make``
  40. invocations and your manual run, due to ``$MALLOC_PERTURB_`` environment
  41. variable (which affects memory reclamation and catches invalid pointers better)
  42. and gtester options. If necessary, you can run
  43. .. code::
  44. make check-unit V=1
  45. and copy the actual command line which executes the unit test, then run
  46. it from the command line.
  47. QTest
  48. -----
  49. QTest is a device emulation testing framework. It can be very useful to test
  50. device models; it could also control certain aspects of QEMU (such as virtual
  51. clock stepping), with a special purpose "qtest" protocol. Refer to the
  52. documentation in ``qtest.c`` for more details of the protocol.
  53. QTest cases can be executed with
  54. .. code::
  55. make check-qtest
  56. The QTest library is implemented by ``tests/libqtest.c`` and the API is defined
  57. in ``tests/libqtest.h``.
  58. Consider adding a new QTest case when you are introducing a new virtual
  59. hardware, or extending one if you are adding functionalities to an existing
  60. virtual device.
  61. On top of libqtest, a higher level library, ``libqos``, was created to
  62. encapsulate common tasks of device drivers, such as memory management and
  63. communicating with system buses or devices. Many virtual device tests use
  64. libqos instead of directly calling into libqtest.
  65. Steps to add a new QTest case are:
  66. 1. Create a new source file for the test. (More than one file can be added as
  67. necessary.) For example, ``tests/test-foo-device.c``.
  68. 2. Write the test code with the glib and libqtest/libqos API. See also existing
  69. tests and the library headers for reference.
  70. 3. Register the new test in ``tests/Makefile.include``. Add the test executable
  71. name to an appropriate ``check-qtest-*-y`` variable. For example:
  72. ``check-qtest-generic-y = tests/test-foo-device$(EXESUF)``
  73. 4. Add object dependencies of the executable in the Makefile, including the
  74. test source file(s) and other interesting objects. For example:
  75. ``tests/test-foo-device$(EXESUF): tests/test-foo-device.o $(libqos-obj-y)``
  76. Debugging a QTest failure is slightly harder than the unit test because the
  77. tests look up QEMU program names in the environment variables, such as
  78. ``QTEST_QEMU_BINARY`` and ``QTEST_QEMU_IMG``, and also because it is not easy
  79. to attach gdb to the QEMU process spawned from the test. But manual invoking
  80. and using gdb on the test is still simple to do: find out the actual command
  81. from the output of
  82. .. code::
  83. make check-qtest V=1
  84. which you can run manually.
  85. QAPI schema tests
  86. -----------------
  87. The QAPI schema tests validate the QAPI parser used by QMP, by feeding
  88. predefined input to the parser and comparing the result with the reference
  89. output.
  90. The input/output data is managed under the ``tests/qapi-schema`` directory.
  91. Each test case includes four files that have a common base name:
  92. * ``${casename}.json`` - the file contains the JSON input for feeding the
  93. parser
  94. * ``${casename}.out`` - the file contains the expected stdout from the parser
  95. * ``${casename}.err`` - the file contains the expected stderr from the parser
  96. * ``${casename}.exit`` - the expected error code
  97. Consider adding a new QAPI schema test when you are making a change on the QAPI
  98. parser (either fixing a bug or extending/modifying the syntax). To do this:
  99. 1. Add four files for the new case as explained above. For example:
  100. ``$EDITOR tests/qapi-schema/foo.{json,out,err,exit}``.
  101. 2. Add the new test in ``tests/Makefile.include``. For example:
  102. ``qapi-schema += foo.json``
  103. check-block
  104. -----------
  105. ``make check-block`` is a legacy command to invoke block layer iotests and is
  106. rarely used. See "QEMU iotests" section below for more information.
  107. GCC gcov support
  108. ----------------
  109. ``gcov`` is a GCC tool to analyze the testing coverage by
  110. instrumenting the tested code. To use it, configure QEMU with
  111. ``--enable-gcov`` option and build. Then run ``make check`` as usual.
  112. If you want to gather coverage information on a single test the ``make
  113. clean-coverage`` target can be used to delete any existing coverage
  114. information before running a single test.
  115. You can generate a HTML coverage report by executing ``make
  116. coverage-report`` which will create
  117. ./reports/coverage/coverage-report.html. If you want to create it
  118. elsewhere simply execute ``make /foo/bar/baz/coverage-report.html``.
  119. Further analysis can be conducted by running the ``gcov`` command
  120. directly on the various .gcda output files. Please read the ``gcov``
  121. documentation for more information.
  122. QEMU iotests
  123. ============
  124. QEMU iotests, under the directory ``tests/qemu-iotests``, is the testing
  125. framework widely used to test block layer related features. It is higher level
  126. than "make check" tests and 99% of the code is written in bash or Python
  127. scripts. The testing success criteria is golden output comparison, and the
  128. test files are named with numbers.
  129. To run iotests, make sure QEMU is built successfully, then switch to the
  130. ``tests/qemu-iotests`` directory under the build directory, and run ``./check``
  131. with desired arguments from there.
  132. By default, "raw" format and "file" protocol is used; all tests will be
  133. executed, except the unsupported ones. You can override the format and protocol
  134. with arguments:
  135. .. code::
  136. # test with qcow2 format
  137. ./check -qcow2
  138. # or test a different protocol
  139. ./check -nbd
  140. It's also possible to list test numbers explicitly:
  141. .. code::
  142. # run selected cases with qcow2 format
  143. ./check -qcow2 001 030 153
  144. Cache mode can be selected with the "-c" option, which may help reveal bugs
  145. that are specific to certain cache mode.
  146. More options are supported by the ``./check`` script, run ``./check -h`` for
  147. help.
  148. Writing a new test case
  149. -----------------------
  150. Consider writing a tests case when you are making any changes to the block
  151. layer. An iotest case is usually the choice for that. There are already many
  152. test cases, so it is possible that extending one of them may achieve the goal
  153. and save the boilerplate to create one. (Unfortunately, there isn't a 100%
  154. reliable way to find a related one out of hundreds of tests. One approach is
  155. using ``git grep``.)
  156. Usually an iotest case consists of two files. One is an executable that
  157. produces output to stdout and stderr, the other is the expected reference
  158. output. They are given the same number in file names. E.g. Test script ``055``
  159. and reference output ``055.out``.
  160. In rare cases, when outputs differ between cache mode ``none`` and others, a
  161. ``.out.nocache`` file is added. In other cases, when outputs differ between
  162. image formats, more than one ``.out`` files are created ending with the
  163. respective format names, e.g. ``178.out.qcow2`` and ``178.out.raw``.
  164. There isn't a hard rule about how to write a test script, but a new test is
  165. usually a (copy and) modification of an existing case. There are a few
  166. commonly used ways to create a test:
  167. * A Bash script. It will make use of several environmental variables related
  168. to the testing procedure, and could source a group of ``common.*`` libraries
  169. for some common helper routines.
  170. * A Python unittest script. Import ``iotests`` and create a subclass of
  171. ``iotests.QMPTestCase``, then call ``iotests.main`` method. The downside of
  172. this approach is that the output is too scarce, and the script is considered
  173. harder to debug.
  174. * A simple Python script without using unittest module. This could also import
  175. ``iotests`` for launching QEMU and utilities etc, but it doesn't inherit
  176. from ``iotests.QMPTestCase`` therefore doesn't use the Python unittest
  177. execution. This is a combination of 1 and 2.
  178. Pick the language per your preference since both Bash and Python have
  179. comparable library support for invoking and interacting with QEMU programs. If
  180. you opt for Python, it is strongly recommended to write Python 3 compatible
  181. code.
  182. Both Python and Bash frameworks in iotests provide helpers to manage test
  183. images. They can be used to create and clean up images under the test
  184. directory. If no I/O or any protocol specific feature is needed, it is often
  185. more convenient to use the pseudo block driver, ``null-co://``, as the test
  186. image, which doesn't require image creation or cleaning up. Avoid system-wide
  187. devices or files whenever possible, such as ``/dev/null`` or ``/dev/zero``.
  188. Otherwise, image locking implications have to be considered. For example,
  189. another application on the host may have locked the file, possibly leading to a
  190. test failure. If using such devices are explicitly desired, consider adding
  191. ``locking=off`` option to disable image locking.
  192. .. _docker-ref:
  193. Docker based tests
  194. ==================
  195. Introduction
  196. ------------
  197. The Docker testing framework in QEMU utilizes public Docker images to build and
  198. test QEMU in predefined and widely accessible Linux environments. This makes
  199. it possible to expand the test coverage across distros, toolchain flavors and
  200. library versions.
  201. Prerequisites
  202. -------------
  203. Install "docker" with the system package manager and start the Docker service
  204. on your development machine, then make sure you have the privilege to run
  205. Docker commands. Typically it means setting up passwordless ``sudo docker``
  206. command or login as root. For example:
  207. .. code::
  208. $ sudo yum install docker
  209. $ # or `apt-get install docker` for Ubuntu, etc.
  210. $ sudo systemctl start docker
  211. $ sudo docker ps
  212. The last command should print an empty table, to verify the system is ready.
  213. An alternative method to set up permissions is by adding the current user to
  214. "docker" group and making the docker daemon socket file (by default
  215. ``/var/run/docker.sock``) accessible to the group:
  216. .. code::
  217. $ sudo groupadd docker
  218. $ sudo usermod $USER -a -G docker
  219. $ sudo chown :docker /var/run/docker.sock
  220. Note that any one of above configurations makes it possible for the user to
  221. exploit the whole host with Docker bind mounting or other privileged
  222. operations. So only do it on development machines.
  223. Quickstart
  224. ----------
  225. From source tree, type ``make docker`` to see the help. Testing can be started
  226. without configuring or building QEMU (``configure`` and ``make`` are done in
  227. the container, with parameters defined by the make target):
  228. .. code::
  229. make docker-test-build@min-glib
  230. This will create a container instance using the ``min-glib`` image (the image
  231. is downloaded and initialized automatically), in which the ``test-build`` job
  232. is executed.
  233. Images
  234. ------
  235. Along with many other images, the ``min-glib`` image is defined in a Dockerfile
  236. in ``tests/docker/dockerfiles/``, called ``min-glib.docker``. ``make docker``
  237. command will list all the available images.
  238. To add a new image, simply create a new ``.docker`` file under the
  239. ``tests/docker/dockerfiles/`` directory.
  240. A ``.pre`` script can be added beside the ``.docker`` file, which will be
  241. executed before building the image under the build context directory. This is
  242. mainly used to do necessary host side setup. One such setup is ``binfmt_misc``,
  243. for example, to make qemu-user powered cross build containers work.
  244. Tests
  245. -----
  246. Different tests are added to cover various configurations to build and test
  247. QEMU. Docker tests are the executables under ``tests/docker`` named
  248. ``test-*``. They are typically shell scripts and are built on top of a shell
  249. library, ``tests/docker/common.rc``, which provides helpers to find the QEMU
  250. source and build it.
  251. The full list of tests is printed in the ``make docker`` help.
  252. Tools
  253. -----
  254. There are executables that are created to run in a specific Docker environment.
  255. This makes it easy to write scripts that have heavy or special dependencies,
  256. but are still very easy to use.
  257. Currently the only tool is ``travis``, which mimics the Travis-CI tests in a
  258. container. It runs in the ``travis`` image:
  259. .. code::
  260. make docker-travis@travis
  261. Debugging a Docker test failure
  262. -------------------------------
  263. When CI tasks, maintainers or yourself report a Docker test failure, follow the
  264. below steps to debug it:
  265. 1. Locally reproduce the failure with the reported command line. E.g. run
  266. ``make docker-test-mingw@fedora J=8``.
  267. 2. Add "V=1" to the command line, try again, to see the verbose output.
  268. 3. Further add "DEBUG=1" to the command line. This will pause in a shell prompt
  269. in the container right before testing starts. You could either manually
  270. build QEMU and run tests from there, or press Ctrl-D to let the Docker
  271. testing continue.
  272. 4. If you press Ctrl-D, the same building and testing procedure will begin, and
  273. will hopefully run into the error again. After that, you will be dropped to
  274. the prompt for debug.
  275. Options
  276. -------
  277. Various options can be used to affect how Docker tests are done. The full
  278. list is in the ``make docker`` help text. The frequently used ones are:
  279. * ``V=1``: the same as in top level ``make``. It will be propagated to the
  280. container and enable verbose output.
  281. * ``J=$N``: the number of parallel tasks in make commands in the container,
  282. similar to the ``-j $N`` option in top level ``make``. (The ``-j`` option in
  283. top level ``make`` will not be propagated into the container.)
  284. * ``DEBUG=1``: enables debug. See the previous "Debugging a Docker test
  285. failure" section.
  286. VM testing
  287. ==========
  288. This test suite contains scripts that bootstrap various guest images that have
  289. necessary packages to build QEMU. The basic usage is documented in ``Makefile``
  290. help which is displayed with ``make vm-help``.
  291. Quickstart
  292. ----------
  293. Run ``make vm-help`` to list available make targets. Invoke a specific make
  294. command to run build test in an image. For example, ``make vm-build-freebsd``
  295. will build the source tree in the FreeBSD image. The command can be executed
  296. from either the source tree or the build dir; if the former, ``./configure`` is
  297. not needed. The command will then generate the test image in ``./tests/vm/``
  298. under the working directory.
  299. Note: images created by the scripts accept a well-known RSA key pair for SSH
  300. access, so they SHOULD NOT be exposed to external interfaces if you are
  301. concerned about attackers taking control of the guest and potentially
  302. exploiting a QEMU security bug to compromise the host.
  303. QEMU binary
  304. -----------
  305. By default, qemu-system-x86_64 is searched in $PATH to run the guest. If there
  306. isn't one, or if it is older than 2.10, the test won't work. In this case,
  307. provide the QEMU binary in env var: ``QEMU=/path/to/qemu-2.10+``.
  308. Make jobs
  309. ---------
  310. The ``-j$X`` option in the make command line is not propagated into the VM,
  311. specify ``J=$X`` to control the make jobs in the guest.
  312. Debugging
  313. ---------
  314. Add ``DEBUG=1`` and/or ``V=1`` to the make command to allow interactive
  315. debugging and verbose output. If this is not enough, see the next section.
  316. ``V=1`` will be propagated down into the make jobs in the guest.
  317. Manual invocation
  318. -----------------
  319. Each guest script is an executable script with the same command line options.
  320. For example to work with the netbsd guest, use ``$QEMU_SRC/tests/vm/netbsd``:
  321. .. code::
  322. $ cd $QEMU_SRC/tests/vm
  323. # To bootstrap the image
  324. $ ./netbsd --build-image --image /var/tmp/netbsd.img
  325. <...>
  326. # To run an arbitrary command in guest (the output will not be echoed unless
  327. # --debug is added)
  328. $ ./netbsd --debug --image /var/tmp/netbsd.img uname -a
  329. # To build QEMU in guest
  330. $ ./netbsd --debug --image /var/tmp/netbsd.img --build-qemu $QEMU_SRC
  331. # To get to an interactive shell
  332. $ ./netbsd --interactive --image /var/tmp/netbsd.img sh
  333. Adding new guests
  334. -----------------
  335. Please look at existing guest scripts for how to add new guests.
  336. Most importantly, create a subclass of BaseVM and implement ``build_image()``
  337. method and define ``BUILD_SCRIPT``, then finally call ``basevm.main()`` from
  338. the script's ``main()``.
  339. * Usually in ``build_image()``, a template image is downloaded from a
  340. predefined URL. ``BaseVM._download_with_cache()`` takes care of the cache and
  341. the checksum, so consider using it.
  342. * Once the image is downloaded, users, SSH server and QEMU build deps should
  343. be set up:
  344. - Root password set to ``BaseVM.ROOT_PASS``
  345. - User ``BaseVM.GUEST_USER`` is created, and password set to
  346. ``BaseVM.GUEST_PASS``
  347. - SSH service is enabled and started on boot,
  348. ``$QEMU_SRC/tests/keys/id_rsa.pub`` is added to ssh's ``authorized_keys``
  349. file of both root and the normal user
  350. - DHCP client service is enabled and started on boot, so that it can
  351. automatically configure the virtio-net-pci NIC and communicate with QEMU
  352. user net (10.0.2.2)
  353. - Necessary packages are installed to untar the source tarball and build
  354. QEMU
  355. * Write a proper ``BUILD_SCRIPT`` template, which should be a shell script that
  356. untars a raw virtio-blk block device, which is the tarball data blob of the
  357. QEMU source tree, then configure/build it. Running "make check" is also
  358. recommended.
  359. Image fuzzer testing
  360. ====================
  361. An image fuzzer was added to exercise format drivers. Currently only qcow2 is
  362. supported. To start the fuzzer, run
  363. .. code::
  364. tests/image-fuzzer/runner.py -c '[["qemu-img", "info", "$test_img"]]' /tmp/test qcow2
  365. Alternatively, some command different from "qemu-img info" can be tested, by
  366. changing the ``-c`` option.
  367. Acceptance tests using the Avocado Framework
  368. ============================================
  369. The ``tests/acceptance`` directory hosts functional tests, also known
  370. as acceptance level tests. They're usually higher level tests, and
  371. may interact with external resources and with various guest operating
  372. systems.
  373. These tests are written using the Avocado Testing Framework (which must
  374. be installed separately) in conjunction with a the ``avocado_qemu.Test``
  375. class, implemented at ``tests/acceptance/avocado_qemu``.
  376. Tests based on ``avocado_qemu.Test`` can easily:
  377. * Customize the command line arguments given to the convenience
  378. ``self.vm`` attribute (a QEMUMachine instance)
  379. * Interact with the QEMU monitor, send QMP commands and check
  380. their results
  381. * Interact with the guest OS, using the convenience console device
  382. (which may be useful to assert the effectiveness and correctness of
  383. command line arguments or QMP commands)
  384. * Interact with external data files that accompany the test itself
  385. (see ``self.get_data()``)
  386. * Download (and cache) remote data files, such as firmware and kernel
  387. images
  388. * Have access to a library of guest OS images (by means of the
  389. ``avocado.utils.vmimage`` library)
  390. * Make use of various other test related utilities available at the
  391. test class itself and at the utility library:
  392. - http://avocado-framework.readthedocs.io/en/latest/api/test/avocado.html#avocado.Test
  393. - http://avocado-framework.readthedocs.io/en/latest/api/utils/avocado.utils.html
  394. Running tests
  395. -------------
  396. You can run the acceptance tests simply by executing:
  397. .. code::
  398. make check-acceptance
  399. This involves the automatic creation of Python virtual environment
  400. within the build tree (at ``tests/venv``) which will have all the
  401. right dependencies, and will save tests results also within the
  402. build tree (at ``tests/results``).
  403. Note: the build environment must be using a Python 3 stack, and have
  404. the ``venv`` and ``pip`` packages installed. If necessary, make sure
  405. ``configure`` is called with ``--python=`` and that those modules are
  406. available. On Debian and Ubuntu based systems, depending on the
  407. specific version, they may be on packages named ``python3-venv`` and
  408. ``python3-pip``.
  409. The scripts installed inside the virtual environment may be used
  410. without an "activation". For instance, the Avocado test runner
  411. may be invoked by running:
  412. .. code::
  413. tests/venv/bin/avocado run $OPTION1 $OPTION2 tests/acceptance/
  414. Manual Installation
  415. -------------------
  416. To manually install Avocado and its dependencies, run:
  417. .. code::
  418. pip install --user avocado-framework
  419. Alternatively, follow the instructions on this link:
  420. http://avocado-framework.readthedocs.io/en/latest/GetStartedGuide.html#installing-avocado
  421. Overview
  422. --------
  423. The ``tests/acceptance/avocado_qemu`` directory provides the
  424. ``avocado_qemu`` Python module, containing the ``avocado_qemu.Test``
  425. class. Here's a simple usage example:
  426. .. code::
  427. from avocado_qemu import Test
  428. class Version(Test):
  429. """
  430. :avocado: tags=quick
  431. """
  432. def test_qmp_human_info_version(self):
  433. self.vm.launch()
  434. res = self.vm.command('human-monitor-command',
  435. command_line='info version')
  436. self.assertRegexpMatches(res, r'^(\d+\.\d+\.\d)')
  437. To execute your test, run:
  438. .. code::
  439. avocado run version.py
  440. Tests may be classified according to a convention by using docstring
  441. directives such as ``:avocado: tags=TAG1,TAG2``. To run all tests
  442. in the current directory, tagged as "quick", run:
  443. .. code::
  444. avocado run -t quick .
  445. The ``avocado_qemu.Test`` base test class
  446. -----------------------------------------
  447. The ``avocado_qemu.Test`` class has a number of characteristics that
  448. are worth being mentioned right away.
  449. First of all, it attempts to give each test a ready to use QEMUMachine
  450. instance, available at ``self.vm``. Because many tests will tweak the
  451. QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
  452. is left to the test writer.
  453. The base test class has also support for tests with more than one
  454. QEMUMachine. The way to get machines is through the ``self.get_vm()``
  455. method which will return a QEMUMachine instance. The ``self.get_vm()``
  456. method accepts arguments that will be passed to the QEMUMachine creation
  457. and also an optional `name` attribute so you can identify a specific
  458. machine and get it more than once through the tests methods. A simple
  459. and hypothetical example follows:
  460. .. code::
  461. from avocado_qemu import Test
  462. class MultipleMachines(Test):
  463. """
  464. :avocado: enable
  465. """
  466. def test_multiple_machines(self):
  467. first_machine = self.get_vm()
  468. second_machine = self.get_vm()
  469. self.get_vm(name='third_machine').launch()
  470. first_machine.launch()
  471. second_machine.launch()
  472. first_res = first_machine.command(
  473. 'human-monitor-command',
  474. command_line='info version')
  475. second_res = second_machine.command(
  476. 'human-monitor-command',
  477. command_line='info version')
  478. third_res = self.get_vm(name='third_machine').command(
  479. 'human-monitor-command',
  480. command_line='info version')
  481. self.assertEquals(first_res, second_res, third_res)
  482. At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines
  483. shutdown.
  484. QEMUMachine
  485. ~~~~~~~~~~~
  486. The QEMUMachine API is already widely used in the Python iotests,
  487. device-crash-test and other Python scripts. It's a wrapper around the
  488. execution of a QEMU binary, giving its users:
  489. * the ability to set command line arguments to be given to the QEMU
  490. binary
  491. * a ready to use QMP connection and interface, which can be used to
  492. send commands and inspect its results, as well as asynchronous
  493. events
  494. * convenience methods to set commonly used command line arguments in
  495. a more succinct and intuitive way
  496. QEMU binary selection
  497. ~~~~~~~~~~~~~~~~~~~~~
  498. The QEMU binary used for the ``self.vm`` QEMUMachine instance will
  499. primarily depend on the value of the ``qemu_bin`` parameter. If it's
  500. not explicitly set, its default value will be the result of a dynamic
  501. probe in the same source tree. A suitable binary will be one that
  502. targets the architecture matching host machine.
  503. Based on this description, test writers will usually rely on one of
  504. the following approaches:
  505. 1) Set ``qemu_bin``, and use the given binary
  506. 2) Do not set ``qemu_bin``, and use a QEMU binary named like
  507. "${arch}-softmmu/qemu-system-${arch}", either in the current
  508. working directory, or in the current source tree.
  509. The resulting ``qemu_bin`` value will be preserved in the
  510. ``avocado_qemu.Test`` as an attribute with the same name.
  511. Attribute reference
  512. -------------------
  513. Besides the attributes and methods that are part of the base
  514. ``avocado.Test`` class, the following attributes are available on any
  515. ``avocado_qemu.Test`` instance.
  516. vm
  517. ~~
  518. A QEMUMachine instance, initially configured according to the given
  519. ``qemu_bin`` parameter.
  520. arch
  521. ~~~~
  522. The architecture can be used on different levels of the stack, e.g. by
  523. the framework or by the test itself. At the framework level, it will
  524. currently influence the selection of a QEMU binary (when one is not
  525. explicitly given).
  526. Tests are also free to use this attribute value, for their own needs.
  527. A test may, for instance, use the same value when selecting the
  528. architecture of a kernel or disk image to boot a VM with.
  529. The ``arch`` attribute will be set to the test parameter of the same
  530. name. If one is not given explicitly, it will either be set to
  531. ``None``, or, if the test is tagged with one (and only one)
  532. ``:avocado: tags=arch:VALUE`` tag, it will be set to ``VALUE``.
  533. qemu_bin
  534. ~~~~~~~~
  535. The preserved value of the ``qemu_bin`` parameter or the result of the
  536. dynamic probe for a QEMU binary in the current working directory or
  537. source tree.
  538. Parameter reference
  539. -------------------
  540. To understand how Avocado parameters are accessed by tests, and how
  541. they can be passed to tests, please refer to::
  542. http://avocado-framework.readthedocs.io/en/latest/WritingTests.html#accessing-test-parameters
  543. Parameter values can be easily seen in the log files, and will look
  544. like the following:
  545. .. code::
  546. PARAMS (key=qemu_bin, path=*, default=x86_64-softmmu/qemu-system-x86_64) => 'x86_64-softmmu/qemu-system-x86_64
  547. arch
  548. ~~~~
  549. The architecture that will influence the selection of a QEMU binary
  550. (when one is not explicitly given).
  551. Tests are also free to use this parameter value, for their own needs.
  552. A test may, for instance, use the same value when selecting the
  553. architecture of a kernel or disk image to boot a VM with.
  554. This parameter has a direct relation with the ``arch`` attribute. If
  555. not given, it will default to None.
  556. qemu_bin
  557. ~~~~~~~~
  558. The exact QEMU binary to be used on QEMUMachine.
  559. Uninstalling Avocado
  560. --------------------
  561. If you've followed the manual installation instructions above, you can
  562. easily uninstall Avocado. Start by listing the packages you have
  563. installed::
  564. pip list --user
  565. And remove any package you want with::
  566. pip uninstall <package_name>
  567. If you've used ``make check-acceptance``, the Python virtual environment where
  568. Avocado is installed will be cleaned up as part of ``make check-clean``.
  569. Testing with "make check-tcg"
  570. =============================
  571. The check-tcg tests are intended for simple smoke tests of both
  572. linux-user and softmmu TCG functionality. However to build test
  573. programs for guest targets you need to have cross compilers available.
  574. If your distribution supports cross compilers you can do something as
  575. simple as::
  576. apt install gcc-aarch64-linux-gnu
  577. The configure script will automatically pick up their presence.
  578. Sometimes compilers have slightly odd names so the availability of
  579. them can be prompted by passing in the appropriate configure option
  580. for the architecture in question, for example::
  581. $(configure) --cross-cc-aarch64=aarch64-cc
  582. There is also a ``--cross-cc-flags-ARCH`` flag in case additional
  583. compiler flags are needed to build for a given target.
  584. If you have the ability to run containers as the user you can also
  585. take advantage of the build systems "Docker" support. It will then use
  586. containers to build any test case for an enabled guest where there is
  587. no system compiler available. See :ref: `_docker-ref` for details.
  588. Running subset of tests
  589. -----------------------
  590. You can build the tests for one architecture::
  591. make build-tcg-tests-$TARGET
  592. And run with::
  593. make run-tcg-tests-$TARGET
  594. Adding ``V=1`` to the invocation will show the details of how to
  595. invoke QEMU for the test which is useful for debugging tests.
  596. TCG test dependencies
  597. ---------------------
  598. The TCG tests are deliberately very light on dependencies and are
  599. either totally bare with minimal gcc lib support (for softmmu tests)
  600. or just glibc (for linux-user tests). This is because getting a cross
  601. compiler to work with additional libraries can be challenging.
  602. Other TCG Tests
  603. ---------------
  604. There are a number of out-of-tree test suites that are used for more
  605. extensive testing of processor features.
  606. KVM Unit Tests
  607. ~~~~~~~~~~~~~~
  608. The KVM unit tests are designed to run as a Guest OS under KVM but
  609. there is no reason why they can't exercise the TCG as well. It
  610. provides a minimal OS kernel with hooks for enabling the MMU as well
  611. as reporting test results via a special device::
  612. https://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
  613. Linux Test Project
  614. ~~~~~~~~~~~~~~~~~~
  615. The LTP is focused on exercising the syscall interface of a Linux
  616. kernel. It checks that syscalls behave as documented and strives to
  617. exercise as many corner cases as possible. It is a useful test suite
  618. to run to exercise QEMU's linux-user code::
  619. https://linux-test-project.github.io/