blob: deddf6e50b1a8f39125ce917df45cf2b35ddf519 [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/)
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 'swig',
69 'syslinux-misc',
70 'systemd-bootchart',
71 'valgrind-ptest',
72 'vim',
73 'watchdog',
74 'xmlto',
75 'xorg-minimal-fonts'
76 ]
77
78def is_excluded(package):
79 package_name = os.path.basename(package)
80 for i in exclude_packages:
81 if package_name.startswith(i):
82 return True
83 return False
84
Brad Bishop15ae2502019-06-18 21:44:24 -040085MISSING = 'MISSING'
86DIFFERENT = 'DIFFERENT'
87SAME = 'SAME'
88
89@functools.total_ordering
90class CompareResult(object):
91 def __init__(self):
92 self.reference = None
93 self.test = None
94 self.status = 'UNKNOWN'
95
96 def __eq__(self, other):
97 return (self.status, self.test) == (other.status, other.test)
98
99 def __lt__(self, other):
100 return (self.status, self.test) < (other.status, other.test)
101
102class PackageCompareResults(object):
103 def __init__(self):
104 self.total = []
105 self.missing = []
106 self.different = []
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600107 self.different_excluded = []
Brad Bishop15ae2502019-06-18 21:44:24 -0400108 self.same = []
109
110 def add_result(self, r):
111 self.total.append(r)
112 if r.status == MISSING:
113 self.missing.append(r)
114 elif r.status == DIFFERENT:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600115 if is_excluded(r.reference):
116 self.different_excluded.append(r)
117 else:
118 self.different.append(r)
Brad Bishop15ae2502019-06-18 21:44:24 -0400119 else:
120 self.same.append(r)
121
122 def sort(self):
123 self.total.sort()
124 self.missing.sort()
125 self.different.sort()
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600126 self.different_excluded.sort()
Brad Bishop15ae2502019-06-18 21:44:24 -0400127 self.same.sort()
128
129 def __str__(self):
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600130 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 -0400131
132def compare_file(reference, test, diffutils_sysroot):
133 result = CompareResult()
134 result.reference = reference
135 result.test = test
136
137 if not os.path.exists(reference):
138 result.status = MISSING
139 return result
140
141 r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True)
142
143 if r.status:
144 result.status = DIFFERENT
145 return result
146
147 result.status = SAME
148 return result
149
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500150def run_diffoscope(a_dir, b_dir, html_dir, **kwargs):
151 return runCmd(['diffoscope', '--no-default-limits', '--exclude-directory-metadata', 'yes', '--html-dir', html_dir, a_dir, b_dir],
152 **kwargs)
153
154class DiffoscopeTests(OESelftestTestCase):
155 diffoscope_test_files = os.path.join(os.path.dirname(os.path.abspath(__file__)), "diffoscope")
156
157 def test_diffoscope(self):
158 bitbake("diffoscope-native -c addto_recipe_sysroot")
159 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
160
161 # Check that diffoscope doesn't return an error when the files compare
162 # the same (a general check that diffoscope is working)
163 with tempfile.TemporaryDirectory() as tmpdir:
164 run_diffoscope('A', 'A', tmpdir,
165 native_sysroot=diffoscope_sysroot, cwd=self.diffoscope_test_files)
166
167 # Check that diffoscope generates an index.html file when the files are
168 # different
169 with tempfile.TemporaryDirectory() as tmpdir:
170 r = run_diffoscope('A', 'B', tmpdir,
171 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=self.diffoscope_test_files)
172
173 self.assertNotEqual(r.status, 0, msg="diffoscope was successful when an error was expected")
174 self.assertTrue(os.path.exists(os.path.join(tmpdir, 'index.html')), "HTML index not found!")
175
Brad Bishop15ae2502019-06-18 21:44:24 -0400176class ReproducibleTests(OESelftestTestCase):
Brad Bishop00e122a2019-10-05 11:10:57 -0400177 package_classes = ['deb', 'ipk']
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600178 images = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline', 'world']
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500179 save_results = False
Andrew Geissler82c905d2020-04-13 13:39:40 -0500180 if 'OEQA_DEBUGGING_SAVED_OUTPUT' in os.environ:
181 save_results = os.environ['OEQA_DEBUGGING_SAVED_OUTPUT']
182
183 # This variable controls if one of the test builds is allowed to pull from
184 # an sstate cache/mirror. The other build is always done clean as a point of
185 # comparison.
186 # If you know that your sstate archives are reproducible, enabling this
187 # will test that and also make the test run faster. If your sstate is not
188 # reproducible, disable this in your derived test class
189 build_from_sstate = True
Brad Bishop15ae2502019-06-18 21:44:24 -0400190
191 def setUpLocal(self):
192 super().setUpLocal()
193 needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
194 bb_vars = get_bb_vars(needed_vars)
195 for v in needed_vars:
196 setattr(self, v.lower(), bb_vars[v])
197
Andrew Geissler82c905d2020-04-13 13:39:40 -0500198 self.extraresults = {}
199 self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
200 self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
Brad Bishop15ae2502019-06-18 21:44:24 -0400201
202 def append_to_log(self, msg):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500203 self.extraresults['reproducible.rawlogs']['log'] += msg
Brad Bishop15ae2502019-06-18 21:44:24 -0400204
205 def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
206 result = PackageCompareResults()
207
208 old_cwd = os.getcwd()
209 try:
210 file_result = {}
211 os.chdir(test_dir)
212 with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
213 for root, dirs, files in os.walk('.'):
214 async_result = []
215 for f in files:
216 reference_path = os.path.join(reference_dir, root, f)
217 test_path = os.path.join(test_dir, root, f)
218 async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
219
220 for a in async_result:
221 result.add_result(a.get())
222
223 finally:
224 os.chdir(old_cwd)
225
226 result.sort()
227 return result
228
Brad Bishop79641f22019-09-10 07:20:22 -0400229 def write_package_list(self, package_class, name, packages):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500230 self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
Brad Bishop79641f22019-09-10 07:20:22 -0400231 {'reference': p.reference, 'test': p.test} for p in packages]
232
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500233 def copy_file(self, source, dest):
234 bb.utils.mkdirhier(os.path.dirname(dest))
235 shutil.copyfile(source, dest)
236
Andrew Geissler82c905d2020-04-13 13:39:40 -0500237 def do_test_build(self, name, use_sstate):
Brad Bishop15ae2502019-06-18 21:44:24 -0400238 capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
239
Andrew Geissler82c905d2020-04-13 13:39:40 -0500240 tmpdir = os.path.join(self.topdir, name, 'tmp')
241 if os.path.exists(tmpdir):
242 bb.utils.remove(tmpdir, recurse=True)
243
244 config = textwrap.dedent('''\
245 INHERIT += "reproducible_build"
246 PACKAGE_CLASSES = "{package_classes}"
247 INHIBIT_PACKAGE_STRIP = "1"
248 TMPDIR = "{tmpdir}"
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600249 LICENSE_FLAGS_WHITELIST = "commercial"
250 DISTRO_FEATURES_append = ' systemd pam'
251 USERADDEXTENSION = "useradd-staticids"
252 USERADD_ERROR_DYNAMIC = "skip"
253 USERADD_UID_TABLES += "files/static-passwd"
254 USERADD_GID_TABLES += "files/static-group"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500255 ''').format(package_classes=' '.join('package_%s' % c for c in self.package_classes),
256 tmpdir=tmpdir)
257
258 if not use_sstate:
259 # This config fragment will disable using shared and the sstate
260 # mirror, forcing a complete build from scratch
261 config += textwrap.dedent('''\
262 SSTATE_DIR = "${TMPDIR}/sstate"
263 SSTATE_MIRROR = ""
264 ''')
265
266 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)
292 vars_B = self.do_test_build('reproducibleB', False)
Brad Bishop79641f22019-09-10 07:20:22 -0400293
294 # NOTE: The temp directories from the reproducible build are purposely
295 # kept after the build so it can be diffed for debugging.
296
Andrew Geissler82c905d2020-04-13 13:39:40 -0500297 fails = []
298
Brad Bishop15ae2502019-06-18 21:44:24 -0400299 for c in self.package_classes:
Brad Bishop79641f22019-09-10 07:20:22 -0400300 with self.subTest(package_class=c):
301 package_class = 'package_' + c
Brad Bishop15ae2502019-06-18 21:44:24 -0400302
Brad Bishop79641f22019-09-10 07:20:22 -0400303 deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
304 deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
Brad Bishop15ae2502019-06-18 21:44:24 -0400305
Brad Bishop79641f22019-09-10 07:20:22 -0400306 result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
Brad Bishop15ae2502019-06-18 21:44:24 -0400307
Brad Bishop79641f22019-09-10 07:20:22 -0400308 self.logger.info('Reproducibility summary for %s: %s' % (c, result))
Brad Bishop15ae2502019-06-18 21:44:24 -0400309
Brad Bishop79641f22019-09-10 07:20:22 -0400310 self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400311
Brad Bishop79641f22019-09-10 07:20:22 -0400312 self.write_package_list(package_class, 'missing', result.missing)
313 self.write_package_list(package_class, 'different', result.different)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600314 self.write_package_list(package_class, 'different_excluded', result.different_excluded)
Brad Bishop79641f22019-09-10 07:20:22 -0400315 self.write_package_list(package_class, 'same', result.same)
316
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500317 if self.save_results:
318 for d in result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500319 self.copy_file(d.reference, '/'.join([save_dir, 'packages', strip_topdir(d.reference)]))
320 self.copy_file(d.test, '/'.join([save_dir, 'packages', strip_topdir(d.test)]))
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500321
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600322 for d in result.different_excluded:
323 self.copy_file(d.reference, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.reference)]))
324 self.copy_file(d.test, '/'.join([save_dir, 'packages-excluded', strip_topdir(d.test)]))
325
Brad Bishop79641f22019-09-10 07:20:22 -0400326 if result.missing or result.different:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600327 fails.append("The following %s packages are missing or different and not in exclusion list: %s" %
Andrew Geissler82c905d2020-04-13 13:39:40 -0500328 (c, '\n'.join(r.test for r in (result.missing + result.different))))
329
330 # Clean up empty directories
331 if self.save_results:
332 if not os.listdir(save_dir):
333 os.rmdir(save_dir)
334 else:
335 self.logger.info('Running diffoscope')
336 package_dir = os.path.join(save_dir, 'packages')
337 package_html_dir = os.path.join(package_dir, 'diff-html')
338
339 # Copy jquery to improve the diffoscope output usability
340 self.copy_file(os.path.join(jquery_sysroot, 'usr/share/javascript/jquery/jquery.min.js'), os.path.join(package_html_dir, 'jquery.js'))
341
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500342 run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500343 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
344
345 if fails:
346 self.fail('\n'.join(fails))
Brad Bishop15ae2502019-06-18 21:44:24 -0400347