blob: 0963c2f11a94c65e8174b9401d8a9f76857975f2 [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 Bishop79641f22019-09-10 07:20:22 -040012import json
Brad Bishop15ae2502019-06-18 21:44:24 -040013import unittest
Brad Bishop1d80a2e2019-11-15 16:35:03 -050014import tempfile
15import shutil
16import stat
17import os
Andrew Geissler82c905d2020-04-13 13:39:40 -050018import datetime
Brad Bishop15ae2502019-06-18 21:44:24 -040019
Andrew Geisslerd1e89492021-02-12 15:35:20 -060020# For sample packages, see:
21# https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20201127-0t7wr_oo/
22# https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20201127-4s9ejwyp/
23# https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20201127-haiwdlbr/
24# https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20201127-hwds3mcl/
25# https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20201203-sua0pzvc/
26# (both packages/ and packages-excluded/)
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060027
28# ruby-ri-docs, meson:
29#https://autobuilder.yocto.io/pub/repro-fail/oe-reproducible-20210215-0_td9la2/packages/diff-html/
Andrew Geisslerd1e89492021-02-12 15:35:20 -060030exclude_packages = [
Andrew Geisslerd1e89492021-02-12 15:35:20 -060031 'babeltrace2-ptest',
32 'bootchart2-doc',
33 'cups',
Andrew Geisslerd1e89492021-02-12 15:35:20 -060034 'efivar',
35 'epiphany',
36 'gcr',
37 'git',
38 'glide',
39 'go-dep',
40 'go-helloworld',
41 'go-runtime',
42 'go_',
43 'groff',
44 'gst-devtools',
45 'gstreamer1.0-python',
46 'gtk-doc',
47 'igt-gpu-tools',
Andrew Geisslerd1e89492021-02-12 15:35:20 -060048 'libaprutil',
49 'libcap-ng',
50 'libhandy-1-src',
51 'libid3tag',
52 'libproxy',
53 'libsecret-dev',
54 'libsecret-src',
55 'lttng-tools-dbg',
56 'lttng-tools-ptest',
57 'ltp',
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060058 'meson',
Andrew Geisslerd1e89492021-02-12 15:35:20 -060059 'ovmf-shell-efi',
60 'parted-ptest',
61 'perf',
62 'python3-cython',
63 'qemu',
Andrew Geisslerd1e89492021-02-12 15:35:20 -060064 'rsync',
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060065 'ruby-ri-docs',
Andrew Geisslerd1e89492021-02-12 15:35:20 -060066 'swig',
67 'syslinux-misc',
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060068 'systemd-bootchart'
Andrew Geisslerd1e89492021-02-12 15:35:20 -060069 ]
70
71def is_excluded(package):
72 package_name = os.path.basename(package)
73 for i in exclude_packages:
74 if package_name.startswith(i):
Andrew Geissler9b4d8b02021-02-19 12:26:16 -060075 return i
76 return None
Andrew Geisslerd1e89492021-02-12 15:35:20 -060077
Brad Bishop15ae2502019-06-18 21:44:24 -040078MISSING = 'MISSING'
79DIFFERENT = 'DIFFERENT'
80SAME = 'SAME'
81
82@functools.total_ordering
83class CompareResult(object):
84 def __init__(self):
85 self.reference = None
86 self.test = None
87 self.status = 'UNKNOWN'
88
89 def __eq__(self, other):
90 return (self.status, self.test) == (other.status, other.test)
91
92 def __lt__(self, other):
93 return (self.status, self.test) < (other.status, other.test)
94
95class PackageCompareResults(object):
96 def __init__(self):
97 self.total = []
98 self.missing = []
99 self.different = []
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600100 self.different_excluded = []
Brad Bishop15ae2502019-06-18 21:44:24 -0400101 self.same = []
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600102 self.active_exclusions = set()
Brad Bishop15ae2502019-06-18 21:44:24 -0400103
104 def add_result(self, r):
105 self.total.append(r)
106 if r.status == MISSING:
107 self.missing.append(r)
108 elif r.status == DIFFERENT:
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600109 exclusion = is_excluded(r.reference)
110 if exclusion:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600111 self.different_excluded.append(r)
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600112 self.active_exclusions.add(exclusion)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600113 else:
114 self.different.append(r)
Brad Bishop15ae2502019-06-18 21:44:24 -0400115 else:
116 self.same.append(r)
117
118 def sort(self):
119 self.total.sort()
120 self.missing.sort()
121 self.different.sort()
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600122 self.different_excluded.sort()
Brad Bishop15ae2502019-06-18 21:44:24 -0400123 self.same.sort()
124
125 def __str__(self):
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600126 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())
127
128 def unused_exclusions(self):
129 return sorted(set(exclude_packages) - self.active_exclusions)
Brad Bishop15ae2502019-06-18 21:44:24 -0400130
131def compare_file(reference, test, diffutils_sysroot):
132 result = CompareResult()
133 result.reference = reference
134 result.test = test
135
136 if not os.path.exists(reference):
137 result.status = MISSING
138 return result
139
140 r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True)
141
142 if r.status:
143 result.status = DIFFERENT
144 return result
145
146 result.status = SAME
147 return result
148
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500149def run_diffoscope(a_dir, b_dir, html_dir, **kwargs):
150 return runCmd(['diffoscope', '--no-default-limits', '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir],
151 **kwargs)
152
153class DiffoscopeTests(OESelftestTestCase):
154 diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope")
155
156 def test_diffoscope(self):
157 bitbake("diffoscope-native -c addto_recipe_sysroot")
158 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
159
160 # Check that diffoscope doesn't return an error when the files compare
161 # the same (a general check that diffoscope is working)
162 with tempfile.TemporaryDirectory() as tmpdir:
163 run_diffoscope('A', 'A', tmpdir,
164 native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files)
165
166 # Check that diffoscope generates an index.html file when the files are
167 # different
168 with tempfile.TemporaryDirectory() as tmpdir:
169 r = run_diffoscope('A', 'B', tmpdir,
170 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files)
171
172 self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected")
173 self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!")
174
Brad Bishop15ae2502019-06-18 21:44:24 -0400175class ReproducibleTests(OESelftestTestCase):
Brad Bishop00e122a2019-10-05 11:10:57 -0400176 package_classes = ['deb', 'ipk']
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600177 images = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline', 'world']
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500178 save_results = False
Andrew Geissler82c905d2020-04-13 13:39:40 -0500179 if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ:
180 save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT']
181
182 # This variable controls if one of the test builds is allowed to pull from
183 # an sstate cache/mirror. The other build is always done clean as a point of
184 # comparison.
185 # If you know that your sstate archives are reproducible, enabling this
186 # will test that and also make the test run faster. If your sstate is not
187 # reproducible, disable this in your derived test class
188 build_from_sstate = True
Brad Bishop15ae2502019-06-18 21:44:24 -0400189
190 def setUpLocal(self):
191 super().setUpLocal()
192 needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
193 bb_vars = get_bb_vars(needed_vars)
194 for v in needed_vars:
195 setattr(self, v.lower(), bb_vars[v])
196
Andrew Geissler82c905d2020-04-13 13:39:40 -0500197 self.extraresults = {}
198 self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
199 self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
Brad Bishop15ae2502019-06-18 21:44:24 -0400200
201 def append_to_log(self, msg):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500202 self.extraresults['reproducible.rawlogs']['log'] += msg
Brad Bishop15ae2502019-06-18 21:44:24 -0400203
204 def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
205 result = PackageCompareResults()
206
207 old_cwd = os.getcwd()
208 try:
209 file_result = {}
210 os.chdir(test_dir)
211 with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
212 for root, dirs, files in os.walk('.'):
213 async_result = []
214 for f in files:
215 reference_path = os.path.join(reference_dir, root, f)
216 test_path = os.path.join(test_dir, root, f)
217 async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
218
219 for a in async_result:
220 result.add_result(a.get())
221
222 finally:
223 os.chdir(old_cwd)
224
225 result.sort()
226 return result
227
Brad Bishop79641f22019-09-10 07:20:22 -0400228 def write_package_list(self, package_class, name, packages):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500229 self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
Brad Bishop79641f22019-09-10 07:20:22 -0400230 {'reference': p.reference, 'test': p.test} for p in packages]
231
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500232 def copy_file(self, source, dest):
233 bb.utils.mkdirhier(os.path.dirname(dest))
234 shutil.copyfile(source, dest)
235
Andrew Geissler82c905d2020-04-13 13:39:40 -0500236 def do_test_build(self, name, use_sstate):
Brad Bishop15ae2502019-06-18 21:44:24 -0400237 capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
238
Andrew Geissler82c905d2020-04-13 13:39:40 -0500239 tmpdir = os.path.join(self.topdir, name, 'tmp')
240 if os.path.exists(tmpdir):
241 bb.utils.remove(tmpdir, recurse=True)
242
243 config = textwrap.dedent('''\
244 INHERIT += "reproducible_build"
245 PACKAGE_CLASSES = "{package_classes}"
246 INHIBIT_PACKAGE_STRIP = "1"
247 TMPDIR = "{tmpdir}"
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600248 LICENSE_FLAGS_WHITELIST = "commercial"
249 DISTRO_FEATURES_append = ' systemd pam'
250 USERADDEXTENSION = "useradd-staticids"
251 USERADD_ERROR_DYNAMIC = "skip"
252 USERADD_UID_TABLES += "files/static-passwd"
253 USERADD_GID_TABLES += "files/static-group"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500254 ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes),
255 tmpdir=tmpdir)
256
257 if not use_sstate:
258 # This config fragment will disable using shared and the sstate
259 # mirror, forcing a complete build from scratch
260 config += textwrap.dedent('''\
261 SSTATE_DIR = "${TMPDIR}/sstate"
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600262 SSTATE_MIRRORS = ""
Andrew Geissler82c905d2020-04-13 13:39:40 -0500263 ''')
264
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600265 self.logger.info("Building %s (sstate%s allowed)..." % (name, '' if use_sstate else ' NOT'))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500266 self.write_config(config)
267 d = get_bb_vars(capture_vars)
268 bitbake(' '.join(self.images))
269 return d
270
271 def test_reproducible_builds(self):
272 def strip_topdir(s):
273 if s.startswith(self.topdir):
274 return s[len(self.topdir):]
275 return s
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500276
Brad Bishop15ae2502019-06-18 21:44:24 -0400277 # Build native utilities
Brad Bishop79641f22019-09-10 07:20:22 -0400278 self.write_config('')
Andrew Geissler82c905d2020-04-13 13:39:40 -0500279 bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400280 diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500281 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
282 jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native")
Brad Bishop15ae2502019-06-18 21:44:24 -0400283
Andrew Geissler82c905d2020-04-13 13:39:40 -0500284 if self.save_results:
285 os.makedirs(self.save_results, exist_ok=True)
286 datestr = datetime.datetime.now().strftime('%Y%m%d')
287 save_dir = tempfile.mkdtemp(prefix='oe-reproducible-%s-' % datestr, dir=self.save_results)
288 os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
289 self.logger.info('Non-reproducible packages will be copied to %s', save_dir)
Brad Bishop79641f22019-09-10 07:20:22 -0400290
Andrew Geissler82c905d2020-04-13 13:39:40 -0500291 vars_A = self.do_test_build('reproducibleA', self.build_from_sstate)
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600292
Andrew Geissler82c905d2020-04-13 13:39:40 -0500293 vars_B = self.do_test_build('reproducibleB', False)
Brad Bishop79641f22019-09-10 07:20:22 -0400294
295 # NOTE: The temp directories from the reproducible build are purposely
296 # kept after the build so it can be diffed for debugging.
297
Andrew Geissler82c905d2020-04-13 13:39:40 -0500298 fails = []
299
Brad Bishop15ae2502019-06-18 21:44:24 -0400300 for c in self.package_classes:
Brad Bishop79641f22019-09-10 07:20:22 -0400301 with self.subTest(package_class=c):
302 package_class = 'package_' + c
Brad Bishop15ae2502019-06-18 21:44:24 -0400303
Brad Bishop79641f22019-09-10 07:20:22 -0400304 deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
305 deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
Brad Bishop15ae2502019-06-18 21:44:24 -0400306
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600307 self.logger.info('Checking %s packages for differences...' % c)
Brad Bishop79641f22019-09-10 07:20:22 -0400308 result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
Brad Bishop15ae2502019-06-18 21:44:24 -0400309
Brad Bishop79641f22019-09-10 07:20:22 -0400310 self.logger.info('Reproducibility summary for %s: %s' % (c, result))
Brad Bishop15ae2502019-06-18 21:44:24 -0400311
Brad Bishop79641f22019-09-10 07:20:22 -0400312 self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400313
Brad Bishop79641f22019-09-10 07:20:22 -0400314 self.write_package_list(package_class, 'missing', result.missing)
315 self.write_package_list(package_class, 'different', result.different)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600316 self.write_package_list(package_class, 'different_excluded', result.different_excluded)
Brad Bishop79641f22019-09-10 07:20:22 -0400317 self.write_package_list(package_class, 'same', result.same)
318
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500319 if self.save_results:
320 for d in result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500321 self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)]))
322 self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)]))
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500323
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600324 for d in result.different_excluded:
325 self.copy_file(d.reference, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.reference)]))
326 self.copy_file(d.test, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.test)]))
327
Brad Bishop79641f22019-09-10 07:20:22 -0400328 if result.missing or result.different:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600329 fails.append("The following %s packages are missing or different and not in exclusion list: %s" %
Andrew Geissler82c905d2020-04-13 13:39:40 -0500330 (c, '\n'.join(r.test for r in (result.missing + result.different))))
331
332 # Clean up empty directories
333 if self.save_results:
334 if not os.listdir(save_dir):
335 os.rmdir(save_dir)
336 else:
337 self.logger.info('Running diffoscope')
338 package_dir = os.path.join(save_dir, 'packages')
339 package_html_dir = os.path.join(package_dir, 'diff-html')
340
341 # Copy jquery to improve the diffoscope output usability
342 self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js'))
343
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500344 run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500345 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
346
347 if fails:
348 self.fail('\n'.join(fails))
Brad Bishop15ae2502019-06-18 21:44:24 -0400349