forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstreams_workspace_test.py
283 lines (246 loc) · 9.66 KB
/
bitstreams_workspace_test.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
import os
import unittest
import unittest.mock
from bitstreams_workspace import BitstreamCache
MOCK_MANIFEST = """{
"schema_version": 3,
"designs": {
"chip_earlgrey_cw310": {
"build_id": "abcd",
"bitstream": {
"file": "lowrisc_systems_chip_earlgrey_cw310_0.1.bit",
"build_target": "//hw/bitstream/vivado:fpga_cw310"
},
"memory_map_info": {
"file": "memories.mmi",
"build_target": "//hw/bitstream/vivado:fpga_cw310",
"memories": ["otp", "rom"]
}
}
}
}"""
class TestBitstreamCache(unittest.TestCase):
def test_make_with_default(self):
# Changes to command-line argument defaults could break this method, so
# it's important to at least have code coverage.
BitstreamCache.MakeWithDefaults()
def test_get_from_cache(self):
MOCK_BITSTREAM = 'lowrisc_systems_chip_earlgrey_cw310_0.1.bit'
MOCKED_OS_WALK_RETURN = [
# os.walk() yields tuples of the form (root, dir, files).
('cache/abcd', [],
[MOCK_BITSTREAM, 'manifest.json', 'memories.mmi']),
]
os.walk = unittest.mock.MagicMock(name='os.walk',
return_value=MOCKED_OS_WALK_RETURN)
cache = BitstreamCache('/',
'/tmp/cache/opentitan-bitstreams',
'latest.txt',
offline=True)
cache.InitRepository = unittest.mock.MagicMock(name='method')
m = unittest.mock.mock_open(read_data=MOCK_MANIFEST)
with unittest.mock.patch('bitstreams_workspace.open', m):
(manifest, manifest_path) = cache.GetFromCache('abcd')
m.assert_called_once_with('cache/abcd/manifest.json', 'r')
# This is more of an implementation detail, but it verifies that we hit
# the mocked `os.walk` function as expected.
os.walk.assert_called_once_with('cache/abcd')
self.maxDiff = None
self.assertEqual(
manifest, {
"schema_version": 3,
"designs": {
"chip_earlgrey_cw310": {
"build_id": "abcd",
"bitstream": {
"file": MOCK_BITSTREAM,
"build_target": "//hw/bitstream/vivado:fpga_cw310",
},
"memory_map_info": {
"file": "memories.mmi",
"build_target": "//hw/bitstream/vivado:fpga_cw310",
"memories": ["otp", "rom"],
},
},
},
})
os.walk.assert_called_once_with('cache/abcd')
def test_write_build_file(self):
BITSTREAM_ORIG = 'lowrisc_systems_chip_earlgrey_cw310_0.1.bit.orig'
BITSTREAM_SPLICE = 'lowrisc_systems_chip_earlgrey_cw310_0.1.bit.splice'
MOCKED_OS_WALK_RETURN = [
# os.walk() yields tuples of the form (root, dir, files).
('cache/abcd', [],
[BITSTREAM_ORIG, BITSTREAM_SPLICE, 'manifest.json', 'memories.mmi']),
]
os.walk = unittest.mock.MagicMock(name='os.walk',
return_value=MOCKED_OS_WALK_RETURN)
BitstreamCache._GetDateTimeStr = unittest.mock.MagicMock(
name='BitstreamCache._GetDateTimeStr',
return_value='2022-07-14T15:02:54.463801')
cache = BitstreamCache('/',
'/tmp/cache/opentitan-bitstreams',
'latest.txt',
offline=True)
manifest = {
"schema_version": 3,
"designs": {
"chip_earlgrey_cw310": {
"build_id": "abcd",
"bitstream": {
"file": "lowrisc_systems_chip_earlgrey_cw310_0.1.bit.orig",
"build_target": "//hw/bitstream/vivado:fpga_cw310",
},
"memory_map_info": {
"file": "memories.mmi",
"build_target": "//hw/bitstream/vivado:fpga_cw310",
"memories": ["otp", "rom"],
},
},
},
}
manifest_path = "cache/abcd/substitute_manifest.json"
bazel_string = cache._ConstructBazelString('BUILD.mock', 'abcd', manifest, manifest_path)
self.maxDiff = None
self.assertEqual(
bazel_string, '''# This file was autogenerated. Do not edit!
# Built at 2022-07-14T15:02:54.463801.
# Configured for bitstream: abcd
package(default_visibility = ["//visibility:public"])
exports_files(glob(["cache/**"]))
filegroup(
name = "chip_earlgrey_cw310_bitstream",
srcs = ["cache/abcd/lowrisc_systems_chip_earlgrey_cw310_0.1.bit.orig"],
)
filegroup(
name = "chip_earlgrey_cw310_mmi",
srcs = ["cache/abcd/memories.mmi"],
)
filegroup(
name = "manifest",
srcs = ["cache/abcd/substitute_manifest.json"],
)
alias(
name = "chip_earlgrey_cw310_hyperdebug_bitstream",
actual = "@//hw/bitstream/vivado:fpga_cw310_test_rom_hyp",
)
alias(
name = "chip_earlgrey_cw310_hyperdebug_mmi",
actual = "@//hw/bitstream/vivado:cw310_hyperdebug_mmi",
)
alias(
name = "chip_earlgrey_cw340_bitstream",
actual = "@//hw/bitstream/vivado:fpga_cw340_test_rom",
)
alias(
name = "chip_earlgrey_cw340_mmi",
actual = "@//hw/bitstream/vivado:cw340_mmi",
)
''')
class TestFetchAvailableBitstreams(unittest.TestCase):
"""
The BitstreamCache.GetBitstreamsAvailable method calls the XML API on
the root level of the GCP bucket to get the list of files in the
bitstream cache. If this list is sufficiently long, the response will
be paginated, as indicated by the <IsTruncated> tag. For p pages,
GetBitstreamsAvailable is expected to make p+1 calls to the API (using
BitstreamCache.Get). The last call is to get latest.txt which indicates
the latest bitstream.
The XML response elements are documented here:
https://cloud.google.com/storage/docs/xml-api/get-bucket-list#response_body_elements
"""
def setUp(self):
self.cache = BitstreamCache('/', '/tmp/cache/opentitan-bitstreams',
'latest.txt')
self.cache.InitRepository = unittest.mock.MagicMock(name='method')
def test_fetch_available_bitstreams_single_page(self):
"""Test fetching the available bitstreams without pagination."""
MOCKED_GET_RETURN = [
b"""<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
<Name>opentitan-bitstreams</Name>
<Prefix/>
<Marker/>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>master/bitstream-0.tar.gz</Key>
<Generation>1669083850593267</Generation>
<MetaGeneration>1</MetaGeneration>
<LastModified>2022-11-22T02:24:10.633Z</LastModified>
<ETag>"e82b93a0f88161e594ef79f41277de92"</ETag>
<Size>18845320</Size>
</Contents>
</ListBucketResult>""", b"""2022-12-01T14:54:13
0"""
]
# Mock out the Get method to simulate a network access
self.cache.Get = unittest.mock.MagicMock(
name='cache.Get',
side_effect=MOCKED_GET_RETURN,
)
self.cache.GetBitstreamsAvailable(branch="earlgrey_1.0.0", refresh=True)
self.assertEqual(self.cache.Get.call_count, 2)
self.assertEqual(self.cache.available, {
"0": "master/bitstream-0.tar.gz",
"latest": "0",
})
def test_fetch_available_bitstreams_with_pagination(self):
"""Test fetching the XML file with the list of available bitstreams."""
MOCKED_GET_RETURN = [
b"""<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
<Name>opentitan-bitstreams</Name>
<Prefix/>
<Marker/>
<NextMarker>master/bitstream-1.tar.gz</NextMarker>
<IsTruncated>true</IsTruncated>
<Contents>
<Key>master/bitstream-0.tar.gz</Key>
<Generation>1656382040594268</Generation>
<MetaGeneration>1</MetaGeneration>
<LastModified>2022-06-28T02:07:20.635Z</LastModified>
<ETag>"5b6f3f9ef43f67b988cac31c73949e85"</ETag>
<Size>12254300</Size>
</Contents>
</ListBucketResult>""",
b"""<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
<Name>opentitan-bitstreams</Name>
<Prefix/>
<Marker>master/bitstream-1.tar.gz</Marker>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>master/bitstream-1.tar.gz</Key>
<Generation>1656382040594268</Generation>
<MetaGeneration>1</MetaGeneration>
<LastModified>2022-06-28T02:07:20.635Z</LastModified>
<ETag>"5b6f3f9ef43f67b988cac31c73949e85"</ETag>
<Size>12254300</Size>
</Contents>
<Contents>
<Key>master/latest.txt</Key>
<Generation>1669836798495359</Generation>
<MetaGeneration>1</MetaGeneration>
<LastModified>2022-11-30T19:33:18.615Z</LastModified>
<ETag>"58498757ff6f02bcbfbae67eb92dfa4b"</ETag>
<Size>60</Size>
</Contents>
</ListBucketResult>
""", b"""2022-12-01T14:54:13
1"""
]
# Mock out the Get method to simulate a network access
self.cache.Get = unittest.mock.MagicMock(
name='cache.Get',
side_effect=MOCKED_GET_RETURN,
)
self.cache.GetBitstreamsAvailable(branch="earlgrey_1.0.0", refresh=True)
self.assertEqual(self.cache.Get.call_count, 3)
self.assertEqual(
self.cache.available, {
"0": "master/bitstream-0.tar.gz",
"1": "master/bitstream-1.tar.gz",
"latest": "1",
})
if __name__ == '__main__':
unittest.main()