Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 4 | # Copyright 2019-2020 by Garmin Ltd. or its subsidiaries |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 5 | |
| 6 | from oeqa.selftest.case import OESelftestTestCase |
| 7 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 8 | import bb.utils |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 9 | import functools |
| 10 | import multiprocessing |
| 11 | import textwrap |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 12 | import json |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 13 | import unittest |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 14 | import tempfile |
| 15 | import shutil |
| 16 | import stat |
| 17 | import os |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 18 | import datetime |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 19 | |
| 20 | MISSING = 'MISSING' |
| 21 | DIFFERENT = 'DIFFERENT' |
| 22 | SAME = 'SAME' |
| 23 | |
| 24 | @functools.total_ordering |
| 25 | class CompareResult(object): |
| 26 | def __init__(self): |
| 27 | self.reference = None |
| 28 | self.test = None |
| 29 | self.status = 'UNKNOWN' |
| 30 | |
| 31 | def __eq__(self, other): |
| 32 | return (self.status, self.test) == (other.status, other.test) |
| 33 | |
| 34 | def __lt__(self, other): |
| 35 | return (self.status, self.test) < (other.status, other.test) |
| 36 | |
| 37 | class PackageCompareResults(object): |
| 38 | def __init__(self): |
| 39 | self.total = [] |
| 40 | self.missing = [] |
| 41 | self.different = [] |
| 42 | self.same = [] |
| 43 | |
| 44 | def add_result(self, r): |
| 45 | self.total.append(r) |
| 46 | if r.status == MISSING: |
| 47 | self.missing.append(r) |
| 48 | elif r.status == DIFFERENT: |
| 49 | self.different.append(r) |
| 50 | else: |
| 51 | self.same.append(r) |
| 52 | |
| 53 | def sort(self): |
| 54 | self.total.sort() |
| 55 | self.missing.sort() |
| 56 | self.different.sort() |
| 57 | self.same.sort() |
| 58 | |
| 59 | def __str__(self): |
| 60 | return 'same=%i different=%i missing=%i total=%i' % (len(self.same), len(self.different), len(self.missing), len(self.total)) |
| 61 | |
| 62 | def compare_file(reference, test, diffutils_sysroot): |
| 63 | result = CompareResult() |
| 64 | result.reference = reference |
| 65 | result.test = test |
| 66 | |
| 67 | if not os.path.exists(reference): |
| 68 | result.status = MISSING |
| 69 | return result |
| 70 | |
| 71 | r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True) |
| 72 | |
| 73 | if r.status: |
| 74 | result.status = DIFFERENT |
| 75 | return result |
| 76 | |
| 77 | result.status = SAME |
| 78 | return result |
| 79 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 80 | def run_diffoscope(a_dir, b_dir, html_dir, **kwargs): |
| 81 | return runCmd(['diffoscope', '--no-default-limits', '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir], |
| 82 | **kwargs) |
| 83 | |
| 84 | class DiffoscopeTests(OESelftestTestCase): |
| 85 | diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope") |
| 86 | |
| 87 | def test_diffoscope(self): |
| 88 | bitbake("diffoscope-native -c addto_recipe_sysroot") |
| 89 | diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native") |
| 90 | |
| 91 | # Check that diffoscope doesn't return an error when the files compare |
| 92 | # the same (a general check that diffoscope is working) |
| 93 | with tempfile.TemporaryDirectory() as tmpdir: |
| 94 | run_diffoscope('A', 'A', tmpdir, |
| 95 | native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files) |
| 96 | |
| 97 | # Check that diffoscope generates an index.html file when the files are |
| 98 | # different |
| 99 | with tempfile.TemporaryDirectory() as tmpdir: |
| 100 | r = run_diffoscope('A', 'B', tmpdir, |
| 101 | native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files) |
| 102 | |
| 103 | self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected") |
| 104 | self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!") |
| 105 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 106 | class ReproducibleTests(OESelftestTestCase): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 107 | package_classes = ['deb', 'ipk'] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 108 | images = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline'] |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 109 | save_results = False |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 110 | if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ: |
| 111 | save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT'] |
| 112 | |
| 113 | # This variable controls if one of the test builds is allowed to pull from |
| 114 | # an sstate cache/mirror. The other build is always done clean as a point of |
| 115 | # comparison. |
| 116 | # If you know that your sstate archives are reproducible, enabling this |
| 117 | # will test that and also make the test run faster. If your sstate is not |
| 118 | # reproducible, disable this in your derived test class |
| 119 | build_from_sstate = True |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 120 | |
| 121 | def setUpLocal(self): |
| 122 | super().setUpLocal() |
| 123 | needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS'] |
| 124 | bb_vars = get_bb_vars(needed_vars) |
| 125 | for v in needed_vars: |
| 126 | setattr(self, v.lower(), bb_vars[v]) |
| 127 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 128 | self.extraresults = {} |
| 129 | self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = '' |
| 130 | self.extraresults.setdefault('reproducible', {}).setdefault('files', {}) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 131 | |
| 132 | def append_to_log(self, msg): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 133 | self.extraresults['reproducible.rawlogs']['log'] += msg |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 134 | |
| 135 | def compare_packages(self, reference_dir, test_dir, diffutils_sysroot): |
| 136 | result = PackageCompareResults() |
| 137 | |
| 138 | old_cwd = os.getcwd() |
| 139 | try: |
| 140 | file_result = {} |
| 141 | os.chdir(test_dir) |
| 142 | with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p: |
| 143 | for root, dirs, files in os.walk('.'): |
| 144 | async_result = [] |
| 145 | for f in files: |
| 146 | reference_path = os.path.join(reference_dir, root, f) |
| 147 | test_path = os.path.join(test_dir, root, f) |
| 148 | async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot))) |
| 149 | |
| 150 | for a in async_result: |
| 151 | result.add_result(a.get()) |
| 152 | |
| 153 | finally: |
| 154 | os.chdir(old_cwd) |
| 155 | |
| 156 | result.sort() |
| 157 | return result |
| 158 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 159 | def write_package_list(self, package_class, name, packages): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 160 | self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [ |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 161 | {'reference': p.reference, 'test': p.test} for p in packages] |
| 162 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 163 | def copy_file(self, source, dest): |
| 164 | bb.utils.mkdirhier(os.path.dirname(dest)) |
| 165 | shutil.copyfile(source, dest) |
| 166 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 167 | def do_test_build(self, name, use_sstate): |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 168 | capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes] |
| 169 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 170 | tmpdir = os.path.join(self.topdir, name, 'tmp') |
| 171 | if os.path.exists(tmpdir): |
| 172 | bb.utils.remove(tmpdir, recurse=True) |
| 173 | |
| 174 | config = textwrap.dedent('''\ |
| 175 | INHERIT += "reproducible_build" |
| 176 | PACKAGE_CLASSES = "{package_classes}" |
| 177 | INHIBIT_PACKAGE_STRIP = "1" |
| 178 | TMPDIR = "{tmpdir}" |
| 179 | ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes), |
| 180 | tmpdir=tmpdir) |
| 181 | |
| 182 | if not use_sstate: |
| 183 | # This config fragment will disable using shared and the sstate |
| 184 | # mirror, forcing a complete build from scratch |
| 185 | config += textwrap.dedent('''\ |
| 186 | SSTATE_DIR = "${TMPDIR}/sstate" |
| 187 | SSTATE_MIRROR = "" |
| 188 | ''') |
| 189 | |
| 190 | self.write_config(config) |
| 191 | d = get_bb_vars(capture_vars) |
| 192 | bitbake(' '.join(self.images)) |
| 193 | return d |
| 194 | |
| 195 | def test_reproducible_builds(self): |
| 196 | def strip_topdir(s): |
| 197 | if s.startswith(self.topdir): |
| 198 | return s[len(self.topdir):] |
| 199 | return s |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 200 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 201 | # Build native utilities |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 202 | self.write_config('') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 203 | bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot") |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 204 | diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 205 | diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native") |
| 206 | jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native") |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 207 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 208 | if self.save_results: |
| 209 | os.makedirs(self.save_results, exist_ok=True) |
| 210 | datestr = datetime.datetime.now().strftime('%Y%m%d') |
| 211 | save_dir = tempfile.mkdtemp(prefix='oe-reproducible-%s-' % datestr, dir=self.save_results) |
| 212 | os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) |
| 213 | self.logger.info('Non-reproducible packages will be copied to %s', save_dir) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 214 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 215 | vars_A = self.do_test_build('reproducibleA', self.build_from_sstate) |
| 216 | vars_B = self.do_test_build('reproducibleB', False) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 217 | |
| 218 | # NOTE: The temp directories from the reproducible build are purposely |
| 219 | # kept after the build so it can be diffed for debugging. |
| 220 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 221 | fails = [] |
| 222 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 223 | for c in self.package_classes: |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 224 | with self.subTest(package_class=c): |
| 225 | package_class = 'package_' + c |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 226 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 227 | deploy_A = vars_A['DEPLOY_DIR_' + c.upper()] |
| 228 | deploy_B = vars_B['DEPLOY_DIR_' + c.upper()] |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 229 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 230 | result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 231 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 232 | self.logger.info('Reproducibility summary for %s: %s' % (c, result)) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 233 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 234 | self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total)) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 235 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 236 | self.write_package_list(package_class, 'missing', result.missing) |
| 237 | self.write_package_list(package_class, 'different', result.different) |
| 238 | self.write_package_list(package_class, 'same', result.same) |
| 239 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 240 | if self.save_results: |
| 241 | for d in result.different: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 242 | self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)])) |
| 243 | self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)])) |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 244 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 245 | if result.missing or result.different: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 246 | fails.append("The following %s packages are missing or different: %s" % |
| 247 | (c, '\n'.join(r.test for r in (result.missing + result.different)))) |
| 248 | |
| 249 | # Clean up empty directories |
| 250 | if self.save_results: |
| 251 | if not os.listdir(save_dir): |
| 252 | os.rmdir(save_dir) |
| 253 | else: |
| 254 | self.logger.info('Running diffoscope') |
| 255 | package_dir = os.path.join(save_dir, 'packages') |
| 256 | package_html_dir = os.path.join(package_dir, 'diff-html') |
| 257 | |
| 258 | # Copy jquery to improve the diffoscope output usability |
| 259 | self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js')) |
| 260 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 261 | run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir, |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 262 | native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir) |
| 263 | |
| 264 | if fails: |
| 265 | self.fail('\n'.join(fails)) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 266 | |