summaryrefslogtreecommitdiffstats
path: root/taskcluster/taskgraph/test/test_util_templates.py
blob: 47f7494a05498f291ebbd0fe1db05da08749165c (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
# 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 mozunit
import textwrap
from taskgraph.util.templates import (
    merge_to,
    merge,
    Templates,
    TemplatesException
)

files = {}
files['/fixtures/circular.yml'] = textwrap.dedent("""\
    $inherits:
      from: 'circular_ref.yml'
      variables:
        woot: 'inherit'
    """)

files['/fixtures/inherit.yml'] = textwrap.dedent("""\
    $inherits:
      from: 'templates.yml'
      variables:
        woot: 'inherit'
    """)

files['/fixtures/extend_child.yml'] = textwrap.dedent("""\
    list: ['1', '2', '3']
    was_list: ['1']
    obj:
      level: 1
      deeper:
        woot: 'bar'
        list: ['baz']
    """)

files['/fixtures/circular_ref.yml'] = textwrap.dedent("""\
    $inherits:
      from: 'circular.yml'
    """)

files['/fixtures/child_pass.yml'] = textwrap.dedent("""\
    values:
      - {{a}}
      - {{b}}
      - {{c}}
    """)

files['/fixtures/inherit_pass.yml'] = textwrap.dedent("""\
    $inherits:
      from: 'child_pass.yml'
      variables:
        a: 'a'
        b: 'b'
        c: 'c'
    """)

files['/fixtures/deep/2.yml'] = textwrap.dedent("""\
    $inherits:
      from: deep/1.yml

    """)

files['/fixtures/deep/3.yml'] = textwrap.dedent("""\
    $inherits:
      from: deep/2.yml

    """)

files['/fixtures/deep/4.yml'] = textwrap.dedent("""\
    $inherits:
      from: deep/3.yml
    """)

files['/fixtures/deep/1.yml'] = textwrap.dedent("""\
    variable: {{value}}
    """)

files['/fixtures/simple.yml'] = textwrap.dedent("""\
    is_simple: true
    """)

files['/fixtures/templates.yml'] = textwrap.dedent("""\
    content: 'content'
    variable: '{{woot}}'
    """)

files['/fixtures/extend_parent.yml'] = textwrap.dedent("""\
    $inherits:
      from: 'extend_child.yml'

    list: ['4']
    was_list:
      replaced: true
    obj:
      level: 2
      from_parent: true
      deeper:
        list: ['bar']
    """)


class TemplatesTest(unittest.TestCase):

    def setUp(self):
        self.mocked_open = mozunit.MockedOpen(files)
        self.mocked_open.__enter__()
        self.subject = Templates('/fixtures')

    def tearDown(self):
        self.mocked_open.__exit__(None, None, None)

    def test_invalid_path(self):
        with self.assertRaisesRegexp(TemplatesException, 'must be a directory'):
            Templates('/zomg/not/a/dir')

    def test_no_templates(self):
        content = self.subject.load('simple.yml', {})
        self.assertEquals(content, {
            'is_simple': True
        })

    def test_with_templates(self):
        content = self.subject.load('templates.yml', {
            'woot': 'bar'
        })

        self.assertEquals(content, {
            'content': 'content',
            'variable': 'bar'
        })

    def test_inheritance(self):
        '''
        The simple single pass inheritance case.
        '''
        content = self.subject.load('inherit.yml', {})
        self.assertEqual(content, {
            'content': 'content',
            'variable': 'inherit'
        })

    def test_inheritance_implicat_pass(self):
        '''
        Implicitly pass parameters from the child to the ancestor.
        '''
        content = self.subject.load('inherit_pass.yml', {
            'a': 'overriden'
        })

        self.assertEqual(content, {'values': ['overriden', 'b', 'c']})

    def test_inheritance_circular(self):
        '''
        Circular reference handling.
        '''
        with self.assertRaisesRegexp(TemplatesException, 'circular'):
            self.subject.load('circular.yml', {})

    def test_deep_inheritance(self):
        content = self.subject.load('deep/4.yml', {
            'value': 'myvalue'
        })
        self.assertEqual(content, {'variable': 'myvalue'})

    def test_inheritance_with_simple_extensions(self):
        content = self.subject.load('extend_parent.yml', {})
        self.assertEquals(content, {
            'list': ['1', '2', '3', '4'],
            'obj': {
                'from_parent': True,
                'deeper': {
                    'woot': 'bar',
                    'list': ['baz', 'bar']
                },
                'level': 2,
            },
            'was_list': {'replaced': True}
        })


class MergeTest(unittest.TestCase):

    def test_merge_to_dicts(self):
        source = {'a': 1, 'b': 2}
        dest = {'b': '20', 'c': 30}
        expected = {
            'a': 1,   # source only
            'b': 2,   # source overrides dest
            'c': 30,  # dest only
        }
        self.assertEqual(merge_to(source, dest), expected)
        self.assertEqual(dest, expected)

    def test_merge_to_lists(self):
        source = {'x': [3, 4]}
        dest = {'x': [1, 2]}
        expected = {'x': [1, 2, 3, 4]}  # dest first
        self.assertEqual(merge_to(source, dest), expected)
        self.assertEqual(dest, expected)

    def test_merge_diff_types(self):
        source = {'x': [1, 2]}
        dest = {'x': 'abc'}
        expected = {'x': [1, 2]}  # source wins
        self.assertEqual(merge_to(source, dest), expected)
        self.assertEqual(dest, expected)

    def test_merge(self):
        first = {'a': 1, 'b': 2, 'd': 11}
        second = {'b': 20, 'c': 30}
        third = {'c': 300, 'd': 400}
        expected = {
            'a': 1,
            'b': 20,
            'c': 300,
            'd': 400,
        }
        self.assertEqual(merge(first, second, third), expected)

        # inputs haven't changed..
        self.assertEqual(first, {'a': 1, 'b': 2, 'd': 11})
        self.assertEqual(second, {'b': 20, 'c': 30})
        self.assertEqual(third, {'c': 300, 'd': 400})

if __name__ == '__main__':
    mozunit.main()