blob: 1c701a40bf0f8c4a9b24c6a4b044da48b22494a6 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005import os
6import shutil
7import tempfile
8import urllib.parse
9
10from oeqa.utils.commands import runCmd, bitbake, get_bb_var
11from oeqa.utils.commands import get_bb_vars, create_temp_layer
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from oeqa.selftest.cases import devtool
13
14templayerdir = None
15
16def setUpModule():
17 global templayerdir
18 templayerdir = tempfile.mkdtemp(prefix='recipetoolqa')
19 create_temp_layer(templayerdir, 'selftestrecipetool')
20 runCmd('bitbake-layers add-layer %s' % templayerdir)
21
22
23def tearDownModule():
24 runCmd('bitbake-layers remove-layer %s' % templayerdir, ignore_status=True)
25 runCmd('rm -rf %s' % templayerdir)
26
27
28class RecipetoolBase(devtool.DevtoolBase):
29
30 def setUpLocal(self):
31 super(RecipetoolBase, self).setUpLocal()
32 self.templayerdir = templayerdir
33 self.tempdir = tempfile.mkdtemp(prefix='recipetoolqa')
34 self.track_for_cleanup(self.tempdir)
35 self.testfile = os.path.join(self.tempdir, 'testfile')
36 with open(self.testfile, 'w') as f:
37 f.write('Test file\n')
38
39 def tearDownLocal(self):
40 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
41 super(RecipetoolBase, self).tearDownLocal()
42
43 def _try_recipetool_appendcmd(self, cmd, testrecipe, expectedfiles, expectedlines=None):
44 result = runCmd(cmd)
45 self.assertNotIn('Traceback', result.output)
46
47 # Check the bbappend was created and applies properly
48 recipefile = get_bb_var('FILE', testrecipe)
49 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
50
51 # Check the bbappend contents
52 if expectedlines is not None:
53 with open(bbappendfile, 'r') as f:
54 self.assertEqual(expectedlines, f.readlines(), "Expected lines are not present in %s" % bbappendfile)
55
56 # Check file was copied
57 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
58 for expectedfile in expectedfiles:
59 self.assertTrue(os.path.isfile(os.path.join(filesdir, expectedfile)), 'Expected file %s to be copied next to bbappend, but it wasn\'t' % expectedfile)
60
61 # Check no other files created
62 createdfiles = []
63 for root, _, files in os.walk(filesdir):
64 for f in files:
65 createdfiles.append(os.path.relpath(os.path.join(root, f), filesdir))
66 self.assertTrue(sorted(createdfiles), sorted(expectedfiles))
67
68 return bbappendfile, result.output
69
70
71class RecipetoolTests(RecipetoolBase):
72
73 @classmethod
74 def setUpClass(cls):
75 super(RecipetoolTests, cls).setUpClass()
76 # Ensure we have the right data in shlibs/pkgdata
77 cls.logger.info('Running bitbake to generate pkgdata')
78 bitbake('-c packagedata base-files coreutils busybox selftest-recipetool-appendfile')
79 bb_vars = get_bb_vars(['COREBASE', 'BBPATH'])
80 cls.corebase = bb_vars['COREBASE']
81 cls.bbpath = bb_vars['BBPATH']
82
83 def _try_recipetool_appendfile(self, testrecipe, destfile, newfile, options, expectedlines, expectedfiles):
84 cmd = 'recipetool appendfile %s %s %s %s' % (self.templayerdir, destfile, newfile, options)
85 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
86
87 def _try_recipetool_appendfile_fail(self, destfile, newfile, checkerror):
88 cmd = 'recipetool appendfile %s %s %s' % (self.templayerdir, destfile, newfile)
89 result = runCmd(cmd, ignore_status=True)
90 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
91 self.assertNotIn('Traceback', result.output)
92 for errorstr in checkerror:
93 self.assertIn(errorstr, result.output)
94
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 def test_recipetool_appendfile_basic(self):
96 # Basic test
97 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
98 '\n']
99 _, output = self._try_recipetool_appendfile('base-files', '/etc/motd', self.testfile, '', expectedlines, ['motd'])
100 self.assertNotIn('WARNING: ', output)
101
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 def test_recipetool_appendfile_invalid(self):
103 # Test some commands that should error
104 self._try_recipetool_appendfile_fail('/etc/passwd', self.testfile, ['ERROR: /etc/passwd cannot be handled by this tool', 'useradd', 'extrausers'])
105 self._try_recipetool_appendfile_fail('/etc/timestamp', self.testfile, ['ERROR: /etc/timestamp cannot be handled by this tool'])
106 self._try_recipetool_appendfile_fail('/dev/console', self.testfile, ['ERROR: /dev/console cannot be handled by this tool'])
107
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500108 def test_recipetool_appendfile_alternatives(self):
109 # Now try with a file we know should be an alternative
110 # (this is very much a fake example, but one we know is reliably an alternative)
111 self._try_recipetool_appendfile_fail('/bin/ls', self.testfile, ['ERROR: File /bin/ls is an alternative possibly provided by the following recipes:', 'coreutils', 'busybox'])
112 # Need a test file - should be executable
113 testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
114 testfile2name = os.path.basename(testfile2)
115 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
116 '\n',
117 'SRC_URI += "file://%s"\n' % testfile2name,
118 '\n',
119 'do_install_append() {\n',
120 ' install -d ${D}${base_bindir}\n',
121 ' install -m 0755 ${WORKDIR}/%s ${D}${base_bindir}/ls\n' % testfile2name,
122 '}\n']
123 self._try_recipetool_appendfile('coreutils', '/bin/ls', testfile2, '-r coreutils', expectedlines, [testfile2name])
124 # Now try bbappending the same file again, contents should not change
125 bbappendfile, _ = self._try_recipetool_appendfile('coreutils', '/bin/ls', self.testfile, '-r coreutils', expectedlines, [testfile2name])
126 # But file should have
127 copiedfile = os.path.join(os.path.dirname(bbappendfile), 'coreutils', testfile2name)
128 result = runCmd('diff -q %s %s' % (testfile2, copiedfile), ignore_status=True)
129 self.assertNotEqual(result.status, 0, 'New file should have been copied but was not %s' % result.output)
130
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131 def test_recipetool_appendfile_binary(self):
132 # Try appending a binary file
133 # /bin/ls can be a symlink to /usr/bin/ls
134 ls = os.path.realpath("/bin/ls")
135 result = runCmd('recipetool appendfile %s /bin/ls %s -r coreutils' % (self.templayerdir, ls))
136 self.assertIn('WARNING: ', result.output)
137 self.assertIn('is a binary', result.output)
138
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139 def test_recipetool_appendfile_add(self):
140 # Try arbitrary file add to a recipe
141 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
142 '\n',
143 'SRC_URI += "file://testfile"\n',
144 '\n',
145 'do_install_append() {\n',
146 ' install -d ${D}${datadir}\n',
147 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
148 '}\n']
149 self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase', expectedlines, ['testfile'])
150 # Try adding another file, this time where the source file is executable
151 # (so we're testing that, plus modifying an existing bbappend)
152 testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
153 testfile2name = os.path.basename(testfile2)
154 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
155 '\n',
156 'SRC_URI += "file://testfile \\\n',
157 ' file://%s \\\n' % testfile2name,
158 ' "\n',
159 '\n',
160 'do_install_append() {\n',
161 ' install -d ${D}${datadir}\n',
162 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
163 ' install -m 0755 ${WORKDIR}/%s ${D}${datadir}/scriptname\n' % testfile2name,
164 '}\n']
165 self._try_recipetool_appendfile('netbase', '/usr/share/scriptname', testfile2, '-r netbase', expectedlines, ['testfile', testfile2name])
166
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500167 def test_recipetool_appendfile_add_bindir(self):
168 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
169 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
170 '\n',
171 'SRC_URI += "file://testfile"\n',
172 '\n',
173 'do_install_append() {\n',
174 ' install -d ${D}${bindir}\n',
175 ' install -m 0755 ${WORKDIR}/testfile ${D}${bindir}/selftest-recipetool-testbin\n',
176 '}\n']
177 _, output = self._try_recipetool_appendfile('netbase', '/usr/bin/selftest-recipetool-testbin', self.testfile, '-r netbase', expectedlines, ['testfile'])
178 self.assertNotIn('WARNING: ', output)
179
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500180 def test_recipetool_appendfile_add_machine(self):
181 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
182 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
183 '\n',
184 'PACKAGE_ARCH = "${MACHINE_ARCH}"\n',
185 '\n',
186 'SRC_URI_append_mymachine = " file://testfile"\n',
187 '\n',
188 'do_install_append_mymachine() {\n',
189 ' install -d ${D}${datadir}\n',
190 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
191 '}\n']
192 _, output = self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase -m mymachine', expectedlines, ['mymachine/testfile'])
193 self.assertNotIn('WARNING: ', output)
194
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 def test_recipetool_appendfile_orig(self):
196 # A file that's in SRC_URI and in do_install with the same name
197 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
198 '\n']
199 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-orig', self.testfile, '', expectedlines, ['selftest-replaceme-orig'])
200 self.assertNotIn('WARNING: ', output)
201
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 def test_recipetool_appendfile_todir(self):
203 # A file that's in SRC_URI and in do_install with destination directory rather than file
204 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
205 '\n']
206 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-todir', self.testfile, '', expectedlines, ['selftest-replaceme-todir'])
207 self.assertNotIn('WARNING: ', output)
208
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 def test_recipetool_appendfile_renamed(self):
210 # A file that's in SRC_URI with a different name to the destination file
211 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
212 '\n']
213 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-renamed', self.testfile, '', expectedlines, ['file1'])
214 self.assertNotIn('WARNING: ', output)
215
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500216 def test_recipetool_appendfile_subdir(self):
217 # A file that's in SRC_URI in a subdir
218 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
219 '\n',
220 'SRC_URI += "file://testfile"\n',
221 '\n',
222 'do_install_append() {\n',
223 ' install -d ${D}${datadir}\n',
224 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-subdir\n',
225 '}\n']
226 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-subdir', self.testfile, '', expectedlines, ['testfile'])
227 self.assertNotIn('WARNING: ', output)
228
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500229 def test_recipetool_appendfile_src_glob(self):
230 # A file that's in SRC_URI as a glob
231 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
232 '\n',
233 'SRC_URI += "file://testfile"\n',
234 '\n',
235 'do_install_append() {\n',
236 ' install -d ${D}${datadir}\n',
237 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-src-globfile\n',
238 '}\n']
239 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-src-globfile', self.testfile, '', expectedlines, ['testfile'])
240 self.assertNotIn('WARNING: ', output)
241
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500242 def test_recipetool_appendfile_inst_glob(self):
243 # A file that's in do_install as a glob
244 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
245 '\n']
246 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-globfile'])
247 self.assertNotIn('WARNING: ', output)
248
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500249 def test_recipetool_appendfile_inst_todir_glob(self):
250 # A file that's in do_install as a glob with destination as a directory
251 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
252 '\n']
253 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-todir-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-todir-globfile'])
254 self.assertNotIn('WARNING: ', output)
255
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256 def test_recipetool_appendfile_patch(self):
257 # A file that's added by a patch in SRC_URI
258 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
259 '\n',
260 'SRC_URI += "file://testfile"\n',
261 '\n',
262 'do_install_append() {\n',
263 ' install -d ${D}${sysconfdir}\n',
264 ' install -m 0644 ${WORKDIR}/testfile ${D}${sysconfdir}/selftest-replaceme-patched\n',
265 '}\n']
266 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/etc/selftest-replaceme-patched', self.testfile, '', expectedlines, ['testfile'])
267 for line in output.splitlines():
268 if 'WARNING: ' in line:
269 self.assertIn('add-file.patch', line, 'Unexpected warning found in output:\n%s' % line)
270 break
271 else:
272 self.fail('Patch warning not found in output:\n%s' % output)
273
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500274 def test_recipetool_appendfile_script(self):
275 # Now, a file that's in SRC_URI but installed by a script (so no mention in do_install)
276 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
277 '\n',
278 'SRC_URI += "file://testfile"\n',
279 '\n',
280 'do_install_append() {\n',
281 ' install -d ${D}${datadir}\n',
282 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-scripted\n',
283 '}\n']
284 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-scripted', self.testfile, '', expectedlines, ['testfile'])
285 self.assertNotIn('WARNING: ', output)
286
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500287 def test_recipetool_appendfile_inst_func(self):
288 # A file that's installed from a function called by do_install
289 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
290 '\n']
291 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-func', self.testfile, '', expectedlines, ['selftest-replaceme-inst-func'])
292 self.assertNotIn('WARNING: ', output)
293
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500294 def test_recipetool_appendfile_postinstall(self):
295 # A file that's created by a postinstall script (and explicitly mentioned in it)
296 # First try without specifying recipe
297 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'])
298 # Now specify recipe
299 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
300 '\n',
301 'SRC_URI += "file://testfile"\n',
302 '\n',
303 'do_install_append() {\n',
304 ' install -d ${D}${datadir}\n',
305 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-postinst\n',
306 '}\n']
307 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-postinst', self.testfile, '-r selftest-recipetool-appendfile', expectedlines, ['testfile'])
308
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 def test_recipetool_appendfile_extlayer(self):
310 # Try creating a bbappend in a layer that's not in bblayers.conf and has a different structure
311 exttemplayerdir = os.path.join(self.tempdir, 'extlayer')
312 self._create_temp_layer(exttemplayerdir, False, 'oeselftestextlayer', recipepathspec='metadata/recipes/recipes-*/*')
313 result = runCmd('recipetool appendfile %s /usr/share/selftest-replaceme-orig %s' % (exttemplayerdir, self.testfile))
314 self.assertNotIn('Traceback', result.output)
315 createdfiles = []
316 for root, _, files in os.walk(exttemplayerdir):
317 for f in files:
318 createdfiles.append(os.path.relpath(os.path.join(root, f), exttemplayerdir))
319 createdfiles.remove('conf/layer.conf')
320 expectedfiles = ['metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile.bbappend',
321 'metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile/selftest-replaceme-orig']
322 self.assertEqual(sorted(createdfiles), sorted(expectedfiles))
323
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 def test_recipetool_appendfile_wildcard(self):
325
326 def try_appendfile_wc(options):
327 result = runCmd('recipetool appendfile %s /etc/profile %s %s' % (self.templayerdir, self.testfile, options))
328 self.assertNotIn('Traceback', result.output)
329 bbappendfile = None
330 for root, _, files in os.walk(self.templayerdir):
331 for f in files:
332 if f.endswith('.bbappend'):
333 bbappendfile = f
334 break
335 if not bbappendfile:
336 self.fail('No bbappend file created')
337 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
338 return bbappendfile
339
340 # Check without wildcard option
341 recipefn = os.path.basename(get_bb_var('FILE', 'base-files'))
342 filename = try_appendfile_wc('')
343 self.assertEqual(filename, recipefn.replace('.bb', '.bbappend'))
344 # Now check with wildcard option
345 filename = try_appendfile_wc('-w')
346 self.assertEqual(filename, recipefn.split('_')[0] + '_%.bbappend')
347
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500348 def test_recipetool_create(self):
349 # Try adding a recipe
350 tempsrc = os.path.join(self.tempdir, 'srctree')
351 os.makedirs(tempsrc)
352 recipefile = os.path.join(self.tempdir, 'logrotate_3.12.3.bb')
353 srcuri = 'https://github.com/logrotate/logrotate/releases/download/3.12.3/logrotate-3.12.3.tar.xz'
354 result = runCmd('recipetool create -o %s %s -x %s' % (recipefile, srcuri, tempsrc))
355 self.assertTrue(os.path.isfile(recipefile))
356 checkvars = {}
357 checkvars['LICENSE'] = 'GPLv2'
358 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'
359 checkvars['SRC_URI'] = 'https://github.com/logrotate/logrotate/releases/download/${PV}/logrotate-${PV}.tar.xz'
360 checkvars['SRC_URI[md5sum]'] = 'a560c57fac87c45b2fc17406cdf79288'
361 checkvars['SRC_URI[sha256sum]'] = '2e6a401cac9024db2288297e3be1a8ab60e7401ba8e91225218aaf4a27e82a07'
362 self._test_recipe_contents(recipefile, checkvars, [])
363
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500364 def test_recipetool_create_git(self):
365 if 'x11' not in get_bb_var('DISTRO_FEATURES'):
366 self.skipTest('Test requires x11 as distro feature')
367 # Ensure we have the right data in shlibs/pkgdata
368 bitbake('libpng pango libx11 libxext jpeg libcheck')
369 # Try adding a recipe
370 tempsrc = os.path.join(self.tempdir, 'srctree')
371 os.makedirs(tempsrc)
372 recipefile = os.path.join(self.tempdir, 'libmatchbox.bb')
373 srcuri = 'git://git.yoctoproject.org/libmatchbox'
374 result = runCmd(['recipetool', 'create', '-o', recipefile, srcuri + ";rev=9f7cf8895ae2d39c465c04cc78e918c157420269", '-x', tempsrc])
375 self.assertTrue(os.path.isfile(recipefile), 'recipetool did not create recipe file; output:\n%s' % result.output)
376 checkvars = {}
377 checkvars['LICENSE'] = 'LGPLv2.1'
378 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=7fbc338309ac38fefcd64b04bb903e34'
379 checkvars['S'] = '${WORKDIR}/git'
380 checkvars['PV'] = '1.11+git${SRCPV}'
381 checkvars['SRC_URI'] = srcuri
382 checkvars['DEPENDS'] = set(['libcheck', 'libjpeg-turbo', 'libpng', 'libx11', 'libxext', 'pango'])
383 inherits = ['autotools', 'pkgconfig']
384 self._test_recipe_contents(recipefile, checkvars, inherits)
385
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500386 def test_recipetool_create_simple(self):
387 # Try adding a recipe
388 temprecipe = os.path.join(self.tempdir, 'recipe')
389 os.makedirs(temprecipe)
390 pv = '1.7.3.0'
391 srcuri = 'http://www.dest-unreach.org/socat/download/socat-%s.tar.bz2' % pv
392 result = runCmd('recipetool create %s -o %s' % (srcuri, temprecipe))
393 dirlist = os.listdir(temprecipe)
394 if len(dirlist) > 1:
395 self.fail('recipetool created more than just one file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
396 if len(dirlist) < 1 or not os.path.isfile(os.path.join(temprecipe, dirlist[0])):
397 self.fail('recipetool did not create recipe file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
398 self.assertEqual(dirlist[0], 'socat_%s.bb' % pv, 'Recipe file incorrectly named')
399 checkvars = {}
400 checkvars['LICENSE'] = set(['Unknown', 'GPLv2'])
401 checkvars['LIC_FILES_CHKSUM'] = set(['file://COPYING.OpenSSL;md5=5c9bccc77f67a8328ef4ebaf468116f4', 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'])
402 # We don't check DEPENDS since they are variable for this recipe depending on what's in the sysroot
403 checkvars['S'] = None
404 checkvars['SRC_URI'] = srcuri.replace(pv, '${PV}')
405 inherits = ['autotools']
406 self._test_recipe_contents(os.path.join(temprecipe, dirlist[0]), checkvars, inherits)
407
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500408 def test_recipetool_create_cmake(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500409 temprecipe = os.path.join(self.tempdir, 'recipe')
410 os.makedirs(temprecipe)
Brad Bishop96ff1982019-08-19 13:50:42 -0400411 recipefile = os.path.join(temprecipe, 'taglib_1.11.1.bb')
412 srcuri = 'http://taglib.github.io/releases/taglib-1.11.1.tar.gz'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500413 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
414 self.assertTrue(os.path.isfile(recipefile))
415 checkvars = {}
Brad Bishop96ff1982019-08-19 13:50:42 -0400416 checkvars['LICENSE'] = set(['LGPLv2.1', 'MPL-1.1'])
417 checkvars['SRC_URI'] = 'http://taglib.github.io/releases/taglib-${PV}.tar.gz'
418 checkvars['SRC_URI[md5sum]'] = 'cee7be0ccfc892fa433d6c837df9522a'
419 checkvars['SRC_URI[sha256sum]'] = 'b6d1a5a610aae6ff39d93de5efd0fdc787aa9e9dc1e7026fa4c961b26563526b'
420 checkvars['DEPENDS'] = set(['boost', 'zlib'])
421 inherits = ['cmake']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500422 self._test_recipe_contents(recipefile, checkvars, inherits)
423
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500424 def test_recipetool_create_github(self):
425 # Basic test to see if github URL mangling works
426 temprecipe = os.path.join(self.tempdir, 'recipe')
427 os.makedirs(temprecipe)
428 recipefile = os.path.join(temprecipe, 'meson_git.bb')
429 srcuri = 'https://github.com/mesonbuild/meson;rev=0.32.0'
430 result = runCmd(['recipetool', 'create', '-o', temprecipe, srcuri])
431 self.assertTrue(os.path.isfile(recipefile))
432 checkvars = {}
433 checkvars['LICENSE'] = set(['Apache-2.0'])
434 checkvars['SRC_URI'] = 'git://github.com/mesonbuild/meson;protocol=https'
Brad Bishop15ae2502019-06-18 21:44:24 -0400435 inherits = ['setuptools3']
436 self._test_recipe_contents(recipefile, checkvars, inherits)
437
438 def test_recipetool_create_python3_setuptools(self):
439 # Test creating python3 package from tarball (using setuptools3 class)
440 temprecipe = os.path.join(self.tempdir, 'recipe')
441 os.makedirs(temprecipe)
442 pn = 'python-magic'
443 pv = '0.4.15'
444 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
445 srcuri = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-%s.tar.gz' % pv
446 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
447 self.assertTrue(os.path.isfile(recipefile))
448 checkvars = {}
449 checkvars['LICENSE'] = set(['MIT'])
450 checkvars['LIC_FILES_CHKSUM'] = 'file://LICENSE;md5=16a934f165e8c3245f241e77d401bb88'
451 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-${PV}.tar.gz'
452 checkvars['SRC_URI[md5sum]'] = 'e384c95a47218f66c6501cd6dd45ff59'
453 checkvars['SRC_URI[sha256sum]'] = 'f3765c0f582d2dfc72c15f3b5a82aecfae9498bd29ca840d72f37d7bd38bfcd5'
454 inherits = ['setuptools3']
455 self._test_recipe_contents(recipefile, checkvars, inherits)
456
457 def test_recipetool_create_python3_distutils(self):
458 # Test creating python3 package from tarball (using distutils3 class)
459 temprecipe = os.path.join(self.tempdir, 'recipe')
460 os.makedirs(temprecipe)
461 pn = 'docutils'
462 pv = '0.14'
463 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
464 srcuri = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-%s.tar.gz' % pv
465 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
466 self.assertTrue(os.path.isfile(recipefile))
467 checkvars = {}
468 checkvars['LICENSE'] = set(['PSF', '&', 'BSD', 'GPL'])
469 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING.txt;md5=35a23d42b615470583563132872c97d6'
470 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-${PV}.tar.gz'
471 checkvars['SRC_URI[md5sum]'] = 'c53768d63db3873b7d452833553469de'
472 checkvars['SRC_URI[sha256sum]'] = '51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274'
473 inherits = ['distutils3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500474 self._test_recipe_contents(recipefile, checkvars, inherits)
475
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500476 def test_recipetool_create_github_tarball(self):
477 # Basic test to ensure github URL mangling doesn't apply to release tarballs
478 temprecipe = os.path.join(self.tempdir, 'recipe')
479 os.makedirs(temprecipe)
480 pv = '0.32.0'
481 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
482 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
483 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
484 self.assertTrue(os.path.isfile(recipefile))
485 checkvars = {}
486 checkvars['LICENSE'] = set(['Apache-2.0'])
487 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
Brad Bishop15ae2502019-06-18 21:44:24 -0400488 inherits = ['setuptools3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500489 self._test_recipe_contents(recipefile, checkvars, inherits)
490
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500491 def test_recipetool_create_git_http(self):
492 # Basic test to check http git URL mangling works
493 temprecipe = os.path.join(self.tempdir, 'recipe')
494 os.makedirs(temprecipe)
495 recipefile = os.path.join(temprecipe, 'matchbox-terminal_git.bb')
496 srcuri = 'http://git.yoctoproject.org/git/matchbox-terminal'
497 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
498 self.assertTrue(os.path.isfile(recipefile))
499 checkvars = {}
500 checkvars['LICENSE'] = set(['GPLv2'])
501 checkvars['SRC_URI'] = 'git://git.yoctoproject.org/git/matchbox-terminal;protocol=http'
502 inherits = ['pkgconfig', 'autotools']
503 self._test_recipe_contents(recipefile, checkvars, inherits)
504
505 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
506 dstdir = basedstdir
507 self.assertTrue(os.path.exists(dstdir))
508 for p in paths:
509 dstdir = os.path.join(dstdir, p)
510 if not os.path.exists(dstdir):
511 os.makedirs(dstdir)
512 self.track_for_cleanup(dstdir)
513 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
514 if srcfile != dstfile:
515 shutil.copy(srcfile, dstfile)
516 self.track_for_cleanup(dstfile)
517
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500518 def test_recipetool_load_plugin(self):
519 """Test that recipetool loads only the first found plugin in BBPATH."""
520
521 recipetool = runCmd("which recipetool")
522 fromname = runCmd("recipetool --quiet pluginfile")
523 srcfile = fromname.output
524 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
525 plugincontent = []
526 with open(srcfile) as fh:
527 plugincontent = fh.readlines()
528 try:
529 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
530 for path in searchpath:
531 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
532 result = runCmd("recipetool --quiet count")
533 self.assertEqual(result.output, '1')
534 result = runCmd("recipetool --quiet multiloaded")
535 self.assertEqual(result.output, "no")
536 for path in searchpath:
537 result = runCmd("recipetool --quiet bbdir")
538 self.assertEqual(result.output, path)
539 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
540 finally:
541 with open(srcfile, 'w') as fh:
542 fh.writelines(plugincontent)
543
544
545class RecipetoolAppendsrcBase(RecipetoolBase):
546 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
547 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
548 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
549
550 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
551
552 if destdir:
553 options += ' -D %s' % destdir
554
555 if expectedfiles is None:
556 expectedfiles = [os.path.basename(f) for f in newfiles]
557
558 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
559 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
560
561 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
562 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
563 result = runCmd(cmd, ignore_status=True)
564 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
565 self.assertNotIn('Traceback', result.output)
566 for errorstr in checkerror:
567 self.assertIn(errorstr, result.output)
568
569 @staticmethod
570 def _get_first_file_uri(recipe):
571 '''Return the first file:// in SRC_URI for the specified recipe.'''
572 src_uri = get_bb_var('SRC_URI', recipe).split()
573 for uri in src_uri:
574 p = urllib.parse.urlparse(uri)
575 if p.scheme == 'file':
576 return p.netloc + p.path
577
578 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
579 if newfile is None:
580 newfile = self.testfile
581
582 if srcdir:
583 if destdir:
584 expected_subdir = os.path.join(srcdir, destdir)
585 else:
586 expected_subdir = srcdir
587 else:
588 options += " -W"
589 expected_subdir = destdir
590
591 if filename:
592 if destdir:
593 destpath = os.path.join(destdir, filename)
594 else:
595 destpath = filename
596 else:
597 filename = os.path.basename(newfile)
598 if destdir:
599 destpath = destdir + os.sep
600 else:
601 destpath = '.' + os.sep
602
603 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
604 '\n']
605 if has_src_uri:
606 uri = 'file://%s' % filename
607 if expected_subdir:
608 uri += ';subdir=%s' % expected_subdir
609 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
610 '\n']
611
612 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
613
614 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
615 if expectedfiles is None:
616 expectedfiles = [os.path.basename(n) for n in newfiles]
617
618 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
619
620 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
621 src_uri = bb_vars['SRC_URI'].split()
622 for f in expectedfiles:
623 if destdir:
624 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
625 else:
626 self.assertIn('file://%s' % f, src_uri)
627
628 recipefile = bb_vars['FILE']
629 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
630 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
631 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
632 self.assertIn(filesdir, filesextrapaths)
633
634
635
636
637class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
638
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500639 def test_recipetool_appendsrcfile_basic(self):
640 self._test_appendsrcfile('base-files', 'a-file')
641
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500642 def test_recipetool_appendsrcfile_basic_wildcard(self):
643 testrecipe = 'base-files'
644 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
645 recipefile = get_bb_var('FILE', testrecipe)
646 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
647 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
648
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500649 def test_recipetool_appendsrcfile_subdir_basic(self):
650 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
651
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500652 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
653 self._test_appendsrcfile('base-files', destdir='tmp')
654
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500655 def test_recipetool_appendsrcfile_srcdir_basic(self):
656 testrecipe = 'bash'
657 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
658 srcdir = bb_vars['S']
659 workdir = bb_vars['WORKDIR']
660 subdir = os.path.relpath(srcdir, workdir)
661 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
662
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500663 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
664 testrecipe = 'base-files'
665 filepath = self._get_first_file_uri(testrecipe)
666 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
667 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
668
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500669 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
670 testrecipe = 'base-files'
671 subdir = 'tmp'
672 filepath = self._get_first_file_uri(testrecipe)
673 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
674
675 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
676 self.assertTrue(any('with different parameters' in l for l in output))
677
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500678 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
679 testrecipe = 'bash'
680 filepath = 'Makefile.in'
681 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
682 srcdir = bb_vars['S']
683 workdir = bb_vars['WORKDIR']
684 subdir = os.path.relpath(srcdir, workdir)
685
686 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
687 bitbake('%s:do_unpack' % testrecipe)
688 self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read())
689
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500690 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
691 newfiles = [self.testfile]
692 for i in range(1, 5):
693 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
694 with open(testfile, 'w') as f:
695 f.write('Test file %d\n' % i)
696 newfiles.append(testfile)
697 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
698
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500699 def test_recipetool_appendsrcfiles_basic_subdir(self):
700 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')