taskcluster/taskgraph/test/test_taskgraph.py
author Dustin J. Mitchell <dustin@mozilla.com>
Tue, 01 Aug 2017 20:02:59 +0000
changeset 668465 e4f1ed133c6826aabc73672cd379037d5c05952b
parent 668121 d88e5dec2638ae6d34527726ff0a9cb8d9336036
child 821676 990fd73cf20fc02af9ee79733b018cfb9e074631
permissions -rw-r--r--
Bug 1383880: allow only one optimization per task; r=ahal,glandium It is not at *all* clear how multiple optimizations for a single task should interact. No simple logical operation is right in all cases, and in fact in most imaginable cases the desired behavior turns out to be independent of all but one of the optimizations. For example, given both `seta` and `skip-unless-files-changed` optimizations, if SETA says to skip a test, it is low value and should be skipped regardless of what files have changed. But if SETA says to run a test, then it has likely been skipped in previous pushes, so it should be run regardless of what has changed in this push. This also adds a bit more output about optimization, that may be useful for anyone wondering why a particular job didn't run. MozReview-Commit-ID: 3OsvRnWjai4

# 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 taskgraph.graph import Graph
from taskgraph.task import Task
from taskgraph.taskgraph import TaskGraph
from mozunit import main


class TestTaskGraph(unittest.TestCase):

    maxDiff = None

    def test_taskgraph_to_json(self):
        tasks = {
            'a': Task(kind='test', label='a',
                      attributes={'attr': 'a-task'},
                      task={'taskdef': True}),
            'b': Task(kind='test', label='b',
                      attributes={},
                      task={'task': 'def'},
                      optimization={'seta': None},
                      # note that this dep is ignored, superseded by that
                      # from the taskgraph's edges
                      dependencies={'first': 'a'}),
        }
        graph = Graph(nodes=set('ab'), edges={('a', 'b', 'edgelabel')})
        taskgraph = TaskGraph(tasks, graph)

        res = taskgraph.to_json()

        self.assertEqual(res, {
            'a': {
                'kind': 'test',
                'label': 'a',
                'attributes': {'attr': 'a-task', 'kind': 'test'},
                'task': {'taskdef': True},
                'dependencies': {'edgelabel': 'b'},
                'optimization': None,
            },
            'b': {
                'kind': 'test',
                'label': 'b',
                'attributes': {'kind': 'test'},
                'task': {'task': 'def'},
                'dependencies': {},
                'optimization': {'seta': None},
            }
        })

    def test_round_trip(self):
        graph = TaskGraph(tasks={
            'a': Task(
                kind='fancy',
                label='a',
                attributes={},
                dependencies={'prereq': 'b'},  # must match edges, below
                optimization={'seta': None},
                task={'task': 'def'}),
            'b': Task(
                kind='pre',
                label='b',
                attributes={},
                dependencies={},
                optimization={'seta': None},
                task={'task': 'def2'}),
        }, graph=Graph(nodes={'a', 'b'}, edges={('a', 'b', 'prereq')}))

        tasks, new_graph = TaskGraph.from_json(graph.to_json())
        self.assertEqual(graph, new_graph)


if __name__ == '__main__':
    main()