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