taskcluster/taskgraph/test/test_util_parameterization.py
author Sebastian Hengst <archaeopteryx@coole-files.de>
Thu, 20 Dec 2018 12:43:22 +0200
changeset 451467 d0e13414d6512c9fe84911a0dd730e4fb4a28c27
parent 451439 e7f30540870849cf6ba49957d37190fbd89b5f0d
child 452055 5a7bea3fb23b1c208725be620aa881645af40f8a
permissions -rw-r--r--
Backed out 21 changesets (bug 1492664) for breaking cron task for nightlies. a=backout Backed out changeset a7d50dbb2c8e (bug 1492664) Backed out changeset 2d876c4ece8b (bug 1492664) Backed out changeset c82285d253de (bug 1492664) Backed out changeset bf6d089640eb (bug 1492664) Backed out changeset d9a7f2ce49c3 (bug 1492664) Backed out changeset 06c466ab4323 (bug 1492664) Backed out changeset c1ea4a10cc8d (bug 1492664) Backed out changeset 4c63a04fdd47 (bug 1492664) Backed out changeset 742b038bb1dd (bug 1492664) Backed out changeset 911b4b0fb683 (bug 1492664) Backed out changeset 870c8cec99e5 (bug 1492664) Backed out changeset 77699b51336b (bug 1492664) Backed out changeset 29f33f22fd8b (bug 1492664) Backed out changeset e7f305408708 (bug 1492664) Backed out changeset 335a92b1f424 (bug 1492664) Backed out changeset c566f1c8dcdf (bug 1492664) Backed out changeset c77ae59aba41 (bug 1492664) Backed out changeset 9c35dd209c6b (bug 1492664) Backed out changeset a972d6b4434e (bug 1492664) Backed out changeset 5ea6f03f845e (bug 1492664) Backed out changeset 0699d3873e44 (bug 1492664)

# 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
import datetime

from mozunit import main
from taskgraph.util.parameterization import (
    resolve_timestamps,
    resolve_task_references,
)


class TestTimestamps(unittest.TestCase):

    def test_no_change(self):
        now = datetime.datetime(2018, 1, 1)
        input = {
            "key": "value",
            "numeric": 10,
            "list": ["a", True, False, None],
        }
        self.assertEqual(resolve_timestamps(now, input), input)

    def test_buried_replacement(self):
        now = datetime.datetime(2018, 1, 1)
        input = {"key": [{"key2": [{'relative-datestamp': '1 day'}]}]}
        self.assertEqual(resolve_timestamps(now, input),
                         {"key": [{"key2": ['2018-01-02T00:00:00Z']}]})

    def test_appears_with_other_keys(self):
        now = datetime.datetime(2018, 1, 1)
        input = [{'relative-datestamp': '1 day', 'another-key': True}]
        self.assertEqual(resolve_timestamps(now, input),
                         [{'relative-datestamp': '1 day', 'another-key': True}])


class TestTaskRefs(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_no_change(self):
        "resolve_task_references does nothing when there are no task references"
        self.do({'in-a-list': ['stuff', {'property': '<edge1>'}]},
                {'in-a-list': ['stuff', {'property': '<edge1>'}]})

    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_multikey(self):
        "resolve_task_references is ignored when there is another key in the dict"
        self.do({'escape': {'task-reference': '<edge3>', 'another-key': True}},
                {'escape': {'task-reference': '<edge3>', 'another-key': True}})

    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>'}, {})
        )


if __name__ == '__main__':
    main()