intro.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. *****************************************
  3. The Yocto Project Test Environment Manual
  4. *****************************************
  5. Welcome
  6. =======
  7. Welcome to the Yocto Project Test Environment Manual! This manual is a
  8. work in progress. The manual contains information about the testing
  9. environment used by the Yocto Project to make sure each major and minor
  10. release works as intended. All the project's testing infrastructure and
  11. processes are publicly visible and available so that the community can
  12. see what testing is being performed, how it's being done and the current
  13. status of the tests and the project at any given time. It is intended
  14. that other organizations can leverage off the process and testing
  15. environment used by the Yocto Project to create their own automated,
  16. production test environment, building upon the foundations from the
  17. project core.
  18. This manual is a work-in-progress and is being initially loaded with
  19. information from the README files and notes from key engineers:
  20. - *yocto-autobuilder2:* This
  21. :yocto_git:`README.md </yocto-autobuilder2/tree/README.md>`
  22. is the main README which details how to set up the Yocto Project
  23. Autobuilder. The ``yocto-autobuilder2`` repository represents the
  24. Yocto Project's console UI plugin to Buildbot and the configuration
  25. necessary to configure Buildbot to perform the testing the project
  26. requires.
  27. - *yocto-autobuilder-helper:* This :yocto_git:`README </yocto-autobuilder-helper/tree/README/>`
  28. and repository contains Yocto Project Autobuilder Helper scripts and
  29. configuration. The ``yocto-autobuilder-helper`` repository contains
  30. the "glue" logic that defines which tests to run and how to run them.
  31. As a result, it can be used by any Continuous Improvement (CI) system
  32. to run builds, support getting the correct code revisions, configure
  33. builds and layers, run builds, and collect results. The code is
  34. independent of any CI system, which means the code can work `Buildbot <https://docs.buildbot.net/current/>`__,
  35. Jenkins, or others. This repository has a branch per release of the
  36. project defining the tests to run on a per release basis.
  37. Yocto Project Autobuilder Overview
  38. ==================================
  39. The Yocto Project Autobuilder collectively refers to the software,
  40. tools, scripts, and procedures used by the Yocto Project to test
  41. released software across supported hardware in an automated and regular
  42. fashion. Basically, during the development of a Yocto Project release,
  43. the Autobuilder tests if things work. The Autobuilder builds all test
  44. targets and runs all the tests.
  45. The Yocto Project uses now uses standard upstream
  46. Buildbot (`version 3.8 <https://docs.buildbot.net/3.8.0/>`__) to
  47. drive its integration and testing. Buildbot has a plug-in interface
  48. that the Yocto Project customizes using code from the
  49. ``yocto-autobuilder2`` repository, adding its own console UI plugin. The
  50. resulting UI plug-in allows you to visualize builds in a way suited to
  51. the project's needs.
  52. A ``helper`` layer provides configuration and job management through
  53. scripts found in the ``yocto-autobuilder-helper`` repository. The
  54. ``helper`` layer contains the bulk of the build configuration
  55. information and is release-specific, which makes it highly customizable
  56. on a per-project basis. The layer is CI system-agnostic and contains a
  57. number of Helper scripts that can generate build configurations from
  58. simple JSON files.
  59. .. note::
  60. The project uses Buildbot for historical reasons but also because
  61. many of the project developers have knowledge of Python. It is
  62. possible to use the outer layers from another Continuous Integration
  63. (CI) system such as :wikipedia:`Jenkins <Jenkins_(software)>`
  64. instead of Buildbot.
  65. The following figure shows the Yocto Project Autobuilder stack with a
  66. topology that includes a controller and a cluster of workers:
  67. .. image:: figures/ab-test-cluster.png
  68. :align: center
  69. :width: 70%
  70. Yocto Project Tests --- Types of Testing Overview
  71. =================================================
  72. The Autobuilder tests different elements of the project by using
  73. the following types of tests:
  74. - *Build Testing:* Tests whether specific configurations build by
  75. varying :term:`MACHINE`,
  76. :term:`DISTRO`, other configuration
  77. options, and the specific target images being built (or ``world``). This is
  78. used to trigger builds of all the different test configurations on the
  79. Autobuilder. Builds usually cover many different targets for
  80. different architectures, machines, and distributions, as well as
  81. different configurations, such as different init systems. The
  82. Autobuilder tests literally hundreds of configurations and targets.
  83. - *Sanity Checks During the Build Process:* Tests initiated through the
  84. :ref:`ref-classes-insane` class. These checks ensure the output of the
  85. builds are correct. For example, does the ELF architecture in the
  86. generated binaries match the target system? ARM binaries would not work
  87. in a MIPS system!
  88. - *Build Performance Testing:* Tests whether or not commonly used steps
  89. during builds work efficiently and avoid regressions. Tests to time
  90. commonly used usage scenarios are run through ``oe-build-perf-test``.
  91. These tests are run on isolated machines so that the time
  92. measurements of the tests are accurate and no other processes
  93. interfere with the timing results. The project currently tests
  94. performance on two different distributions, Fedora and Ubuntu, to
  95. ensure we have no single point of failure and can ensure the
  96. different distros work effectively.
  97. - *eSDK Testing:* Image tests initiated through the following command::
  98. $ bitbake image -c testsdkext
  99. The tests use the :ref:`ref-classes-testsdk` class and the
  100. ``do_testsdkext`` task.
  101. - *Feature Testing:* Various scenario-based tests are run through the
  102. :ref:`OpenEmbedded Self test (oe-selftest) <ref-manual/release-process:Testing and Quality Assurance>`. We test oe-selftest on each of the main distributions
  103. we support.
  104. - *Image Testing:* Image tests initiated through the following command::
  105. $ bitbake image -c testimage
  106. The tests use the :ref:`ref-classes-testimage`
  107. class and the :ref:`ref-tasks-testimage` task. See the
  108. :ref:`test-manual/runtime-testing:Performing Automated Runtime Testing`
  109. section of the Yocto Project Test Environment Manual for more information.
  110. - *Layer Testing:* The Autobuilder has the possibility to test whether
  111. specific layers work with the test of the system. The layers tested
  112. may be selected by members of the project. Some key community layers
  113. are also tested periodically.
  114. - *Package Testing:* A Package Test (ptest) runs tests against packages
  115. built by the OpenEmbedded build system on the target machine. See the
  116. :ref:`Testing Packages With
  117. ptest <test-manual/ptest:Testing Packages With ptest>` section
  118. in the Yocto Project Development Tasks Manual and the
  119. ":yocto_wiki:`Ptest </Ptest>`" Wiki page for more
  120. information on Ptest.
  121. - *SDK Testing:* Image tests initiated through the following command::
  122. $ bitbake image -c testsdk
  123. The tests use the :ref:`ref-classes-testsdk` class and
  124. the ``do_testsdk`` task.
  125. - *Unit Testing:* Unit tests on various components of the system run
  126. through :ref:`bitbake-selftest <ref-manual/release-process:Testing and Quality Assurance>` and
  127. :ref:`oe-selftest <ref-manual/release-process:Testing and Quality Assurance>`.
  128. - *Automatic Upgrade Helper:* This target tests whether new versions of
  129. software are available and whether we can automatically upgrade to
  130. those new versions. If so, this target emails the maintainers with a
  131. patch to let them know this is possible.
  132. How Tests Map to Areas of Code
  133. ==============================
  134. Tests map into the codebase as follows:
  135. - *bitbake-selftest:*
  136. These tests are self-contained and test BitBake as well as its APIs,
  137. which include the fetchers. The tests are located in
  138. ``bitbake/lib/*/tests``.
  139. Some of these tests run the ``bitbake`` command, so ``bitbake/bin``
  140. must be added to the ``PATH`` before running ``bitbake-selftest``.
  141. From within the BitBake repository, run the following::
  142. $ export PATH=$PWD/bin:$PATH
  143. After that, you can run the selftest script::
  144. $ bitbake-selftest
  145. The default output is quiet and just prints a summary of what was
  146. run. To see more information, there is a verbose option::
  147. $ bitbake-selftest -v
  148. To skip tests that access the Internet, use the ``BB_SKIP_NETTESTS``
  149. variable when running ``bitbake-selftest`` as follows::
  150. $ BB_SKIP_NETTESTS=yes bitbake-selftest
  151. Use this option when you wish to skip tests that access the network,
  152. which are mostly necessary to test the fetcher modules. To specify
  153. individual test modules to run, append the test module name to the
  154. ``bitbake-selftest`` command. For example, to specify the tests for
  155. ``bb.tests.data.DataExpansions``, run::
  156. $ bitbake-selftest bb.tests.data.DataExpansions
  157. You can also specify individual tests by defining the full name and module
  158. plus the class path of the test, for example::
  159. $ bitbake-selftest bb.tests.data.DataExpansions.test_one_var
  160. The tests are based on
  161. `Python unittest <https://docs.python.org/3/library/unittest.html>`__.
  162. - *oe-selftest:*
  163. - These tests use OE to test the workflows, which include testing
  164. specific features, behaviors of tasks, and API unit tests.
  165. - The tests can take advantage of parallelism through the ``-j``
  166. option, which can specify a number of threads to spread the tests
  167. across. Note that all tests from a given class of tests will run
  168. in the same thread. To parallelize large numbers of tests you can
  169. split the class into multiple units.
  170. - The tests are based on
  171. `Python unittest <https://docs.python.org/3/library/unittest.html>`__.
  172. - The code for the tests resides in
  173. ``meta/lib/oeqa/selftest/cases/``.
  174. - To run all the tests, enter the following command::
  175. $ oe-selftest -a
  176. - To run a specific test, use the following command form where
  177. ``testname`` is the name of the specific test::
  178. $ oe-selftest -r <testname>
  179. For example, the following command would run the ``tinfoil``
  180. ``getVar`` API test::
  181. $ oe-selftest -r tinfoil.TinfoilTests.test_getvar
  182. It is also possible to run a set
  183. of tests. For example the following command will run all of the
  184. ``tinfoil`` tests::
  185. $ oe-selftest -r tinfoil
  186. - *testimage:*
  187. - These tests build an image, boot it, and run tests against the
  188. image's content.
  189. - The code for these tests resides in ``meta/lib/oeqa/runtime/cases/``.
  190. - You need to set the :term:`IMAGE_CLASSES` variable as follows::
  191. IMAGE_CLASSES += "testimage"
  192. - Run the tests using the following command form::
  193. $ bitbake image -c testimage
  194. - *testsdk:*
  195. - These tests build an SDK, install it, and then run tests against
  196. that SDK.
  197. - The code for these tests resides in ``meta/lib/oeqa/sdk/cases/``.
  198. - Run the test using the following command form::
  199. $ bitbake image -c testsdk
  200. - *testsdk_ext:*
  201. - These tests build an extended SDK (eSDK), install that eSDK, and
  202. run tests against the eSDK.
  203. - The code for these tests resides in ``meta/lib/oeqa/sdkext/cases/``.
  204. - To run the tests, use the following command form::
  205. $ bitbake image -c testsdkext
  206. - *oe-build-perf-test:*
  207. - These tests run through commonly used usage scenarios and measure
  208. the performance times.
  209. - The code for these tests resides in ``meta/lib/oeqa/buildperf``.
  210. - To run the tests, use the following command form::
  211. $ oe-build-perf-test <options>
  212. The command takes a number of options,
  213. such as where to place the test results. The Autobuilder Helper
  214. Scripts include the ``build-perf-test-wrapper`` script with
  215. examples of how to use the oe-build-perf-test from the command
  216. line.
  217. Use the ``oe-git-archive`` command to store test results into a
  218. Git repository.
  219. Use the ``oe-build-perf-report`` command to generate text reports
  220. and HTML reports with graphs of the performance data. See
  221. :yocto_dl:`html </releases/yocto/yocto-4.3/testresults/buildperf-debian11/perf-debian11_nanbield_20231019191258_15b576c410.html>`
  222. and
  223. :yocto_dl:`txt </releases/yocto/yocto-4.3/testresults/buildperf-debian11/perf-debian11_nanbield_20231019191258_15b576c410.txt>`
  224. examples.
  225. - The tests are contained in ``meta/lib/oeqa/buildperf/test_basic.py``.
  226. Test Examples
  227. =============
  228. This section provides example tests for each of the tests listed in the
  229. :ref:`test-manual/intro:How Tests Map to Areas of Code` section.
  230. - ``oe-selftest`` testcases reside in the ``meta/lib/oeqa/selftest/cases`` directory.
  231. - ``bitbake-selftest`` testcases reside in the ``bitbake/lib/bb/tests/`` directory.
  232. ``bitbake-selftest``
  233. --------------------
  234. A simple test example from ``bitbake/lib/bb/tests/data.py`` is::
  235. class DataExpansions(unittest.TestCase):
  236. def setUp(self):
  237. self.d = bb.data.init()
  238. self.d["foo"] = "value_of_foo"
  239. self.d["bar"] = "value_of_bar"
  240. self.d["value_of_foo"] = "value_of_'value_of_foo'"
  241. def test_one_var(self):
  242. val = self.d.expand("${foo}")
  243. self.assertEqual(str(val), "value_of_foo")
  244. In this example, a ``DataExpansions`` class of tests is created, derived from
  245. standard `Python unittest <https://docs.python.org/3/library/unittest.html>`__.
  246. The class has a common ``setUp`` function which is shared by all the tests in
  247. the class. A simple test is then added to test that when a variable is
  248. expanded, the correct value is found.
  249. BitBake selftests are straightforward
  250. `Python unittest <https://docs.python.org/3/library/unittest.html>`__.
  251. Refer to the `Python unittest documentation
  252. <https://docs.python.org/3/library/unittest.html>`__ for additional information
  253. on writing such tests.
  254. ``oe-selftest``
  255. ---------------
  256. These tests are more complex due to the setup required behind the scenes
  257. for full builds. Rather than directly using `Python unittest
  258. <https://docs.python.org/3/library/unittest.html>`__, the code
  259. wraps most of the standard objects. The tests can be simple, such as
  260. testing a command from within the OE build environment using the
  261. following example::
  262. class BitbakeLayers(OESelftestTestCase):
  263. def test_bitbakelayers_showcrossdepends(self):
  264. result = runCmd('bitbake-layers show-cross-depends')
  265. self.assertTrue('aspell' in result.output, msg = "No dependencies were shown. bitbake-layers show-cross-depends output: %s"% result.output)
  266. This example, taken from ``meta/lib/oeqa/selftest/cases/bblayers.py``,
  267. creates a testcase from the ``OESelftestTestCase`` class, derived
  268. from ``unittest.TestCase``, which runs the ``bitbake-layers`` command
  269. and checks the output to ensure it contains something we know should be
  270. here.
  271. The ``oeqa.utils.commands`` module contains Helpers which can assist
  272. with common tasks, including:
  273. - *Obtaining the value of a bitbake variable:* Use
  274. ``oeqa.utils.commands.get_bb_var()`` or use
  275. ``oeqa.utils.commands.get_bb_vars()`` for more than one variable
  276. - *Running a bitbake invocation for a build:* Use
  277. ``oeqa.utils.commands.bitbake()``
  278. - *Running a command:* Use ``oeqa.utils.commands.runCmd()``
  279. There is also a ``oeqa.utils.commands.runqemu()`` function for launching
  280. the ``runqemu`` command for testing things within a running, virtualized
  281. image.
  282. You can run these tests in parallel. Parallelism works per test class,
  283. so tests within a given test class should always run in the same build,
  284. while tests in different classes or modules may be split into different
  285. builds. There is no data store available for these tests since the tests
  286. launch the ``bitbake`` command and exist outside of its context. As a
  287. result, common BitBake library functions (``bb.\*``) are also unavailable.
  288. ``testimage``
  289. -------------
  290. These tests are run once an image is up and running, either on target
  291. hardware or under QEMU. As a result, they are assumed to be running in a
  292. target image environment, as opposed to in a host build environment. A
  293. simple example from ``meta/lib/oeqa/runtime/cases/python.py`` contains
  294. the following::
  295. class PythonTest(OERuntimeTestCase):
  296. @OETestDepends(['ssh.SSHTest.test_ssh'])
  297. @OEHasPackage(['python3-core'])
  298. def test_python3(self):
  299. cmd = "python3 -c \\"import codecs; print(codecs.encode('Uryyb, jbeyq', 'rot13'))\""
  300. status, output = self.target.run(cmd)
  301. msg = 'Exit status was not 0. Output: %s' % output
  302. self.assertEqual(status, 0, msg=msg)
  303. In this example, the ``OERuntimeTestCase`` class wraps
  304. ``unittest.TestCase``. Within the test, ``self.target`` represents the
  305. target system, where commands can be run using the ``run()``
  306. method.
  307. To ensure certain tests or package dependencies are met, you can use the
  308. ``OETestDepends`` and ``OEHasPackage`` decorators. For example, the test
  309. in this example would only make sense if ``python3-core`` is installed in
  310. the image.
  311. ``testsdk_ext``
  312. ---------------
  313. These tests are run against built extensible SDKs (eSDKs). The tests can
  314. assume that the eSDK environment has already been set up. An example from
  315. ``meta/lib/oeqa/sdk/cases/devtool.py`` contains the following::
  316. class DevtoolTest(OESDKExtTestCase):
  317. @classmethod def setUpClass(cls):
  318. myapp_src = os.path.join(cls.tc.esdk_files_dir, "myapp")
  319. cls.myapp_dst = os.path.join(cls.tc.sdk_dir, "myapp")
  320. shutil.copytree(myapp_src, cls.myapp_dst)
  321. subprocess.check_output(['git', 'init', '.'], cwd=cls.myapp_dst)
  322. subprocess.check_output(['git', 'add', '.'], cwd=cls.myapp_dst)
  323. subprocess.check_output(['git', 'commit', '-m', "'test commit'"], cwd=cls.myapp_dst)
  324. @classmethod
  325. def tearDownClass(cls):
  326. shutil.rmtree(cls.myapp_dst)
  327. def _test_devtool_build(self, directory):
  328. self._run('devtool add myapp %s' % directory)
  329. try:
  330. self._run('devtool build myapp')
  331. finally:
  332. self._run('devtool reset myapp')
  333. def test_devtool_build_make(self):
  334. self._test_devtool_build(self.myapp_dst)
  335. In this example, the ``devtool``
  336. command is tested to see whether a sample application can be built with
  337. the ``devtool build`` command within the eSDK.
  338. ``testsdk``
  339. -----------
  340. These tests are run against built SDKs. The tests can assume that an SDK
  341. has already been extracted and its environment file has been sourced. A
  342. simple example from ``meta/lib/oeqa/sdk/cases/python.py`` contains the
  343. following::
  344. class Python3Test(OESDKTestCase):
  345. def setUp(self):
  346. if not (self.tc.hasHostPackage("nativesdk-python3-core") or
  347. self.tc.hasHostPackage("python3-core-native")):
  348. raise unittest.SkipTest("No python3 package in the SDK")
  349. def test_python3(self):
  350. cmd = "python3 -c \\"import codecs; print(codecs.encode('Uryyb, jbeyq', 'rot13'))\""
  351. output = self._run(cmd)
  352. self.assertEqual(output, "Hello, world\n")
  353. In this example, if ``nativesdk-python3-core`` has been installed into the SDK,
  354. the code runs the ``python3`` interpreter with a basic command to check it is
  355. working correctly. The test would only run if Python3 is installed in the SDK.
  356. ``oe-build-perf-test``
  357. ----------------------
  358. The performance tests usually measure how long operations take and the
  359. resource utilization as that happens. An example from
  360. ``meta/lib/oeqa/buildperf/test_basic.py`` contains the following::
  361. class Test3(BuildPerfTestCase):
  362. def test3(self):
  363. """Bitbake parsing (bitbake -p)"""
  364. # Drop all caches and parse
  365. self.rm_cache()
  366. oe.path.remove(os.path.join(self.bb_vars['TMPDIR'], 'cache'), True)
  367. self.measure_cmd_resources(['bitbake', '-p'], 'parse_1',
  368. 'bitbake -p (no caches)')
  369. # Drop tmp/cache
  370. oe.path.remove(os.path.join(self.bb_vars['TMPDIR'], 'cache'), True)
  371. self.measure_cmd_resources(['bitbake', '-p'], 'parse_2',
  372. 'bitbake -p (no tmp/cache)')
  373. # Parse with fully cached data
  374. self.measure_cmd_resources(['bitbake', '-p'], 'parse_3',
  375. 'bitbake -p (cached)')
  376. This example shows how three specific parsing timings are
  377. measured, with and without various caches, to show how BitBake's parsing
  378. performance trends over time.
  379. Considerations When Writing Tests
  380. =================================
  381. When writing good tests, there are several things to keep in mind. Since
  382. things running on the Autobuilder are accessed concurrently by multiple
  383. workers, consider the following:
  384. **Running "cleanall" is not permitted.**
  385. This can delete files from :term:`DL_DIR` which would potentially break other
  386. builds running in parallel. If this is required, :term:`DL_DIR` must be set to
  387. an isolated directory.
  388. **Running "cleansstate" is not permitted.**
  389. This can delete files from :term:`SSTATE_DIR` which would potentially break
  390. other builds running in parallel. If this is required, :term:`SSTATE_DIR` must
  391. be set to an isolated directory. Alternatively, you can use the ``-f``
  392. option with the ``bitbake`` command to "taint" tasks by changing the
  393. sstate checksums to ensure sstate cache items will not be reused.
  394. **Tests should not change the metadata.**
  395. This is particularly true for oe-selftests since these can run in
  396. parallel and changing metadata leads to changing checksums, which
  397. confuses BitBake while running in parallel. If this is necessary, copy
  398. layers to a temporary location and modify them. Some tests need to
  399. change metadata, such as the devtool tests. To protect the metadata from
  400. changes, set up temporary copies of that data first.