1c3e24cffSThomas Huth.. _checkfunctional-ref: 2c3e24cffSThomas Huth 3c3e24cffSThomas HuthFunctional testing with Python 4c3e24cffSThomas Huth============================== 5c3e24cffSThomas Huth 6c3e24cffSThomas HuthThe ``tests/functional`` directory hosts functional tests written in 7c3e24cffSThomas HuthPython. They are usually higher level tests, and may interact with 8c3e24cffSThomas Huthexternal resources and with various guest operating systems. 9c3e24cffSThomas HuthThe functional tests have initially evolved from the Avocado tests, so there 10c3e24cffSThomas Huthis a lot of similarity to those tests here (see :ref:`checkavocado-ref` for 11c3e24cffSThomas Huthdetails about the Avocado tests). 12c3e24cffSThomas Huth 13c3e24cffSThomas HuthThe tests should be written in the style of the Python `unittest`_ framework, 14c3e24cffSThomas Huthusing stdio for the TAP protocol. The folder ``tests/functional/qemu_test`` 15c3e24cffSThomas Huthprovides classes (e.g. the ``QemuBaseTest``, ``QemuUserTest`` and the 16c3e24cffSThomas Huth``QemuSystemTest`` classes) and utility functions that help to get your test 17c3e24cffSThomas Huthinto the right shape, e.g. by replacing the 'stdout' python object to redirect 18c3e24cffSThomas Huththe normal output of your test to stderr instead. 19c3e24cffSThomas Huth 20c3e24cffSThomas HuthNote that if you don't use one of the QemuBaseTest based classes for your 21c3e24cffSThomas Huthtest, or if you spawn subprocesses from your test, you have to make sure 22c3e24cffSThomas Huththat there is no TAP-incompatible output written to stdio, e.g. either by 23c3e24cffSThomas Huthprefixing every line with a "# " to mark the output as a TAP comment, or 24c3e24cffSThomas Huthe.g. by capturing the stdout output of subprocesses (redirecting it to 25c3e24cffSThomas Huthstderr is OK). 26c3e24cffSThomas Huth 27c3e24cffSThomas HuthTests based on ``qemu_test.QemuSystemTest`` can easily: 28c3e24cffSThomas Huth 29c3e24cffSThomas Huth * Customize the command line arguments given to the convenience 30c3e24cffSThomas Huth ``self.vm`` attribute (a QEMUMachine instance) 31c3e24cffSThomas Huth 32c3e24cffSThomas Huth * Interact with the QEMU monitor, send QMP commands and check 33c3e24cffSThomas Huth their results 34c3e24cffSThomas Huth 35c3e24cffSThomas Huth * Interact with the guest OS, using the convenience console device 36c3e24cffSThomas Huth (which may be useful to assert the effectiveness and correctness of 37c3e24cffSThomas Huth command line arguments or QMP commands) 38c3e24cffSThomas Huth 39c3e24cffSThomas Huth * Download (and cache) remote data files, such as firmware and kernel 40c3e24cffSThomas Huth images 41c3e24cffSThomas Huth 42c3e24cffSThomas HuthRunning tests 43c3e24cffSThomas Huth------------- 44c3e24cffSThomas Huth 45c3e24cffSThomas HuthYou can run the functional tests simply by executing: 46c3e24cffSThomas Huth 47c3e24cffSThomas Huth.. code:: 48c3e24cffSThomas Huth 49c3e24cffSThomas Huth make check-functional 50c3e24cffSThomas Huth 51c3e24cffSThomas HuthIt is also possible to run tests for a certain target only, for example 52c3e24cffSThomas Huththe following line will only run the tests for the x86_64 target: 53c3e24cffSThomas Huth 54c3e24cffSThomas Huth.. code:: 55c3e24cffSThomas Huth 56c3e24cffSThomas Huth make check-functional-x86_64 57c3e24cffSThomas Huth 58c3e24cffSThomas HuthTo run a single test file without the meson test runner, you can also 59c3e24cffSThomas Huthexecute the file directly by specifying two environment variables first, 60c3e24cffSThomas Huththe PYTHONPATH that has to include the python folder and the tests/functional 61c3e24cffSThomas Huthfolder of the source tree, and QEMU_TEST_QEMU_BINARY that has to point 62c3e24cffSThomas Huthto the QEMU binary that should be used for the test, for example:: 63c3e24cffSThomas Huth 64c3e24cffSThomas Huth $ export PYTHONPATH=../python:../tests/functional 65c3e24cffSThomas Huth $ export QEMU_TEST_QEMU_BINARY=$PWD/qemu-system-x86_64 66c3e24cffSThomas Huth $ python3 ../tests/functional/test_file.py 67c3e24cffSThomas Huth 68*dbaaef7dSDaniel P. BerrangéThe test framework will automatically purge any scratch files created during 69*dbaaef7dSDaniel P. Berrangéthe tests. If needing to debug a failed test, it is possible to keep these 70*dbaaef7dSDaniel P. Berrangéfiles around on disk by setting ```QEMU_TEST_KEEP_SCRATCH=1``` as an env 71*dbaaef7dSDaniel P. Berrangévariable. Any preserved files will be deleted the next time the test is run 72*dbaaef7dSDaniel P. Berrangéwithout this variable set. 73*dbaaef7dSDaniel P. Berrangé 74c3e24cffSThomas HuthOverview 75c3e24cffSThomas Huth-------- 76c3e24cffSThomas Huth 77c3e24cffSThomas HuthThe ``tests/functional/qemu_test`` directory provides the ``qemu_test`` 78c3e24cffSThomas HuthPython module, containing the ``qemu_test.QemuSystemTest`` class. 79c3e24cffSThomas HuthHere is a simple usage example: 80c3e24cffSThomas Huth 81c3e24cffSThomas Huth.. code:: 82c3e24cffSThomas Huth 83c3e24cffSThomas Huth #!/usr/bin/env python3 84c3e24cffSThomas Huth 85c3e24cffSThomas Huth from qemu_test import QemuSystemTest 86c3e24cffSThomas Huth 87c3e24cffSThomas Huth class Version(QemuSystemTest): 88c3e24cffSThomas Huth 89c3e24cffSThomas Huth def test_qmp_human_info_version(self): 90c3e24cffSThomas Huth self.vm.launch() 91c3e24cffSThomas Huth res = self.vm.cmd('human-monitor-command', 92c3e24cffSThomas Huth command_line='info version') 93c3e24cffSThomas Huth self.assertRegex(res, r'^(\d+\.\d+\.\d)') 94c3e24cffSThomas Huth 95c3e24cffSThomas Huth if __name__ == '__main__': 96c3e24cffSThomas Huth QemuSystemTest.main() 97c3e24cffSThomas Huth 98c3e24cffSThomas HuthBy providing the "hash bang" line at the beginning of the script, marking 99c3e24cffSThomas Huththe file as executable and by calling into QemuSystemTest.main(), the test 100c3e24cffSThomas Huthcan also be run stand-alone, without a test runner. OTOH when run via a test 101c3e24cffSThomas Huthrunner, the QemuSystemTest.main() function takes care of running the test 102c3e24cffSThomas Huthfunctions in the right fassion (e.g. with TAP output that is required by the 103c3e24cffSThomas Huthmeson test runner). 104c3e24cffSThomas Huth 105c3e24cffSThomas HuthThe ``qemu_test.QemuSystemTest`` base test class 106c3e24cffSThomas Huth^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 107c3e24cffSThomas Huth 108c3e24cffSThomas HuthThe ``qemu_test.QemuSystemTest`` class has a number of characteristics 109c3e24cffSThomas Huththat are worth being mentioned. 110c3e24cffSThomas Huth 111c3e24cffSThomas HuthFirst of all, it attempts to give each test a ready to use QEMUMachine 112c3e24cffSThomas Huthinstance, available at ``self.vm``. Because many tests will tweak the 113c3e24cffSThomas HuthQEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``) 114c3e24cffSThomas Huthis left to the test writer. 115c3e24cffSThomas Huth 116c3e24cffSThomas HuthThe base test class has also support for tests with more than one 117c3e24cffSThomas HuthQEMUMachine. The way to get machines is through the ``self.get_vm()`` 118c3e24cffSThomas Huthmethod which will return a QEMUMachine instance. The ``self.get_vm()`` 119c3e24cffSThomas Huthmethod accepts arguments that will be passed to the QEMUMachine creation 120c3e24cffSThomas Huthand also an optional ``name`` attribute so you can identify a specific 121c3e24cffSThomas Huthmachine and get it more than once through the tests methods. A simple 122c3e24cffSThomas Huthand hypothetical example follows: 123c3e24cffSThomas Huth 124c3e24cffSThomas Huth.. code:: 125c3e24cffSThomas Huth 126c3e24cffSThomas Huth from qemu_test import QemuSystemTest 127c3e24cffSThomas Huth 128c3e24cffSThomas Huth class MultipleMachines(QemuSystemTest): 129c3e24cffSThomas Huth def test_multiple_machines(self): 130c3e24cffSThomas Huth first_machine = self.get_vm() 131c3e24cffSThomas Huth second_machine = self.get_vm() 132c3e24cffSThomas Huth self.get_vm(name='third_machine').launch() 133c3e24cffSThomas Huth 134c3e24cffSThomas Huth first_machine.launch() 135c3e24cffSThomas Huth second_machine.launch() 136c3e24cffSThomas Huth 137c3e24cffSThomas Huth first_res = first_machine.cmd( 138c3e24cffSThomas Huth 'human-monitor-command', 139c3e24cffSThomas Huth command_line='info version') 140c3e24cffSThomas Huth 141c3e24cffSThomas Huth second_res = second_machine.cmd( 142c3e24cffSThomas Huth 'human-monitor-command', 143c3e24cffSThomas Huth command_line='info version') 144c3e24cffSThomas Huth 145c3e24cffSThomas Huth third_res = self.get_vm(name='third_machine').cmd( 146c3e24cffSThomas Huth 'human-monitor-command', 147c3e24cffSThomas Huth command_line='info version') 148c3e24cffSThomas Huth 149c3e24cffSThomas Huth self.assertEqual(first_res, second_res, third_res) 150c3e24cffSThomas Huth 151c3e24cffSThomas HuthAt test "tear down", ``qemu_test.QemuSystemTest`` handles all the QEMUMachines 152c3e24cffSThomas Huthshutdown. 153c3e24cffSThomas Huth 154c3e24cffSThomas HuthQEMUMachine 155c3e24cffSThomas Huth----------- 156c3e24cffSThomas Huth 157c3e24cffSThomas HuthThe QEMUMachine API is already widely used in the Python iotests, 158c3e24cffSThomas Huthdevice-crash-test and other Python scripts. It's a wrapper around the 159c3e24cffSThomas Huthexecution of a QEMU binary, giving its users: 160c3e24cffSThomas Huth 161c3e24cffSThomas Huth * the ability to set command line arguments to be given to the QEMU 162c3e24cffSThomas Huth binary 163c3e24cffSThomas Huth 164c3e24cffSThomas Huth * a ready to use QMP connection and interface, which can be used to 165c3e24cffSThomas Huth send commands and inspect its results, as well as asynchronous 166c3e24cffSThomas Huth events 167c3e24cffSThomas Huth 168c3e24cffSThomas Huth * convenience methods to set commonly used command line arguments in 169c3e24cffSThomas Huth a more succinct and intuitive way 170c3e24cffSThomas Huth 171c3e24cffSThomas HuthQEMU binary selection 172c3e24cffSThomas Huth^^^^^^^^^^^^^^^^^^^^^ 173c3e24cffSThomas Huth 174c3e24cffSThomas HuthThe QEMU binary used for the ``self.vm`` QEMUMachine instance will 175c3e24cffSThomas Huthprimarily depend on the value of the ``qemu_bin`` class attribute. 176c3e24cffSThomas HuthIf it is not explicitly set by the test code, its default value will 177c3e24cffSThomas Huthbe the result the QEMU_TEST_QEMU_BINARY environment variable. 178c3e24cffSThomas Huth 179c3e24cffSThomas HuthAttribute reference 180c3e24cffSThomas Huth------------------- 181c3e24cffSThomas Huth 182c3e24cffSThomas HuthQemuBaseTest 183c3e24cffSThomas Huth^^^^^^^^^^^^ 184c3e24cffSThomas Huth 185c3e24cffSThomas HuthThe following attributes are available on any ``qemu_test.QemuBaseTest`` 186c3e24cffSThomas Huthinstance. 187c3e24cffSThomas Huth 188c3e24cffSThomas Hutharch 189c3e24cffSThomas Huth"""" 190c3e24cffSThomas Huth 191c3e24cffSThomas HuthThe target architecture of the QEMU binary. 192c3e24cffSThomas Huth 193c3e24cffSThomas HuthTests are also free to use this attribute value, for their own needs. 194c3e24cffSThomas HuthA test may, for instance, use this value when selecting the architecture 195c3e24cffSThomas Huthof a kernel or disk image to boot a VM with. 196c3e24cffSThomas Huth 197c3e24cffSThomas Huthqemu_bin 198c3e24cffSThomas Huth"""""""" 199c3e24cffSThomas Huth 200c3e24cffSThomas HuthThe preserved value of the ``QEMU_TEST_QEMU_BINARY`` environment 201c3e24cffSThomas Huthvariable. 202c3e24cffSThomas Huth 203c3e24cffSThomas HuthQemuUserTest 204c3e24cffSThomas Huth^^^^^^^^^^^^ 205c3e24cffSThomas Huth 206c3e24cffSThomas HuthThe QemuUserTest class can be used for running an executable via the 207c3e24cffSThomas Huthusermode emulation binaries. 208c3e24cffSThomas Huth 209c3e24cffSThomas HuthQemuSystemTest 210c3e24cffSThomas Huth^^^^^^^^^^^^^^ 211c3e24cffSThomas Huth 212c3e24cffSThomas HuthThe QemuSystemTest class can be used for running tests via one of the 213c3e24cffSThomas Huthqemu-system-* binaries. 214c3e24cffSThomas Huth 215c3e24cffSThomas Huthvm 216c3e24cffSThomas Huth"" 217c3e24cffSThomas Huth 218c3e24cffSThomas HuthA QEMUMachine instance, initially configured according to the given 219c3e24cffSThomas Huth``qemu_bin`` parameter. 220c3e24cffSThomas Huth 221c3e24cffSThomas Huthcpu 222c3e24cffSThomas Huth""" 223c3e24cffSThomas Huth 224c3e24cffSThomas HuthThe cpu model that will be set to all QEMUMachine instances created 225c3e24cffSThomas Huthby the test. 226c3e24cffSThomas Huth 227c3e24cffSThomas Huthmachine 228c3e24cffSThomas Huth""""""" 229c3e24cffSThomas Huth 230c3e24cffSThomas HuthThe machine type that will be set to all QEMUMachine instances created 231c3e24cffSThomas Huthby the test. By using the set_machine() function of the QemuSystemTest 232c3e24cffSThomas Huthclass to set this attribute, you can automatically check whether the 233c3e24cffSThomas Huthmachine is available to skip the test in case it is not built into the 234c3e24cffSThomas HuthQEMU binary. 235c3e24cffSThomas Huth 236c3e24cffSThomas HuthAsset handling 237c3e24cffSThomas Huth-------------- 238c3e24cffSThomas Huth 239c3e24cffSThomas HuthMany functional tests download assets (e.g. Linux kernels, initrds, 240c3e24cffSThomas Huthfirmware images, etc.) from the internet to be able to run tests with 241c3e24cffSThomas Huththem. This imposes additional challenges to the test framework. 242c3e24cffSThomas Huth 243c3e24cffSThomas HuthFirst there is the the problem that some people might not have an 244c3e24cffSThomas Huthunconstrained internet connection, so such tests should not be run by 245c3e24cffSThomas Huthdefault when running ``make check``. To accomplish this situation, 246c3e24cffSThomas Huththe tests that download files should only be added to the "thorough" 247c3e24cffSThomas Huthspeed mode in the meson.build file, while the "quick" speed mode is 248c3e24cffSThomas Huthfine for functional tests that can be run without downloading files. 249c3e24cffSThomas Huth``make check`` then only runs the quick functional tests along with 250c3e24cffSThomas Huththe other quick tests from the other test suites. If you choose to 251c3e24cffSThomas Huthrun only run ``make check-functional``, the "thorough" tests will be 252c3e24cffSThomas Huthexecuted, too. And to run all functional tests along with the others, 253c3e24cffSThomas Huthyou can use something like:: 254c3e24cffSThomas Huth 255c3e24cffSThomas Huth make -j$(nproc) check SPEED=thorough 256c3e24cffSThomas Huth 257c3e24cffSThomas HuthThe second problem with downloading files from the internet are time 258c3e24cffSThomas Huthconstraints. The time for downloading files should not be taken into 259c3e24cffSThomas Huthaccount when the test is running and the timeout of the test is ticking 260c3e24cffSThomas Huth(since downloading can be very slow, depending on the network bandwidth). 261c3e24cffSThomas HuthThis problem is solved by downloading the assets ahead of time, before 262c3e24cffSThomas Huththe tests are run. This pre-caching is done with the qemu_test.Asset 263c3e24cffSThomas Huthclass. To use it in your test, declare an asset in your test class with 264c3e24cffSThomas Huthits URL and SHA256 checksum like this:: 265c3e24cffSThomas Huth 266c3e24cffSThomas Huth ASSET_somename = ( 267c3e24cffSThomas Huth ('https://www.qemu.org/assets/images/qemu_head_200.png'), 268c3e24cffSThomas Huth '34b74cad46ea28a2966c1d04e102510daf1fd73e6582b6b74523940d5da029dd') 269c3e24cffSThomas Huth 270c3e24cffSThomas HuthIn your test function, you can then get the file name of the cached 271c3e24cffSThomas Huthasset like this:: 272c3e24cffSThomas Huth 273c3e24cffSThomas Huth def test_function(self): 274c3e24cffSThomas Huth file_path = self.ASSET_somename.fetch() 275c3e24cffSThomas Huth 276c3e24cffSThomas HuthThe pre-caching will be done automatically when running 277c3e24cffSThomas Huth``make check-functional`` (but not when running e.g. 278c3e24cffSThomas Huth``make check-functional-<target>``). In case you just want to download 279c3e24cffSThomas Huththe assets without running the tests, you can do so by running:: 280c3e24cffSThomas Huth 281c3e24cffSThomas Huth make precache-functional 282c3e24cffSThomas Huth 283c3e24cffSThomas HuthThe cache is populated in the ``~/.cache/qemu/download`` directory by 284c3e24cffSThomas Huthdefault, but the location can be changed by setting the 285c3e24cffSThomas Huth``QEMU_TEST_CACHE_DIR`` environment variable. 286c3e24cffSThomas Huth 287c3e24cffSThomas HuthSkipping tests 288c3e24cffSThomas Huth-------------- 289c3e24cffSThomas Huth 290c3e24cffSThomas HuthSince the test framework is based on the common Python unittest framework, 291c3e24cffSThomas Huthyou can use the usual Python decorators which allow for easily skipping 292c3e24cffSThomas Huthtests running under certain conditions, for example, on the lack of a binary 293c3e24cffSThomas Huthon the test system or when the running environment is a CI system. For further 294c3e24cffSThomas Huthinformation about those decorators, please refer to: 295c3e24cffSThomas Huth 296c3e24cffSThomas Huth https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures 297c3e24cffSThomas Huth 298c3e24cffSThomas HuthWhile the conditions for skipping tests are often specifics of each one, there 299c3e24cffSThomas Huthare recurring scenarios identified by the QEMU developers and the use of 300c3e24cffSThomas Huthenvironment variables became a kind of standard way to enable/disable tests. 301c3e24cffSThomas Huth 302c3e24cffSThomas HuthHere is a list of the most used variables: 303c3e24cffSThomas Huth 304c3e24cffSThomas HuthQEMU_TEST_ALLOW_LARGE_STORAGE 305c3e24cffSThomas Huth^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 306c3e24cffSThomas HuthTests which are going to fetch or produce assets considered *large* are not 307c3e24cffSThomas Huthgoing to run unless that ``QEMU_TEST_ALLOW_LARGE_STORAGE=1`` is exported on 308c3e24cffSThomas Huththe environment. 309c3e24cffSThomas Huth 310c3e24cffSThomas HuthThe definition of *large* is a bit arbitrary here, but it usually means an 311c3e24cffSThomas Huthasset which occupies at least 1GB of size on disk when uncompressed. 312c3e24cffSThomas Huth 313c3e24cffSThomas HuthQEMU_TEST_ALLOW_UNTRUSTED_CODE 314c3e24cffSThomas Huth^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 315c3e24cffSThomas HuthThere are tests which will boot a kernel image or firmware that can be 316c3e24cffSThomas Huthconsidered not safe to run on the developer's workstation, thus they are 317c3e24cffSThomas Huthskipped by default. The definition of *not safe* is also arbitrary but 318c3e24cffSThomas Huthusually it means a blob which either its source or build process aren't 319c3e24cffSThomas Huthpublic available. 320c3e24cffSThomas Huth 321c3e24cffSThomas HuthYou should export ``QEMU_TEST_ALLOW_UNTRUSTED_CODE=1`` on the environment in 322c3e24cffSThomas Huthorder to allow tests which make use of those kind of assets. 323c3e24cffSThomas Huth 324c3e24cffSThomas HuthQEMU_TEST_FLAKY_TESTS 325c3e24cffSThomas Huth^^^^^^^^^^^^^^^^^^^^^ 326c3e24cffSThomas HuthSome tests are not working reliably and thus are disabled by default. 327c3e24cffSThomas HuthThis includes tests that don't run reliably on GitLab's CI which 328c3e24cffSThomas Huthusually expose real issues that are rarely seen on developer machines 329c3e24cffSThomas Huthdue to the constraints of the CI environment. If you encounter a 330c3e24cffSThomas Huthsimilar situation then raise a bug and then mark the test as shown on 331c3e24cffSThomas Huththe code snippet below: 332c3e24cffSThomas Huth 333c3e24cffSThomas Huth.. code:: 334c3e24cffSThomas Huth 335c3e24cffSThomas Huth # See https://gitlab.com/qemu-project/qemu/-/issues/nnnn 336c3e24cffSThomas Huth @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab') 337c3e24cffSThomas Huth def test(self): 338c3e24cffSThomas Huth do_something() 339c3e24cffSThomas Huth 340c3e24cffSThomas HuthTests should not live in this state forever and should either be fixed 341c3e24cffSThomas Huthor eventually removed. 342c3e24cffSThomas Huth 343c3e24cffSThomas Huth 344c3e24cffSThomas Huth.. _unittest: https://docs.python.org/3/library/unittest.html 345