summaryrefslogtreecommitdiff
path: root/taskcluster/taskgraph/test/test_optimize.py
blob: 8d2ddf24781414c5bcbbf794d57e6a4ad5f77d39 (plain)
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
# 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/.

from __future__ import absolute_import, print_function, unicode_literals

import unittest

from ..optimize import optimize_task_graph, resolve_task_references
from ..optimize import annotate_task_graph, get_subgraph
from ..taskgraph import TaskGraph
from .. import graph
from .util import TestTask


class TestResolveTaskReferences(unittest.TestCase):

    def do(self, input, output):
        taskid_for_edge_name = {'edge%d' % n: 'tid%d' % n for n in range(1, 4)}
        self.assertEqual(resolve_task_references('subject', input, taskid_for_edge_name), output)

    def test_in_list(self):
        "resolve_task_references resolves task references in a list"
        self.do({'in-a-list': ['stuff', {'task-reference': '<edge1>'}]},
                {'in-a-list': ['stuff', 'tid1']})

    def test_in_dict(self):
        "resolve_task_references resolves task references in a dict"
        self.do({'in-a-dict': {'stuff': {'task-reference': '<edge2>'}}},
                {'in-a-dict': {'stuff': 'tid2'}})

    def test_multiple(self):
        "resolve_task_references resolves multiple references in the same string"
        self.do({'multiple': {'task-reference': 'stuff <edge1> stuff <edge2> after'}},
                {'multiple': 'stuff tid1 stuff tid2 after'})

    def test_embedded(self):
        "resolve_task_references resolves ebmedded references"
        self.do({'embedded': {'task-reference': 'stuff before <edge3> stuff after'}},
                {'embedded': 'stuff before tid3 stuff after'})

    def test_escaping(self):
        "resolve_task_references resolves escapes in task references"
        self.do({'escape': {'task-reference': '<<><edge3>>'}},
                {'escape': '<tid3>'})

    def test_invalid(self):
        "resolve_task_references raises a KeyError on reference to an invalid task"
        self.assertRaisesRegexp(
            KeyError,
            "task 'subject' has no dependency named 'no-such'",
            lambda: resolve_task_references('subject', {'task-reference': '<no-such>'}, {})
        )


class OptimizingTask(TestTask):
    # the `optimize` method on this class is overridden direclty in the tests
    # below.
    pass


class TestOptimize(unittest.TestCase):

    kind = None

    def make_task(self, label, task_def=None, optimized=None, task_id=None):
        task_def = task_def or {'sample': 'task-def'}
        task = OptimizingTask(label=label, task=task_def)
        task.optimized = optimized
        task.task_id = task_id
        return task

    def make_graph(self, *tasks_and_edges):
        tasks = {t.label: t for t in tasks_and_edges if isinstance(t, OptimizingTask)}
        edges = {e for e in tasks_and_edges if not isinstance(e, OptimizingTask)}
        return TaskGraph(tasks, graph.Graph(set(tasks), edges))

    def assert_annotations(self, graph, **annotations):
        def repl(task_id):
            return 'SLUGID' if task_id and len(task_id) == 22 else task_id
        got_annotations = {
            t.label: (t.optimized, repl(t.task_id)) for t in graph.tasks.itervalues()
        }
        self.assertEqual(got_annotations, annotations)

    def test_annotate_task_graph_no_optimize(self):
        "annotating marks everything as un-optimized if the kind returns that"
        OptimizingTask.optimize = lambda self, params: (False, None)
        graph = self.make_graph(
            self.make_task('task1'),
            self.make_task('task2'),
            self.make_task('task3'),
            ('task2', 'task1', 'build'),
            ('task2', 'task3', 'image'),
        )
        annotate_task_graph(graph, {}, set(), graph.graph.named_links_dict(), {}, None)
        self.assert_annotations(
            graph,
            task1=(False, None),
            task2=(False, None),
            task3=(False, None)
        )

    def test_annotate_task_graph_taskid_without_optimize(self):
        "raises exception if kind returns a taskid without optimizing"
        OptimizingTask.optimize = lambda self, params: (False, 'some-taskid')
        graph = self.make_graph(self.make_task('task1'))
        self.assertRaises(
            Exception,
            lambda: annotate_task_graph(graph, {}, set(), graph.graph.named_links_dict(), {}, None)
        )

    def test_annotate_task_graph_optimize_away_dependency(self):
        "raises exception if kind optimizes away a task on which another depends"
        OptimizingTask.optimize = \
            lambda self, params: (True, None) if self.label == 'task1' else (False, None)
        graph = self.make_graph(
            self.make_task('task1'),
            self.make_task('task2'),
            ('task2', 'task1', 'build'),
        )
        self.assertRaises(
            Exception,
            lambda: annotate_task_graph(graph, {}, set(), graph.graph.named_links_dict(), {}, None)
        )

    def test_annotate_task_graph_do_not_optimize(self):
        "annotating marks everything as un-optimized if in do_not_optimize"
        OptimizingTask.optimize = lambda self, params: (True, 'taskid')
        graph = self.make_graph(
            self.make_task('task1'),
            self.make_task('task2'),
            ('task2', 'task1', 'build'),
        )
        label_to_taskid = {}
        annotate_task_graph(graph, {}, {'task1', 'task2'},
                            graph.graph.named_links_dict(), label_to_taskid, None)
        self.assert_annotations(
            graph,
            task1=(False, None),
            task2=(False, None)
        )
        self.assertEqual

    def test_annotate_task_graph_nos_do_not_propagate(self):
        "a task with a non-optimized dependency can be optimized"
        OptimizingTask.optimize = \
            lambda self, params: (False, None) if self.label == 'task1' else (True, 'taskid')
        graph = self.make_graph(
            self.make_task('task1'),
            self.make_task('task2'),
            self.make_task('task3'),
            ('task2', 'task1', 'build'),
            ('task2', 'task3', 'image'),
        )
        annotate_task_graph(graph, {}, set(),
                            graph.graph.named_links_dict(), {}, None)
        self.assert_annotations(
            graph,
            task1=(False, None),
            task2=(True, 'taskid'),
            task3=(True, 'taskid')
        )

    def test_get_subgraph_single_dep(self):
        "when a single dependency is optimized, it is omitted from the graph"
        graph = self.make_graph(
            self.make_task('task1', optimized=True, task_id='dep1'),
            self.make_task('task2', optimized=False),
            self.make_task('task3', optimized=False),
            ('task2', 'task1', 'build'),
            ('task2', 'task3', 'image'),
        )
        label_to_taskid = {'task1': 'dep1'}
        sub = get_subgraph(graph, graph.graph.named_links_dict(), label_to_taskid)
        task2 = label_to_taskid['task2']
        task3 = label_to_taskid['task3']
        self.assertEqual(sub.graph.nodes, {task2, task3})
        self.assertEqual(sub.graph.edges, {(task2, task3, 'image')})
        self.assertEqual(sub.tasks[task2].task_id, task2)
        self.assertEqual(sorted(sub.tasks[task2].task['dependencies']),
                         sorted([task3, 'dep1']))
        self.assertEqual(sub.tasks[task3].task_id, task3)
        self.assertEqual(sorted(sub.tasks[task3].task['dependencies']), [])

    def test_get_subgraph_dep_chain(self):
        "when a dependency chain is optimized, it is omitted from the graph"
        graph = self.make_graph(
            self.make_task('task1', optimized=True, task_id='dep1'),
            self.make_task('task2', optimized=True, task_id='dep2'),
            self.make_task('task3', optimized=False),
            ('task2', 'task1', 'build'),
            ('task3', 'task2', 'image'),
        )
        label_to_taskid = {'task1': 'dep1', 'task2': 'dep2'}
        sub = get_subgraph(graph, graph.graph.named_links_dict(), label_to_taskid)
        task3 = label_to_taskid['task3']
        self.assertEqual(sub.graph.nodes, {task3})
        self.assertEqual(sub.graph.edges, set())
        self.assertEqual(sub.tasks[task3].task_id, task3)
        self.assertEqual(sorted(sub.tasks[task3].task['dependencies']), ['dep2'])

    def test_get_subgraph_opt_away(self):
        "when a leaf task is optimized away, it is omitted from the graph"
        graph = self.make_graph(
            self.make_task('task1', optimized=False),
            self.make_task('task2', optimized=True),
            ('task2', 'task1', 'build'),
        )
        label_to_taskid = {'task2': 'dep2'}
        sub = get_subgraph(graph, graph.graph.named_links_dict(), label_to_taskid)
        task1 = label_to_taskid['task1']
        self.assertEqual(sub.graph.nodes, {task1})
        self.assertEqual(sub.graph.edges, set())
        self.assertEqual(sub.tasks[task1].task_id, task1)
        self.assertEqual(sorted(sub.tasks[task1].task['dependencies']), [])

    def test_get_subgraph_refs_resolved(self):
        "get_subgraph resolves task references"
        graph = self.make_graph(
            self.make_task('task1', optimized=True, task_id='dep1'),
            self.make_task(
                'task2',
                optimized=False,
                task_def={'payload': {'task-reference': 'http://<build>/<test>'}}
            ),
            ('task2', 'task1', 'build'),
            ('task2', 'task3', 'test'),
            self.make_task('task3', optimized=False),
        )
        label_to_taskid = {'task1': 'dep1'}
        sub = get_subgraph(graph, graph.graph.named_links_dict(), label_to_taskid)
        task2 = label_to_taskid['task2']
        task3 = label_to_taskid['task3']
        self.assertEqual(sub.graph.nodes, {task2, task3})
        self.assertEqual(sub.graph.edges, {(task2, task3, 'test')})
        self.assertEqual(sub.tasks[task2].task_id, task2)
        self.assertEqual(sorted(sub.tasks[task2].task['dependencies']), sorted([task3, 'dep1']))
        self.assertEqual(sub.tasks[task2].task['payload'], 'http://dep1/' + task3)
        self.assertEqual(sub.tasks[task3].task_id, task3)

    def test_optimize(self):
        "optimize_task_graph annotates and extracts the subgraph from a simple graph"
        OptimizingTask.optimize = \
            lambda self, params: (True, 'dep1') if self.label == 'task1' else (False, None)
        input = self.make_graph(
            self.make_task('task1'),
            self.make_task('task2'),
            self.make_task('task3'),
            ('task2', 'task1', 'build'),
            ('task2', 'task3', 'image'),
        )
        opt, label_to_taskid = optimize_task_graph(input, {}, set())
        self.assertEqual(opt.graph, graph.Graph(
            {label_to_taskid['task2'], label_to_taskid['task3']},
            {(label_to_taskid['task2'], label_to_taskid['task3'], 'image')}))