xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/selftest/cases/reproducible.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# SPDX-License-Identifier: MIT
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# Copyright 2019-2020 by Garmin Ltd. or its subsidiaries
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunfrom oeqa.selftest.case import OESelftestTestCase
7*4882a593Smuzhiyunfrom oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
8*4882a593Smuzhiyunimport bb.utils
9*4882a593Smuzhiyunimport functools
10*4882a593Smuzhiyunimport multiprocessing
11*4882a593Smuzhiyunimport textwrap
12*4882a593Smuzhiyunimport tempfile
13*4882a593Smuzhiyunimport shutil
14*4882a593Smuzhiyunimport stat
15*4882a593Smuzhiyunimport os
16*4882a593Smuzhiyunimport datetime
17*4882a593Smuzhiyun
18*4882a593Smuzhiyunexclude_packages = [
19*4882a593Smuzhiyun	]
20*4882a593Smuzhiyun
21*4882a593Smuzhiyundef is_excluded(package):
22*4882a593Smuzhiyun    package_name = os.path.basename(package)
23*4882a593Smuzhiyun    for i in exclude_packages:
24*4882a593Smuzhiyun        if package_name.startswith(i):
25*4882a593Smuzhiyun            return i
26*4882a593Smuzhiyun    return None
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunMISSING = 'MISSING'
29*4882a593SmuzhiyunDIFFERENT = 'DIFFERENT'
30*4882a593SmuzhiyunSAME = 'SAME'
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun@functools.total_ordering
33*4882a593Smuzhiyunclass CompareResult(object):
34*4882a593Smuzhiyun    def __init__(self):
35*4882a593Smuzhiyun        self.reference = None
36*4882a593Smuzhiyun        self.test = None
37*4882a593Smuzhiyun        self.status = 'UNKNOWN'
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun    def __eq__(self, other):
40*4882a593Smuzhiyun        return (self.status, self.test) == (other.status, other.test)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun    def __lt__(self, other):
43*4882a593Smuzhiyun        return (self.status, self.test) < (other.status, other.test)
44*4882a593Smuzhiyun
45*4882a593Smuzhiyunclass PackageCompareResults(object):
46*4882a593Smuzhiyun    def __init__(self):
47*4882a593Smuzhiyun        self.total = []
48*4882a593Smuzhiyun        self.missing = []
49*4882a593Smuzhiyun        self.different = []
50*4882a593Smuzhiyun        self.different_excluded = []
51*4882a593Smuzhiyun        self.same = []
52*4882a593Smuzhiyun        self.active_exclusions = set()
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun    def add_result(self, r):
55*4882a593Smuzhiyun        self.total.append(r)
56*4882a593Smuzhiyun        if r.status == MISSING:
57*4882a593Smuzhiyun            self.missing.append(r)
58*4882a593Smuzhiyun        elif r.status == DIFFERENT:
59*4882a593Smuzhiyun            exclusion = is_excluded(r.reference)
60*4882a593Smuzhiyun            if exclusion:
61*4882a593Smuzhiyun                self.different_excluded.append(r)
62*4882a593Smuzhiyun                self.active_exclusions.add(exclusion)
63*4882a593Smuzhiyun            else:
64*4882a593Smuzhiyun                self.different.append(r)
65*4882a593Smuzhiyun        else:
66*4882a593Smuzhiyun            self.same.append(r)
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun    def sort(self):
69*4882a593Smuzhiyun        self.total.sort()
70*4882a593Smuzhiyun        self.missing.sort()
71*4882a593Smuzhiyun        self.different.sort()
72*4882a593Smuzhiyun        self.different_excluded.sort()
73*4882a593Smuzhiyun        self.same.sort()
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun    def __str__(self):
76*4882a593Smuzhiyun        return 'same=%i different=%i different_excluded=%i missing=%i total=%i\nunused_exclusions=%s' % (len(self.same), len(self.different), len(self.different_excluded), len(self.missing), len(self.total), self.unused_exclusions())
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun    def unused_exclusions(self):
79*4882a593Smuzhiyun        return sorted(set(exclude_packages) - self.active_exclusions)
80*4882a593Smuzhiyun
81*4882a593Smuzhiyundef compare_file(reference, test, diffutils_sysroot):
82*4882a593Smuzhiyun    result = CompareResult()
83*4882a593Smuzhiyun    result.reference = reference
84*4882a593Smuzhiyun    result.test = test
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun    if not os.path.exists(reference):
87*4882a593Smuzhiyun        result.status = MISSING
88*4882a593Smuzhiyun        return result
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun    r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True, sync=False)
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun    if r.status:
93*4882a593Smuzhiyun        result.status = DIFFERENT
94*4882a593Smuzhiyun        return result
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun    result.status = SAME
97*4882a593Smuzhiyun    return result
98*4882a593Smuzhiyun
99*4882a593Smuzhiyundef run_diffoscope(a_dir, b_dir, html_dir, max_report_size=0, **kwargs):
100*4882a593Smuzhiyun    return runCmd(['diffoscope', '--no-default-limits', '--max-report-size', str(max_report_size),
101*4882a593Smuzhiyun                   '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir],
102*4882a593Smuzhiyun                **kwargs)
103*4882a593Smuzhiyun
104*4882a593Smuzhiyunclass DiffoscopeTests(OESelftestTestCase):
105*4882a593Smuzhiyun    diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope")
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun    def test_diffoscope(self):
108*4882a593Smuzhiyun        bitbake("diffoscope-native -c addto_recipe_sysroot")
109*4882a593Smuzhiyun        diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun        # Check that diffoscope doesn't return an error when the files compare
112*4882a593Smuzhiyun        # the same (a general check that diffoscope is working)
113*4882a593Smuzhiyun        with tempfile.TemporaryDirectory() as tmpdir:
114*4882a593Smuzhiyun            run_diffoscope('A', 'A', tmpdir,
115*4882a593Smuzhiyun                native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files)
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun        # Check that diffoscope generates an index.html file when the files are
118*4882a593Smuzhiyun        # different
119*4882a593Smuzhiyun        with tempfile.TemporaryDirectory() as tmpdir:
120*4882a593Smuzhiyun            r = run_diffoscope('A', 'B', tmpdir,
121*4882a593Smuzhiyun                native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files)
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun            self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected")
124*4882a593Smuzhiyun            self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!")
125*4882a593Smuzhiyun
126*4882a593Smuzhiyunclass ReproducibleTests(OESelftestTestCase):
127*4882a593Smuzhiyun    # Test the reproducibility of whatever is built between sstate_targets and targets
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun    package_classes = ['deb', 'ipk', 'rpm']
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun    # Maximum report size, in bytes
132*4882a593Smuzhiyun    max_report_size = 250 * 1024 * 1024
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun    # targets are the things we want to test the reproducibility of
135*4882a593Smuzhiyun    targets = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline', 'core-image-weston', 'world']
136*4882a593Smuzhiyun    # sstate targets are things to pull from sstate to potentially cut build/debugging time
137*4882a593Smuzhiyun    sstate_targets = []
138*4882a593Smuzhiyun    save_results = False
139*4882a593Smuzhiyun    if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ:
140*4882a593Smuzhiyun        save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT']
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun    # This variable controls if one of the test builds is allowed to pull from
143*4882a593Smuzhiyun    # an sstate cache/mirror. The other build is always done clean as a point of
144*4882a593Smuzhiyun    # comparison.
145*4882a593Smuzhiyun    # If you know that your sstate archives are reproducible, enabling this
146*4882a593Smuzhiyun    # will test that and also make the test run faster. If your sstate is not
147*4882a593Smuzhiyun    # reproducible, disable this in your derived test class
148*4882a593Smuzhiyun    build_from_sstate = True
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun    def setUpLocal(self):
151*4882a593Smuzhiyun        super().setUpLocal()
152*4882a593Smuzhiyun        needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
153*4882a593Smuzhiyun        bb_vars = get_bb_vars(needed_vars)
154*4882a593Smuzhiyun        for v in needed_vars:
155*4882a593Smuzhiyun            setattr(self, v.lower(), bb_vars[v])
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun        self.extraresults = {}
158*4882a593Smuzhiyun        self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
159*4882a593Smuzhiyun        self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun    def append_to_log(self, msg):
162*4882a593Smuzhiyun        self.extraresults['reproducible.rawlogs']['log'] += msg
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun    def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
165*4882a593Smuzhiyun        result = PackageCompareResults()
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun        old_cwd = os.getcwd()
168*4882a593Smuzhiyun        try:
169*4882a593Smuzhiyun            file_result = {}
170*4882a593Smuzhiyun            os.chdir(test_dir)
171*4882a593Smuzhiyun            with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
172*4882a593Smuzhiyun                for root, dirs, files in os.walk('.'):
173*4882a593Smuzhiyun                    async_result = []
174*4882a593Smuzhiyun                    for f in files:
175*4882a593Smuzhiyun                        reference_path = os.path.join(reference_dir, root, f)
176*4882a593Smuzhiyun                        test_path = os.path.join(test_dir, root, f)
177*4882a593Smuzhiyun                        async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun                    for a in async_result:
180*4882a593Smuzhiyun                        result.add_result(a.get())
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun        finally:
183*4882a593Smuzhiyun            os.chdir(old_cwd)
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun        result.sort()
186*4882a593Smuzhiyun        return result
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun    def write_package_list(self, package_class, name, packages):
189*4882a593Smuzhiyun        self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
190*4882a593Smuzhiyun                {'reference': p.reference, 'test': p.test} for p in packages]
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun    def copy_file(self, source, dest):
193*4882a593Smuzhiyun        bb.utils.mkdirhier(os.path.dirname(dest))
194*4882a593Smuzhiyun        shutil.copyfile(source, dest)
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun    def do_test_build(self, name, use_sstate):
197*4882a593Smuzhiyun        capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun        tmpdir = os.path.join(self.topdir, name, 'tmp')
200*4882a593Smuzhiyun        if os.path.exists(tmpdir):
201*4882a593Smuzhiyun            bb.utils.remove(tmpdir, recurse=True)
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun        config = textwrap.dedent('''\
204*4882a593Smuzhiyun            PACKAGE_CLASSES = "{package_classes}"
205*4882a593Smuzhiyun            INHIBIT_PACKAGE_STRIP = "1"
206*4882a593Smuzhiyun            TMPDIR = "{tmpdir}"
207*4882a593Smuzhiyun            LICENSE_FLAGS_ACCEPTED = "commercial"
208*4882a593Smuzhiyun            DISTRO_FEATURES:append = ' systemd pam'
209*4882a593Smuzhiyun            USERADDEXTENSION = "useradd-staticids"
210*4882a593Smuzhiyun            USERADD_ERROR_DYNAMIC = "skip"
211*4882a593Smuzhiyun            USERADD_UID_TABLES += "files/static-passwd"
212*4882a593Smuzhiyun            USERADD_GID_TABLES += "files/static-group"
213*4882a593Smuzhiyun            ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes),
214*4882a593Smuzhiyun                        tmpdir=tmpdir)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun        if not use_sstate:
217*4882a593Smuzhiyun            if self.sstate_targets:
218*4882a593Smuzhiyun               self.logger.info("Building prebuild for %s (sstate allowed)..." % (name))
219*4882a593Smuzhiyun               self.write_config(config)
220*4882a593Smuzhiyun               bitbake(' '.join(self.sstate_targets))
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun            # This config fragment will disable using shared and the sstate
223*4882a593Smuzhiyun            # mirror, forcing a complete build from scratch
224*4882a593Smuzhiyun            config += textwrap.dedent('''\
225*4882a593Smuzhiyun                SSTATE_DIR = "${TMPDIR}/sstate"
226*4882a593Smuzhiyun                SSTATE_MIRRORS = ""
227*4882a593Smuzhiyun                ''')
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun        self.logger.info("Building %s (sstate%s allowed)..." % (name, '' if use_sstate else ' NOT'))
230*4882a593Smuzhiyun        self.write_config(config)
231*4882a593Smuzhiyun        d = get_bb_vars(capture_vars)
232*4882a593Smuzhiyun        # targets used to be called images
233*4882a593Smuzhiyun        bitbake(' '.join(getattr(self, 'images', self.targets)))
234*4882a593Smuzhiyun        return d
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun    def test_reproducible_builds(self):
237*4882a593Smuzhiyun        def strip_topdir(s):
238*4882a593Smuzhiyun            if s.startswith(self.topdir):
239*4882a593Smuzhiyun                return s[len(self.topdir):]
240*4882a593Smuzhiyun            return s
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun        # Build native utilities
243*4882a593Smuzhiyun        self.write_config('')
244*4882a593Smuzhiyun        bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot")
245*4882a593Smuzhiyun        diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
246*4882a593Smuzhiyun        diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
247*4882a593Smuzhiyun        jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native")
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun        if self.save_results:
250*4882a593Smuzhiyun            os.makedirs(self.save_results, exist_ok=True)
251*4882a593Smuzhiyun            datestr = datetime.datetime.now().strftime('%Y%m%d')
252*4882a593Smuzhiyun            save_dir = tempfile.mkdtemp(prefix='oe-reproducible-%s-' % datestr, dir=self.save_results)
253*4882a593Smuzhiyun            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
254*4882a593Smuzhiyun            self.logger.info('Non-reproducible packages will be copied to %s', save_dir)
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun        vars_A = self.do_test_build('reproducibleA', self.build_from_sstate)
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun        vars_B = self.do_test_build('reproducibleB', False)
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun        # NOTE: The temp directories from the reproducible build are purposely
261*4882a593Smuzhiyun        # kept after the build so it can be diffed for debugging.
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun        fails = []
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun        for c in self.package_classes:
266*4882a593Smuzhiyun            with self.subTest(package_class=c):
267*4882a593Smuzhiyun                package_class = 'package_' + c
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun                deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
270*4882a593Smuzhiyun                deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun                self.logger.info('Checking %s packages for differences...' % c)
273*4882a593Smuzhiyun                result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun                self.logger.info('Reproducibility summary for %s: %s' % (c, result))
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun                self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun                self.write_package_list(package_class, 'missing', result.missing)
280*4882a593Smuzhiyun                self.write_package_list(package_class, 'different', result.different)
281*4882a593Smuzhiyun                self.write_package_list(package_class, 'different_excluded', result.different_excluded)
282*4882a593Smuzhiyun                self.write_package_list(package_class, 'same', result.same)
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun                if self.save_results:
285*4882a593Smuzhiyun                    for d in result.different:
286*4882a593Smuzhiyun                        self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)]))
287*4882a593Smuzhiyun                        self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)]))
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun                    for d in result.different_excluded:
290*4882a593Smuzhiyun                        self.copy_file(d.reference, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.reference)]))
291*4882a593Smuzhiyun                        self.copy_file(d.test, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.test)]))
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun                if result.missing or result.different:
294*4882a593Smuzhiyun                    fails.append("The following %s packages are missing or different and not in exclusion list: %s" %
295*4882a593Smuzhiyun                            (c, '\n'.join(r.test for r in (result.missing + result.different))))
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun        # Clean up empty directories
298*4882a593Smuzhiyun        if self.save_results:
299*4882a593Smuzhiyun            if not os.listdir(save_dir):
300*4882a593Smuzhiyun                os.rmdir(save_dir)
301*4882a593Smuzhiyun            else:
302*4882a593Smuzhiyun                self.logger.info('Running diffoscope')
303*4882a593Smuzhiyun                package_dir = os.path.join(save_dir, 'packages')
304*4882a593Smuzhiyun                package_html_dir = os.path.join(package_dir, 'diff-html')
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun                # Copy jquery to improve the diffoscope output usability
307*4882a593Smuzhiyun                self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js'))
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun                run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir, max_report_size=self.max_report_size,
310*4882a593Smuzhiyun                        native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun        if fails:
313*4882a593Smuzhiyun            self.fail('\n'.join(fails))
314*4882a593Smuzhiyun
315