bblayers.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, get_bb_var, get_bb_vars, bitbake
  10. from oeqa.selftest.case import OESelftestTestCase
  11. class BitbakeLayers(OESelftestTestCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. super(BitbakeLayers, cls).setUpClass()
  15. bitbake("python3-jsonschema-native")
  16. bitbake("-c addto_recipe_sysroot python3-jsonschema-native")
  17. def test_bitbakelayers_layerindexshowdepends(self):
  18. result = runCmd('bitbake-layers layerindex-show-depends meta-poky')
  19. find_in_contents = re.search("openembedded-core", result.output)
  20. self.assertTrue(find_in_contents, msg = "openembedded-core should have been listed at this step. bitbake-layers layerindex-show-depends meta-poky output: %s" % result.output)
  21. def test_bitbakelayers_showcrossdepends(self):
  22. result = runCmd('bitbake-layers show-cross-depends')
  23. self.assertIn('aspell', result.output)
  24. def test_bitbakelayers_showlayers(self):
  25. result = runCmd('bitbake-layers show-layers')
  26. self.assertIn('meta-selftest', result.output)
  27. def test_bitbakelayers_showappends(self):
  28. recipe = "xcursor-transparent-theme"
  29. bb_file = self.get_recipe_basename(recipe)
  30. result = runCmd('bitbake-layers show-appends')
  31. self.assertIn(bb_file, result.output)
  32. def test_bitbakelayers_showoverlayed(self):
  33. result = runCmd('bitbake-layers show-overlayed')
  34. self.assertIn('aspell', result.output)
  35. def test_bitbakelayers_flatten(self):
  36. recipe = "xcursor-transparent-theme"
  37. recipe_path = "recipes-graphics/xcursor-transparent-theme"
  38. recipe_file = self.get_recipe_basename(recipe)
  39. testoutdir = os.path.join(self.builddir, 'test_bitbakelayers_flatten')
  40. self.assertFalse(os.path.isdir(testoutdir), msg = "test_bitbakelayers_flatten should not exist at this point in time")
  41. self.track_for_cleanup(testoutdir)
  42. result = runCmd('bitbake-layers flatten %s' % testoutdir)
  43. bb_file = os.path.join(testoutdir, recipe_path, recipe_file)
  44. self.assertTrue(os.path.isfile(bb_file), msg = "Cannot find xcursor-transparent-theme_0.1.1.bb in the test_bitbakelayers_flatten local dir.")
  45. contents = ftools.read_file(bb_file)
  46. find_in_contents = re.search(r"##### bbappended from meta-selftest #####\n(.*\n)*include test_recipe.inc", contents)
  47. self.assertTrue(find_in_contents, msg = "Flattening layers did not work. bitbake-layers flatten output: %s" % result.output)
  48. def test_bitbakelayers_add_remove(self):
  49. test_layer = os.path.join(get_bb_var('COREBASE'), 'meta-skeleton')
  50. result = runCmd('bitbake-layers show-layers')
  51. self.assertNotIn('meta-skeleton', result.output, "This test cannot run with meta-skeleton in bblayers.conf. bitbake-layers show-layers output: %s" % result.output)
  52. result = runCmd('bitbake-layers add-layer %s' % test_layer)
  53. result = runCmd('bitbake-layers show-layers')
  54. self.assertIn('meta-skeleton', result.output, msg = "Something wrong happened. meta-skeleton layer was not added to conf/bblayers.conf. bitbake-layers show-layers output: %s" % result.output)
  55. result = runCmd('bitbake-layers remove-layer %s' % test_layer)
  56. result = runCmd('bitbake-layers show-layers')
  57. self.assertNotIn('meta-skeleton', result.output, msg = "meta-skeleton should have been removed at this step. bitbake-layers show-layers output: %s" % result.output)
  58. result = runCmd('bitbake-layers add-layer %s' % test_layer)
  59. result = runCmd('bitbake-layers show-layers')
  60. self.assertIn('meta-skeleton', result.output, msg = "Something wrong happened. meta-skeleton layer was not added to conf/bblayers.conf. bitbake-layers show-layers output: %s" % result.output)
  61. result = runCmd('bitbake-layers remove-layer */meta-skeleton')
  62. result = runCmd('bitbake-layers show-layers')
  63. self.assertNotIn('meta-skeleton', result.output, msg = "meta-skeleton should have been removed at this step. bitbake-layers show-layers output: %s" % result.output)
  64. def test_bitbakelayers_showrecipes(self):
  65. result = runCmd('bitbake-layers show-recipes')
  66. self.assertIn('aspell:', result.output)
  67. self.assertIn('mtd-utils:', result.output)
  68. self.assertIn('core-image-minimal:', result.output)
  69. result = runCmd('bitbake-layers show-recipes mtd-utils')
  70. self.assertIn('mtd-utils:', result.output)
  71. self.assertNotIn('aspell:', result.output)
  72. result = runCmd('bitbake-layers show-recipes -i image')
  73. self.assertIn('core-image-minimal', result.output)
  74. self.assertNotIn('mtd-utils:', result.output)
  75. result = runCmd('bitbake-layers show-recipes -i meson,pkgconfig')
  76. self.assertIn('libproxy:', result.output)
  77. result = runCmd('bitbake-layers show-recipes -i cmake,pkgconfig')
  78. self.assertNotIn('mtd-utils:', result.output) # doesn't inherit either
  79. self.assertNotIn('wget:', result.output) # doesn't inherit cmake
  80. self.assertNotIn('waffle:', result.output) # doesn't inherit pkgconfig
  81. result = runCmd('bitbake-layers show-recipes -i nonexistentclass', ignore_status=True)
  82. self.assertNotEqual(result.status, 0, 'bitbake-layers show-recipes -i nonexistentclass should have failed')
  83. self.assertIn('ERROR:', result.output)
  84. def test_bitbakelayers_createlayer(self):
  85. priority = 10
  86. layername = 'test-bitbakelayer-layercreate'
  87. layerpath = os.path.join(self.builddir, layername)
  88. self.assertFalse(os.path.exists(layerpath), '%s should not exist at this point in time' % layerpath)
  89. result = runCmd('bitbake-layers create-layer --priority=%d %s' % (priority, layerpath))
  90. self.track_for_cleanup(layerpath)
  91. result = runCmd('bitbake-layers add-layer %s' % layerpath)
  92. self.add_command_to_tearDown('bitbake-layers remove-layer %s' % layerpath)
  93. result = runCmd('bitbake-layers show-layers')
  94. find_in_contents = re.search(re.escape(layername) + r'\s+' + re.escape(layerpath) + r'\s+' + re.escape(str(priority)), result.output)
  95. self.assertTrue(find_in_contents, "%s not found in layers\n%s" % (layername, result.output))
  96. layervars = ['BBFILE_PRIORITY', 'BBFILE_PATTERN', 'LAYERDEPENDS', 'LAYERSERIES_COMPAT']
  97. bb_vars = get_bb_vars(['BBFILE_COLLECTIONS'] + ['%s_%s' % (v, layername) for v in layervars])
  98. for v in layervars:
  99. varname = '%s_%s' % (v, layername)
  100. self.assertIsNotNone(bb_vars[varname], "%s not found" % varname)
  101. find_in_contents = re.search(r'(^|\s)' + re.escape(layername) + r'($|\s)', bb_vars['BBFILE_COLLECTIONS'])
  102. self.assertTrue(find_in_contents, "%s not in BBFILE_COLLECTIONS" % layername)
  103. self.assertEqual(bb_vars['BBFILE_PRIORITY_%s' % layername], str(priority), 'BBFILE_PRIORITY_%s != %d' % (layername, priority))
  104. result = runCmd('bitbake-layers save-build-conf {} {}'.format(layerpath, "buildconf-1"))
  105. for f in ('local.conf.sample', 'bblayers.conf.sample', 'conf-summary.txt', 'conf-notes.txt'):
  106. fullpath = os.path.join(layerpath, "conf", "templates", "buildconf-1", f)
  107. self.assertTrue(os.path.exists(fullpath), "Template configuration file {} not found".format(fullpath))
  108. def get_recipe_basename(self, recipe):
  109. recipe_file = ""
  110. result = runCmd("bitbake-layers show-recipes -f %s" % recipe)
  111. for line in result.output.splitlines():
  112. if recipe in line:
  113. recipe_file = line
  114. break
  115. self.assertTrue(os.path.isfile(recipe_file), msg = "Can't find recipe file for %s" % recipe)
  116. return os.path.basename(recipe_file)
  117. def validate_layersjson(self, json):
  118. python = os.path.join(get_bb_var('STAGING_BINDIR', 'python3-jsonschema-native'), 'nativepython3')
  119. jsonvalidator = os.path.join(get_bb_var('STAGING_BINDIR', 'python3-jsonschema-native'), 'jsonschema')
  120. jsonschema = os.path.join(get_bb_var('COREBASE'), 'meta/files/layers.schema.json')
  121. result = runCmd("{} {} -i {} {}".format(python, jsonvalidator, json, jsonschema))
  122. def test_validate_examplelayersjson(self):
  123. json = os.path.join(get_bb_var('COREBASE'), "meta/files/layers.example.json")
  124. self.validate_layersjson(json)
  125. def test_bitbakelayers_setup(self):
  126. result = runCmd('bitbake-layers create-layers-setup {}'.format(self.testlayer_path))
  127. jsonfile = os.path.join(self.testlayer_path, "setup-layers.json")
  128. self.validate_layersjson(jsonfile)
  129. # The revision-under-test may not necessarily be available on the remote server,
  130. # so replace it with a revision that has a yocto-4.1 tag.
  131. import json
  132. with open(jsonfile) as f:
  133. data = json.load(f)
  134. for s in data['sources']:
  135. data['sources'][s]['git-remote']['rev'] = '5200799866b92259e855051112520006e1aaaac0'
  136. with open(jsonfile, 'w') as f:
  137. json.dump(data, f)
  138. testcheckoutdir = os.path.join(self.builddir, 'test-layer-checkout')
  139. result = runCmd('{}/setup-layers --destdir {}'.format(self.testlayer_path, testcheckoutdir))
  140. layers_json = os.path.join(testcheckoutdir, ".oe-layers.json")
  141. self.assertTrue(os.path.exists(layers_json), "File {} not found in test layer checkout".format(layers_json))
  142. # As setup-layers checkout out an old revision of poky, there is no setup-build symlink,
  143. # and we need to run oe-setup-build directly from the current poky tree under test
  144. oe_setup_build = os.path.join(get_bb_var('COREBASE'), 'scripts/oe-setup-build')
  145. oe_setup_build_l = os.path.join(testcheckoutdir, 'setup-build')
  146. os.symlink(oe_setup_build,oe_setup_build_l)
  147. cmd = '{} --layerlist {} list -v'.format(oe_setup_build_l, layers_json)
  148. result = runCmd(cmd)
  149. cond = "conf/templates/default" in result.output
  150. self.assertTrue(cond, "Incorrect output from {}: {}".format(cmd, result.output))
  151. # rather than hardcode the build setup cmdline here, let's actually run what the tool suggests to the user
  152. conf = None
  153. if 'poky-default' in result.output:
  154. conf = 'poky-default'
  155. elif 'meta-default' in result.output:
  156. conf = 'meta-default'
  157. self.assertIsNotNone(conf, "Could not find the configuration to set up a build in the output: {}".format(result.output))
  158. cmd = '{} --layerlist {} setup -c {} --no-shell'.format(oe_setup_build_l, layers_json, conf)
  159. result = runCmd(cmd)
  160. def test_bitbakelayers_updatelayer(self):
  161. result = runCmd('bitbake-layers create-layers-setup {}'.format(self.testlayer_path))
  162. jsonfile = os.path.join(self.testlayer_path, "setup-layers.json")
  163. self.validate_layersjson(jsonfile)
  164. import json
  165. with open(jsonfile) as f:
  166. data = json.load(f)
  167. repos = []
  168. for s in data['sources']:
  169. repos.append(s)
  170. self.assertTrue(len(repos) > 1, "Not enough repositories available")
  171. self.validate_layersjson(jsonfile)
  172. test_ref_1 = 'ref_1'
  173. test_ref_2 = 'ref_2'
  174. # Create a new layers setup using custom references
  175. result = runCmd('bitbake-layers create-layers-setup --use-custom-reference {first_repo}:{test_ref} --use-custom-reference {second_repo}:{test_ref} {path}'
  176. .format(first_repo=repos[0], second_repo=repos[1], test_ref=test_ref_1, path=self.testlayer_path))
  177. self.validate_layersjson(jsonfile)
  178. with open(jsonfile) as f:
  179. data = json.load(f)
  180. first_rev_1 = data['sources'][repos[0]]['git-remote']['rev']
  181. first_desc_1 = data['sources'][repos[0]]['git-remote']['describe']
  182. second_rev_1 = data['sources'][repos[1]]['git-remote']['rev']
  183. second_desc_1 = data['sources'][repos[1]]['git-remote']['describe']
  184. self.assertEqual(first_rev_1, test_ref_1, "Revision not set correctly: '{}'".format(first_rev_1))
  185. self.assertEqual(first_desc_1, '', "Describe not cleared: '{}'".format(first_desc_1))
  186. self.assertEqual(second_rev_1, test_ref_1, "Revision not set correctly: '{}'".format(second_rev_1))
  187. self.assertEqual(second_desc_1, '', "Describe not cleared: '{}'".format(second_desc_1))
  188. # Update one of the repositories in the layers setup using a different custom reference
  189. # This should only update the selected repository, everything else should remain as is
  190. result = runCmd('bitbake-layers create-layers-setup --update --use-custom-reference {first_repo}:{test_ref} {path}'
  191. .format(first_repo=repos[0], test_ref=test_ref_2, path=self.testlayer_path))
  192. self.validate_layersjson(jsonfile)
  193. with open(jsonfile) as f:
  194. data = json.load(f)
  195. first_rev_2 = data['sources'][repos[0]]['git-remote']['rev']
  196. first_desc_2 = data['sources'][repos[0]]['git-remote']['describe']
  197. second_rev_2 = data['sources'][repos[1]]['git-remote']['rev']
  198. second_desc_2 = data['sources'][repos[1]]['git-remote']['describe']
  199. self.assertEqual(first_rev_2, test_ref_2, "Revision not set correctly: '{}'".format(first_rev_2))
  200. self.assertEqual(first_desc_2, '', "Describe not cleared: '{}'".format(first_desc_2))
  201. self.assertEqual(second_rev_2, second_rev_1, "Revision should not be updated: '{}'".format(second_rev_2))
  202. self.assertEqual(second_desc_2, second_desc_1, "Describe should not be updated: '{}'".format(second_desc_2))
  203. class BitbakeConfigBuild(OESelftestTestCase):
  204. def test_enable_disable_fragments(self):
  205. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
  206. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
  207. runCmd('bitbake-config-build enable-fragment selftest/test-fragment')
  208. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), 'somevalue')
  209. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
  210. runCmd('bitbake-config-build enable-fragment selftest/more-fragments-here/test-another-fragment')
  211. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), 'somevalue')
  212. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), 'someothervalue')
  213. fragment_metadata_command = "bitbake-getvar -f {} --value {}"
  214. result = runCmd(fragment_metadata_command.format("selftest/test-fragment", "BB_CONF_FRAGMENT_SUMMARY"))
  215. self.assertIn("This is a configuration fragment intended for testing in oe-selftest context", result.output)
  216. result = runCmd(fragment_metadata_command.format("selftest/test-fragment", "BB_CONF_FRAGMENT_DESCRIPTION"))
  217. self.assertIn("It defines a variable that can be checked inside the test.", result.output)
  218. result = runCmd(fragment_metadata_command.format("selftest/more-fragments-here/test-another-fragment", "BB_CONF_FRAGMENT_SUMMARY"))
  219. self.assertIn("This is a second configuration fragment intended for testing in oe-selftest context", result.output)
  220. result = runCmd(fragment_metadata_command.format("selftest/more-fragments-here/test-another-fragment", "BB_CONF_FRAGMENT_DESCRIPTION"))
  221. self.assertIn("It defines another variable that can be checked inside the test.", result.output)
  222. runCmd('bitbake-config-build disable-fragment selftest/test-fragment')
  223. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
  224. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), 'someothervalue')
  225. runCmd('bitbake-config-build disable-fragment selftest/more-fragments-here/test-another-fragment')
  226. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
  227. self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
  228. def test_enable_disable_builtin_fragments(self):
  229. """
  230. Tests that the meta-selftest properly adds a new built-in fragment from
  231. its layer.conf configuration file.
  232. The test sequence goes as follows:
  233. 1. Verify that SELFTEST_BUILTIN_FRAGMENT_VARIABLE is not set yet.
  234. 2. Verify that SELFTEST_BUILTIN_FRAGMENT_VARIABLE is set after setting
  235. the fragment.
  236. 3. Verify that SELFTEST_BUILTIN_FRAGMENT_VARIABLE is set after setting
  237. the fragment with another value that overrides the first one.
  238. 4. Verify that SELFTEST_BUILTIN_FRAGMENT_VARIABLE is set to the previous
  239. value after removing the second assignment (from step 3).
  240. 5. Verify that SELFTEST_BUILTIN_FRAGMENT_VARIABLE is not set after
  241. removing the original assignment.
  242. """
  243. self.assertEqual(get_bb_var('SELFTEST_BUILTIN_FRAGMENT_VARIABLE'), None)
  244. runCmd('bitbake-config-build enable-fragment selftest-fragment/somevalue')
  245. self.assertEqual(get_bb_var('SELFTEST_BUILTIN_FRAGMENT_VARIABLE'), 'somevalue')
  246. runCmd('bitbake-config-build enable-fragment selftest-fragment/someothervalue')
  247. self.assertEqual(get_bb_var('SELFTEST_BUILTIN_FRAGMENT_VARIABLE'), 'someothervalue')
  248. runCmd('bitbake-config-build disable-fragment selftest-fragment/someothervalue')
  249. self.assertEqual(get_bb_var('SELFTEST_BUILTIN_FRAGMENT_VARIABLE'), 'somevalue')
  250. runCmd('bitbake-config-build disable-fragment selftest-fragment/somevalue')
  251. self.assertEqual(get_bb_var('SELFTEST_BUILTIN_FRAGMENT_VARIABLE'), None)
  252. def test_show_fragment(self):
  253. """
  254. Test that bitbake-config-build show-fragment returns the expected
  255. output. Use bitbake-config-build list-fragments --verbose to get the
  256. path to the fragment.
  257. """
  258. result = runCmd('bitbake-config-build --quiet list-fragments --verbose')
  259. test_fragment_re = re.compile(r'^Path: .*conf/fragments/test-fragment.conf$')
  260. fragment_path, fragment_content = '', ''
  261. for line in result.output.splitlines():
  262. m = re.match(test_fragment_re, line)
  263. if m:
  264. fragment_path = ' '.join(line.split()[1:])
  265. break
  266. if not fragment_path:
  267. raise Exception("Couldn't find the fragment")
  268. with open(fragment_path, 'r') as f:
  269. fragment_content = f'{fragment_path}:\n\n{f.read()}'.strip()
  270. result = runCmd('bitbake-config-build --quiet show-fragment selftest/test-fragment')
  271. self.assertEqual(result.output.strip(), fragment_content)