blob: eee494e5cc8034539e5f1b50235de398611abd23 [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 Geisslerc723b722021-01-08 16:14:09 -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/)
27exclude_packages = [
28 'acpica-src',
29 'babeltrace2-ptest',
30 'bootchart2-doc',
31 'cups',
32 'cwautomacros',
33 'dtc',
34 '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',
48 'kernel-devsrc',
49 'libaprutil',
50 'libcap-ng',
51 'libhandy-1-src',
52 'libid3tag',
53 'libproxy',
54 'libsecret-dev',
55 'libsecret-src',
56 'lttng-tools-dbg',
57 'lttng-tools-ptest',
58 'ltp',
59 'meson',
60 'ovmf-shell-efi',
61 'parted-ptest',
62 'perf',
63 'python3-cython',
64 'qemu',
65 'quilt-ptest',
66 'rsync',
67 'ruby',
68 'spirv-tools-dev',
69 'swig',
70 'syslinux-misc',
71 'systemd-bootchart',
72 'valgrind-ptest',
73 'vim',
74 'watchdog',
75 'xmlto',
76 'xorg-minimal-fonts'
77 ]
78
79def is_excluded(package):
80 package_name = os.path.basename(package)
81 for i in exclude_packages:
82 if package_name.startswith(i):
83 return True
84 return False
85
Brad Bishop15ae2502019-06-18 21:44:24 -040086MISSING = 'MISSING'
87DIFFERENT = 'DIFFERENT'
88SAME = 'SAME'
89
90@functools.total_ordering
91class CompareResult(object):
92 def __init__(self):
93 self.reference = None
94 self.test = None
95 self.status = 'UNKNOWN'
96
97 def __eq__(self, other):
98 return (self.status, self.test) == (other.status, other.test)
99
100 def __lt__(self, other):
101 return (self.status, self.test) < (other.status, other.test)
102
103class PackageCompareResults(object):
104 def __init__(self):
105 self.total = []
106 self.missing = []
107 self.different = []
Andrew Geisslerc723b722021-01-08 16:14:09 -0600108 self.different_excluded = []
Brad Bishop15ae2502019-06-18 21:44:24 -0400109 self.same = []
110
111 def add_result(self, r):
112 self.total.append(r)
113 if r.status == MISSING:
114 self.missing.append(r)
115 elif r.status == DIFFERENT:
Andrew Geisslerc723b722021-01-08 16:14:09 -0600116 if is_excluded(r.reference):
117 self.different_excluded.append(r)
118 else:
119 self.different.append(r)
Brad Bishop15ae2502019-06-18 21:44:24 -0400120 else:
121 self.same.append(r)
122
123 def sort(self):
124 self.total.sort()
125 self.missing.sort()
126 self.different.sort()
Andrew Geisslerc723b722021-01-08 16:14:09 -0600127 self.different_excluded.sort()
Brad Bishop15ae2502019-06-18 21:44:24 -0400128 self.same.sort()
129
130 def __str__(self):
Andrew Geisslerc723b722021-01-08 16:14:09 -0600131 return 'same=%i different=%i different_excluded=%i missing=%i total=%i' % (len(self.same), len(self.different), len(self.different_excluded), len(self.missing), len(self.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400132
133def compare_file(reference, test, diffutils_sysroot):
134 result = CompareResult()
135 result.reference = reference
136 result.test = test
137
138 if not os.path.exists(reference):
139 result.status = MISSING
140 return result
141
142 r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True)
143
144 if r.status:
145 result.status = DIFFERENT
146 return result
147
148 result.status = SAME
149 return result
150
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500151def run_diffoscope(a_dir, b_dir, html_dir, **kwargs):
152 return runCmd(['diffoscope', '--no-default-limits', '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir],
153 **kwargs)
154
155class DiffoscopeTests(OESelftestTestCase):
156 diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope")
157
158 def test_diffoscope(self):
159 bitbake("diffoscope-native -c addto_recipe_sysroot")
160 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
161
162 # Check that diffoscope doesn't return an error when the files compare
163 # the same (a general check that diffoscope is working)
164 with tempfile.TemporaryDirectory() as tmpdir:
165 run_diffoscope('A', 'A', tmpdir,
166 native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files)
167
168 # Check that diffoscope generates an index.html file when the files are
169 # different
170 with tempfile.TemporaryDirectory() as tmpdir:
171 r = run_diffoscope('A', 'B', tmpdir,
172 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files)
173
174 self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected")
175 self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!")
176
Brad Bishop15ae2502019-06-18 21:44:24 -0400177class ReproducibleTests(OESelftestTestCase):
Brad Bishop00e122a2019-10-05 11:10:57 -0400178 package_classes = ['deb', 'ipk']
Andrew Geisslerc723b722021-01-08 16:14:09 -0600179 images = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline', 'world']
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500180 save_results = False
Andrew Geissler82c905d2020-04-13 13:39:40 -0500181 if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ:
182 save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT']
183
184 # This variable controls if one of the test builds is allowed to pull from
185 # an sstate cache/mirror. The other build is always done clean as a point of
186 # comparison.
187 # If you know that your sstate archives are reproducible, enabling this
188 # will test that and also make the test run faster. If your sstate is not
189 # reproducible, disable this in your derived test class
190 build_from_sstate = True
Brad Bishop15ae2502019-06-18 21:44:24 -0400191
192 def setUpLocal(self):
193 super().setUpLocal()
194 needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
195 bb_vars = get_bb_vars(needed_vars)
196 for v in needed_vars:
197 setattr(self, v.lower(), bb_vars[v])
198
Andrew Geissler82c905d2020-04-13 13:39:40 -0500199 self.extraresults = {}
200 self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
201 self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
Brad Bishop15ae2502019-06-18 21:44:24 -0400202
203 def append_to_log(self, msg):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500204 self.extraresults['reproducible.rawlogs']['log'] += msg
Brad Bishop15ae2502019-06-18 21:44:24 -0400205
206 def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
207 result = PackageCompareResults()
208
209 old_cwd = os.getcwd()
210 try:
211 file_result = {}
212 os.chdir(test_dir)
213 with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
214 for root, dirs, files in os.walk('.'):
215 async_result = []
216 for f in files:
217 reference_path = os.path.join(reference_dir, root, f)
218 test_path = os.path.join(test_dir, root, f)
219 async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
220
221 for a in async_result:
222 result.add_result(a.get())
223
224 finally:
225 os.chdir(old_cwd)
226
227 result.sort()
228 return result
229
Brad Bishop79641f22019-09-10 07:20:22 -0400230 def write_package_list(self, package_class, name, packages):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500231 self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
Brad Bishop79641f22019-09-10 07:20:22 -0400232 {'reference': p.reference, 'test': p.test} for p in packages]
233
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500234 def copy_file(self, source, dest):
235 bb.utils.mkdirhier(os.path.dirname(dest))
236 shutil.copyfile(source, dest)
237
Andrew Geissler82c905d2020-04-13 13:39:40 -0500238 def do_test_build(self, name, use_sstate):
Brad Bishop15ae2502019-06-18 21:44:24 -0400239 capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
240
Andrew Geissler82c905d2020-04-13 13:39:40 -0500241 tmpdir = os.path.join(self.topdir, name, 'tmp')
242 if os.path.exists(tmpdir):
243 bb.utils.remove(tmpdir, recurse=True)
244
245 config = textwrap.dedent('''\
246 INHERIT += "reproducible_build"
247 PACKAGE_CLASSES = "{package_classes}"
248 INHIBIT_PACKAGE_STRIP = "1"
249 TMPDIR = "{tmpdir}"
Andrew Geisslerc723b722021-01-08 16:14:09 -0600250 LICENSE_FLAGS_WHITELIST = "commercial"
251 DISTRO_FEATURES_append = ' systemd pam'
252 USERADDEXTENSION = "useradd-staticids"
253 USERADD_ERROR_DYNAMIC = "skip"
254 USERADD_UID_TABLES += "files/static-passwd"
255 USERADD_GID_TABLES += "files/static-group"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500256 ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes),
257 tmpdir=tmpdir)
258
259 if not use_sstate:
260 # This config fragment will disable using shared and the sstate
261 # mirror, forcing a complete build from scratch
262 config += textwrap.dedent('''\
263 SSTATE_DIR = "${TMPDIR}/sstate"
264 SSTATE_MIRROR = ""
265 ''')
266
267 self.write_config(config)
268 d = get_bb_vars(capture_vars)
269 bitbake(' '.join(self.images))
270 return d
271
272 def test_reproducible_builds(self):
273 def strip_topdir(s):
274 if s.startswith(self.topdir):
275 return s[len(self.topdir):]
276 return s
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500277
Brad Bishop15ae2502019-06-18 21:44:24 -0400278 # Build native utilities
Brad Bishop79641f22019-09-10 07:20:22 -0400279 self.write_config('')
Andrew Geissler82c905d2020-04-13 13:39:40 -0500280 bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400281 diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500282 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
283 jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native")
Brad Bishop15ae2502019-06-18 21:44:24 -0400284
Andrew Geissler82c905d2020-04-13 13:39:40 -0500285 if self.save_results:
286 os.makedirs(self.save_results, exist_ok=True)
287 datestr = datetime.datetime.now().strftime('%Y%m%d')
288 save_dir = tempfile.mkdtemp(prefix='oe-reproducible-%s-' % datestr, dir=self.save_results)
289 os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
290 self.logger.info('Non-reproducible packages will be copied to %s', save_dir)
Brad Bishop79641f22019-09-10 07:20:22 -0400291
Andrew Geissler82c905d2020-04-13 13:39:40 -0500292 vars_A = self.do_test_build('reproducibleA', self.build_from_sstate)
293 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
Brad Bishop79641f22019-09-10 07:20:22 -0400307 result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
Brad Bishop15ae2502019-06-18 21:44:24 -0400308
Brad Bishop79641f22019-09-10 07:20:22 -0400309 self.logger.info('Reproducibility summary for %s: %s' % (c, result))
Brad Bishop15ae2502019-06-18 21:44:24 -0400310
Brad Bishop79641f22019-09-10 07:20:22 -0400311 self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400312
Brad Bishop79641f22019-09-10 07:20:22 -0400313 self.write_package_list(package_class, 'missing', result.missing)
314 self.write_package_list(package_class, 'different', result.different)
Andrew Geisslerc723b722021-01-08 16:14:09 -0600315 self.write_package_list(package_class, 'different_excluded', result.different_excluded)
Brad Bishop79641f22019-09-10 07:20:22 -0400316 self.write_package_list(package_class, 'same', result.same)
317
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500318 if self.save_results:
319 for d in result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500320 self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)]))
321 self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)]))
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500322
Andrew Geisslerc723b722021-01-08 16:14:09 -0600323 for d in result.different_excluded:
324 self.copy_file(d.reference, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.reference)]))
325 self.copy_file(d.test, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.test)]))
326
Brad Bishop79641f22019-09-10 07:20:22 -0400327 if result.missing or result.different:
Andrew Geisslerc723b722021-01-08 16:14:09 -0600328 fails.append("The following %s packages are missing or different and not in exclusion list: %s" %
Andrew Geissler82c905d2020-04-13 13:39:40 -0500329 (c, '\n'.join(r.test for r in (result.missing + result.different))))
330
331 # Clean up empty directories
332 if self.save_results:
333 if not os.listdir(save_dir):
334 os.rmdir(save_dir)
335 else:
336 self.logger.info('Running diffoscope')
337 package_dir = os.path.join(save_dir, 'packages')
338 package_html_dir = os.path.join(package_dir, 'diff-html')
339
340 # Copy jquery to improve the diffoscope output usability
341 self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js'))
342
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500343 run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500344 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
345
346 if fails:
347 self.fail('\n'.join(fails))
Brad Bishop15ae2502019-06-18 21:44:24 -0400348