blob: e3f5c71666f33995ce9e35b337928330c5c14100 [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 Bishop1a4b7ee2018-12-16 17:11:34 -0800409 bitbake('-c packagedata gtk+')
410
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 # Try adding a recipe
412 temprecipe = os.path.join(self.tempdir, 'recipe')
413 os.makedirs(temprecipe)
414 recipefile = os.path.join(temprecipe, 'navit_0.5.0.bb')
Brad Bishopc4ea0752018-11-15 14:30:15 -0800415 srcuri = 'http://downloads.yoctoproject.org/mirror/sources/navit-0.5.0.tar.gz'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500416 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
417 self.assertTrue(os.path.isfile(recipefile))
418 checkvars = {}
419 checkvars['LICENSE'] = set(['Unknown', 'GPLv2', 'LGPLv2'])
Brad Bishopc4ea0752018-11-15 14:30:15 -0800420 checkvars['SRC_URI'] = 'http://downloads.yoctoproject.org/mirror/sources/navit-${PV}.tar.gz'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500421 checkvars['SRC_URI[md5sum]'] = '242f398e979a6b8c0f3c802b63435b68'
422 checkvars['SRC_URI[sha256sum]'] = '13353481d7fc01a4f64e385dda460b51496366bba0fd2cc85a89a0747910e94d'
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800423 checkvars['DEPENDS'] = set(['freetype', 'zlib', 'openssl', 'glib-2.0', 'virtual/libgl', 'virtual/egl', 'gtk+', 'libpng', 'libsdl', 'freeglut', 'dbus-glib', 'fribidi'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500424 inherits = ['cmake', 'python-dir', 'gettext', 'pkgconfig']
425 self._test_recipe_contents(recipefile, checkvars, inherits)
426
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500427 def test_recipetool_create_github(self):
428 # Basic test to see if github URL mangling works
429 temprecipe = os.path.join(self.tempdir, 'recipe')
430 os.makedirs(temprecipe)
431 recipefile = os.path.join(temprecipe, 'meson_git.bb')
432 srcuri = 'https://github.com/mesonbuild/meson;rev=0.32.0'
433 result = runCmd(['recipetool', 'create', '-o', temprecipe, srcuri])
434 self.assertTrue(os.path.isfile(recipefile))
435 checkvars = {}
436 checkvars['LICENSE'] = set(['Apache-2.0'])
437 checkvars['SRC_URI'] = 'git://github.com/mesonbuild/meson;protocol=https'
Brad Bishop15ae2502019-06-18 21:44:24 -0400438 inherits = ['setuptools3']
439 self._test_recipe_contents(recipefile, checkvars, inherits)
440
441 def test_recipetool_create_python3_setuptools(self):
442 # Test creating python3 package from tarball (using setuptools3 class)
443 temprecipe = os.path.join(self.tempdir, 'recipe')
444 os.makedirs(temprecipe)
445 pn = 'python-magic'
446 pv = '0.4.15'
447 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
448 srcuri = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-%s.tar.gz' % pv
449 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
450 self.assertTrue(os.path.isfile(recipefile))
451 checkvars = {}
452 checkvars['LICENSE'] = set(['MIT'])
453 checkvars['LIC_FILES_CHKSUM'] = 'file://LICENSE;md5=16a934f165e8c3245f241e77d401bb88'
454 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-${PV}.tar.gz'
455 checkvars['SRC_URI[md5sum]'] = 'e384c95a47218f66c6501cd6dd45ff59'
456 checkvars['SRC_URI[sha256sum]'] = 'f3765c0f582d2dfc72c15f3b5a82aecfae9498bd29ca840d72f37d7bd38bfcd5'
457 inherits = ['setuptools3']
458 self._test_recipe_contents(recipefile, checkvars, inherits)
459
460 def test_recipetool_create_python3_distutils(self):
461 # Test creating python3 package from tarball (using distutils3 class)
462 temprecipe = os.path.join(self.tempdir, 'recipe')
463 os.makedirs(temprecipe)
464 pn = 'docutils'
465 pv = '0.14'
466 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
467 srcuri = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-%s.tar.gz' % pv
468 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
469 self.assertTrue(os.path.isfile(recipefile))
470 checkvars = {}
471 checkvars['LICENSE'] = set(['PSF', '&', 'BSD', 'GPL'])
472 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING.txt;md5=35a23d42b615470583563132872c97d6'
473 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-${PV}.tar.gz'
474 checkvars['SRC_URI[md5sum]'] = 'c53768d63db3873b7d452833553469de'
475 checkvars['SRC_URI[sha256sum]'] = '51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274'
476 inherits = ['distutils3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500477 self._test_recipe_contents(recipefile, checkvars, inherits)
478
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500479 def test_recipetool_create_github_tarball(self):
480 # Basic test to ensure github URL mangling doesn't apply to release tarballs
481 temprecipe = os.path.join(self.tempdir, 'recipe')
482 os.makedirs(temprecipe)
483 pv = '0.32.0'
484 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
485 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
486 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
487 self.assertTrue(os.path.isfile(recipefile))
488 checkvars = {}
489 checkvars['LICENSE'] = set(['Apache-2.0'])
490 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
Brad Bishop15ae2502019-06-18 21:44:24 -0400491 inherits = ['setuptools3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500492 self._test_recipe_contents(recipefile, checkvars, inherits)
493
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500494 def test_recipetool_create_git_http(self):
495 # Basic test to check http git URL mangling works
496 temprecipe = os.path.join(self.tempdir, 'recipe')
497 os.makedirs(temprecipe)
498 recipefile = os.path.join(temprecipe, 'matchbox-terminal_git.bb')
499 srcuri = 'http://git.yoctoproject.org/git/matchbox-terminal'
500 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
501 self.assertTrue(os.path.isfile(recipefile))
502 checkvars = {}
503 checkvars['LICENSE'] = set(['GPLv2'])
504 checkvars['SRC_URI'] = 'git://git.yoctoproject.org/git/matchbox-terminal;protocol=http'
505 inherits = ['pkgconfig', 'autotools']
506 self._test_recipe_contents(recipefile, checkvars, inherits)
507
508 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
509 dstdir = basedstdir
510 self.assertTrue(os.path.exists(dstdir))
511 for p in paths:
512 dstdir = os.path.join(dstdir, p)
513 if not os.path.exists(dstdir):
514 os.makedirs(dstdir)
515 self.track_for_cleanup(dstdir)
516 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
517 if srcfile != dstfile:
518 shutil.copy(srcfile, dstfile)
519 self.track_for_cleanup(dstfile)
520
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500521 def test_recipetool_load_plugin(self):
522 """Test that recipetool loads only the first found plugin in BBPATH."""
523
524 recipetool = runCmd("which recipetool")
525 fromname = runCmd("recipetool --quiet pluginfile")
526 srcfile = fromname.output
527 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
528 plugincontent = []
529 with open(srcfile) as fh:
530 plugincontent = fh.readlines()
531 try:
532 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
533 for path in searchpath:
534 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
535 result = runCmd("recipetool --quiet count")
536 self.assertEqual(result.output, '1')
537 result = runCmd("recipetool --quiet multiloaded")
538 self.assertEqual(result.output, "no")
539 for path in searchpath:
540 result = runCmd("recipetool --quiet bbdir")
541 self.assertEqual(result.output, path)
542 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
543 finally:
544 with open(srcfile, 'w') as fh:
545 fh.writelines(plugincontent)
546
547
548class RecipetoolAppendsrcBase(RecipetoolBase):
549 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
550 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
551 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
552
553 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
554
555 if destdir:
556 options += ' -D %s' % destdir
557
558 if expectedfiles is None:
559 expectedfiles = [os.path.basename(f) for f in newfiles]
560
561 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
562 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
563
564 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
565 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
566 result = runCmd(cmd, ignore_status=True)
567 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
568 self.assertNotIn('Traceback', result.output)
569 for errorstr in checkerror:
570 self.assertIn(errorstr, result.output)
571
572 @staticmethod
573 def _get_first_file_uri(recipe):
574 '''Return the first file:// in SRC_URI for the specified recipe.'''
575 src_uri = get_bb_var('SRC_URI', recipe).split()
576 for uri in src_uri:
577 p = urllib.parse.urlparse(uri)
578 if p.scheme == 'file':
579 return p.netloc + p.path
580
581 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
582 if newfile is None:
583 newfile = self.testfile
584
585 if srcdir:
586 if destdir:
587 expected_subdir = os.path.join(srcdir, destdir)
588 else:
589 expected_subdir = srcdir
590 else:
591 options += " -W"
592 expected_subdir = destdir
593
594 if filename:
595 if destdir:
596 destpath = os.path.join(destdir, filename)
597 else:
598 destpath = filename
599 else:
600 filename = os.path.basename(newfile)
601 if destdir:
602 destpath = destdir + os.sep
603 else:
604 destpath = '.' + os.sep
605
606 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
607 '\n']
608 if has_src_uri:
609 uri = 'file://%s' % filename
610 if expected_subdir:
611 uri += ';subdir=%s' % expected_subdir
612 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
613 '\n']
614
615 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
616
617 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
618 if expectedfiles is None:
619 expectedfiles = [os.path.basename(n) for n in newfiles]
620
621 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
622
623 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
624 src_uri = bb_vars['SRC_URI'].split()
625 for f in expectedfiles:
626 if destdir:
627 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
628 else:
629 self.assertIn('file://%s' % f, src_uri)
630
631 recipefile = bb_vars['FILE']
632 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
633 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
634 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
635 self.assertIn(filesdir, filesextrapaths)
636
637
638
639
640class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
641
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500642 def test_recipetool_appendsrcfile_basic(self):
643 self._test_appendsrcfile('base-files', 'a-file')
644
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500645 def test_recipetool_appendsrcfile_basic_wildcard(self):
646 testrecipe = 'base-files'
647 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
648 recipefile = get_bb_var('FILE', testrecipe)
649 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
650 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
651
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500652 def test_recipetool_appendsrcfile_subdir_basic(self):
653 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
654
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500655 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
656 self._test_appendsrcfile('base-files', destdir='tmp')
657
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500658 def test_recipetool_appendsrcfile_srcdir_basic(self):
659 testrecipe = 'bash'
660 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
661 srcdir = bb_vars['S']
662 workdir = bb_vars['WORKDIR']
663 subdir = os.path.relpath(srcdir, workdir)
664 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
665
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500666 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
667 testrecipe = 'base-files'
668 filepath = self._get_first_file_uri(testrecipe)
669 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
670 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
671
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500672 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
673 testrecipe = 'base-files'
674 subdir = 'tmp'
675 filepath = self._get_first_file_uri(testrecipe)
676 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
677
678 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
679 self.assertTrue(any('with different parameters' in l for l in output))
680
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500681 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
682 testrecipe = 'bash'
683 filepath = 'Makefile.in'
684 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
685 srcdir = bb_vars['S']
686 workdir = bb_vars['WORKDIR']
687 subdir = os.path.relpath(srcdir, workdir)
688
689 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
690 bitbake('%s:do_unpack' % testrecipe)
691 self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read())
692
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500693 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
694 newfiles = [self.testfile]
695 for i in range(1, 5):
696 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
697 with open(testfile, 'w') as f:
698 f.write('Test file %d\n' % i)
699 newfiles.append(testfile)
700 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
701
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500702 def test_recipetool_appendsrcfiles_basic_subdir(self):
703 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')