blob: f1cb37b53f8bdc315a7fadd1a389ce5d17a4e4fe [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'
438 inherits = ['setuptools']
439 self._test_recipe_contents(recipefile, checkvars, inherits)
440
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500441 def test_recipetool_create_github_tarball(self):
442 # Basic test to ensure github URL mangling doesn't apply to release tarballs
443 temprecipe = os.path.join(self.tempdir, 'recipe')
444 os.makedirs(temprecipe)
445 pv = '0.32.0'
446 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
447 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
448 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
449 self.assertTrue(os.path.isfile(recipefile))
450 checkvars = {}
451 checkvars['LICENSE'] = set(['Apache-2.0'])
452 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
453 inherits = ['setuptools']
454 self._test_recipe_contents(recipefile, checkvars, inherits)
455
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500456 def test_recipetool_create_git_http(self):
457 # Basic test to check http git URL mangling works
458 temprecipe = os.path.join(self.tempdir, 'recipe')
459 os.makedirs(temprecipe)
460 recipefile = os.path.join(temprecipe, 'matchbox-terminal_git.bb')
461 srcuri = 'http://git.yoctoproject.org/git/matchbox-terminal'
462 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
463 self.assertTrue(os.path.isfile(recipefile))
464 checkvars = {}
465 checkvars['LICENSE'] = set(['GPLv2'])
466 checkvars['SRC_URI'] = 'git://git.yoctoproject.org/git/matchbox-terminal;protocol=http'
467 inherits = ['pkgconfig', 'autotools']
468 self._test_recipe_contents(recipefile, checkvars, inherits)
469
470 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
471 dstdir = basedstdir
472 self.assertTrue(os.path.exists(dstdir))
473 for p in paths:
474 dstdir = os.path.join(dstdir, p)
475 if not os.path.exists(dstdir):
476 os.makedirs(dstdir)
477 self.track_for_cleanup(dstdir)
478 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
479 if srcfile != dstfile:
480 shutil.copy(srcfile, dstfile)
481 self.track_for_cleanup(dstfile)
482
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500483 def test_recipetool_load_plugin(self):
484 """Test that recipetool loads only the first found plugin in BBPATH."""
485
486 recipetool = runCmd("which recipetool")
487 fromname = runCmd("recipetool --quiet pluginfile")
488 srcfile = fromname.output
489 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
490 plugincontent = []
491 with open(srcfile) as fh:
492 plugincontent = fh.readlines()
493 try:
494 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
495 for path in searchpath:
496 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
497 result = runCmd("recipetool --quiet count")
498 self.assertEqual(result.output, '1')
499 result = runCmd("recipetool --quiet multiloaded")
500 self.assertEqual(result.output, "no")
501 for path in searchpath:
502 result = runCmd("recipetool --quiet bbdir")
503 self.assertEqual(result.output, path)
504 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
505 finally:
506 with open(srcfile, 'w') as fh:
507 fh.writelines(plugincontent)
508
509
510class RecipetoolAppendsrcBase(RecipetoolBase):
511 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
512 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
513 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
514
515 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
516
517 if destdir:
518 options += ' -D %s' % destdir
519
520 if expectedfiles is None:
521 expectedfiles = [os.path.basename(f) for f in newfiles]
522
523 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
524 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
525
526 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
527 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
528 result = runCmd(cmd, ignore_status=True)
529 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
530 self.assertNotIn('Traceback', result.output)
531 for errorstr in checkerror:
532 self.assertIn(errorstr, result.output)
533
534 @staticmethod
535 def _get_first_file_uri(recipe):
536 '''Return the first file:// in SRC_URI for the specified recipe.'''
537 src_uri = get_bb_var('SRC_URI', recipe).split()
538 for uri in src_uri:
539 p = urllib.parse.urlparse(uri)
540 if p.scheme == 'file':
541 return p.netloc + p.path
542
543 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
544 if newfile is None:
545 newfile = self.testfile
546
547 if srcdir:
548 if destdir:
549 expected_subdir = os.path.join(srcdir, destdir)
550 else:
551 expected_subdir = srcdir
552 else:
553 options += " -W"
554 expected_subdir = destdir
555
556 if filename:
557 if destdir:
558 destpath = os.path.join(destdir, filename)
559 else:
560 destpath = filename
561 else:
562 filename = os.path.basename(newfile)
563 if destdir:
564 destpath = destdir + os.sep
565 else:
566 destpath = '.' + os.sep
567
568 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
569 '\n']
570 if has_src_uri:
571 uri = 'file://%s' % filename
572 if expected_subdir:
573 uri += ';subdir=%s' % expected_subdir
574 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
575 '\n']
576
577 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
578
579 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
580 if expectedfiles is None:
581 expectedfiles = [os.path.basename(n) for n in newfiles]
582
583 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
584
585 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
586 src_uri = bb_vars['SRC_URI'].split()
587 for f in expectedfiles:
588 if destdir:
589 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
590 else:
591 self.assertIn('file://%s' % f, src_uri)
592
593 recipefile = bb_vars['FILE']
594 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
595 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
596 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
597 self.assertIn(filesdir, filesextrapaths)
598
599
600
601
602class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
603
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500604 def test_recipetool_appendsrcfile_basic(self):
605 self._test_appendsrcfile('base-files', 'a-file')
606
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500607 def test_recipetool_appendsrcfile_basic_wildcard(self):
608 testrecipe = 'base-files'
609 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
610 recipefile = get_bb_var('FILE', testrecipe)
611 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
612 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
613
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500614 def test_recipetool_appendsrcfile_subdir_basic(self):
615 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
616
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500617 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
618 self._test_appendsrcfile('base-files', destdir='tmp')
619
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500620 def test_recipetool_appendsrcfile_srcdir_basic(self):
621 testrecipe = 'bash'
622 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
623 srcdir = bb_vars['S']
624 workdir = bb_vars['WORKDIR']
625 subdir = os.path.relpath(srcdir, workdir)
626 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
627
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500628 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
629 testrecipe = 'base-files'
630 filepath = self._get_first_file_uri(testrecipe)
631 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
632 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
633
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500634 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
635 testrecipe = 'base-files'
636 subdir = 'tmp'
637 filepath = self._get_first_file_uri(testrecipe)
638 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
639
640 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
641 self.assertTrue(any('with different parameters' in l for l in output))
642
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500643 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
644 testrecipe = 'bash'
645 filepath = 'Makefile.in'
646 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
647 srcdir = bb_vars['S']
648 workdir = bb_vars['WORKDIR']
649 subdir = os.path.relpath(srcdir, workdir)
650
651 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
652 bitbake('%s:do_unpack' % testrecipe)
653 self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read())
654
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500655 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
656 newfiles = [self.testfile]
657 for i in range(1, 5):
658 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
659 with open(testfile, 'w') as f:
660 f.write('Test file %d\n' % i)
661 newfiles.append(testfile)
662 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
663
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500664 def test_recipetool_appendsrcfiles_basic_subdir(self):
665 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')