bbtests.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. import os
  7. import re
  8. import oeqa.utils.ftools as ftools
  9. from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
  10. from oeqa.selftest.case import OESelftestTestCase
  11. class BitbakeTests(OESelftestTestCase):
  12. def getline(self, res, line):
  13. for l in res.output.split('\n'):
  14. if line in l:
  15. return l
  16. # Test bitbake can run from the <builddir>/conf directory
  17. def test_run_bitbake_from_dir_1(self):
  18. os.chdir(os.path.join(self.builddir, 'conf'))
  19. self.assertEqual(bitbake('-e').status, 0, msg = "bitbake couldn't run from \"conf\" dir")
  20. # Test bitbake can run from the <builddir>'s parent directory
  21. def test_run_bitbake_from_dir_2(self):
  22. my_env = os.environ.copy()
  23. my_env['BBPATH'] = my_env['BUILDDIR']
  24. os.chdir(os.path.dirname(os.environ['BUILDDIR']))
  25. self.assertEqual(bitbake('-e', env=my_env).status, 0, msg = "bitbake couldn't run from builddir's parent directory")
  26. # Test bitbake can run from some other random system location (we use /tmp/)
  27. def test_run_bitbake_from_dir_3(self):
  28. my_env = os.environ.copy()
  29. my_env['BBPATH'] = my_env['BUILDDIR']
  30. os.chdir("/tmp/")
  31. self.assertEqual(bitbake('-e', env=my_env).status, 0, msg = "bitbake couldn't run from /tmp/")
  32. def test_event_handler(self):
  33. self.write_config("INHERIT += \"test_events\"")
  34. result = bitbake('selftest-hello-native')
  35. find_build_started = re.search(r"NOTE: Test for bb\.event\.BuildStarted(\n.*)*NOTE: Executing.*Tasks", result.output)
  36. find_build_completed = re.search(r"Tasks Summary:.*(\n.*)*NOTE: Test for bb\.event\.BuildCompleted", result.output)
  37. self.assertTrue(find_build_started, msg = "Match failed in:\n%s" % result.output)
  38. self.assertTrue(find_build_completed, msg = "Match failed in:\n%s" % result.output)
  39. self.assertNotIn('Test for bb.event.InvalidEvent', result.output)
  40. def test_local_sstate(self):
  41. bitbake('selftest-hello-native')
  42. bitbake('selftest-hello-native -cclean')
  43. result = bitbake('selftest-hello-native')
  44. find_setscene = re.search("selftest-hello-native.*do_.*_setscene", result.output)
  45. self.assertTrue(find_setscene, msg = "No \"selftest-hello-native.*do_.*_setscene\" message found during bitbake selftest-hello-native. bitbake output: %s" % result.output )
  46. def test_bitbake_invalid_recipe(self):
  47. result = bitbake('-b asdf', ignore_status=True)
  48. self.assertTrue("ERROR: Unable to find any recipe file matching 'asdf'" in result.output, msg = "Though asdf recipe doesn't exist, bitbake didn't output any err. message. bitbake output: %s" % result.output)
  49. def test_bitbake_invalid_target(self):
  50. result = bitbake('asdf', ignore_status=True)
  51. self.assertIn("ERROR: Nothing PROVIDES 'asdf'", result.output)
  52. def test_warnings_errors(self):
  53. result = bitbake('-b asdf', ignore_status=True)
  54. find_warnings = re.search("Summary: There w.{2,3}? [1-9][0-9]* WARNING messages*", result.output)
  55. find_errors = re.search("Summary: There w.{2,3}? [1-9][0-9]* ERROR messages*", result.output)
  56. self.assertTrue(find_warnings, msg="Did not find the mumber of warnings at the end of the build:\n" + result.output)
  57. self.assertTrue(find_errors, msg="Did not find the mumber of errors at the end of the build:\n" + result.output)
  58. def test_invalid_patch(self):
  59. # This patch should fail to apply.
  60. self.write_recipeinc('man-db', 'FILESEXTRAPATHS:prepend := "${THISDIR}/files:"\nSRC_URI += "file://0001-Test-patch-here.patch"')
  61. self.write_config("INHERIT:remove = \"report-error\"")
  62. result = bitbake('man-db -c patch', ignore_status=True)
  63. self.delete_recipeinc('man-db')
  64. bitbake('-cclean man-db')
  65. found = False
  66. for l in result.output.split('\n'):
  67. if l.startswith("ERROR:") and "failed" in l and "do_patch" in l:
  68. found = l
  69. self.assertTrue(found and found.startswith("ERROR:"), msg = "Incorrectly formed patch application didn't fail. bitbake output: %s" % result.output)
  70. def test_force_task_1(self):
  71. # test 1 from bug 5875
  72. import uuid
  73. test_recipe = 'zlib'
  74. # Need to use uuid otherwise hash equivlance would change the workflow
  75. test_data = "Microsoft Made No Profit From Anyone's Zunes Yo %s" % uuid.uuid1()
  76. bb_vars = get_bb_vars(['D', 'PKGDEST', 'mandir'], test_recipe)
  77. image_dir = bb_vars['D']
  78. pkgsplit_dir = bb_vars['PKGDEST']
  79. man_dir = bb_vars['mandir']
  80. self.write_config("PACKAGE_CLASSES = \"package_rpm\"")
  81. bitbake('-c clean %s' % test_recipe)
  82. bitbake('-c package -f %s' % test_recipe)
  83. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  84. man_file = os.path.join(image_dir + man_dir, 'man3/zlib.3')
  85. ftools.append_file(man_file, test_data)
  86. bitbake('-c package -f %s' % test_recipe)
  87. man_split_file = os.path.join(pkgsplit_dir, 'zlib-doc' + man_dir, 'man3/zlib.3')
  88. man_split_content = ftools.read_file(man_split_file)
  89. self.assertIn(test_data, man_split_content, 'The man file has not changed in packages-split.')
  90. ret = bitbake(test_recipe)
  91. self.assertIn('task do_package_write_rpm:', ret.output, 'Task do_package_write_rpm did not re-executed.')
  92. def test_force_task_2(self):
  93. # test 2 from bug 5875
  94. test_recipe = 'zlib'
  95. bitbake(test_recipe)
  96. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  97. result = bitbake('-C compile %s' % test_recipe)
  98. look_for_tasks = ['do_compile:', 'do_install:', 'do_populate_sysroot:', 'do_package:']
  99. for task in look_for_tasks:
  100. self.assertIn(task, result.output, msg="Couldn't find %s task.")
  101. def test_bitbake_g(self):
  102. recipe = 'base-files'
  103. result = bitbake('-g %s' % recipe)
  104. for f in ['pn-buildlist', 'task-depends.dot']:
  105. self.addCleanup(os.remove, f)
  106. self.assertTrue('Task dependencies saved to \'task-depends.dot\'' in result.output, msg = "No task dependency \"task-depends.dot\" file was generated for the given task target. bitbake output: %s" % result.output)
  107. self.assertIn(recipe, ftools.read_file(os.path.join(self.builddir, 'task-depends.dot')))
  108. def test_image_manifest(self):
  109. bitbake('core-image-minimal')
  110. bb_vars = get_bb_vars(["DEPLOY_DIR_IMAGE", "IMAGE_LINK_NAME"], "core-image-minimal")
  111. deploydir = bb_vars["DEPLOY_DIR_IMAGE"]
  112. imagename = bb_vars["IMAGE_LINK_NAME"]
  113. manifest = os.path.join(deploydir, imagename + ".manifest")
  114. self.assertTrue(os.path.islink(manifest), msg="No manifest file created for image. It should have been created in %s" % manifest)
  115. def test_invalid_recipe_src_uri(self):
  116. data = 'SRC_URI = "file://invalid"'
  117. self.write_recipeinc('man-db', data)
  118. self.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
  119. SSTATE_DIR = \"${TOPDIR}/download-selftest\"
  120. INHERIT:remove = \"report-error\"
  121. """)
  122. self.track_for_cleanup(os.path.join(self.builddir, "download-selftest"))
  123. result = bitbake('-c fetch man-db', ignore_status=True)
  124. self.delete_recipeinc('man-db')
  125. self.assertEqual(result.status, 1, msg="Command succeded when it should have failed. bitbake output: %s" % result.output)
  126. self.assertIn('Unable to get checksum for man-db SRC_URI entry invalid: file could not be found', result.output)
  127. def test_rename_downloaded_file(self):
  128. # TODO unique dldir instead of using cleanall
  129. # TODO: need to set sstatedir?
  130. self.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
  131. SSTATE_DIR = \"${TOPDIR}/download-selftest\"
  132. """)
  133. self.track_for_cleanup(os.path.join(self.builddir, "download-selftest"))
  134. data = 'SRC_URI = "https://downloads.yoctoproject.org/mirror/sources/aspell-${PV}.tar.gz;downloadfilename=test-aspell.tar.gz"'
  135. self.write_recipeinc('aspell', data)
  136. result = bitbake('-f -c fetch aspell', ignore_status=True)
  137. self.delete_recipeinc('aspell')
  138. self.assertEqual(result.status, 0, msg = "Couldn't fetch aspell. %s" % result.output)
  139. dl_dir = get_bb_var("DL_DIR")
  140. self.assertTrue(os.path.isfile(os.path.join(dl_dir, 'test-aspell.tar.gz')), msg = "File rename failed. No corresponding test-aspell.tar.gz file found under %s" % dl_dir)
  141. self.assertTrue(os.path.isfile(os.path.join(dl_dir, 'test-aspell.tar.gz.done')), "File rename failed. No corresponding test-aspell.tar.gz.done file found under %s" % dl_dir)
  142. def test_environment(self):
  143. self.write_config("TEST_ENV=\"localconf\"")
  144. result = runCmd('bitbake -e | grep TEST_ENV=')
  145. self.assertIn('localconf', result.output)
  146. def test_dry_run(self):
  147. result = runCmd('bitbake -n selftest-hello-native')
  148. self.assertEqual(0, result.status, "bitbake dry run didn't run as expected. %s" % result.output)
  149. def test_just_parse(self):
  150. result = runCmd('bitbake -p')
  151. self.assertEqual(0, result.status, "errors encountered when parsing recipes. %s" % result.output)
  152. def test_version(self):
  153. result = runCmd('bitbake -s | grep wget')
  154. find = re.search(r"wget *:([0-9a-zA-Z\.\-]+)", result.output)
  155. self.assertTrue(find, "No version returned for searched recipe. bitbake output: %s" % result.output)
  156. def test_prefile(self):
  157. # Test when the prefile does not exist
  158. result = runCmd('bitbake -r conf/prefile.conf', ignore_status=True)
  159. self.assertEqual(1, result.status, "bitbake didn't error and should have when a specified prefile didn't exist: %s" % result.output)
  160. # Test when the prefile exists
  161. preconf = os.path.join(self.builddir, 'conf/prefile.conf')
  162. self.track_for_cleanup(preconf)
  163. ftools.write_file(preconf ,"TEST_PREFILE=\"prefile\"")
  164. result = runCmd('bitbake -r conf/prefile.conf -e | grep TEST_PREFILE=')
  165. self.assertIn('prefile', result.output)
  166. self.write_config("TEST_PREFILE=\"localconf\"")
  167. result = runCmd('bitbake -r conf/prefile.conf -e | grep TEST_PREFILE=')
  168. self.assertIn('localconf', result.output)
  169. def test_postfile(self):
  170. # Test when the postfile does not exist
  171. result = runCmd('bitbake -R conf/postfile.conf', ignore_status=True)
  172. self.assertEqual(1, result.status, "bitbake didn't error and should have when a specified postfile didn't exist: %s" % result.output)
  173. # Test when the postfile exists
  174. postconf = os.path.join(self.builddir, 'conf/postfile.conf')
  175. self.track_for_cleanup(postconf)
  176. ftools.write_file(postconf , "TEST_POSTFILE=\"postfile\"")
  177. self.write_config("TEST_POSTFILE=\"localconf\"")
  178. result = runCmd('bitbake -R conf/postfile.conf -e | grep TEST_POSTFILE=')
  179. self.assertIn('postfile', result.output)
  180. def test_checkuri(self):
  181. result = runCmd('bitbake -c checkuri m4')
  182. self.assertEqual(0, result.status, msg = "\"checkuri\" task was not executed. bitbake output: %s" % result.output)
  183. def test_continue(self):
  184. self.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
  185. SSTATE_DIR = \"${TOPDIR}/download-selftest\"
  186. INHERIT:remove = \"report-error\"
  187. """)
  188. self.track_for_cleanup(os.path.join(self.builddir, "download-selftest"))
  189. self.write_recipeinc('man-db',"\ndo_fail_task () {\nexit 1 \n}\n\naddtask do_fail_task before do_fetch\n" )
  190. runCmd('bitbake -c cleanall man-db xcursor-transparent-theme')
  191. result = runCmd('bitbake -c unpack -k man-db xcursor-transparent-theme', ignore_status=True)
  192. errorpos = result.output.find('ERROR: Function failed: do_fail_task')
  193. manver = re.search("NOTE: recipe xcursor-transparent-theme-(.*?): task do_unpack: Started", result.output)
  194. continuepos = result.output.find('NOTE: recipe xcursor-transparent-theme-%s: task do_unpack: Started' % manver.group(1))
  195. self.assertLess(errorpos,continuepos, msg = "bitbake didn't pass do_fail_task. bitbake output: %s" % result.output)
  196. def test_non_gplv3(self):
  197. self.write_config('''INCOMPATIBLE_LICENSE = "GPL-3.0-or-later"
  198. OVERRIDES .= ":gplv3test"
  199. require conf/distro/include/no-gplv3.inc
  200. ''')
  201. result = bitbake('selftest-ed', ignore_status=True)
  202. self.assertEqual(result.status, 0, "Bitbake failed, exit code %s, output %s" % (result.status, result.output))
  203. lic_dir = get_bb_var('LICENSE_DIRECTORY')
  204. arch = get_bb_var('SSTATE_PKGARCH')
  205. filename = os.path.join(lic_dir, arch, 'selftest-ed', 'generic_GPL-3.0-or-later')
  206. self.assertFalse(os.path.isfile(filename), msg="License file %s exists and shouldn't" % filename)
  207. filename = os.path.join(lic_dir, arch, 'selftest-ed', 'generic_GPL-2.0-only')
  208. self.assertTrue(os.path.isfile(filename), msg="License file %s doesn't exist" % filename)
  209. def test_setscene_only(self):
  210. """ Bitbake option to restore from sstate only within a build (i.e. execute no real tasks, only setscene)"""
  211. test_recipe = 'selftest-hello-native'
  212. bitbake(test_recipe)
  213. bitbake('-c clean %s' % test_recipe)
  214. ret = bitbake('--setscene-only %s' % test_recipe)
  215. tasks = re.findall(r'task\s+(do_\S+):', ret.output)
  216. for task in tasks:
  217. self.assertIn('_setscene', task, 'A task different from _setscene ran: %s.\n'
  218. 'Executed tasks were: %s' % (task, str(tasks)))
  219. def test_skip_setscene(self):
  220. test_recipe = 'selftest-hello-native'
  221. bitbake(test_recipe)
  222. bitbake('-c clean %s' % test_recipe)
  223. ret = bitbake('--setscene-only %s' % test_recipe)
  224. tasks = re.findall(r'task\s+(do_\S+):', ret.output)
  225. for task in tasks:
  226. self.assertIn('_setscene', task, 'A task different from _setscene ran: %s.\n'
  227. 'Executed tasks were: %s' % (task, str(tasks)))
  228. # Run without setscene. Should do nothing
  229. ret = bitbake('--skip-setscene %s' % test_recipe)
  230. tasks = re.findall(r'task\s+(do_\S+):', ret.output)
  231. self.assertFalse(tasks, 'Tasks %s ran when they should not have' % (str(tasks)))
  232. # Clean (leave sstate cache) and run with --skip-setscene. No setscene
  233. # tasks should run
  234. bitbake('-c clean %s' % test_recipe)
  235. ret = bitbake('--skip-setscene %s' % test_recipe)
  236. tasks = re.findall(r'task\s+(do_\S+):', ret.output)
  237. for task in tasks:
  238. self.assertNotIn('_setscene', task, 'A _setscene task ran: %s.\n'
  239. 'Executed tasks were: %s' % (task, str(tasks)))
  240. def test_bbappend_order(self):
  241. """ Bitbake should bbappend to recipe in a predictable order """
  242. test_recipe = 'ed'
  243. bb_vars = get_bb_vars(['SUMMARY', 'PV'], test_recipe)
  244. test_recipe_summary_before = bb_vars['SUMMARY']
  245. test_recipe_pv = bb_vars['PV']
  246. recipe_append_file = test_recipe + '_' + test_recipe_pv + '.bbappend'
  247. expected_recipe_summary = test_recipe_summary_before
  248. for i in range(5):
  249. recipe_append_dir = test_recipe + '_test_' + str(i)
  250. recipe_append_path = os.path.join(self.testlayer_path, 'recipes-test', recipe_append_dir, recipe_append_file)
  251. os.mkdir(os.path.join(self.testlayer_path, 'recipes-test', recipe_append_dir))
  252. feature = 'SUMMARY += "%s"\n' % i
  253. ftools.write_file(recipe_append_path, feature)
  254. expected_recipe_summary += ' %s' % i
  255. self.add_command_to_tearDown('rm -rf %s' % os.path.join(self.testlayer_path, 'recipes-test',
  256. test_recipe + '_test_*'))
  257. test_recipe_summary_after = get_bb_var('SUMMARY', test_recipe)
  258. self.assertEqual(expected_recipe_summary, test_recipe_summary_after)
  259. def test_git_patchtool(self):
  260. """ PATCHTOOL=git should work with non-git sources like tarballs
  261. test recipe for the test must NOT containt git:// repository in SRC_URI
  262. """
  263. test_recipe = "man-db"
  264. self.write_recipeinc(test_recipe, 'PATCHTOOL=\"git\"')
  265. src = get_bb_var("SRC_URI",test_recipe)
  266. gitscm = re.search("git://", src)
  267. self.assertFalse(gitscm, "test_git_patchtool pre-condition failed: {} test recipe contains git repo!".format(test_recipe))
  268. result = bitbake('{} -c patch'.format(test_recipe), ignore_status=False)
  269. fatal = re.search("fatal: not a git repository (or any of the parent directories)", result.output)
  270. self.assertFalse(fatal, "Failed to patch using PATCHTOOL=\"git\"")
  271. self.delete_recipeinc(test_recipe)
  272. bitbake('-cclean {}'.format(test_recipe))
  273. def test_git_patchtool2(self):
  274. """ Test if PATCHTOOL=git works with git repo and doesn't reinitialize it
  275. """
  276. test_recipe = "gitrepotest"
  277. src = get_bb_var("SRC_URI",test_recipe)
  278. gitscm = re.search("git://", src)
  279. self.assertTrue(gitscm, "test_git_patchtool pre-condition failed: {} test recipe doesn't contains git repo!".format(test_recipe))
  280. result = bitbake('{} -c patch'.format(test_recipe), ignore_status=False)
  281. srcdir = get_bb_var('S', test_recipe)
  282. result = runCmd("git log", cwd = srcdir)
  283. self.assertFalse("bitbake_patching_started" in result.output, msg = "Repository has been reinitialized. {}".format(srcdir))
  284. self.delete_recipeinc(test_recipe)
  285. bitbake('-cclean {}'.format(test_recipe))
  286. def test_git_unpack_nonetwork(self):
  287. """
  288. Test that a recipe with a floating tag that needs to be resolved upstream doesn't
  289. access the network in a patch task run in a separate builld invocation
  290. """
  291. # Enable the recipe to float using a distro override
  292. self.write_config("DISTROOVERRIDES .= \":gitunpack-enable-recipe\"")
  293. bitbake('gitunpackoffline -c fetch')
  294. bitbake('gitunpackoffline -c patch')
  295. def test_git_unpack_nonetwork_fail(self):
  296. """
  297. Test that a recipe with a floating tag which doesn't call get_srcrev() in the fetcher
  298. raises an error when the fetcher is called.
  299. """
  300. # Enable the recipe to float using a distro override
  301. self.write_config("DISTROOVERRIDES .= \":gitunpack-enable-recipe\"")
  302. result = bitbake('gitunpackoffline-fail -c fetch', ignore_status=True)
  303. self.assertTrue(re.search("Recipe uses a floating tag/branch .* for repo .* without a fixed SRCREV yet doesn't call bb.fetch2.get_srcrev()", result.output), msg = "Recipe without PV set to SRCPV should have failed: %s" % result.output)
  304. def test_unexpanded_variable_in_path(self):
  305. """
  306. Test that bitbake fails if directory contains unexpanded bitbake variable in the name
  307. """
  308. recipe_name = "gitunpackoffline"
  309. self.write_config('PV:pn-gitunpackoffline:append = "+${UNDEFVAL}"')
  310. result = bitbake('{}'.format(recipe_name), ignore_status=True)
  311. self.assertGreater(result.status, 0, "Build should have failed if ${ is in the path")
  312. self.assertTrue(re.search("ERROR: Directory name /.* contains unexpanded bitbake variable. This may cause build failures and WORKDIR polution",
  313. result.output), msg = "mkdirhier with unexpanded variable should have failed: %s" % result.output)
  314. def test_bb_env_bb_getvar_equality(self):
  315. """ Test if "bitbake -e" output is identical to "bitbake-getvar" output for a variable set from an anonymous function
  316. """
  317. self.write_config('''INHERIT += "test_anon_func"
  318. TEST_SET_FROM_ANON_FUNC ?= ""''')
  319. result_bb_e = runCmd('bitbake -e')
  320. bb_e_var_match = re.search('^TEST_SET_FROM_ANON_FUNC="(?P<value>.*)"$', result_bb_e.output, re.MULTILINE)
  321. self.assertTrue(bb_e_var_match, msg = "Can't find TEST_SET_FROM_ANON_FUNC value in \"bitbake -e\" output")
  322. bb_e_var_value = bb_e_var_match.group("value")
  323. result_bb_getvar = runCmd('bitbake-getvar TEST_SET_FROM_ANON_FUNC --value')
  324. bb_getvar_var_value = result_bb_getvar.output.strip()
  325. self.assertEqual(bb_e_var_value, bb_getvar_var_value,
  326. msg='''"bitbake -e" output differs from bitbake-getvar output for TEST_SET_FROM_ANON_FUNC (set from anonymous function)
  327. bitbake -e: "%s"
  328. bitbake-getvar: "%s"''' % (bb_e_var_value, bb_getvar_var_value))