intro.rst 21 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. Currently, the Yocto Project Test Environment Manual has no projected
  19. release date. This manual is a work-in-progress and is being initially
  20. loaded with information from the README files and notes from key
  21. engineers:
  22. - *yocto-autobuilder2:* This
  23. :yocto_git:`README.md </yocto-autobuilder2/tree/README.md>`
  24. is the main README which details how to set up the Yocto Project
  25. Autobuilder. The ``yocto-autobuilder2`` repository represents the
  26. Yocto Project's console UI plugin to Buildbot and the configuration
  27. necessary to configure Buildbot to perform the testing the project
  28. requires.
  29. - *yocto-autobuilder-helper:* This :yocto_git:`README </yocto-autobuilder-helper/tree/README/>`
  30. and repository contains Yocto Project Autobuilder Helper scripts and
  31. configuration. The ``yocto-autobuilder-helper`` repository contains
  32. the "glue" logic that defines which tests to run and how to run them.
  33. As a result, it can be used by any Continuous Improvement (CI) system
  34. to run builds, support getting the correct code revisions, configure
  35. builds and layers, run builds, and collect results. The code is
  36. independent of any CI system, which means the code can work `Buildbot <https://docs.buildbot.net/0.9.15.post1/>`__,
  37. Jenkins, or others. This repository has a branch per release of the
  38. project defining the tests to run on a per release basis.
  39. Yocto Project Autobuilder Overview
  40. ==================================
  41. The Yocto Project Autobuilder collectively refers to the software,
  42. tools, scripts, and procedures used by the Yocto Project to test
  43. released software across supported hardware in an automated and regular
  44. fashion. Basically, during the development of a Yocto Project release,
  45. the Autobuilder tests if things work. The Autobuilder builds all test
  46. targets and runs all the tests.
  47. The Yocto Project uses now uses standard upstream
  48. `Buildbot <https://docs.buildbot.net/0.9.15.post1/>`__ (version 9) to
  49. drive its integration and testing. Buildbot Nine has a plug-in interface
  50. that the Yocto Project customizes using code from the
  51. ``yocto-autobuilder2`` repository, adding its own console UI plugin. The
  52. resulting UI plug-in allows you to visualize builds in a way suited to
  53. the project's needs.
  54. A ``helper`` layer provides configuration and job management through
  55. scripts found in the ``yocto-autobuilder-helper`` repository. The
  56. ``helper`` layer contains the bulk of the build configuration
  57. information and is release-specific, which makes it highly customizable
  58. on a per-project basis. The layer is CI system-agnostic and contains a
  59. number of Helper scripts that can generate build configurations from
  60. simple JSON files.
  61. .. note::
  62. The project uses Buildbot for historical reasons but also because
  63. many of the project developers have knowledge of Python. It is
  64. possible to use the outer layers from another Continuous Integration
  65. (CI) system such as :wikipedia:`Jenkins <Jenkins_(software)>`
  66. instead of Buildbot.
  67. The following figure shows the Yocto Project Autobuilder stack with a
  68. topology that includes a controller and a cluster of workers:
  69. .. image:: figures/ab-test-cluster.png
  70. :align: center
  71. :width: 70%
  72. Yocto Project Tests --- Types of Testing Overview
  73. =================================================
  74. The Autobuilder tests different elements of the project by using
  75. the following types of tests:
  76. - *Build Testing:* Tests whether specific configurations build by
  77. varying :term:`MACHINE`,
  78. :term:`DISTRO`, other configuration
  79. options, and the specific target images being built (or world). Used
  80. to trigger builds of all the different test configurations on the
  81. Autobuilder. Builds usually cover many different targets for
  82. different architectures, machines, and distributions, as well as
  83. different configurations, such as different init systems. The
  84. Autobuilder tests literally hundreds of configurations and targets.
  85. - *Sanity Checks During the Build Process:* Tests initiated through
  86. the :ref:`insane <ref-classes-insane>`
  87. class. These checks ensure the output of the builds are correct.
  88. For example, does the ELF architecture in the generated binaries
  89. match the target system? ARM binaries would not work in a MIPS
  90. system!
  91. - *Build Performance Testing:* Tests whether or not commonly used steps
  92. during builds work efficiently and avoid regressions. Tests to time
  93. commonly used usage scenarios are run through ``oe-build-perf-test``.
  94. These tests are run on isolated machines so that the time
  95. measurements of the tests are accurate and no other processes
  96. interfere with the timing results. The project currently tests
  97. performance on two different distributions, Fedora and Ubuntu, to
  98. ensure we have no single point of failure and can ensure the
  99. different distros work effectively.
  100. - *eSDK Testing:* Image tests initiated through the following command::
  101. $ bitbake image -c testsdkext
  102. The tests utilize the :ref:`testsdkext <ref-classes-testsdk>` class and the ``do_testsdkext`` task.
  103. - *Feature Testing:* Various scenario-based tests are run through the
  104. :ref:`OpenEmbedded Self test (oe-selftest) <ref-manual/release-process:Testing and Quality Assurance>`. We test oe-selftest on each of the main distributions
  105. we support.
  106. - *Image Testing:* Image tests initiated through the following command::
  107. $ bitbake image -c testimage
  108. The tests utilize the :ref:`testimage <ref-classes-testimage>`
  109. class and the :ref:`ref-tasks-testimage` task.
  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 <dev-manual/packages: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 utilize the :ref:`testsdk <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 the
  155. bb.data.module, run::
  156. $ bitbake-selftest bb.test.data.module
  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.TestOverrides.test_one_override
  160. The tests are based on `Python
  161. 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 Python unittest.
  171. - The code for the tests resides in
  172. ``meta/lib/oeqa/selftest/cases/``.
  173. - To run all the tests, enter the following command::
  174. $ oe-selftest -a
  175. - To run a specific test, use the following command form where
  176. testname is the name of the specific test::
  177. $ oe-selftest -r <testname>
  178. For example, the following command would run the tinfoil
  179. getVar API test::
  180. $ oe-selftest -r tinfoil.TinfoilTests.test_getvar
  181. It is also possible to run a set
  182. of tests. For example the following command will run all of the
  183. tinfoil tests::
  184. $ oe-selftest -r tinfoil
  185. - *testimage:*
  186. - These tests build an image, boot it, and run tests against the
  187. image's content.
  188. - The code for these tests resides in ``meta/lib/oeqa/runtime/cases/``.
  189. - You need to set the :term:`IMAGE_CLASSES` variable as follows::
  190. IMAGE_CLASSES += "testimage"
  191. - Run the tests using the following command form::
  192. $ bitbake image -c testimage
  193. - *testsdk:*
  194. - These tests build an SDK, install it, and then run tests against
  195. that SDK.
  196. - The code for these tests resides in ``meta/lib/oeqa/sdk/cases/``.
  197. - Run the test using the following command form::
  198. $ bitbake image -c testsdk
  199. - *testsdk_ext:*
  200. - These tests build an extended SDK (eSDK), install that eSDK, and
  201. run tests against the eSDK.
  202. - The code for these tests resides in ``meta/lib/oeqa/esdk``.
  203. - To run the tests, use the following command form::
  204. $ bitbake image -c testsdkext
  205. - *oe-build-perf-test:*
  206. - These tests run through commonly used usage scenarios and measure
  207. the performance times.
  208. - The code for these tests resides in ``meta/lib/oeqa/buildperf``.
  209. - To run the tests, use the following command form::
  210. $ oe-build-perf-test <options>
  211. The command takes a number of options,
  212. such as where to place the test results. The Autobuilder Helper
  213. Scripts include the ``build-perf-test-wrapper`` script with
  214. examples of how to use the oe-build-perf-test from the command
  215. line.
  216. Use the ``oe-git-archive`` command to store test results into a
  217. Git repository.
  218. Use the ``oe-build-perf-report`` command to generate text reports
  219. and HTML reports with graphs of the performance data. For
  220. examples, see
  221. :yocto_dl:`/releases/yocto/yocto-2.7/testresults/buildperf-centos7/perf-centos7.yoctoproject.org_warrior_20190414204758_0e39202.html`
  222. and
  223. :yocto_dl:`/releases/yocto/yocto-2.7/testresults/buildperf-centos7/perf-centos7.yoctoproject.org_warrior_20190414204758_0e39202.txt`.
  224. - The tests are contained in ``lib/oeqa/buildperf/test_basic.py``.
  225. Test Examples
  226. =============
  227. This section provides example tests for each of the tests listed in the
  228. :ref:`test-manual/intro:How Tests Map to Areas of Code` section.
  229. For oeqa tests, testcases for each area reside in the main test
  230. directory at ``meta/lib/oeqa/selftest/cases`` directory.
  231. For oe-selftest. bitbake testcases reside in the ``lib/bb/tests/``
  232. directory.
  233. ``bitbake-selftest``
  234. --------------------
  235. A simple test example from ``lib/bb/tests/data.py`` is::
  236. class DataExpansions(unittest.TestCase):
  237. def setUp(self):
  238. self.d = bb.data.init()
  239. self.d["foo"] = "value_of_foo"
  240. self.d["bar"] = "value_of_bar"
  241. self.d["value_of_foo"] = "value_of_'value_of_foo'"
  242. def test_one_var(self):
  243. val = self.d.expand("${foo}")
  244. self.assertEqual(str(val), "value_of_foo")
  245. In this example, a ``DataExpansions`` class of tests is created,
  246. derived from standard Python unittest. The class has a common ``setUp``
  247. function which is shared by all the tests in the class. A simple test is
  248. then added to test that when a variable is expanded, the correct value
  249. is found.
  250. BitBake selftests are straightforward Python unittest. Refer to the
  251. Python unittest documentation for additional information on writing
  252. these tests at: https://docs.python.org/3/library/unittest.html.
  253. ``oe-selftest``
  254. ---------------
  255. These tests are more complex due to the setup required behind the scenes
  256. for full builds. Rather than directly using Python's unittest, 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.commandsrunCmd()``
  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 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 on it using the ``run()``
  304. method.
  305. To ensure certain test 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 setup. 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/python2.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, the code runs
  352. the python3 interpreter with a basic command to check it is working
  353. 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 DL_DIR which would potentially break other
  384. builds running in parallel. If this is required, DL_DIR must be set to
  385. an isolated directory.
  386. **Running "cleansstate" is not permitted.**
  387. This can delete files from SSTATE_DIR which would potentially break
  388. other builds running in parallel. If this is required, 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.