blob: f4dd779842a3dd56b1f22482c9c90316f8d1f1db [file] [log] [blame]
Brad Bishop15ae2502019-06-18 21:44:24 -04001#
2# SPDX-License-Identifier: MIT
3#
Andrew Geissler82c905d2020-04-13 13:39:40 -05004# Copyright 2019-2020 by Garmin Ltd. or its subsidiaries
Brad Bishop15ae2502019-06-18 21:44:24 -04005
6from oeqa.selftest.case import OESelftestTestCase
7from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
Brad Bishop1d80a2e2019-11-15 16:35:03 -05008import bb.utils
Brad Bishop15ae2502019-06-18 21:44:24 -04009import functools
10import multiprocessing
11import textwrap
Brad Bishop1d80a2e2019-11-15 16:35:03 -050012import tempfile
13import shutil
14import stat
15import os
Andrew Geissler82c905d2020-04-13 13:39:40 -050016import datetime
Brad Bishop15ae2502019-06-18 21:44:24 -040017
Andrew Geisslerd1e89492021-02-12 15:35:20 -060018exclude_packages = [
Patrick Williams92b42cb2022-09-03 06:53:57 -050019 'rust',
20 'rust-dbg'
Andrew Geisslerd1e89492021-02-12 15:35:20 -060021 ]
22
23def is_excluded(package):
24 package_name = os.path.basename(package)
25 for i in exclude_packages:
26 if package_name.startswith(i):
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060027 return i
28 return None
Andrew Geisslerd1e89492021-02-12 15:35:20 -060029
Brad Bishop15ae2502019-06-18 21:44:24 -040030MISSING = 'MISSING'
31DIFFERENT = 'DIFFERENT'
32SAME = 'SAME'
33
34@functools.total_ordering
35class CompareResult(object):
36 def __init__(self):
37 self.reference = None
38 self.test = None
39 self.status = 'UNKNOWN'
40
41 def __eq__(self, other):
42 return (self.status, self.test) == (other.status, other.test)
43
44 def __lt__(self, other):
45 return (self.status, self.test) < (other.status, other.test)
46
47class PackageCompareResults(object):
48 def __init__(self):
49 self.total = []
50 self.missing = []
51 self.different = []
Andrew Geisslerd1e89492021-02-12 15:35:20 -060052 self.different_excluded = []
Brad Bishop15ae2502019-06-18 21:44:24 -040053 self.same = []
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060054 self.active_exclusions = set()
Brad Bishop15ae2502019-06-18 21:44:24 -040055
56 def add_result(self, r):
57 self.total.append(r)
58 if r.status == MISSING:
59 self.missing.append(r)
60 elif r.status == DIFFERENT:
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060061 exclusion = is_excluded(r.reference)
62 if exclusion:
Andrew Geisslerd1e89492021-02-12 15:35:20 -060063 self.different_excluded.append(r)
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060064 self.active_exclusions.add(exclusion)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060065 else:
66 self.different.append(r)
Brad Bishop15ae2502019-06-18 21:44:24 -040067 else:
68 self.same.append(r)
69
70 def sort(self):
71 self.total.sort()
72 self.missing.sort()
73 self.different.sort()
Andrew Geisslerd1e89492021-02-12 15:35:20 -060074 self.different_excluded.sort()
Brad Bishop15ae2502019-06-18 21:44:24 -040075 self.same.sort()
76
77 def __str__(self):
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060078 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())
79
80 def unused_exclusions(self):
81 return sorted(set(exclude_packages) - self.active_exclusions)
Brad Bishop15ae2502019-06-18 21:44:24 -040082
83def compare_file(reference, test, diffutils_sysroot):
84 result = CompareResult()
85 result.reference = reference
86 result.test = test
87
88 if not os.path.exists(reference):
89 result.status = MISSING
90 return result
91
Andrew Geissler90fd73c2021-03-05 15:25:55 -060092 r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True, sync=False)
Brad Bishop15ae2502019-06-18 21:44:24 -040093
94 if r.status:
95 result.status = DIFFERENT
96 return result
97
98 result.status = SAME
99 return result
100
Andrew Geissler595f6302022-01-24 19:11:47 +0000101def run_diffoscope(a_dir, b_dir, html_dir, max_report_size=0, **kwargs):
102 return runCmd(['diffoscope', '--no-default-limits', '--max-report-size', str(max_report_size),
103 '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir],
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500104 **kwargs)
105
106class DiffoscopeTests(OESelftestTestCase):
107 diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope")
108
109 def test_diffoscope(self):
110 bitbake("diffoscope-native -c addto_recipe_sysroot")
111 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
112
113 # Check that diffoscope doesn't return an error when the files compare
114 # the same (a general check that diffoscope is working)
115 with tempfile.TemporaryDirectory() as tmpdir:
116 run_diffoscope('A', 'A', tmpdir,
117 native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files)
118
119 # Check that diffoscope generates an index.html file when the files are
120 # different
121 with tempfile.TemporaryDirectory() as tmpdir:
122 r = run_diffoscope('A', 'B', tmpdir,
123 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files)
124
125 self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected")
126 self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!")
127
Brad Bishop15ae2502019-06-18 21:44:24 -0400128class ReproducibleTests(OESelftestTestCase):
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600129 # Test the reproducibility of whatever is built between sstate_targets and targets
130
131 package_classes = ['deb', 'ipk', 'rpm']
132
Andrew Geissler595f6302022-01-24 19:11:47 +0000133 # Maximum report size, in bytes
134 max_report_size = 250 * 1024 * 1024
135
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600136 # targets are the things we want to test the reproducibility of
137 targets = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline', 'core-image-weston', 'world']
138 # sstate targets are things to pull from sstate to potentially cut build/debugging time
139 sstate_targets = []
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500140 save_results = False
Andrew Geissler82c905d2020-04-13 13:39:40 -0500141 if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ:
142 save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT']
143
144 # This variable controls if one of the test builds is allowed to pull from
145 # an sstate cache/mirror. The other build is always done clean as a point of
146 # comparison.
147 # If you know that your sstate archives are reproducible, enabling this
148 # will test that and also make the test run faster. If your sstate is not
149 # reproducible, disable this in your derived test class
150 build_from_sstate = True
Brad Bishop15ae2502019-06-18 21:44:24 -0400151
152 def setUpLocal(self):
153 super().setUpLocal()
154 needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
155 bb_vars = get_bb_vars(needed_vars)
156 for v in needed_vars:
157 setattr(self, v.lower(), bb_vars[v])
158
Andrew Geissler82c905d2020-04-13 13:39:40 -0500159 self.extraresults = {}
160 self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
161 self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
Brad Bishop15ae2502019-06-18 21:44:24 -0400162
163 def append_to_log(self, msg):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500164 self.extraresults['reproducible.rawlogs']['log'] += msg
Brad Bishop15ae2502019-06-18 21:44:24 -0400165
166 def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
167 result = PackageCompareResults()
168
169 old_cwd = os.getcwd()
170 try:
171 file_result = {}
172 os.chdir(test_dir)
173 with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
174 for root, dirs, files in os.walk('.'):
175 async_result = []
176 for f in files:
177 reference_path = os.path.join(reference_dir, root, f)
178 test_path = os.path.join(test_dir, root, f)
179 async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
180
181 for a in async_result:
182 result.add_result(a.get())
183
184 finally:
185 os.chdir(old_cwd)
186
187 result.sort()
188 return result
189
Brad Bishop79641f22019-09-10 07:20:22 -0400190 def write_package_list(self, package_class, name, packages):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500191 self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
Brad Bishop79641f22019-09-10 07:20:22 -0400192 {'reference': p.reference, 'test': p.test} for p in packages]
193
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500194 def copy_file(self, source, dest):
195 bb.utils.mkdirhier(os.path.dirname(dest))
196 shutil.copyfile(source, dest)
197
Andrew Geissler82c905d2020-04-13 13:39:40 -0500198 def do_test_build(self, name, use_sstate):
Brad Bishop15ae2502019-06-18 21:44:24 -0400199 capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
200
Andrew Geissler82c905d2020-04-13 13:39:40 -0500201 tmpdir = os.path.join(self.topdir, name, 'tmp')
202 if os.path.exists(tmpdir):
203 bb.utils.remove(tmpdir, recurse=True)
204
205 config = textwrap.dedent('''\
Andrew Geissler82c905d2020-04-13 13:39:40 -0500206 PACKAGE_CLASSES = "{package_classes}"
207 INHIBIT_PACKAGE_STRIP = "1"
208 TMPDIR = "{tmpdir}"
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000209 LICENSE_FLAGS_ACCEPTED = "commercial"
Patrick Williams213cb262021-08-07 19:21:33 -0500210 DISTRO_FEATURES:append = ' systemd pam'
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600211 USERADDEXTENSION = "useradd-staticids"
212 USERADD_ERROR_DYNAMIC = "skip"
213 USERADD_UID_TABLES += "files/static-passwd"
214 USERADD_GID_TABLES += "files/static-group"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500215 ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes),
216 tmpdir=tmpdir)
217
218 if not use_sstate:
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600219 if self.sstate_targets:
220 self.logger.info("Building prebuild for %s (sstate allowed)..." % (name))
221 self.write_config(config)
222 bitbake(' '.join(self.sstate_targets))
223
Andrew Geissler82c905d2020-04-13 13:39:40 -0500224 # This config fragment will disable using shared and the sstate
225 # mirror, forcing a complete build from scratch
226 config += textwrap.dedent('''\
227 SSTATE_DIR = "${TMPDIR}/sstate"
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600228 SSTATE_MIRRORS = ""
Andrew Geissler82c905d2020-04-13 13:39:40 -0500229 ''')
230
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600231 self.logger.info("Building %s (sstate%s allowed)..." % (name, '' if use_sstate else ' NOT'))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500232 self.write_config(config)
233 d = get_bb_vars(capture_vars)
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600234 # targets used to be called images
235 bitbake(' '.join(getattr(self, 'images', self.targets)))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500236 return d
237
238 def test_reproducible_builds(self):
239 def strip_topdir(s):
240 if s.startswith(self.topdir):
241 return s[len(self.topdir):]
242 return s
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500243
Brad Bishop15ae2502019-06-18 21:44:24 -0400244 # Build native utilities
Brad Bishop79641f22019-09-10 07:20:22 -0400245 self.write_config('')
Andrew Geissler82c905d2020-04-13 13:39:40 -0500246 bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400247 diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500248 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
249 jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native")
Brad Bishop15ae2502019-06-18 21:44:24 -0400250
Andrew Geissler82c905d2020-04-13 13:39:40 -0500251 if self.save_results:
252 os.makedirs(self.save_results, exist_ok=True)
253 datestr = datetime.datetime.now().strftime('%Y%m%d')
254 save_dir = tempfile.mkdtemp(prefix='oe-reproducible-%s-' % datestr, dir=self.save_results)
255 os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
256 self.logger.info('Non-reproducible packages will be copied to %s', save_dir)
Brad Bishop79641f22019-09-10 07:20:22 -0400257
Andrew Geissler82c905d2020-04-13 13:39:40 -0500258 vars_A = self.do_test_build('reproducibleA', self.build_from_sstate)
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600259
Andrew Geissler82c905d2020-04-13 13:39:40 -0500260 vars_B = self.do_test_build('reproducibleB', False)
Brad Bishop79641f22019-09-10 07:20:22 -0400261
262 # NOTE: The temp directories from the reproducible build are purposely
263 # kept after the build so it can be diffed for debugging.
264
Andrew Geissler82c905d2020-04-13 13:39:40 -0500265 fails = []
266
Brad Bishop15ae2502019-06-18 21:44:24 -0400267 for c in self.package_classes:
Brad Bishop79641f22019-09-10 07:20:22 -0400268 with self.subTest(package_class=c):
269 package_class = 'package_' + c
Brad Bishop15ae2502019-06-18 21:44:24 -0400270
Brad Bishop79641f22019-09-10 07:20:22 -0400271 deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
272 deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
Brad Bishop15ae2502019-06-18 21:44:24 -0400273
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600274 self.logger.info('Checking %s packages for differences...' % c)
Brad Bishop79641f22019-09-10 07:20:22 -0400275 result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
Brad Bishop15ae2502019-06-18 21:44:24 -0400276
Brad Bishop79641f22019-09-10 07:20:22 -0400277 self.logger.info('Reproducibility summary for %s: %s' % (c, result))
Brad Bishop15ae2502019-06-18 21:44:24 -0400278
Brad Bishop79641f22019-09-10 07:20:22 -0400279 self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400280
Brad Bishop79641f22019-09-10 07:20:22 -0400281 self.write_package_list(package_class, 'missing', result.missing)
282 self.write_package_list(package_class, 'different', result.different)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600283 self.write_package_list(package_class, 'different_excluded', result.different_excluded)
Brad Bishop79641f22019-09-10 07:20:22 -0400284 self.write_package_list(package_class, 'same', result.same)
285
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500286 if self.save_results:
287 for d in result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500288 self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)]))
289 self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)]))
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500290
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600291 for d in result.different_excluded:
292 self.copy_file(d.reference, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.reference)]))
293 self.copy_file(d.test, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.test)]))
294
Brad Bishop79641f22019-09-10 07:20:22 -0400295 if result.missing or result.different:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600296 fails.append("The following %s packages are missing or different and not in exclusion list: %s" %
Andrew Geissler82c905d2020-04-13 13:39:40 -0500297 (c, '\n'.join(r.test for r in (result.missing + result.different))))
298
299 # Clean up empty directories
300 if self.save_results:
301 if not os.listdir(save_dir):
302 os.rmdir(save_dir)
303 else:
304 self.logger.info('Running diffoscope')
305 package_dir = os.path.join(save_dir, 'packages')
306 package_html_dir = os.path.join(package_dir, 'diff-html')
307
308 # Copy jquery to improve the diffoscope output usability
309 self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js'))
310
Andrew Geissler595f6302022-01-24 19:11:47 +0000311 run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir, max_report_size=self.max_report_size,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500312 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
313
314 if fails:
315 self.fail('\n'.join(fails))
Brad Bishop15ae2502019-06-18 21:44:24 -0400316