forked from taskcluster/taskgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transforms_cached_tasks.py
More file actions
258 lines (236 loc) · 8.6 KB
/
test_transforms_cached_tasks.py
File metadata and controls
258 lines (236 loc) · 8.6 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
import base64
from pprint import pprint
import pytest
from pytest_taskgraph import make_task
from taskgraph.transforms import cached_tasks
def handle_exception(obj, exc=None):
if exc:
assert isinstance(obj, exc)
elif isinstance(obj, Exception):
raise obj
def assert_no_cache(tasks):
handle_exception(tasks)
assert len(tasks) == 1
assert tasks[0] == {
"label": "cached-task",
"description": "description",
"attributes": {},
}
def assert_cache_basic(tasks):
handle_exception(tasks)
assert len(tasks) == 1
assert tasks[0] == {
"attributes": {
"cached_task": {
"digest": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"name": "cache-foo",
"type": "cached-task.v2",
}
},
"description": "description",
"label": "cached-task",
"optimization": {
"index-search": [
"test-domain.cache.level-3.cached-task.v2.cache-foo.hash.ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"test-domain.cache.level-2.cached-task.v2.cache-foo.hash.ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"test-domain.cache.level-1.cached-task.v2.cache-foo.hash.ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
]
},
"routes": [
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.hash.ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.latest",
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.pushdate.1970.01.01.19700101000000",
],
}
def assert_cache_with_dependency(tasks):
handle_exception(tasks)
assert len(tasks) == 2
tasks = sorted(tasks, key=lambda t: t["label"])
assert tasks[1] == {
"attributes": {
"cached_task": {
"digest": "db201e53944fccbb16736c8153a14de39748c0d290de84bd976c11ddcc413089",
"name": "cache-foo",
"type": "cached-task.v2",
}
},
"dependencies": {"edge": "dep-cached"},
"description": "description",
"label": "cached-with-dep",
"optimization": {
"index-search": [
"test-domain.cache.level-3.cached-task.v2.cache-foo.hash.db201e53944fccbb16736c8153a14de39748c0d290de84bd976c11ddcc413089",
"test-domain.cache.level-2.cached-task.v2.cache-foo.hash.db201e53944fccbb16736c8153a14de39748c0d290de84bd976c11ddcc413089",
"test-domain.cache.level-1.cached-task.v2.cache-foo.hash.db201e53944fccbb16736c8153a14de39748c0d290de84bd976c11ddcc413089",
]
},
"routes": [
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.hash.db201e53944fccbb16736c8153a14de39748c0d290de84bd976c11ddcc413089",
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.latest",
"index.test-domain.cache.level-1.cached-task.v2.cache-foo.pushdate.1970.01.01.19700101000000",
],
}
# The digest should not be the same as above, as it takes the dependency digest into account.
digest_0 = tasks[0]["attributes"]["cached_task"]["digest"]
digest_1 = tasks[1]["attributes"]["cached_task"]["digest"]
assert digest_0 != digest_1
def assert_cache_with_non_cached_dependency(e):
handle_exception(e, exc=Exception)
def assert_chain_of_trust_influences_digest(tasks):
assert len(tasks) == 3
tasks = sorted(tasks, key=lambda t: t["label"]) # cot-false, cot-true, no-cot
# The first and third tasks are chain-of-trust: false, and chain-of-trust unspecified
# which should result in the same digest.
digest_0 = tasks[0]["attributes"]["cached_task"]["digest"]
digest_2 = tasks[2]["attributes"]["cached_task"]["digest"]
assert digest_0 == digest_2
# The second task is chain-of-trust: True, and should have a different digest
digest_1 = tasks[1]["attributes"]["cached_task"]["digest"]
assert digest_0 != digest_1
@pytest.mark.parametrize(
"tasks, kind_config, deps",
(
pytest.param(
# tasks
[{}],
# kind config
{},
# kind deps
None,
id="no_cache",
),
pytest.param(
# tasks
[
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
}
}
],
# kind config
{},
# kind deps
None,
id="cache_basic",
),
pytest.param(
# tasks
[
# This task has same digest-data, and no dependencies.
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"label": "cached-no-dep",
},
# This task has same digest-data, but a dependency on a cached task.
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"dependencies": {"edge": "dep-cached"},
"label": "cached-with-dep",
},
],
# kind config
{},
# kind deps
{
"dep-cached": make_task(
"dep-cached",
attributes={
"cached_task": {
"type": "cached-dep.v2",
"name": "cache-dep",
"digest": base64.b64encode(b"def").decode("utf-8"),
}
},
)
},
id="cache_with_dependency",
),
pytest.param(
# tasks
[
# This task depends on a non-cached task.
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"dependencies": {"edge": "dep"},
},
],
# kind config
{},
# kind deps
{"dep": make_task("dep")},
id="cache_with_non_cached_dependency",
),
pytest.param(
# tasks
[
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"label": "no-cot",
# no explicit chain of trust configuration; should be the
# same as when it is set to False
},
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"label": "cot-false",
"worker": {
"chain-of-trust": False,
},
},
{
"cache": {
"type": "cached-task.v2",
"name": "cache-foo",
"digest-data": ["abc"],
},
"label": "cot-true",
"worker": {"chain-of-trust": True},
},
],
# kind config
{},
# kind deps
{},
id="chain_of_trust_influences_digest",
),
),
)
def test_transforms(
request, make_transform_config, run_transform, tasks, kind_config, deps
):
for task in tasks:
task.setdefault("label", "cached-task")
task.setdefault("description", "description")
task.setdefault("attributes", {})
config = make_transform_config(kind_config, deps)
try:
result = run_transform(cached_tasks.transforms, tasks, config)
except Exception as e:
result = e
print("Dumping result:")
pprint(result, indent=2)
param_id = request.node.callspec.id
assert_func = globals()[f"assert_{param_id}"]
assert_func(result)