xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/selftest/cases/buildhistory.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#
2# SPDX-License-Identifier: MIT
3#
4
5import os
6import re
7import datetime
8
9from oeqa.selftest.case import OESelftestTestCase
10from oeqa.utils.commands import bitbake, get_bb_vars
11
12
13class BuildhistoryBase(OESelftestTestCase):
14
15    def config_buildhistory(self, tmp_bh_location=False):
16        bb_vars = get_bb_vars(['USER_CLASSES', 'INHERIT'])
17        if (not 'buildhistory' in bb_vars['USER_CLASSES']) and (not 'buildhistory' in bb_vars['INHERIT']):
18            add_buildhistory_config = 'INHERIT += "buildhistory"\nBUILDHISTORY_COMMIT = "1"'
19            self.append_config(add_buildhistory_config)
20
21        if tmp_bh_location:
22            # Using a temporary buildhistory location for testing
23            tmp_bh_dir = os.path.join(self.builddir, "tmp_buildhistory_%s" % datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
24            buildhistory_dir_config = "BUILDHISTORY_DIR = \"%s\"" % tmp_bh_dir
25            self.append_config(buildhistory_dir_config)
26            self.track_for_cleanup(tmp_bh_dir)
27
28    def run_buildhistory_operation(self, target, global_config='', target_config='', change_bh_location=False, expect_error=False, error_regex=''):
29        if change_bh_location:
30            tmp_bh_location = True
31        else:
32            tmp_bh_location = False
33        self.config_buildhistory(tmp_bh_location)
34
35        self.append_config(global_config)
36        self.append_recipeinc(target, target_config)
37        bitbake("-cclean %s" % target)
38        result = bitbake(target, ignore_status=True)
39        self.remove_config(global_config)
40        self.remove_recipeinc(target, target_config)
41
42        if expect_error:
43            self.assertEqual(result.status, 1, msg="Error expected for global config '%s' and target config '%s'" % (global_config, target_config))
44            search_for_error = re.search(error_regex, result.output)
45            self.assertTrue(search_for_error, msg="Could not find desired error in output: %s (%s)" % (error_regex, result.output))
46        else:
47            self.assertEqual(result.status, 0, msg="Command 'bitbake %s' has failed unexpectedly: %s" % (target, result.output))
48
49    # No tests should be added to the base class.
50    # Please create a new class that inherit this one, or use one of those already available for adding tests.
51