blob: a7ef336143be2bfa7a352e753e3a8314288ccdd2 [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
20MISSING = 'MISSING'
21DIFFERENT = 'DIFFERENT'
22SAME = 'SAME'
23
24@functools.total_ordering
25class 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
37class 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
62def 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 Geisslerc9f78652020-09-18 14:11:35 -050080def 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
84class 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 Bishop15ae2502019-06-18 21:44:24 -0400106class ReproducibleTests(OESelftestTestCase):
Brad Bishop00e122a2019-10-05 11:10:57 -0400107 package_classes = ['deb', 'ipk']
Andrew Geissler82c905d2020-04-13 13:39:40 -0500108 images = ['core-image-minimal', 'core-image-sato', 'core-image-full-cmdline']
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500109 save_results = False
Andrew Geissler82c905d2020-04-13 13:39:40 -0500110 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 Bishop15ae2502019-06-18 21:44:24 -0400120
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 Geissler82c905d2020-04-13 13:39:40 -0500128 self.extraresults = {}
129 self.extraresults.setdefault('reproducible.rawlogs', {})['log'] = ''
130 self.extraresults.setdefault('reproducible', {}).setdefault('files', {})
Brad Bishop15ae2502019-06-18 21:44:24 -0400131
132 def append_to_log(self, msg):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500133 self.extraresults['reproducible.rawlogs']['log'] += msg
Brad Bishop15ae2502019-06-18 21:44:24 -0400134
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 Bishop79641f22019-09-10 07:20:22 -0400159 def write_package_list(self, package_class, name, packages):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500160 self.extraresults['reproducible']['files'].setdefault(package_class, {})[name] = [
Brad Bishop79641f22019-09-10 07:20:22 -0400161 {'reference': p.reference, 'test': p.test} for p in packages]
162
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500163 def copy_file(self, source, dest):
164 bb.utils.mkdirhier(os.path.dirname(dest))
165 shutil.copyfile(source, dest)
166
Andrew Geissler82c905d2020-04-13 13:39:40 -0500167 def do_test_build(self, name, use_sstate):
Brad Bishop15ae2502019-06-18 21:44:24 -0400168 capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
169
Andrew Geissler82c905d2020-04-13 13:39:40 -0500170 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 Bishop1d80a2e2019-11-15 16:35:03 -0500200
Brad Bishop15ae2502019-06-18 21:44:24 -0400201 # Build native utilities
Brad Bishop79641f22019-09-10 07:20:22 -0400202 self.write_config('')
Andrew Geissler82c905d2020-04-13 13:39:40 -0500203 bitbake("diffoscope-native diffutils-native jquery-native -c addto_recipe_sysroot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400204 diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500205 diffoscope_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffoscope-native")
206 jquery_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "jquery-native")
Brad Bishop15ae2502019-06-18 21:44:24 -0400207
Andrew Geissler82c905d2020-04-13 13:39:40 -0500208 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 Bishop79641f22019-09-10 07:20:22 -0400214
Andrew Geissler82c905d2020-04-13 13:39:40 -0500215 vars_A = self.do_test_build('reproducibleA', self.build_from_sstate)
216 vars_B = self.do_test_build('reproducibleB', False)
Brad Bishop79641f22019-09-10 07:20:22 -0400217
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 Geissler82c905d2020-04-13 13:39:40 -0500221 fails = []
222
Brad Bishop15ae2502019-06-18 21:44:24 -0400223 for c in self.package_classes:
Brad Bishop79641f22019-09-10 07:20:22 -0400224 with self.subTest(package_class=c):
225 package_class = 'package_' + c
Brad Bishop15ae2502019-06-18 21:44:24 -0400226
Brad Bishop79641f22019-09-10 07:20:22 -0400227 deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
228 deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
Brad Bishop15ae2502019-06-18 21:44:24 -0400229
Brad Bishop79641f22019-09-10 07:20:22 -0400230 result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
Brad Bishop15ae2502019-06-18 21:44:24 -0400231
Brad Bishop79641f22019-09-10 07:20:22 -0400232 self.logger.info('Reproducibility summary for %s: %s' % (c, result))
Brad Bishop15ae2502019-06-18 21:44:24 -0400233
Brad Bishop79641f22019-09-10 07:20:22 -0400234 self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
Brad Bishop15ae2502019-06-18 21:44:24 -0400235
Brad Bishop79641f22019-09-10 07:20:22 -0400236 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 Bishop1d80a2e2019-11-15 16:35:03 -0500240 if self.save_results:
241 for d in result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500242 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 Bishop1d80a2e2019-11-15 16:35:03 -0500244
Brad Bishop79641f22019-09-10 07:20:22 -0400245 if result.missing or result.different:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500246 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 Geisslerc9f78652020-09-18 14:11:35 -0500261 run_diffoscope('reproducibleA', 'reproducibleB', package_html_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500262 native_sysroot=diffoscope_sysroot, ignore_status=True, cwd=package_dir)
263
264 if fails:
265 self.fail('\n'.join(fails))
Brad Bishop15ae2502019-06-18 21:44:24 -0400266