test-manual-intro.xml 37 KB

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