Repository navigation
Expand file tree
/
Copy pathtest_content_equiv_order.py
More file actions
358 lines (325 loc) · 10 KB
/
Copy pathtest_content_equiv_order.py
File metadata and controls
358 lines (325 loc) · 10 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Order subcomponent content equivalence — mesh.cat ≡ HTTP GET."""
from __future__ import annotations
import json
from unittest.mock import MagicMock
import pytest
from cats.network.registry import (
assert_order_content_equiv,
assert_order_function_content_equiv,
assert_order_invoice_content_equiv,
assert_order_structure_content_equiv,
assert_order_subcomponent_equiv,
)
def _mesh(bodies: dict):
mesh = MagicMock()
def cat_obj(locator):
if locator not in bodies:
raise KeyError(locator)
value = bodies[locator]
if isinstance(value, (bytes, bytearray)):
return bytes(value)
if isinstance(value, str):
return value.encode('utf-8')
return json.dumps(value).encode('utf-8')
mesh.catObj.side_effect = cat_obj
mesh.cat.side_effect = lambda loc: cat_obj(loc).decode('utf-8')
return mesh
def _http(bodies: dict):
def http_get_json(url):
value = bodies[url]
if isinstance(value, (bytes, bytearray)):
return json.loads(value.decode('utf-8'))
if isinstance(value, str):
return json.loads(value)
return value
def http_get(url):
value = bodies[url]
if isinstance(value, (bytes, bytearray)):
return bytes(value)
if isinstance(value, str):
return value.encode('utf-8')
return json.dumps(value).encode('utf-8')
return http_get_json, http_get
def test_order_function_fetch_equiv():
fn_uri = 'http://n/ldp/cas/fn'
ifs = 'http://n/ldp/cas/ifs'
iff = 'http://n/ldp/cas/if'
ps = 'http://n/ldp/cas/ps'
p = 'http://n/ldp/cas/p'
function = {
'infrafunction_source_uri': ifs,
'infrafunction_uri': iff,
'process_source_uri': ps,
'process_uri': p,
}
bodies = {
fn_uri: function,
ifs: {'module': 'a'},
iff: {'module': 'b'},
ps: {'module': 'c'},
p: {'module': 'd'},
}
order = {'function_uri': fn_uri}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
out = assert_order_subcomponent_equiv(
order,
'function',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
assert out == function
assert_order_function_content_equiv(
function,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_structure_fetch_equiv():
st_uri = 'http://n/ldp/cas/st'
infra = 'http://n/ldp/cas/infra'
plant = 'http://n/ldp/cas/plant'
root = 'http://n/ldp/cas/root'
structure = {
'infrastructure_uri': infra,
'plant_uri': plant,
'root_uri': root,
}
bodies = {
st_uri: structure,
infra: {'kind': 'infra'},
plant: {'kind': 'plant'},
root: {'kind': 'root'},
}
order = {'structure_uri': st_uri}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
out = assert_order_subcomponent_equiv(
order,
'structure',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
assert out == structure
assert_order_structure_content_equiv(
structure,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_invoice_fetch_equiv_with_data():
inv_uri = 'http://n/ldp/cas/ii'
data_uri = 'http://n/ldp/cas/indata'
invoice = {'data_uri': data_uri}
bodies = {inv_uri: invoice, data_uri: b'input-bytes'}
order = {'invoice_uri': inv_uri}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
out = assert_order_subcomponent_equiv(
order,
'invoice',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
assert out == invoice
assert_order_invoice_content_equiv(
invoice,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_input_invoice_ignores_registry_output_invoice():
"""Order.invoice_uri is input; record.invoice_uri is Executor output."""
input_inv = 'http://n/ldp/cas/' + ('1' * 64)
output_inv = 'http://n/ldp/cas/' + ('2' * 64)
data_uri = 'http://n/ldp/cas/' + ('3' * 64)
invoice = {'data_uri': data_uri}
bodies = {
input_inv: invoice,
data_uri: b'input-bytes',
# Output invoice must not be used as mesh locator for input fetch.
output_inv: {'order_uri': 'http://n/o', 'data_uri': 'http://n/egress'},
}
order = {'invoice_uri': input_inv}
record = {
'invoice_uri': output_inv,
'invoice': 'ni:///sha-256;' + ('A' * 43),
'data_uri': 'http://n/ldp/cas/' + ('e' * 64),
'data': 'ni:///sha-256;' + ('B' * 43),
}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
out = assert_order_subcomponent_equiv(
order,
'invoice',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
record=record,
)
assert out == invoice
mesh.catObj.assert_any_call(input_inv)
def test_order_content_equiv_umbrella():
fn_uri = 'http://n/f'
st_uri = 'http://n/s'
inv_uri = 'http://n/i'
order = {
'function_uri': fn_uri,
'structure_uri': st_uri,
'invoice_uri': inv_uri,
}
bodies = {
fn_uri: {},
st_uri: {},
inv_uri: {},
}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
assert_order_content_equiv(
order,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_ebom_stems_fetch_equiv():
fn_sbom_uri = 'http://n/ldp/cas/fnsbom'
st_sbom_uri = 'http://n/ldp/cas/stsbom'
nest_uri = 'http://n/ldp/cas/indatasbom'
dcat_uri = 'http://n/ldp/cas/dcat'
spdx_uri = 'http://n/ldp/cas/spdx'
fn_sbom = {'spdxVersion': '3.0.1', 'name': 'function'}
st_sbom = {'spdxVersion': '3.0.1', 'name': 'structure'}
nest = {'dcat_uri': dcat_uri, 'spdx_uri': spdx_uri}
dcat = {'@type': 'dcat:Catalog'}
spdx = {'spdxVersion': '3.0.1', 'name': 'input'}
order = {
'function_sbom_uri': fn_sbom_uri,
'structure_sbom_uri': st_sbom_uri,
'input_data_sbom_uri': nest_uri,
}
bodies = {
fn_sbom_uri: fn_sbom,
st_sbom_uri: st_sbom,
nest_uri: nest,
dcat_uri: dcat,
spdx_uri: spdx,
}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
assert (
assert_order_subcomponent_equiv(
order,
'function_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
== fn_sbom
)
assert (
assert_order_subcomponent_equiv(
order,
'structure_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
== st_sbom
)
assert (
assert_order_subcomponent_equiv(
order,
'input_data_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
== nest
)
assert_order_content_equiv(
order,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_ebom_stems_missing_still_pass_umbrella():
order = {
'function_uri': 'http://n/f',
'structure_uri': 'http://n/s',
'invoice_uri': 'http://n/i',
}
bodies = {'http://n/f': {}, 'http://n/s': {}, 'http://n/i': {}}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
assert (
assert_order_subcomponent_equiv(
order,
'function_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
is None
)
assert_order_content_equiv(
order,
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_ebom_stem_unfetchable_fails():
order = {'function_sbom_uri': 'http://n/ldp/cas/missing'}
mesh = _mesh({})
http_get_json, http_get = _http({})
with pytest.raises((AssertionError, KeyError)):
assert_order_subcomponent_equiv(
order,
'function_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_input_data_sbom_missing_dcat_fails():
nest_uri = 'http://n/ldp/cas/indatasbom'
nest = {'spdx_uri': 'http://n/ldp/cas/spdx'}
order = {'input_data_sbom_uri': nest_uri}
bodies = {nest_uri: nest, 'http://n/ldp/cas/spdx': {'spdxVersion': '3.0.1'}}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
with pytest.raises(AssertionError, match='dcat_uri'):
assert_order_subcomponent_equiv(
order,
'input_data_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_input_data_sbom_missing_spdx_fails():
nest_uri = 'http://n/ldp/cas/indatasbom'
nest = {'dcat_uri': 'http://n/ldp/cas/dcat'}
order = {'input_data_sbom_uri': nest_uri}
bodies = {nest_uri: nest, 'http://n/ldp/cas/dcat': {'@type': 'dcat:Catalog'}}
mesh = _mesh(bodies)
http_get_json, http_get = _http(bodies)
with pytest.raises(AssertionError, match='spdx_uri'):
assert_order_subcomponent_equiv(
order,
'input_data_sbom',
content_mesh=mesh,
http_get_json=http_get_json,
http_get=http_get,
)
def test_order_fetch_mismatch_raises():
uri = 'http://n/f'
mesh = _mesh({uri: {'k': 1}})
with pytest.raises(AssertionError, match='must match'):
assert_order_subcomponent_equiv(
{'function_uri': uri},
'function',
content_mesh=mesh,
http_get_json=lambda u: {'k': 2},
http_get=lambda u: b'{}',
)