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