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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import contextlib
import re
import unittest
import pytest
from mozunit import main
from taskgraph.graph import Graph
from taskgraph.task import Task
from taskgraph.taskgraph import TaskGraph
from gecko_taskgraph import target_tasks, try_option_syntax
class FakeTryOptionSyntax:
def __init__(self, message, task_graph, graph_config):
self.trigger_tests = 0
self.talos_trigger_tests = 0
self.raptor_trigger_tests = 0
self.notifications = None
self.env = []
self.profile = False
self.tag = None
self.no_retry = False
def task_matches(self, task):
return "at-at" in task.attributes
class TestTargetTasks(unittest.TestCase):
def default_matches_project(self, run_on_projects, project):
return self.default_matches(
attributes={
"run_on_projects": run_on_projects,
},
parameters={
"project": project,
"hg_branch": "default",
},
)
def default_matches_hg_branch(self, run_on_hg_branches, hg_branch):
attributes = {"run_on_projects": ["all"]}
if run_on_hg_branches is not None:
attributes["run_on_hg_branches"] = run_on_hg_branches
return self.default_matches(
attributes=attributes,
parameters={
"project": "mozilla-central",
"hg_branch": hg_branch,
},
)
def default_matches(self, attributes, parameters):
method = target_tasks.get_method("default")
graph = TaskGraph(
tasks={
"a": Task(kind="build", label="a", attributes=attributes, task={}),
},
graph=Graph(nodes={"a"}, edges=set()),
)
return "a" in method(graph, parameters, {})
def test_default_all(self):
"""run_on_projects=[all] includes release, integration, and other projects"""
self.assertTrue(self.default_matches_project(["all"], "mozilla-central"))
self.assertTrue(self.default_matches_project(["all"], "baobab"))
def test_default_integration(self):
"""run_on_projects=[integration] includes integration projects"""
self.assertFalse(
self.default_matches_project(["integration"], "mozilla-central")
)
self.assertFalse(self.default_matches_project(["integration"], "baobab"))
def test_default_release(self):
"""run_on_projects=[release] includes release projects"""
self.assertTrue(self.default_matches_project(["release"], "mozilla-central"))
self.assertFalse(self.default_matches_project(["release"], "baobab"))
def test_default_nothing(self):
"""run_on_projects=[] includes nothing"""
self.assertFalse(self.default_matches_project([], "mozilla-central"))
self.assertFalse(self.default_matches_project([], "baobab"))
def test_default_hg_branch(self):
self.assertTrue(self.default_matches_hg_branch(None, "default"))
self.assertTrue(self.default_matches_hg_branch(None, "GECKOVIEW_62_RELBRANCH"))
self.assertFalse(self.default_matches_hg_branch([], "default"))
self.assertFalse(self.default_matches_hg_branch([], "GECKOVIEW_62_RELBRANCH"))
self.assertTrue(self.default_matches_hg_branch(["all"], "default"))
self.assertTrue(
self.default_matches_hg_branch(["all"], "GECKOVIEW_62_RELBRANCH")
)
self.assertTrue(self.default_matches_hg_branch(["default"], "default"))
self.assertTrue(self.default_matches_hg_branch([r"default"], "default"))
self.assertFalse(
self.default_matches_hg_branch([r"default"], "GECKOVIEW_62_RELBRANCH")
)
self.assertTrue(
self.default_matches_hg_branch(
["GECKOVIEW_62_RELBRANCH"], "GECKOVIEW_62_RELBRANCH"
)
)
self.assertTrue(
self.default_matches_hg_branch(
[r"GECKOVIEW_\d+_RELBRANCH"], "GECKOVIEW_62_RELBRANCH"
)
)
self.assertTrue(
self.default_matches_hg_branch(
[r"GECKOVIEW_\d+_RELBRANCH"], "GECKOVIEW_62_RELBRANCH"
)
)
self.assertFalse(
self.default_matches_hg_branch([r"GECKOVIEW_\d+_RELBRANCH"], "default")
)
def make_task_graph(self):
tasks = {
"a": Task(kind=None, label="a", attributes={}, task={}),
"b": Task(kind=None, label="b", attributes={"at-at": "yep"}, task={}),
"c": Task(
kind=None, label="c", attributes={"run_on_projects": ["try"]}, task={}
),
"ddd-1": Task(kind="test", label="ddd-1", attributes={}, task={}),
"ddd-2": Task(kind="test", label="ddd-2", attributes={}, task={}),
"ddd-1-cf": Task(kind="test", label="ddd-1-cf", attributes={}, task={}),
"ddd-2-cf": Task(kind="test", label="ddd-2-cf", attributes={}, task={}),
"ddd-var-1": Task(kind="test", label="ddd-var-1", attributes={}, task={}),
"ddd-var-2": Task(kind="test", label="ddd-var-2", attributes={}, task={}),
}
graph = Graph(
nodes=set(
[
"a",
"b",
"c",
"ddd-1",
"ddd-2",
"ddd-1-cf",
"ddd-2-cf",
"ddd-var-1",
"ddd-var-2",
]
),
edges=set(),
)
return TaskGraph(tasks, graph)
@contextlib.contextmanager
def fake_TryOptionSyntax(self):
orig_TryOptionSyntax = try_option_syntax.TryOptionSyntax
try:
try_option_syntax.TryOptionSyntax = FakeTryOptionSyntax
yield
finally:
try_option_syntax.TryOptionSyntax = orig_TryOptionSyntax
def test_empty_try(self):
"try_mode = None runs nothing"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
params = {
"try_mode": None,
"project": "try",
"message": "",
}
# only runs the task with run_on_projects: try
self.assertEqual(method(tg, params, {}), [])
def test_try_option_syntax(self):
"try_mode = try_option_syntax uses TryOptionSyntax"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
with self.fake_TryOptionSyntax():
params = {
"try_mode": "try_option_syntax",
"message": "try: -p all",
}
self.assertEqual(method(tg, params, {}), ["b"])
def test_try_task_config(self):
"try_mode = try_task_config uses the try config"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
params = {
"try_mode": "try_task_config",
"try_task_config": {"tasks": ["a"]},
}
self.assertEqual(method(tg, params, {}), ["a"])
def test_try_task_config_regex(self):
"try_mode = try_task_config uses the try config with regex instead of chunk numbers"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
params = {
"try_mode": "try_task_config",
"try_task_config": {"new-test-config": True, "tasks": ["ddd-*"]},
"project": "try",
}
self.assertEqual(sorted(method(tg, params, {})), ["ddd-1", "ddd-2"])
def test_try_task_config_absolute(self):
"try_mode = try_task_config uses the try config with full task labels"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
params = {
"try_mode": "try_task_config",
"try_task_config": {
"new-test-config": True,
"tasks": ["ddd-var-2", "ddd-1"],
},
"project": "try",
}
self.assertEqual(sorted(method(tg, params, {})), ["ddd-1", "ddd-var-2"])
def test_try_task_config_regex_var(self):
"try_mode = try_task_config uses the try config with regex instead of chunk numbers and a test variant"
tg = self.make_task_graph()
method = target_tasks.get_method("try_tasks")
params = {
"try_mode": "try_task_config",
"try_task_config": {"new-test-config": True, "tasks": ["ddd-var-*"]},
"project": "try",
}
self.assertEqual(sorted(method(tg, params, {})), ["ddd-var-1", "ddd-var-2"])
# tests for specific filters
@pytest.mark.parametrize(
"name,params,expected",
(
pytest.param(
"filter_tests_without_manifests",
{
"task": Task(kind="test", label="a", attributes={}, task={}),
"parameters": None,
},
True,
id="filter_tests_without_manifests_not_in_attributes",
),
pytest.param(
"filter_tests_without_manifests",
{
"task": Task(
kind="test",
label="a",
attributes={"test_manifests": ["foo"]},
task={},
),
"parameters": None,
},
True,
id="filter_tests_without_manifests_has_test_manifests",
),
pytest.param(
"filter_tests_without_manifests",
{
"task": Task(
kind="build",
label="a",
attributes={"test_manifests": None},
task={},
),
"parameters": None,
},
True,
id="filter_tests_without_manifests_not_a_test",
),
pytest.param(
"filter_tests_without_manifests",
{
"task": Task(
kind="test", label="a", attributes={"test_manifests": None}, task={}
),
"parameters": None,
},
False,
id="filter_tests_without_manifests_has_no_test_manifests",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("build")],
"mode": "include",
},
True,
id="filter_regex_simple_include",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("linux(.+)debug")],
"mode": "include",
},
True,
id="filter_regex_re_include",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("nothing"), re.compile("linux(.+)debug")],
"mode": "include",
},
True,
id="filter_regex_re_include_multiple",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("build")],
"mode": "exclude",
},
False,
id="filter_regex_simple_exclude",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("linux(.+)debug")],
"mode": "exclude",
},
False,
id="filter_regex_re_exclude",
),
pytest.param(
"filter_by_regex",
{
"task_label": "build-linux64-debug",
"regexes": [re.compile("linux(.+)debug"), re.compile("nothing")],
"mode": "exclude",
},
False,
id="filter_regex_re_exclude_multiple",
),
pytest.param(
"filter_unsupported_artifact_builds",
{
"task": Task(
kind="test",
label="a",
attributes={"supports-artifact-builds": False},
task={},
),
"parameters": {
"try_task_config": {
"use-artifact-builds": False,
},
},
},
True,
id="filter_unsupported_artifact_builds_no_artifact_builds",
),
pytest.param(
"filter_unsupported_artifact_builds",
{
"task": Task(
kind="test",
label="a",
attributes={"supports-artifact-builds": False},
task={},
),
"parameters": {
"try_task_config": {
"use-artifact-builds": True,
},
},
},
False,
id="filter_unsupported_artifact_builds_removed",
),
pytest.param(
"filter_unsupported_artifact_builds",
{
"task": Task(
kind="test",
label="a",
attributes={"supports-artifact-builds": True},
task={},
),
"parameters": {
"try_task_config": {
"use-artifact-builds": True,
},
},
},
True,
id="filter_unsupported_artifact_builds_not_removed",
),
pytest.param(
"filter_unsupported_artifact_builds",
{
"task": Task(kind="test", label="a", attributes={}, task={}),
"parameters": {
"try_task_config": {
"use-artifact-builds": True,
},
},
},
True,
id="filter_unsupported_artifact_builds_not_removed",
),
),
)
def test_filters(name, params, expected):
func = getattr(target_tasks, name)
assert func(**params) is expected
if __name__ == "__main__":
main()
|