2 # Copyright OpenEmbedded Contributors
4 # SPDX-License-Identifier: MIT
10 import oeqa
.utils
.ftools
as ftools
11 from oeqa
.utils
.commands
import runCmd
, bitbake
, get_bb_var
, get_bb_vars
13 from oeqa
.selftest
.case
import OESelftestTestCase
15 class BitbakeTests(OESelftestTestCase
):
17 def getline(self
, res
, line
):
18 for l
in res
.output
.split('\n'):
22 # Test bitbake can run from the <builddir>/conf directory
23 def test_run_bitbake_from_dir_1(self
):
24 os
.chdir(os
.path
.join(self
.builddir
, 'conf'))
25 self
.assertEqual(bitbake('-e').status
, 0, msg
= "bitbake couldn't run from \"conf\" dir")
27 # Test bitbake can run from the <builddir>'s parent directory
28 def test_run_bitbake_from_dir_2(self
):
29 my_env
= os
.environ
.copy()
30 my_env
['BBPATH'] = my_env
['BUILDDIR']
31 os
.chdir(os
.path
.dirname(os
.environ
['BUILDDIR']))
32 self
.assertEqual(bitbake('-e', env
=my_env
).status
, 0, msg
= "bitbake couldn't run from builddir's parent directory")
34 # Test bitbake can run from some other random system location (we use /tmp/)
35 def test_run_bitbake_from_dir_3(self
):
36 my_env
= os
.environ
.copy()
37 my_env
['BBPATH'] = my_env
['BUILDDIR']
39 self
.assertEqual(bitbake('-e', env
=my_env
).status
, 0, msg
= "bitbake couldn't run from /tmp/")
42 def test_event_handler(self
):
43 self
.write_config("INHERIT += \"test_events\"")
44 result
= bitbake('selftest-hello-native')
45 find_build_started
= re
.search(r
"NOTE: Test for bb\.event\.BuildStarted(\n.*)*NOTE: Executing.*Tasks", result
.output
)
46 find_build_completed
= re
.search(r
"Tasks Summary:.*(\n.*)*NOTE: Test for bb\.event\.BuildCompleted", result
.output
)
47 self
.assertTrue(find_build_started
, msg
= "Match failed in:\n%s" % result
.output
)
48 self
.assertTrue(find_build_completed
, msg
= "Match failed in:\n%s" % result
.output
)
49 self
.assertNotIn('Test for bb.event.InvalidEvent', result
.output
)
51 def test_local_sstate(self
):
52 bitbake('selftest-hello-native')
53 bitbake('selftest-hello-native -cclean')
54 result
= bitbake('selftest-hello-native')
55 find_setscene
= re
.search("selftest-hello-native.*do_.*_setscene", result
.output
)
56 self
.assertTrue(find_setscene
, msg
= "No \"selftest-hello-native.*do_.*_setscene\" message found during bitbake selftest-hello-native. bitbake output: %s" % result
.output
)
58 def test_bitbake_invalid_recipe(self
):
59 result
= bitbake('-b asdf', ignore_status
=True)
60 self
.assertTrue("ERROR: Unable to find any recipe file matching 'asdf'" in result
.output
, msg
= "Though asdf recipe doesn't exist, bitbake didn't output any err. message. bitbake output: %s" % result
.output
)
62 def test_bitbake_invalid_target(self
):
63 result
= bitbake('asdf', ignore_status
=True)
64 self
.assertIn("ERROR: Nothing PROVIDES 'asdf'", result
.output
)
66 def test_warnings_errors(self
):
67 result
= bitbake('-b asdf', ignore_status
=True)
68 find_warnings
= re
.search("Summary: There w.{2,3}? [1-9][0-9]* WARNING messages*", result
.output
)
69 find_errors
= re
.search("Summary: There w.{2,3}? [1-9][0-9]* ERROR messages*", result
.output
)
70 self
.assertTrue(find_warnings
, msg
="Did not find the mumber of warnings at the end of the build:\n" + result
.output
)
71 self
.assertTrue(find_errors
, msg
="Did not find the mumber of errors at the end of the build:\n" + result
.output
)
73 def test_invalid_patch(self
):
74 # This patch should fail to apply.
75 self
.write_recipeinc('man-db', 'FILESEXTRAPATHS:prepend := "${THISDIR}/files:"\nSRC_URI += "file://0001-Test-patch-here.patch"')
76 self
.write_config("INHERIT:remove = \"report-error\"")
77 result
= bitbake('man-db -c patch', ignore_status
=True)
78 self
.delete_recipeinc('man-db')
79 bitbake('-cclean man-db')
81 for l
in result
.output
.split('\n'):
82 if l
.startswith("ERROR:") and "failed" in l
and "do_patch" in l
:
84 self
.assertTrue(found
and found
.startswith("ERROR:"), msg
= "Incorrectly formed patch application didn't fail. bitbake output: %s" % result
.output
)
86 def test_force_task_1(self
):
87 # test 1 from bug 5875
90 # Need to use uuid otherwise hash equivlance would change the workflow
91 test_data
= "Microsoft Made No Profit From Anyone's Zunes Yo %s" % uuid
.uuid1()
92 bb_vars
= get_bb_vars(['D', 'PKGDEST', 'mandir'], test_recipe
)
93 image_dir
= bb_vars
['D']
94 pkgsplit_dir
= bb_vars
['PKGDEST']
95 man_dir
= bb_vars
['mandir']
96 self
.write_config("PACKAGE_CLASSES = \"package_rpm\"")
98 bitbake('-c clean %s' % test_recipe
)
99 bitbake('-c package -f %s' % test_recipe
)
100 self
.add_command_to_tearDown('bitbake -c clean %s' % test_recipe
)
102 man_file
= os
.path
.join(image_dir
+ man_dir
, 'man3/zlib.3')
103 ftools
.append_file(man_file
, test_data
)
104 bitbake('-c package -f %s' % test_recipe
)
106 man_split_file
= os
.path
.join(pkgsplit_dir
, 'zlib-doc' + man_dir
, 'man3/zlib.3')
107 man_split_content
= ftools
.read_file(man_split_file
)
108 self
.assertIn(test_data
, man_split_content
, 'The man file has not changed in packages-split.')
110 ret
= bitbake(test_recipe
)
111 self
.assertIn('task do_package_write_rpm:', ret
.output
, 'Task do_package_write_rpm did not re-executed.')
113 def test_force_task_2(self
):
114 # test 2 from bug 5875
118 self
.add_command_to_tearDown('bitbake -c clean %s' % test_recipe
)
120 result
= bitbake('-C compile %s' % test_recipe
)
121 look_for_tasks
= ['do_compile:', 'do_install:', 'do_populate_sysroot:', 'do_package:']
122 for task
in look_for_tasks
:
123 self
.assertIn(task
, result
.output
, msg
="Couldn't find %s task.")
125 def test_bitbake_g(self
):
126 recipe
= 'base-files'
127 result
= bitbake('-g %s' % recipe
)
128 for f
in ['pn-buildlist', 'task-depends.dot']:
129 self
.addCleanup(os
.remove
, f
)
130 self
.assertTrue('Task dependencies saved to \'task-depends.dot\'' in result
.output
, msg
= "No task dependency \"task-depends.dot\" file was generated for the given task target. bitbake output: %s" % result
.output
)
131 self
.assertIn(recipe
, ftools
.read_file(os
.path
.join(self
.builddir
, 'task-depends.dot')))
133 def test_image_manifest(self
):
134 bitbake('core-image-minimal')
135 bb_vars
= get_bb_vars(["DEPLOY_DIR_IMAGE", "IMAGE_LINK_NAME"], "core-image-minimal")
136 deploydir
= bb_vars
["DEPLOY_DIR_IMAGE"]
137 imagename
= bb_vars
["IMAGE_LINK_NAME"]
138 manifest
= os
.path
.join(deploydir
, imagename
+ ".manifest")
139 self
.assertTrue(os
.path
.islink(manifest
), msg
="No manifest file created for image. It should have been created in %s" % manifest
)
141 def test_invalid_recipe_src_uri(self
):
142 data
= 'SRC_URI = "file://invalid"'
143 self
.write_recipeinc('man-db', data
)
144 self
.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
145 SSTATE_DIR = \"${TOPDIR}/download-selftest\"
146 INHERIT:remove = \"report-error\"
148 self
.track_for_cleanup(os
.path
.join(self
.builddir
, "download-selftest"))
150 result
= bitbake('-c fetch man-db', ignore_status
=True)
151 self
.delete_recipeinc('man-db')
152 self
.assertEqual(result
.status
, 1, msg
="Command succeded when it should have failed. bitbake output: %s" % result
.output
)
153 self
.assertIn('Unable to get checksum for man-db SRC_URI entry invalid: file could not be found', result
.output
)
155 def test_rename_downloaded_file(self
):
156 # TODO unique dldir instead of using cleanall
157 # TODO: need to set sstatedir?
158 self
.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
159 SSTATE_DIR = \"${TOPDIR}/download-selftest\"
161 self
.track_for_cleanup(os
.path
.join(self
.builddir
, "download-selftest"))
163 data
= 'SRC_URI = "https://downloads.yoctoproject.org/mirror/sources/aspell-${PV}.tar.gz;downloadfilename=test-aspell.tar.gz"'
164 self
.write_recipeinc('aspell', data
)
165 result
= bitbake('-f -c fetch aspell', ignore_status
=True)
166 self
.delete_recipeinc('aspell')
167 self
.assertEqual(result
.status
, 0, msg
= "Couldn't fetch aspell. %s" % result
.output
)
168 dl_dir
= get_bb_var("DL_DIR")
169 self
.assertTrue(os
.path
.isfile(os
.path
.join(dl_dir
, 'test-aspell.tar.gz')), msg
= "File rename failed. No corresponding test-aspell.tar.gz file found under %s" % dl_dir
)
170 self
.assertTrue(os
.path
.isfile(os
.path
.join(dl_dir
, 'test-aspell.tar.gz.done')), "File rename failed. No corresponding test-aspell.tar.gz.done file found under %s" % dl_dir
)
172 def test_environment(self
):
173 self
.write_config("TEST_ENV=\"localconf\"")
174 result
= runCmd('bitbake -e | grep TEST_ENV=')
175 self
.assertIn('localconf', result
.output
)
177 def test_dry_run(self
):
178 result
= runCmd('bitbake -n selftest-hello-native')
179 self
.assertEqual(0, result
.status
, "bitbake dry run didn't run as expected. %s" % result
.output
)
181 def test_just_parse(self
):
182 result
= runCmd('bitbake -p')
183 self
.assertEqual(0, result
.status
, "errors encountered when parsing recipes. %s" % result
.output
)
185 def test_version(self
):
186 result
= runCmd('bitbake -s | grep wget')
187 find
= re
.search(r
"wget *:([0-9a-zA-Z\.\-]+)", result
.output
)
188 self
.assertTrue(find
, "No version returned for searched recipe. bitbake output: %s" % result
.output
)
190 def test_prefile(self
):
191 # Test when the prefile does not exist
192 result
= runCmd('bitbake -r conf/prefile.conf', ignore_status
=True)
193 self
.assertEqual(1, result
.status
, "bitbake didn't error and should have when a specified prefile didn't exist: %s" % result
.output
)
194 # Test when the prefile exists
195 preconf
= os
.path
.join(self
.builddir
, 'conf/prefile.conf')
196 self
.track_for_cleanup(preconf
)
197 ftools
.write_file(preconf
,"TEST_PREFILE=\"prefile\"")
198 result
= runCmd('bitbake -r conf/prefile.conf -e | grep TEST_PREFILE=')
199 self
.assertIn('prefile', result
.output
)
200 self
.write_config("TEST_PREFILE=\"localconf\"")
201 result
= runCmd('bitbake -r conf/prefile.conf -e | grep TEST_PREFILE=')
202 self
.assertIn('localconf', result
.output
)
204 def test_postfile(self
):
205 # Test when the postfile does not exist
206 result
= runCmd('bitbake -R conf/postfile.conf', ignore_status
=True)
207 self
.assertEqual(1, result
.status
, "bitbake didn't error and should have when a specified postfile didn't exist: %s" % result
.output
)
208 # Test when the postfile exists
209 postconf
= os
.path
.join(self
.builddir
, 'conf/postfile.conf')
210 self
.track_for_cleanup(postconf
)
211 ftools
.write_file(postconf
, "TEST_POSTFILE=\"postfile\"")
212 self
.write_config("TEST_POSTFILE=\"localconf\"")
213 result
= runCmd('bitbake -R conf/postfile.conf -e | grep TEST_POSTFILE=')
214 self
.assertIn('postfile', result
.output
)
216 def test_checkuri(self
):
217 result
= runCmd('bitbake -c checkuri m4')
218 self
.assertEqual(0, result
.status
, msg
= "\"checkuri\" task was not executed. bitbake output: %s" % result
.output
)
220 def test_continue(self
):
221 self
.write_config("""DL_DIR = \"${TOPDIR}/download-selftest\"
222 SSTATE_DIR = \"${TOPDIR}/download-selftest\"
223 INHERIT:remove = \"report-error\"
225 self
.track_for_cleanup(os
.path
.join(self
.builddir
, "download-selftest"))
226 self
.write_recipeinc('man-db',"\ndo_fail_task () {\nexit 1 \n}\n\naddtask do_fail_task before do_fetch\n" )
227 runCmd('bitbake -c cleanall man-db xcursor-transparent-theme')
228 result
= runCmd('bitbake -c unpack -k man-db xcursor-transparent-theme', ignore_status
=True)
229 errorpos
= result
.output
.find('ERROR: Function failed: do_fail_task')
230 manver
= re
.search("NOTE: recipe xcursor-transparent-theme-(.*?): task do_unpack: Started", result
.output
)
231 continuepos
= result
.output
.find('NOTE: recipe xcursor-transparent-theme-%s: task do_unpack: Started' % manver
.group(1))
232 self
.assertLess(errorpos
,continuepos
, msg
= "bitbake didn't pass do_fail_task. bitbake output: %s" % result
.output
)
234 def test_non_gplv3(self
):
235 self
.write_config('''INCOMPATIBLE_LICENSE = "GPL-3.0-or-later"
236 require conf/distro/include/no-gplv3.inc
238 result
= bitbake('selftest-ed', ignore_status
=True)
239 self
.assertEqual(result
.status
, 0, "Bitbake failed, exit code %s, output %s" % (result
.status
, result
.output
))
240 lic_dir
= get_bb_var('LICENSE_DIRECTORY')
241 arch
= get_bb_var('SSTATE_PKGARCH')
242 filename
= os
.path
.join(lic_dir
, arch
, 'selftest-ed', 'generic_GPL-3.0-or-later')
243 self
.assertFalse(os
.path
.isfile(filename
), msg
="License file %s exists and shouldn't" % filename
)
244 filename
= os
.path
.join(lic_dir
, arch
, 'selftest-ed', 'generic_GPL-2.0-or-later')
245 self
.assertTrue(os
.path
.isfile(filename
), msg
="License file %s doesn't exist" % filename
)
247 def test_setscene_only(self
):
248 """ Bitbake option to restore from sstate only within a build (i.e. execute no real tasks, only setscene)"""
249 test_recipe
= 'selftest-hello-native'
252 bitbake('-c clean %s' % test_recipe
)
253 ret
= bitbake('--setscene-only %s' % test_recipe
)
255 tasks
= re
.findall(r
'task\s+(do_\S+):', ret
.output
)
258 self
.assertIn('_setscene', task
, 'A task different from _setscene ran: %s.\n'
259 'Executed tasks were: %s' % (task
, str(tasks
)))
261 def test_skip_setscene(self
):
262 test_recipe
= 'selftest-hello-native'
265 bitbake('-c clean %s' % test_recipe
)
267 ret
= bitbake('--setscene-only %s' % test_recipe
)
268 tasks
= re
.findall(r
'task\s+(do_\S+):', ret
.output
)
271 self
.assertIn('_setscene', task
, 'A task different from _setscene ran: %s.\n'
272 'Executed tasks were: %s' % (task
, str(tasks
)))
274 # Run without setscene. Should do nothing
275 ret
= bitbake('--skip-setscene %s' % test_recipe
)
276 tasks
= re
.findall(r
'task\s+(do_\S+):', ret
.output
)
278 self
.assertFalse(tasks
, 'Tasks %s ran when they should not have' % (str(tasks
)))
280 # Clean (leave sstate cache) and run with --skip-setscene. No setscene
282 bitbake('-c clean %s' % test_recipe
)
284 ret
= bitbake('--skip-setscene %s' % test_recipe
)
285 tasks
= re
.findall(r
'task\s+(do_\S+):', ret
.output
)
288 self
.assertNotIn('_setscene', task
, 'A _setscene task ran: %s.\n'
289 'Executed tasks were: %s' % (task
, str(tasks
)))
291 def test_bbappend_order(self
):
292 """ Bitbake should bbappend to recipe in a predictable order """
294 bb_vars
= get_bb_vars(['SUMMARY', 'PV'], test_recipe
)
295 test_recipe_summary_before
= bb_vars
['SUMMARY']
296 test_recipe_pv
= bb_vars
['PV']
297 recipe_append_file
= test_recipe
+ '_' + test_recipe_pv
+ '.bbappend'
298 expected_recipe_summary
= test_recipe_summary_before
301 recipe_append_dir
= test_recipe
+ '_test_' + str(i
)
302 recipe_append_path
= os
.path
.join(self
.testlayer_path
, 'recipes-test', recipe_append_dir
, recipe_append_file
)
303 os
.mkdir(os
.path
.join(self
.testlayer_path
, 'recipes-test', recipe_append_dir
))
304 feature
= 'SUMMARY += "%s"\n' % i
305 ftools
.write_file(recipe_append_path
, feature
)
306 expected_recipe_summary
+= ' %s' % i
308 self
.add_command_to_tearDown('rm -rf %s' % os
.path
.join(self
.testlayer_path
, 'recipes-test',
309 test_recipe
+ '_test_*'))
311 test_recipe_summary_after
= get_bb_var('SUMMARY', test_recipe
)
312 self
.assertEqual(expected_recipe_summary
, test_recipe_summary_after
)
314 def test_git_patchtool(self
):
315 """ PATCHTOOL=git should work with non-git sources like tarballs
316 test recipe for the test must NOT containt git:// repository in SRC_URI
318 test_recipe
= "man-db"
319 self
.write_recipeinc(test_recipe
, 'PATCHTOOL=\"git\"')
320 src
= get_bb_var("SRC_URI",test_recipe
)
321 gitscm
= re
.search("git://", src
)
322 self
.assertFalse(gitscm
, "test_git_patchtool pre-condition failed: {} test recipe contains git repo!".format(test_recipe
))
323 result
= bitbake('{} -c patch'.format(test_recipe
), ignore_status
=False)
324 fatal
= re
.search("fatal: not a git repository (or any of the parent directories)", result
.output
)
325 self
.assertFalse(fatal
, "Failed to patch using PATCHTOOL=\"git\"")
326 self
.delete_recipeinc(test_recipe
)
327 bitbake('-cclean {}'.format(test_recipe
))
329 def test_git_patchtool2(self
):
330 """ Test if PATCHTOOL=git works with git repo and doesn't reinitialize it
332 test_recipe
= "gitrepotest"
333 src
= get_bb_var("SRC_URI",test_recipe
)
334 gitscm
= re
.search("git://", src
)
335 self
.assertTrue(gitscm
, "test_git_patchtool pre-condition failed: {} test recipe doesn't contains git repo!".format(test_recipe
))
336 result
= bitbake('{} -c patch'.format(test_recipe
), ignore_status
=False)
337 srcdir
= get_bb_var('S', test_recipe
)
338 result
= runCmd("git log", cwd
= srcdir
)
339 self
.assertFalse("bitbake_patching_started" in result
.output
, msg
= "Repository has been reinitialized. {}".format(srcdir
))
340 self
.delete_recipeinc(test_recipe
)
341 bitbake('-cclean {}'.format(test_recipe
))
344 def test_git_unpack_nonetwork(self
):
346 Test that a recipe with a floating tag that needs to be resolved upstream doesn't
347 access the network in a patch task run in a separate builld invocation
350 # Enable the recipe to float using a distro override
351 self
.write_config("DISTROOVERRIDES .= \":gitunpack-enable-recipe\"")
353 bitbake('gitunpackoffline -c fetch')
354 bitbake('gitunpackoffline -c patch')
356 def test_git_unpack_nonetwork_fail(self
):
358 Test that a recipe with a floating tag which doesn't call get_srcrev() in the fetcher
359 raises an error when the fetcher is called.
362 # Enable the recipe to float using a distro override
363 self
.write_config("DISTROOVERRIDES .= \":gitunpack-enable-recipe\"")
365 result
= bitbake('gitunpackoffline-fail -c fetch', ignore_status
=True)
366 self
.assertTrue(re
.search("Recipe uses a floating tag/branch .* for repo .* without a fixed SRCREV yet doesn't call bb.fetch2.get_srcrev()", result
.output
), msg
= "Recipe without PV set to SRCPV should have failed: %s" % result
.output
)
368 def test_unexpanded_variable_in_path(self
):
370 Test that bitbake fails if directory contains unexpanded bitbake variable in the name
372 recipe_name
= "gitunpackoffline"
373 self
.write_config('PV:pn-gitunpackoffline:append = "+${UNDEFVAL}"')
374 result
= bitbake('{}'.format(recipe_name
), ignore_status
=True)
375 self
.assertGreater(result
.status
, 0, "Build should have failed if ${ is in the path")
376 self
.assertTrue(re
.search("ERROR: Directory name /.* contains unexpanded bitbake variable. This may cause build failures and WORKDIR polution",
377 result
.output
), msg
= "mkdirhier with unexpanded variable should have failed: %s" % result
.output
)