1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#
# SPDX-License-Identifier: MIT
#
# Copyright 2019 by Garmin Ltd. or its subsidiaries
from oeqa.selftest.case import OESelftestTestCase
from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
import bb.utils
import functools
import multiprocessing
import textwrap
import json
import unittest
import tempfile
import shutil
import stat
import os
MISSING = 'MISSING'
DIFFERENT = 'DIFFERENT'
SAME = 'SAME'
@functools.total_ordering
class CompareResult(object):
def __init__(self):
self.reference = None
self.test = None
self.status = 'UNKNOWN'
def __eq__(self, other):
return (self.status, self.test) == (other.status, other.test)
def __lt__(self, other):
return (self.status, self.test) < (other.status, other.test)
class PackageCompareResults(object):
def __init__(self):
self.total = []
self.missing = []
self.different = []
self.same = []
def add_result(self, r):
self.total.append(r)
if r.status == MISSING:
self.missing.append(r)
elif r.status == DIFFERENT:
self.different.append(r)
else:
self.same.append(r)
def sort(self):
self.total.sort()
self.missing.sort()
self.different.sort()
self.same.sort()
def __str__(self):
return 'same=%i different=%i missing=%i total=%i' % (len(self.same), len(self.different), len(self.missing), len(self.total))
def compare_file(reference, test, diffutils_sysroot):
result = CompareResult()
result.reference = reference
result.test = test
if not os.path.exists(reference):
result.status = MISSING
return result
r = runCmd(['cmp', '--quiet', reference, test], native_sysroot=diffutils_sysroot, ignore_status=True)
if r.status:
result.status = DIFFERENT
return result
result.status = SAME
return result
class ReproducibleTests(OESelftestTestCase):
package_classes = ['deb', 'ipk']
images = ['core-image-minimal']
save_results = False
def setUpLocal(self):
super().setUpLocal()
needed_vars = ['TOPDIR', 'TARGET_PREFIX', 'BB_NUMBER_THREADS']
bb_vars = get_bb_vars(needed_vars)
for v in needed_vars:
setattr(self, v.lower(), bb_vars[v])
self.extrasresults = {}
self.extrasresults.setdefault('reproducible.rawlogs', {})['log'] = ''
self.extrasresults.setdefault('reproducible', {}).setdefault('files', {})
def append_to_log(self, msg):
self.extrasresults['reproducible.rawlogs']['log'] += msg
def compare_packages(self, reference_dir, test_dir, diffutils_sysroot):
result = PackageCompareResults()
old_cwd = os.getcwd()
try:
file_result = {}
os.chdir(test_dir)
with multiprocessing.Pool(processes=int(self.bb_number_threads or 0)) as p:
for root, dirs, files in os.walk('.'):
async_result = []
for f in files:
reference_path = os.path.join(reference_dir, root, f)
test_path = os.path.join(test_dir, root, f)
async_result.append(p.apply_async(compare_file, (reference_path, test_path, diffutils_sysroot)))
for a in async_result:
result.add_result(a.get())
finally:
os.chdir(old_cwd)
result.sort()
return result
def write_package_list(self, package_class, name, packages):
self.extrasresults['reproducible']['files'].setdefault(package_class, {})[name] = [
{'reference': p.reference, 'test': p.test} for p in packages]
def copy_file(self, source, dest):
bb.utils.mkdirhier(os.path.dirname(dest))
shutil.copyfile(source, dest)
def test_reproducible_builds(self):
capture_vars = ['DEPLOY_DIR_' + c.upper() for c in self.package_classes]
if self.save_results:
save_dir = tempfile.mkdtemp(prefix='oe-reproducible-')
os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
self.logger.info('Non-reproducible packages will be copied to %s', save_dir)
# Build native utilities
self.write_config('')
bitbake("diffutils-native -c addto_recipe_sysroot")
diffutils_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "diffutils-native")
# Reproducible builds should not pull from sstate or mirrors, but
# sharing DL_DIR is fine
common_config = textwrap.dedent('''\
INHERIT += "reproducible_build"
PACKAGE_CLASSES = "%s"
SSTATE_DIR = "${TMPDIR}/sstate"
''') % (' '.join('package_%s' % c for c in self.package_classes))
# Perform a build.
reproducibleA_tmp = os.path.join(self.topdir, 'reproducibleA', 'tmp')
if os.path.exists(reproducibleA_tmp):
bb.utils.remove(reproducibleA_tmp, recurse=True)
self.write_config((textwrap.dedent('''\
TMPDIR = "%s"
''') % reproducibleA_tmp) + common_config)
vars_A = get_bb_vars(capture_vars)
bitbake(' '.join(self.images))
# Perform another build.
reproducibleB_tmp = os.path.join(self.topdir, 'reproducibleB', 'tmp')
if os.path.exists(reproducibleB_tmp):
bb.utils.remove(reproducibleB_tmp, recurse=True)
self.write_config((textwrap.dedent('''\
SSTATE_MIRROR = ""
TMPDIR = "%s"
''') % reproducibleB_tmp) + common_config)
vars_B = get_bb_vars(capture_vars)
bitbake(' '.join(self.images))
# NOTE: The temp directories from the reproducible build are purposely
# kept after the build so it can be diffed for debugging.
for c in self.package_classes:
with self.subTest(package_class=c):
package_class = 'package_' + c
deploy_A = vars_A['DEPLOY_DIR_' + c.upper()]
deploy_B = vars_B['DEPLOY_DIR_' + c.upper()]
result = self.compare_packages(deploy_A, deploy_B, diffutils_sysroot)
self.logger.info('Reproducibility summary for %s: %s' % (c, result))
self.append_to_log('\n'.join("%s: %s" % (r.status, r.test) for r in result.total))
self.write_package_list(package_class, 'missing', result.missing)
self.write_package_list(package_class, 'different', result.different)
self.write_package_list(package_class, 'same', result.same)
if self.save_results:
for d in result.different:
self.copy_file(d.reference, '/'.join([save_dir, d.reference]))
self.copy_file(d.test, '/'.join([save_dir, d.test]))
if result.missing or result.different:
self.fail("The following %s packages are missing or different: %s" %
(c, ' '.join(r.test for r in (result.missing + result.different))))
|