recipetool.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. import os
  2. import shutil
  3. import tempfile
  4. import urllib.parse
  5. from oeqa.utils.commands import runCmd, bitbake, get_bb_var
  6. from oeqa.utils.commands import get_bb_vars, create_temp_layer
  7. from oeqa.selftest.cases import devtool
  8. templayerdir = None
  9. def setUpModule():
  10. global templayerdir
  11. templayerdir = tempfile.mkdtemp(prefix='recipetoolqa')
  12. create_temp_layer(templayerdir, 'selftestrecipetool')
  13. runCmd('bitbake-layers add-layer %s' % templayerdir)
  14. def tearDownModule():
  15. runCmd('bitbake-layers remove-layer %s' % templayerdir, ignore_status=True)
  16. runCmd('rm -rf %s' % templayerdir)
  17. class RecipetoolBase(devtool.DevtoolBase):
  18. def setUpLocal(self):
  19. super(RecipetoolBase, self).setUpLocal()
  20. self.templayerdir = templayerdir
  21. self.tempdir = tempfile.mkdtemp(prefix='recipetoolqa')
  22. self.track_for_cleanup(self.tempdir)
  23. self.testfile = os.path.join(self.tempdir, 'testfile')
  24. with open(self.testfile, 'w') as f:
  25. f.write('Test file\n')
  26. def tearDownLocal(self):
  27. runCmd('rm -rf %s/recipes-*' % self.templayerdir)
  28. super(RecipetoolBase, self).tearDownLocal()
  29. def _try_recipetool_appendcmd(self, cmd, testrecipe, expectedfiles, expectedlines=None):
  30. result = runCmd(cmd)
  31. self.assertNotIn('Traceback', result.output)
  32. # Check the bbappend was created and applies properly
  33. recipefile = get_bb_var('FILE', testrecipe)
  34. bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
  35. # Check the bbappend contents
  36. if expectedlines is not None:
  37. with open(bbappendfile, 'r') as f:
  38. self.assertEqual(expectedlines, f.readlines(), "Expected lines are not present in %s" % bbappendfile)
  39. # Check file was copied
  40. filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
  41. for expectedfile in expectedfiles:
  42. self.assertTrue(os.path.isfile(os.path.join(filesdir, expectedfile)), 'Expected file %s to be copied next to bbappend, but it wasn\'t' % expectedfile)
  43. # Check no other files created
  44. createdfiles = []
  45. for root, _, files in os.walk(filesdir):
  46. for f in files:
  47. createdfiles.append(os.path.relpath(os.path.join(root, f), filesdir))
  48. self.assertTrue(sorted(createdfiles), sorted(expectedfiles))
  49. return bbappendfile, result.output
  50. class RecipetoolTests(RecipetoolBase):
  51. @classmethod
  52. def setUpClass(cls):
  53. super(RecipetoolTests, cls).setUpClass()
  54. # Ensure we have the right data in shlibs/pkgdata
  55. cls.logger.info('Running bitbake to generate pkgdata')
  56. bitbake('-c packagedata base-files coreutils busybox selftest-recipetool-appendfile')
  57. bb_vars = get_bb_vars(['COREBASE', 'BBPATH'])
  58. cls.corebase = bb_vars['COREBASE']
  59. cls.bbpath = bb_vars['BBPATH']
  60. def _try_recipetool_appendfile(self, testrecipe, destfile, newfile, options, expectedlines, expectedfiles):
  61. cmd = 'recipetool appendfile %s %s %s %s' % (self.templayerdir, destfile, newfile, options)
  62. return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
  63. def _try_recipetool_appendfile_fail(self, destfile, newfile, checkerror):
  64. cmd = 'recipetool appendfile %s %s %s' % (self.templayerdir, destfile, newfile)
  65. result = runCmd(cmd, ignore_status=True)
  66. self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
  67. self.assertNotIn('Traceback', result.output)
  68. for errorstr in checkerror:
  69. self.assertIn(errorstr, result.output)
  70. def test_recipetool_appendfile_basic(self):
  71. # Basic test
  72. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  73. '\n']
  74. _, output = self._try_recipetool_appendfile('base-files', '/etc/motd', self.testfile, '', expectedlines, ['motd'])
  75. self.assertNotIn('WARNING: ', output)
  76. def test_recipetool_appendfile_invalid(self):
  77. # Test some commands that should error
  78. self._try_recipetool_appendfile_fail('/etc/passwd', self.testfile, ['ERROR: /etc/passwd cannot be handled by this tool', 'useradd', 'extrausers'])
  79. self._try_recipetool_appendfile_fail('/etc/timestamp', self.testfile, ['ERROR: /etc/timestamp cannot be handled by this tool'])
  80. self._try_recipetool_appendfile_fail('/dev/console', self.testfile, ['ERROR: /dev/console cannot be handled by this tool'])
  81. def test_recipetool_appendfile_alternatives(self):
  82. # Now try with a file we know should be an alternative
  83. # (this is very much a fake example, but one we know is reliably an alternative)
  84. self._try_recipetool_appendfile_fail('/bin/ls', self.testfile, ['ERROR: File /bin/ls is an alternative possibly provided by the following recipes:', 'coreutils', 'busybox'])
  85. # Need a test file - should be executable
  86. testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
  87. testfile2name = os.path.basename(testfile2)
  88. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  89. '\n',
  90. 'SRC_URI += "file://%s"\n' % testfile2name,
  91. '\n',
  92. 'do_install_append() {\n',
  93. ' install -d ${D}${base_bindir}\n',
  94. ' install -m 0755 ${WORKDIR}/%s ${D}${base_bindir}/ls\n' % testfile2name,
  95. '}\n']
  96. self._try_recipetool_appendfile('coreutils', '/bin/ls', testfile2, '-r coreutils', expectedlines, [testfile2name])
  97. # Now try bbappending the same file again, contents should not change
  98. bbappendfile, _ = self._try_recipetool_appendfile('coreutils', '/bin/ls', self.testfile, '-r coreutils', expectedlines, [testfile2name])
  99. # But file should have
  100. copiedfile = os.path.join(os.path.dirname(bbappendfile), 'coreutils', testfile2name)
  101. result = runCmd('diff -q %s %s' % (testfile2, copiedfile), ignore_status=True)
  102. self.assertNotEqual(result.status, 0, 'New file should have been copied but was not %s' % result.output)
  103. def test_recipetool_appendfile_binary(self):
  104. # Try appending a binary file
  105. # /bin/ls can be a symlink to /usr/bin/ls
  106. ls = os.path.realpath("/bin/ls")
  107. result = runCmd('recipetool appendfile %s /bin/ls %s -r coreutils' % (self.templayerdir, ls))
  108. self.assertIn('WARNING: ', result.output)
  109. self.assertIn('is a binary', result.output)
  110. def test_recipetool_appendfile_add(self):
  111. # Try arbitrary file add to a recipe
  112. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  113. '\n',
  114. 'SRC_URI += "file://testfile"\n',
  115. '\n',
  116. 'do_install_append() {\n',
  117. ' install -d ${D}${datadir}\n',
  118. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
  119. '}\n']
  120. self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase', expectedlines, ['testfile'])
  121. # Try adding another file, this time where the source file is executable
  122. # (so we're testing that, plus modifying an existing bbappend)
  123. testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
  124. testfile2name = os.path.basename(testfile2)
  125. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  126. '\n',
  127. 'SRC_URI += "file://testfile \\\n',
  128. ' file://%s \\\n' % testfile2name,
  129. ' "\n',
  130. '\n',
  131. 'do_install_append() {\n',
  132. ' install -d ${D}${datadir}\n',
  133. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
  134. ' install -m 0755 ${WORKDIR}/%s ${D}${datadir}/scriptname\n' % testfile2name,
  135. '}\n']
  136. self._try_recipetool_appendfile('netbase', '/usr/share/scriptname', testfile2, '-r netbase', expectedlines, ['testfile', testfile2name])
  137. def test_recipetool_appendfile_add_bindir(self):
  138. # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
  139. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  140. '\n',
  141. 'SRC_URI += "file://testfile"\n',
  142. '\n',
  143. 'do_install_append() {\n',
  144. ' install -d ${D}${bindir}\n',
  145. ' install -m 0755 ${WORKDIR}/testfile ${D}${bindir}/selftest-recipetool-testbin\n',
  146. '}\n']
  147. _, output = self._try_recipetool_appendfile('netbase', '/usr/bin/selftest-recipetool-testbin', self.testfile, '-r netbase', expectedlines, ['testfile'])
  148. self.assertNotIn('WARNING: ', output)
  149. def test_recipetool_appendfile_add_machine(self):
  150. # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
  151. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  152. '\n',
  153. 'PACKAGE_ARCH = "${MACHINE_ARCH}"\n',
  154. '\n',
  155. 'SRC_URI_append_mymachine = " file://testfile"\n',
  156. '\n',
  157. 'do_install_append_mymachine() {\n',
  158. ' install -d ${D}${datadir}\n',
  159. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
  160. '}\n']
  161. _, output = self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase -m mymachine', expectedlines, ['mymachine/testfile'])
  162. self.assertNotIn('WARNING: ', output)
  163. def test_recipetool_appendfile_orig(self):
  164. # A file that's in SRC_URI and in do_install with the same name
  165. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  166. '\n']
  167. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-orig', self.testfile, '', expectedlines, ['selftest-replaceme-orig'])
  168. self.assertNotIn('WARNING: ', output)
  169. def test_recipetool_appendfile_todir(self):
  170. # A file that's in SRC_URI and in do_install with destination directory rather than file
  171. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  172. '\n']
  173. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-todir', self.testfile, '', expectedlines, ['selftest-replaceme-todir'])
  174. self.assertNotIn('WARNING: ', output)
  175. def test_recipetool_appendfile_renamed(self):
  176. # A file that's in SRC_URI with a different name to the destination file
  177. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  178. '\n']
  179. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-renamed', self.testfile, '', expectedlines, ['file1'])
  180. self.assertNotIn('WARNING: ', output)
  181. def test_recipetool_appendfile_subdir(self):
  182. # A file that's in SRC_URI in a subdir
  183. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  184. '\n',
  185. 'SRC_URI += "file://testfile"\n',
  186. '\n',
  187. 'do_install_append() {\n',
  188. ' install -d ${D}${datadir}\n',
  189. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-subdir\n',
  190. '}\n']
  191. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-subdir', self.testfile, '', expectedlines, ['testfile'])
  192. self.assertNotIn('WARNING: ', output)
  193. def test_recipetool_appendfile_src_glob(self):
  194. # A file that's in SRC_URI as a glob
  195. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  196. '\n',
  197. 'SRC_URI += "file://testfile"\n',
  198. '\n',
  199. 'do_install_append() {\n',
  200. ' install -d ${D}${datadir}\n',
  201. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-src-globfile\n',
  202. '}\n']
  203. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-src-globfile', self.testfile, '', expectedlines, ['testfile'])
  204. self.assertNotIn('WARNING: ', output)
  205. def test_recipetool_appendfile_inst_glob(self):
  206. # A file that's in do_install as a glob
  207. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  208. '\n']
  209. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-globfile'])
  210. self.assertNotIn('WARNING: ', output)
  211. def test_recipetool_appendfile_inst_todir_glob(self):
  212. # A file that's in do_install as a glob with destination as a directory
  213. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  214. '\n']
  215. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-todir-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-todir-globfile'])
  216. self.assertNotIn('WARNING: ', output)
  217. def test_recipetool_appendfile_patch(self):
  218. # A file that's added by a patch in SRC_URI
  219. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  220. '\n',
  221. 'SRC_URI += "file://testfile"\n',
  222. '\n',
  223. 'do_install_append() {\n',
  224. ' install -d ${D}${sysconfdir}\n',
  225. ' install -m 0644 ${WORKDIR}/testfile ${D}${sysconfdir}/selftest-replaceme-patched\n',
  226. '}\n']
  227. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/etc/selftest-replaceme-patched', self.testfile, '', expectedlines, ['testfile'])
  228. for line in output.splitlines():
  229. if 'WARNING: ' in line:
  230. self.assertIn('add-file.patch', line, 'Unexpected warning found in output:\n%s' % line)
  231. break
  232. else:
  233. self.fail('Patch warning not found in output:\n%s' % output)
  234. def test_recipetool_appendfile_script(self):
  235. # Now, a file that's in SRC_URI but installed by a script (so no mention in do_install)
  236. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  237. '\n',
  238. 'SRC_URI += "file://testfile"\n',
  239. '\n',
  240. 'do_install_append() {\n',
  241. ' install -d ${D}${datadir}\n',
  242. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-scripted\n',
  243. '}\n']
  244. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-scripted', self.testfile, '', expectedlines, ['testfile'])
  245. self.assertNotIn('WARNING: ', output)
  246. def test_recipetool_appendfile_inst_func(self):
  247. # A file that's installed from a function called by do_install
  248. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  249. '\n']
  250. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-func', self.testfile, '', expectedlines, ['selftest-replaceme-inst-func'])
  251. self.assertNotIn('WARNING: ', output)
  252. def test_recipetool_appendfile_postinstall(self):
  253. # A file that's created by a postinstall script (and explicitly mentioned in it)
  254. # First try without specifying recipe
  255. self._try_recipetool_appendfile_fail('/usr/share/selftest-replaceme-postinst', self.testfile, ['File /usr/share/selftest-replaceme-postinst may be written out in a pre/postinstall script of the following recipes:', 'selftest-recipetool-appendfile'])
  256. # Now specify recipe
  257. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  258. '\n',
  259. 'SRC_URI += "file://testfile"\n',
  260. '\n',
  261. 'do_install_append() {\n',
  262. ' install -d ${D}${datadir}\n',
  263. ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-postinst\n',
  264. '}\n']
  265. _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-postinst', self.testfile, '-r selftest-recipetool-appendfile', expectedlines, ['testfile'])
  266. def test_recipetool_appendfile_extlayer(self):
  267. # Try creating a bbappend in a layer that's not in bblayers.conf and has a different structure
  268. exttemplayerdir = os.path.join(self.tempdir, 'extlayer')
  269. self._create_temp_layer(exttemplayerdir, False, 'oeselftestextlayer', recipepathspec='metadata/recipes/recipes-*/*')
  270. result = runCmd('recipetool appendfile %s /usr/share/selftest-replaceme-orig %s' % (exttemplayerdir, self.testfile))
  271. self.assertNotIn('Traceback', result.output)
  272. createdfiles = []
  273. for root, _, files in os.walk(exttemplayerdir):
  274. for f in files:
  275. createdfiles.append(os.path.relpath(os.path.join(root, f), exttemplayerdir))
  276. createdfiles.remove('conf/layer.conf')
  277. expectedfiles = ['metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile.bbappend',
  278. 'metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile/selftest-replaceme-orig']
  279. self.assertEqual(sorted(createdfiles), sorted(expectedfiles))
  280. def test_recipetool_appendfile_wildcard(self):
  281. def try_appendfile_wc(options):
  282. result = runCmd('recipetool appendfile %s /etc/profile %s %s' % (self.templayerdir, self.testfile, options))
  283. self.assertNotIn('Traceback', result.output)
  284. bbappendfile = None
  285. for root, _, files in os.walk(self.templayerdir):
  286. for f in files:
  287. if f.endswith('.bbappend'):
  288. bbappendfile = f
  289. break
  290. if not bbappendfile:
  291. self.fail('No bbappend file created')
  292. runCmd('rm -rf %s/recipes-*' % self.templayerdir)
  293. return bbappendfile
  294. # Check without wildcard option
  295. recipefn = os.path.basename(get_bb_var('FILE', 'base-files'))
  296. filename = try_appendfile_wc('')
  297. self.assertEqual(filename, recipefn.replace('.bb', '.bbappend'))
  298. # Now check with wildcard option
  299. filename = try_appendfile_wc('-w')
  300. self.assertEqual(filename, recipefn.split('_')[0] + '_%.bbappend')
  301. def test_recipetool_create(self):
  302. # Try adding a recipe
  303. tempsrc = os.path.join(self.tempdir, 'srctree')
  304. os.makedirs(tempsrc)
  305. recipefile = os.path.join(self.tempdir, 'logrotate_3.12.3.bb')
  306. srcuri = 'https://github.com/logrotate/logrotate/releases/download/3.12.3/logrotate-3.12.3.tar.xz'
  307. result = runCmd('recipetool create -o %s %s -x %s' % (recipefile, srcuri, tempsrc))
  308. self.assertTrue(os.path.isfile(recipefile))
  309. checkvars = {}
  310. checkvars['LICENSE'] = 'GPLv2'
  311. checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'
  312. checkvars['SRC_URI'] = 'https://github.com/logrotate/logrotate/releases/download/${PV}/logrotate-${PV}.tar.xz'
  313. checkvars['SRC_URI[md5sum]'] = 'a560c57fac87c45b2fc17406cdf79288'
  314. checkvars['SRC_URI[sha256sum]'] = '2e6a401cac9024db2288297e3be1a8ab60e7401ba8e91225218aaf4a27e82a07'
  315. self._test_recipe_contents(recipefile, checkvars, [])
  316. def test_recipetool_create_git(self):
  317. if 'x11' not in get_bb_var('DISTRO_FEATURES'):
  318. self.skipTest('Test requires x11 as distro feature')
  319. # Ensure we have the right data in shlibs/pkgdata
  320. bitbake('libpng pango libx11 libxext jpeg libcheck')
  321. # Try adding a recipe
  322. tempsrc = os.path.join(self.tempdir, 'srctree')
  323. os.makedirs(tempsrc)
  324. recipefile = os.path.join(self.tempdir, 'libmatchbox.bb')
  325. srcuri = 'git://git.yoctoproject.org/libmatchbox'
  326. result = runCmd(['recipetool', 'create', '-o', recipefile, srcuri + ";rev=9f7cf8895ae2d39c465c04cc78e918c157420269", '-x', tempsrc])
  327. self.assertTrue(os.path.isfile(recipefile), 'recipetool did not create recipe file; output:\n%s' % result.output)
  328. checkvars = {}
  329. checkvars['LICENSE'] = 'LGPLv2.1'
  330. checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=7fbc338309ac38fefcd64b04bb903e34'
  331. checkvars['S'] = '${WORKDIR}/git'
  332. checkvars['PV'] = '1.11+git${SRCPV}'
  333. checkvars['SRC_URI'] = srcuri
  334. checkvars['DEPENDS'] = set(['libcheck', 'libjpeg-turbo', 'libpng', 'libx11', 'libxext', 'pango'])
  335. inherits = ['autotools', 'pkgconfig']
  336. self._test_recipe_contents(recipefile, checkvars, inherits)
  337. def test_recipetool_create_simple(self):
  338. # Try adding a recipe
  339. temprecipe = os.path.join(self.tempdir, 'recipe')
  340. os.makedirs(temprecipe)
  341. pv = '1.7.3.0'
  342. srcuri = 'http://www.dest-unreach.org/socat/download/socat-%s.tar.bz2' % pv
  343. result = runCmd('recipetool create %s -o %s' % (srcuri, temprecipe))
  344. dirlist = os.listdir(temprecipe)
  345. if len(dirlist) > 1:
  346. self.fail('recipetool created more than just one file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
  347. if len(dirlist) < 1 or not os.path.isfile(os.path.join(temprecipe, dirlist[0])):
  348. self.fail('recipetool did not create recipe file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
  349. self.assertEqual(dirlist[0], 'socat_%s.bb' % pv, 'Recipe file incorrectly named')
  350. checkvars = {}
  351. checkvars['LICENSE'] = set(['Unknown', 'GPLv2'])
  352. checkvars['LIC_FILES_CHKSUM'] = set(['file://COPYING.OpenSSL;md5=5c9bccc77f67a8328ef4ebaf468116f4', 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'])
  353. # We don't check DEPENDS since they are variable for this recipe depending on what's in the sysroot
  354. checkvars['S'] = None
  355. checkvars['SRC_URI'] = srcuri.replace(pv, '${PV}')
  356. inherits = ['autotools']
  357. self._test_recipe_contents(os.path.join(temprecipe, dirlist[0]), checkvars, inherits)
  358. def test_recipetool_create_cmake(self):
  359. bitbake('-c packagedata gtk+')
  360. # Try adding a recipe
  361. temprecipe = os.path.join(self.tempdir, 'recipe')
  362. os.makedirs(temprecipe)
  363. recipefile = os.path.join(temprecipe, 'navit_0.5.0.bb')
  364. srcuri = 'http://downloads.yoctoproject.org/mirror/sources/navit-0.5.0.tar.gz'
  365. result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
  366. self.assertTrue(os.path.isfile(recipefile))
  367. checkvars = {}
  368. checkvars['LICENSE'] = set(['Unknown', 'GPLv2', 'LGPLv2'])
  369. checkvars['SRC_URI'] = 'http://downloads.yoctoproject.org/mirror/sources/navit-${PV}.tar.gz'
  370. checkvars['SRC_URI[md5sum]'] = '242f398e979a6b8c0f3c802b63435b68'
  371. checkvars['SRC_URI[sha256sum]'] = '13353481d7fc01a4f64e385dda460b51496366bba0fd2cc85a89a0747910e94d'
  372. checkvars['DEPENDS'] = set(['freetype', 'zlib', 'openssl', 'glib-2.0', 'virtual/libgl', 'virtual/egl', 'gtk+', 'libpng', 'libsdl', 'freeglut', 'dbus-glib', 'fribidi'])
  373. inherits = ['cmake', 'python-dir', 'gettext', 'pkgconfig']
  374. self._test_recipe_contents(recipefile, checkvars, inherits)
  375. def test_recipetool_create_github(self):
  376. # Basic test to see if github URL mangling works
  377. temprecipe = os.path.join(self.tempdir, 'recipe')
  378. os.makedirs(temprecipe)
  379. recipefile = os.path.join(temprecipe, 'meson_git.bb')
  380. srcuri = 'https://github.com/mesonbuild/meson;rev=0.32.0'
  381. result = runCmd(['recipetool', 'create', '-o', temprecipe, srcuri])
  382. self.assertTrue(os.path.isfile(recipefile))
  383. checkvars = {}
  384. checkvars['LICENSE'] = set(['Apache-2.0'])
  385. checkvars['SRC_URI'] = 'git://github.com/mesonbuild/meson;protocol=https'
  386. inherits = ['setuptools']
  387. self._test_recipe_contents(recipefile, checkvars, inherits)
  388. def test_recipetool_create_github_tarball(self):
  389. # Basic test to ensure github URL mangling doesn't apply to release tarballs
  390. temprecipe = os.path.join(self.tempdir, 'recipe')
  391. os.makedirs(temprecipe)
  392. pv = '0.32.0'
  393. recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
  394. srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
  395. result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
  396. self.assertTrue(os.path.isfile(recipefile))
  397. checkvars = {}
  398. checkvars['LICENSE'] = set(['Apache-2.0'])
  399. checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
  400. inherits = ['setuptools']
  401. self._test_recipe_contents(recipefile, checkvars, inherits)
  402. def test_recipetool_create_git_http(self):
  403. # Basic test to check http git URL mangling works
  404. temprecipe = os.path.join(self.tempdir, 'recipe')
  405. os.makedirs(temprecipe)
  406. recipefile = os.path.join(temprecipe, 'matchbox-terminal_git.bb')
  407. srcuri = 'http://git.yoctoproject.org/git/matchbox-terminal'
  408. result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
  409. self.assertTrue(os.path.isfile(recipefile))
  410. checkvars = {}
  411. checkvars['LICENSE'] = set(['GPLv2'])
  412. checkvars['SRC_URI'] = 'git://git.yoctoproject.org/git/matchbox-terminal;protocol=http'
  413. inherits = ['pkgconfig', 'autotools']
  414. self._test_recipe_contents(recipefile, checkvars, inherits)
  415. def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
  416. dstdir = basedstdir
  417. self.assertTrue(os.path.exists(dstdir))
  418. for p in paths:
  419. dstdir = os.path.join(dstdir, p)
  420. if not os.path.exists(dstdir):
  421. os.makedirs(dstdir)
  422. self.track_for_cleanup(dstdir)
  423. dstfile = os.path.join(dstdir, os.path.basename(srcfile))
  424. if srcfile != dstfile:
  425. shutil.copy(srcfile, dstfile)
  426. self.track_for_cleanup(dstfile)
  427. def test_recipetool_load_plugin(self):
  428. """Test that recipetool loads only the first found plugin in BBPATH."""
  429. recipetool = runCmd("which recipetool")
  430. fromname = runCmd("recipetool --quiet pluginfile")
  431. srcfile = fromname.output
  432. searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
  433. plugincontent = []
  434. with open(srcfile) as fh:
  435. plugincontent = fh.readlines()
  436. try:
  437. self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
  438. for path in searchpath:
  439. self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
  440. result = runCmd("recipetool --quiet count")
  441. self.assertEqual(result.output, '1')
  442. result = runCmd("recipetool --quiet multiloaded")
  443. self.assertEqual(result.output, "no")
  444. for path in searchpath:
  445. result = runCmd("recipetool --quiet bbdir")
  446. self.assertEqual(result.output, path)
  447. os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
  448. finally:
  449. with open(srcfile, 'w') as fh:
  450. fh.writelines(plugincontent)
  451. class RecipetoolAppendsrcBase(RecipetoolBase):
  452. def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
  453. cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
  454. return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
  455. def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
  456. if destdir:
  457. options += ' -D %s' % destdir
  458. if expectedfiles is None:
  459. expectedfiles = [os.path.basename(f) for f in newfiles]
  460. cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
  461. return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
  462. def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
  463. cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
  464. result = runCmd(cmd, ignore_status=True)
  465. self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
  466. self.assertNotIn('Traceback', result.output)
  467. for errorstr in checkerror:
  468. self.assertIn(errorstr, result.output)
  469. @staticmethod
  470. def _get_first_file_uri(recipe):
  471. '''Return the first file:// in SRC_URI for the specified recipe.'''
  472. src_uri = get_bb_var('SRC_URI', recipe).split()
  473. for uri in src_uri:
  474. p = urllib.parse.urlparse(uri)
  475. if p.scheme == 'file':
  476. return p.netloc + p.path
  477. def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
  478. if newfile is None:
  479. newfile = self.testfile
  480. if srcdir:
  481. if destdir:
  482. expected_subdir = os.path.join(srcdir, destdir)
  483. else:
  484. expected_subdir = srcdir
  485. else:
  486. options += " -W"
  487. expected_subdir = destdir
  488. if filename:
  489. if destdir:
  490. destpath = os.path.join(destdir, filename)
  491. else:
  492. destpath = filename
  493. else:
  494. filename = os.path.basename(newfile)
  495. if destdir:
  496. destpath = destdir + os.sep
  497. else:
  498. destpath = '.' + os.sep
  499. expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
  500. '\n']
  501. if has_src_uri:
  502. uri = 'file://%s' % filename
  503. if expected_subdir:
  504. uri += ';subdir=%s' % expected_subdir
  505. expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
  506. '\n']
  507. return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
  508. def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
  509. if expectedfiles is None:
  510. expectedfiles = [os.path.basename(n) for n in newfiles]
  511. self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
  512. bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
  513. src_uri = bb_vars['SRC_URI'].split()
  514. for f in expectedfiles:
  515. if destdir:
  516. self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
  517. else:
  518. self.assertIn('file://%s' % f, src_uri)
  519. recipefile = bb_vars['FILE']
  520. bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
  521. filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
  522. filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
  523. self.assertIn(filesdir, filesextrapaths)
  524. class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
  525. def test_recipetool_appendsrcfile_basic(self):
  526. self._test_appendsrcfile('base-files', 'a-file')
  527. def test_recipetool_appendsrcfile_basic_wildcard(self):
  528. testrecipe = 'base-files'
  529. self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
  530. recipefile = get_bb_var('FILE', testrecipe)
  531. bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
  532. self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
  533. def test_recipetool_appendsrcfile_subdir_basic(self):
  534. self._test_appendsrcfile('base-files', 'a-file', 'tmp')
  535. def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
  536. self._test_appendsrcfile('base-files', destdir='tmp')
  537. def test_recipetool_appendsrcfile_srcdir_basic(self):
  538. testrecipe = 'bash'
  539. bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
  540. srcdir = bb_vars['S']
  541. workdir = bb_vars['WORKDIR']
  542. subdir = os.path.relpath(srcdir, workdir)
  543. self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
  544. def test_recipetool_appendsrcfile_existing_in_src_uri(self):
  545. testrecipe = 'base-files'
  546. filepath = self._get_first_file_uri(testrecipe)
  547. self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
  548. self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
  549. def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
  550. testrecipe = 'base-files'
  551. subdir = 'tmp'
  552. filepath = self._get_first_file_uri(testrecipe)
  553. self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
  554. output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
  555. self.assertTrue(any('with different parameters' in l for l in output))
  556. def test_recipetool_appendsrcfile_replace_file_srcdir(self):
  557. testrecipe = 'bash'
  558. filepath = 'Makefile.in'
  559. bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
  560. srcdir = bb_vars['S']
  561. workdir = bb_vars['WORKDIR']
  562. subdir = os.path.relpath(srcdir, workdir)
  563. self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
  564. bitbake('%s:do_unpack' % testrecipe)
  565. self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read())
  566. def test_recipetool_appendsrcfiles_basic(self, destdir=None):
  567. newfiles = [self.testfile]
  568. for i in range(1, 5):
  569. testfile = os.path.join(self.tempdir, 'testfile%d' % i)
  570. with open(testfile, 'w') as f:
  571. f.write('Test file %d\n' % i)
  572. newfiles.append(testfile)
  573. self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
  574. def test_recipetool_appendsrcfiles_basic_subdir(self):
  575. self.test_recipetool_appendsrcfiles_basic(destdir='testdir')