blob: 6bfe8f177f26b94897506d3bac683248dd4790c7 [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
Andrew Geissler82c905d2020-04-13 13:39:40 -0500424 def test_recipetool_create_npm(self):
425 temprecipe = os.path.join(self.tempdir, 'recipe')
426 os.makedirs(temprecipe)
427 recipefile = os.path.join(temprecipe, 'savoirfairelinux-node-server-example_1.0.0.bb')
428 shrinkwrap = os.path.join(temprecipe, 'savoirfairelinux-node-server-example', 'npm-shrinkwrap.json')
429 srcuri = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
430 result = runCmd('recipetool create -o %s \'%s\'' % (temprecipe, srcuri))
431 self.assertTrue(os.path.isfile(recipefile))
432 self.assertTrue(os.path.isfile(shrinkwrap))
433 checkvars = {}
434 checkvars['SUMMARY'] = 'Node Server Example'
435 checkvars['HOMEPAGE'] = 'https://github.com/savoirfairelinux/node-server-example#readme'
436 checkvars['LICENSE'] = set(['MIT', 'ISC', 'Unknown'])
437 urls = []
438 urls.append('npm://registry.npmjs.org/;package=@savoirfairelinux/node-server-example;version=${PV}')
439 urls.append('npmsw://${THISDIR}/${BPN}/npm-shrinkwrap.json')
440 checkvars['SRC_URI'] = set(urls)
441 checkvars['S'] = '${WORKDIR}/npm'
442 checkvars['LICENSE_${PN}'] = 'MIT'
443 checkvars['LICENSE_${PN}-base64'] = 'Unknown'
444 checkvars['LICENSE_${PN}-accepts'] = 'MIT'
445 checkvars['LICENSE_${PN}-inherits'] = 'ISC'
446 inherits = ['npm']
447 self._test_recipe_contents(recipefile, checkvars, inherits)
448
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500449 def test_recipetool_create_github(self):
450 # Basic test to see if github URL mangling works
451 temprecipe = os.path.join(self.tempdir, 'recipe')
452 os.makedirs(temprecipe)
453 recipefile = os.path.join(temprecipe, 'meson_git.bb')
454 srcuri = 'https://github.com/mesonbuild/meson;rev=0.32.0'
455 result = runCmd(['recipetool', 'create', '-o', temprecipe, srcuri])
456 self.assertTrue(os.path.isfile(recipefile))
457 checkvars = {}
458 checkvars['LICENSE'] = set(['Apache-2.0'])
459 checkvars['SRC_URI'] = 'git://github.com/mesonbuild/meson;protocol=https'
Brad Bishop15ae2502019-06-18 21:44:24 -0400460 inherits = ['setuptools3']
461 self._test_recipe_contents(recipefile, checkvars, inherits)
462
463 def test_recipetool_create_python3_setuptools(self):
464 # Test creating python3 package from tarball (using setuptools3 class)
465 temprecipe = os.path.join(self.tempdir, 'recipe')
466 os.makedirs(temprecipe)
467 pn = 'python-magic'
468 pv = '0.4.15'
469 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
470 srcuri = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-%s.tar.gz' % pv
471 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
472 self.assertTrue(os.path.isfile(recipefile))
473 checkvars = {}
474 checkvars['LICENSE'] = set(['MIT'])
475 checkvars['LIC_FILES_CHKSUM'] = 'file://LICENSE;md5=16a934f165e8c3245f241e77d401bb88'
476 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-${PV}.tar.gz'
477 checkvars['SRC_URI[md5sum]'] = 'e384c95a47218f66c6501cd6dd45ff59'
478 checkvars['SRC_URI[sha256sum]'] = 'f3765c0f582d2dfc72c15f3b5a82aecfae9498bd29ca840d72f37d7bd38bfcd5'
479 inherits = ['setuptools3']
480 self._test_recipe_contents(recipefile, checkvars, inherits)
481
482 def test_recipetool_create_python3_distutils(self):
483 # Test creating python3 package from tarball (using distutils3 class)
484 temprecipe = os.path.join(self.tempdir, 'recipe')
485 os.makedirs(temprecipe)
486 pn = 'docutils'
487 pv = '0.14'
488 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
489 srcuri = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-%s.tar.gz' % pv
490 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
491 self.assertTrue(os.path.isfile(recipefile))
492 checkvars = {}
493 checkvars['LICENSE'] = set(['PSF', '&', 'BSD', 'GPL'])
494 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING.txt;md5=35a23d42b615470583563132872c97d6'
495 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-${PV}.tar.gz'
496 checkvars['SRC_URI[md5sum]'] = 'c53768d63db3873b7d452833553469de'
497 checkvars['SRC_URI[sha256sum]'] = '51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274'
498 inherits = ['distutils3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500499 self._test_recipe_contents(recipefile, checkvars, inherits)
500
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500501 def test_recipetool_create_github_tarball(self):
502 # Basic test to ensure github URL mangling doesn't apply to release tarballs
503 temprecipe = os.path.join(self.tempdir, 'recipe')
504 os.makedirs(temprecipe)
505 pv = '0.32.0'
506 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
507 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
508 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
509 self.assertTrue(os.path.isfile(recipefile))
510 checkvars = {}
511 checkvars['LICENSE'] = set(['Apache-2.0'])
512 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
Brad Bishop15ae2502019-06-18 21:44:24 -0400513 inherits = ['setuptools3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500514 self._test_recipe_contents(recipefile, checkvars, inherits)
515
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500516 def test_recipetool_create_git_http(self):
517 # Basic test to check http git URL mangling works
518 temprecipe = os.path.join(self.tempdir, 'recipe')
519 os.makedirs(temprecipe)
520 recipefile = os.path.join(temprecipe, 'matchbox-terminal_git.bb')
521 srcuri = 'http://git.yoctoproject.org/git/matchbox-terminal'
522 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
523 self.assertTrue(os.path.isfile(recipefile))
524 checkvars = {}
525 checkvars['LICENSE'] = set(['GPLv2'])
526 checkvars['SRC_URI'] = 'git://git.yoctoproject.org/git/matchbox-terminal;protocol=http'
527 inherits = ['pkgconfig', 'autotools']
528 self._test_recipe_contents(recipefile, checkvars, inherits)
529
530 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
531 dstdir = basedstdir
532 self.assertTrue(os.path.exists(dstdir))
533 for p in paths:
534 dstdir = os.path.join(dstdir, p)
535 if not os.path.exists(dstdir):
536 os.makedirs(dstdir)
537 self.track_for_cleanup(dstdir)
538 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
539 if srcfile != dstfile:
540 shutil.copy(srcfile, dstfile)
541 self.track_for_cleanup(dstfile)
542
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500543 def test_recipetool_load_plugin(self):
544 """Test that recipetool loads only the first found plugin in BBPATH."""
545
546 recipetool = runCmd("which recipetool")
547 fromname = runCmd("recipetool --quiet pluginfile")
548 srcfile = fromname.output
549 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
550 plugincontent = []
551 with open(srcfile) as fh:
552 plugincontent = fh.readlines()
553 try:
554 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
555 for path in searchpath:
556 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
557 result = runCmd("recipetool --quiet count")
558 self.assertEqual(result.output, '1')
559 result = runCmd("recipetool --quiet multiloaded")
560 self.assertEqual(result.output, "no")
561 for path in searchpath:
562 result = runCmd("recipetool --quiet bbdir")
563 self.assertEqual(result.output, path)
564 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
565 finally:
566 with open(srcfile, 'w') as fh:
567 fh.writelines(plugincontent)
568
569
570class RecipetoolAppendsrcBase(RecipetoolBase):
571 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
572 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
573 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
574
575 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
576
577 if destdir:
578 options += ' -D %s' % destdir
579
580 if expectedfiles is None:
581 expectedfiles = [os.path.basename(f) for f in newfiles]
582
583 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
584 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
585
586 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
587 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
588 result = runCmd(cmd, ignore_status=True)
589 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
590 self.assertNotIn('Traceback', result.output)
591 for errorstr in checkerror:
592 self.assertIn(errorstr, result.output)
593
594 @staticmethod
595 def _get_first_file_uri(recipe):
596 '''Return the first file:// in SRC_URI for the specified recipe.'''
597 src_uri = get_bb_var('SRC_URI', recipe).split()
598 for uri in src_uri:
599 p = urllib.parse.urlparse(uri)
600 if p.scheme == 'file':
601 return p.netloc + p.path
602
603 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
604 if newfile is None:
605 newfile = self.testfile
606
607 if srcdir:
608 if destdir:
609 expected_subdir = os.path.join(srcdir, destdir)
610 else:
611 expected_subdir = srcdir
612 else:
613 options += " -W"
614 expected_subdir = destdir
615
616 if filename:
617 if destdir:
618 destpath = os.path.join(destdir, filename)
619 else:
620 destpath = filename
621 else:
622 filename = os.path.basename(newfile)
623 if destdir:
624 destpath = destdir + os.sep
625 else:
626 destpath = '.' + os.sep
627
628 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
629 '\n']
630 if has_src_uri:
631 uri = 'file://%s' % filename
632 if expected_subdir:
633 uri += ';subdir=%s' % expected_subdir
634 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
635 '\n']
636
637 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
638
639 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
640 if expectedfiles is None:
641 expectedfiles = [os.path.basename(n) for n in newfiles]
642
643 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
644
645 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
646 src_uri = bb_vars['SRC_URI'].split()
647 for f in expectedfiles:
648 if destdir:
649 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
650 else:
651 self.assertIn('file://%s' % f, src_uri)
652
653 recipefile = bb_vars['FILE']
654 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
655 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
656 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
657 self.assertIn(filesdir, filesextrapaths)
658
659
660
661
662class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
663
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500664 def test_recipetool_appendsrcfile_basic(self):
665 self._test_appendsrcfile('base-files', 'a-file')
666
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500667 def test_recipetool_appendsrcfile_basic_wildcard(self):
668 testrecipe = 'base-files'
669 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
670 recipefile = get_bb_var('FILE', testrecipe)
671 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
672 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
673
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500674 def test_recipetool_appendsrcfile_subdir_basic(self):
675 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
676
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500677 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
678 self._test_appendsrcfile('base-files', destdir='tmp')
679
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500680 def test_recipetool_appendsrcfile_srcdir_basic(self):
681 testrecipe = 'bash'
682 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
683 srcdir = bb_vars['S']
684 workdir = bb_vars['WORKDIR']
685 subdir = os.path.relpath(srcdir, workdir)
686 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
687
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500688 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
689 testrecipe = 'base-files'
690 filepath = self._get_first_file_uri(testrecipe)
691 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
692 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
693
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500694 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
695 testrecipe = 'base-files'
696 subdir = 'tmp'
697 filepath = self._get_first_file_uri(testrecipe)
698 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
699
700 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
701 self.assertTrue(any('with different parameters' in l for l in output))
702
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500703 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
704 testrecipe = 'bash'
705 filepath = 'Makefile.in'
706 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
707 srcdir = bb_vars['S']
708 workdir = bb_vars['WORKDIR']
709 subdir = os.path.relpath(srcdir, workdir)
710
711 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
712 bitbake('%s:do_unpack' % testrecipe)
Brad Bishop64c979e2019-11-04 13:55:29 -0500713 with open(self.testfile, 'r') as testfile:
714 with open(os.path.join(srcdir, filepath), 'r') as makefilein:
715 self.assertEqual(testfile.read(), makefilein.read())
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500716
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500717 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
718 newfiles = [self.testfile]
719 for i in range(1, 5):
720 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
721 with open(testfile, 'w') as f:
722 f.write('Test file %d\n' % i)
723 newfiles.append(testfile)
724 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
725
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500726 def test_recipetool_appendsrcfiles_basic_subdir(self):
727 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')