intro.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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
  66. `Jenkins <https://en.wikipedia.org/wiki/Jenkins_(software)>`__
  67. instead of Buildbot.
  68. The following figure shows the Yocto Project Autobuilder stack with a
  69. topology that includes a controller and a cluster of workers:
  70. .. image:: figures/ab-test-cluster.png
  71. :align: center
  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. classes 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/common-tasks: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. From within the BitBake repository, run the following::
  140. $ bitbake-selftest
  141. To skip tests that access the Internet, use the ``BB_SKIP_NETTESTS``
  142. variable when running "bitbake-selftest" as follows::
  143. $ BB_SKIP_NETTESTS=yes bitbake-selftest
  144. The default output is quiet and just prints a summary of what was
  145. run. To see more information, there is a verbose option::
  146. $ bitbake-selftest -v
  147. Use this option when you wish to skip tests that access the network,
  148. which are mostly necessary to test the fetcher modules. To specify
  149. individual test modules to run, append the test module name to the
  150. "bitbake-selftest" command. For example, to specify the tests for the
  151. bb.data.module, run::
  152. $ bitbake-selftest bb.test.data.module
  153. You can also specify individual tests by defining the full name and module
  154. plus the class path of the test, for example::
  155. $ bitbake-selftest bb.tests.data.TestOverrides.test_one_override
  156. The tests are based on `Python
  157. unittest <https://docs.python.org/3/library/unittest.html>`__.
  158. - *oe-selftest:*
  159. - These tests use OE to test the workflows, which include testing
  160. specific features, behaviors of tasks, and API unit tests.
  161. - The tests can take advantage of parallelism through the "-j"
  162. option, which can specify a number of threads to spread the tests
  163. across. Note that all tests from a given class of tests will run
  164. in the same thread. To parallelize large numbers of tests you can
  165. split the class into multiple units.
  166. - The tests are based on Python unittest.
  167. - The code for the tests resides in
  168. ``meta/lib/oeqa/selftest/cases/``.
  169. - To run all the tests, enter the following command::
  170. $ oe-selftest -a
  171. - To run a specific test, use the following command form where
  172. testname is the name of the specific test::
  173. $ oe-selftest -r <testname>
  174. For example, the following command would run the tinfoil
  175. getVar API test::
  176. $ oe-selftest -r tinfoil.TinfoilTests.test_getvar
  177. It is also possible to run a set
  178. of tests. For example the following command will run all of the
  179. tinfoil tests::
  180. $ oe-selftest -r tinfoil
  181. - *testimage:*
  182. - These tests build an image, boot it, and run tests against the
  183. image's content.
  184. - The code for these tests resides in ``meta/lib/oeqa/runtime/cases/``.
  185. - You need to set the :term:`IMAGE_CLASSES` variable as follows::
  186. IMAGE_CLASSES += "testimage"
  187. - Run the tests using the following command form::
  188. $ bitbake image -c testimage
  189. - *testsdk:*
  190. - These tests build an SDK, install it, and then run tests against
  191. that SDK.
  192. - The code for these tests resides in ``meta/lib/oeqa/sdk/cases/``.
  193. - Run the test using the following command form::
  194. $ bitbake image -c testsdk
  195. - *testsdk_ext:*
  196. - These tests build an extended SDK (eSDK), install that eSDK, and
  197. run tests against the eSDK.
  198. - The code for these tests resides in ``meta/lib/oeqa/esdk``.
  199. - To run the tests, use the following command form::
  200. $ bitbake image -c testsdkext
  201. - *oe-build-perf-test:*
  202. - These tests run through commonly used usage scenarios and measure
  203. the performance times.
  204. - The code for these tests resides in ``meta/lib/oeqa/buildperf``.
  205. - To run the tests, use the following command form::
  206. $ oe-build-perf-test <options>
  207. The command takes a number of options,
  208. such as where to place the test results. The Autobuilder Helper
  209. Scripts include the ``build-perf-test-wrapper`` script with
  210. examples of how to use the oe-build-perf-test from the command
  211. line.
  212. Use the ``oe-git-archive`` command to store test results into a
  213. Git repository.
  214. Use the ``oe-build-perf-report`` command to generate text reports
  215. and HTML reports with graphs of the performance data. For
  216. examples, see
  217. :yocto_dl:`/releases/yocto/yocto-2.7/testresults/buildperf-centos7/perf-centos7.yoctoproject.org_warrior_20190414204758_0e39202.html`
  218. and
  219. :yocto_dl:`/releases/yocto/yocto-2.7/testresults/buildperf-centos7/perf-centos7.yoctoproject.org_warrior_20190414204758_0e39202.txt`.
  220. - The tests are contained in ``lib/oeqa/buildperf/test_basic.py``.
  221. Test Examples
  222. =============
  223. This section provides example tests for each of the tests listed in the
  224. :ref:`test-manual/intro:How Tests Map to Areas of Code` section.
  225. For oeqa tests, testcases for each area reside in the main test
  226. directory at ``meta/lib/oeqa/selftest/cases`` directory.
  227. For oe-selftest. bitbake testcases reside in the ``lib/bb/tests/``
  228. directory.
  229. ``bitbake-selftest``
  230. --------------------
  231. A simple test example from ``lib/bb/tests/data.py`` is::
  232. class DataExpansions(unittest.TestCase):
  233. def setUp(self):
  234. self.d = bb.data.init()
  235. self.d["foo"] = "value_of_foo"
  236. self.d["bar"] = "value_of_bar"
  237. self.d["value_of_foo"] = "value_of_'value_of_foo'"
  238. def test_one_var(self):
  239. val = self.d.expand("${foo}")
  240. self.assertEqual(str(val), "value_of_foo")
  241. In this example, a ``DataExpansions`` class of tests is created,
  242. derived from standard Python unittest. The class has a common ``setUp``
  243. function which is shared by all the tests in the class. A simple test is
  244. then added to test that when a variable is expanded, the correct value
  245. is found.
  246. BitBake selftests are straightforward Python unittest. Refer to the
  247. Python unittest documentation for additional information on writing
  248. these tests at: https://docs.python.org/3/library/unittest.html.
  249. ``oe-selftest``
  250. ---------------
  251. These tests are more complex due to the setup required behind the scenes
  252. for full builds. Rather than directly using Python's unittest, the code
  253. wraps most of the standard objects. The tests can be simple, such as
  254. testing a command from within the OE build environment using the
  255. following example::
  256. class BitbakeLayers(OESelftestTestCase):
  257. def test_bitbakelayers_showcrossdepends(self):
  258. result = runCmd('bitbake-layers show-cross-depends')
  259. self.assertTrue('aspell' in result.output, msg = "No dependencies were shown. bitbake-layers show-cross-depends output: %s"% result.output)
  260. This example, taken from ``meta/lib/oeqa/selftest/cases/bblayers.py``,
  261. creates a testcase from the ``OESelftestTestCase`` class, derived
  262. from ``unittest.TestCase``, which runs the ``bitbake-layers`` command
  263. and checks the output to ensure it contains something we know should be
  264. here.
  265. The ``oeqa.utils.commands`` module contains Helpers which can assist
  266. with common tasks, including:
  267. - *Obtaining the value of a bitbake variable:* Use
  268. ``oeqa.utils.commands.get_bb_var()`` or use
  269. ``oeqa.utils.commands.get_bb_vars()`` for more than one variable
  270. - *Running a bitbake invocation for a build:* Use
  271. ``oeqa.utils.commands.bitbake()``
  272. - *Running a command:* Use ``oeqa.utils.commandsrunCmd()``
  273. There is also a ``oeqa.utils.commands.runqemu()`` function for launching
  274. the ``runqemu`` command for testing things within a running, virtualized
  275. image.
  276. You can run these tests in parallel. Parallelism works per test class,
  277. so tests within a given test class should always run in the same build,
  278. while tests in different classes or modules may be split into different
  279. builds. There is no data store available for these tests since the tests
  280. launch the ``bitbake`` command and exist outside of its context. As a
  281. result, common bitbake library functions (bb.\*) are also unavailable.
  282. ``testimage``
  283. -------------
  284. These tests are run once an image is up and running, either on target
  285. hardware or under QEMU. As a result, they are assumed to be running in a
  286. target image environment, as opposed to a host build environment. A
  287. simple example from ``meta/lib/oeqa/runtime/cases/python.py`` contains
  288. the following::
  289. class PythonTest(OERuntimeTestCase):
  290. @OETestDepends(['ssh.SSHTest.test_ssh'])
  291. @OEHasPackage(['python3-core'])
  292. def test_python3(self):
  293. cmd = "python3 -c \\"import codecs; print(codecs.encode('Uryyb, jbeyq', 'rot13'))\""
  294. status, output = self.target.run(cmd)
  295. msg = 'Exit status was not 0. Output: %s' % output
  296. self.assertEqual(status, 0, msg=msg)
  297. In this example, the ``OERuntimeTestCase`` class wraps
  298. ``unittest.TestCase``. Within the test, ``self.target`` represents the
  299. target system, where commands can be run on it using the ``run()``
  300. method.
  301. To ensure certain test or package dependencies are met, you can use the
  302. ``OETestDepends`` and ``OEHasPackage`` decorators. For example, the test
  303. in this example would only make sense if python3-core is installed in
  304. the image.
  305. ``testsdk_ext``
  306. ---------------
  307. These tests are run against built extensible SDKs (eSDKs). The tests can
  308. assume that the eSDK environment has already been setup. An example from
  309. ``meta/lib/oeqa/sdk/cases/devtool.py`` contains the following::
  310. class DevtoolTest(OESDKExtTestCase):
  311. @classmethod def setUpClass(cls):
  312. myapp_src = os.path.join(cls.tc.esdk_files_dir, "myapp")
  313. cls.myapp_dst = os.path.join(cls.tc.sdk_dir, "myapp")
  314. shutil.copytree(myapp_src, cls.myapp_dst)
  315. subprocess.check_output(['git', 'init', '.'], cwd=cls.myapp_dst)
  316. subprocess.check_output(['git', 'add', '.'], cwd=cls.myapp_dst)
  317. subprocess.check_output(['git', 'commit', '-m', "'test commit'"], cwd=cls.myapp_dst)
  318. @classmethod
  319. def tearDownClass(cls):
  320. shutil.rmtree(cls.myapp_dst)
  321. def _test_devtool_build(self, directory):
  322. self._run('devtool add myapp %s' % directory)
  323. try:
  324. self._run('devtool build myapp')
  325. finally:
  326. self._run('devtool reset myapp')
  327. def test_devtool_build_make(self):
  328. self._test_devtool_build(self.myapp_dst)
  329. In this example, the ``devtool``
  330. command is tested to see whether a sample application can be built with
  331. the ``devtool build`` command within the eSDK.
  332. ``testsdk``
  333. -----------
  334. These tests are run against built SDKs. The tests can assume that an SDK
  335. has already been extracted and its environment file has been sourced. A
  336. simple example from ``meta/lib/oeqa/sdk/cases/python2.py`` contains the
  337. following::
  338. class Python3Test(OESDKTestCase):
  339. def setUp(self):
  340. if not (self.tc.hasHostPackage("nativesdk-python3-core") or
  341. self.tc.hasHostPackage("python3-core-native")):
  342. raise unittest.SkipTest("No python3 package in the SDK")
  343. def test_python3(self):
  344. cmd = "python3 -c \\"import codecs; print(codecs.encode('Uryyb, jbeyq', 'rot13'))\""
  345. output = self._run(cmd)
  346. self.assertEqual(output, "Hello, world\n")
  347. In this example, if nativesdk-python3-core has been installed into the SDK, the code runs
  348. the python3 interpreter with a basic command to check it is working
  349. correctly. The test would only run if Python3 is installed in the SDK.
  350. ``oe-build-perf-test``
  351. ----------------------
  352. The performance tests usually measure how long operations take and the
  353. resource utilization as that happens. An example from
  354. ``meta/lib/oeqa/buildperf/test_basic.py`` contains the following::
  355. class Test3(BuildPerfTestCase):
  356. def test3(self):
  357. """Bitbake parsing (bitbake -p)"""
  358. # Drop all caches and parse
  359. self.rm_cache()
  360. oe.path.remove(os.path.join(self.bb_vars['TMPDIR'], 'cache'), True)
  361. self.measure_cmd_resources(['bitbake', '-p'], 'parse_1',
  362. 'bitbake -p (no caches)')
  363. # Drop tmp/cache
  364. oe.path.remove(os.path.join(self.bb_vars['TMPDIR'], 'cache'), True)
  365. self.measure_cmd_resources(['bitbake', '-p'], 'parse_2',
  366. 'bitbake -p (no tmp/cache)')
  367. # Parse with fully cached data
  368. self.measure_cmd_resources(['bitbake', '-p'], 'parse_3',
  369. 'bitbake -p (cached)')
  370. This example shows how three specific parsing timings are
  371. measured, with and without various caches, to show how BitBake's parsing
  372. performance trends over time.
  373. Considerations When Writing Tests
  374. =================================
  375. When writing good tests, there are several things to keep in mind. Since
  376. things running on the Autobuilder are accessed concurrently by multiple
  377. workers, consider the following:
  378. **Running "cleanall" is not permitted.**
  379. This can delete files from DL_DIR which would potentially break other
  380. builds running in parallel. If this is required, DL_DIR must be set to
  381. an isolated directory.
  382. **Running "cleansstate" is not permitted.**
  383. This can delete files from SSTATE_DIR which would potentially break
  384. other builds running in parallel. If this is required, SSTATE_DIR must
  385. be set to an isolated directory. Alternatively, you can use the "-f"
  386. option with the ``bitbake`` command to "taint" tasks by changing the
  387. sstate checksums to ensure sstate cache items will not be reused.
  388. **Tests should not change the metadata.**
  389. This is particularly true for oe-selftests since these can run in
  390. parallel and changing metadata leads to changing checksums, which
  391. confuses BitBake while running in parallel. If this is necessary, copy
  392. layers to a temporary location and modify them. Some tests need to
  393. change metadata, such as the devtool tests. To protect the metadata from
  394. changes, set up temporary copies of that data first.