intro.rst 22 KB

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