TestUtils.py 9.47 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import unittest

import ansible.utils

class TestUtils(unittest.TestCase):

    #####################################
    ### varReplace function tests

    def test_varReplace_simple(self):
        template = 'hello $who'
        vars = {
            'who': 'world',
        }

18
        res = ansible.utils.varReplace(None, template, vars)
19 20 21

        assert res == 'hello world'

22 23 24
    def test_varReplace_trailing_dollar(self):
        template = '$what $who $'
        vars = dict(what='hello', who='world')
25
        res = ansible.utils.varReplace(None, template, vars)
26 27
        assert res == 'hello world $'

28 29 30 31 32 33 34
    def test_varReplace_multiple(self):
        template = '$what $who'
        vars = {
            'what': 'hello',
            'who': 'world',
        }

35
        res = ansible.utils.varReplace(None, template, vars)
36 37 38

        assert res == 'hello world'

39 40 41 42 43 44
    def test_varReplace_caps(self):
        template = 'hello $whoVar'
        vars = {
            'whoVar': 'world',
        }

45
        res = ansible.utils.varReplace(None, template, vars)
46 47 48
        print res
        assert res == 'hello world'

49 50 51 52 53 54
    def test_varReplace_middle(self):
        template = 'hello $who!'
        vars = {
            'who': 'world',
        }

55
        res = ansible.utils.varReplace(None, template, vars)
56 57 58 59 60 61 62 63 64

        assert res == 'hello world!'

    def test_varReplace_alternative(self):
        template = 'hello ${who}'
        vars = {
            'who': 'world',
        }

65
        res = ansible.utils.varReplace(None, template, vars)
66 67 68 69 70 71 72 73 74

        assert res == 'hello world'

    def test_varReplace_almost_alternative(self):
        template = 'hello $who}'
        vars = {
            'who': 'world',
        }

75
        res = ansible.utils.varReplace(None, template, vars)
76 77 78 79 80 81 82 83 84

        assert res == 'hello world}'

    def test_varReplace_almost_alternative2(self):
        template = 'hello ${who'
        vars = {
            'who': 'world',
        }

85
        res = ansible.utils.varReplace(None, template, vars)
86 87 88 89 90 91 92 93 94

        assert res == template

    def test_varReplace_alternative_greed(self):
        template = 'hello ${who} }'
        vars = {
            'who': 'world',
        }

95
        res = ansible.utils.varReplace(None, template, vars)
96 97 98 99 100 101 102 103 104 105 106

        assert res == 'hello world }'

    def test_varReplace_notcomplex(self):
        template = 'hello $mydata.who'
        vars = {
            'data': {
                'who': 'world',
            },
        }

107
        res = ansible.utils.varReplace(None, template, vars)
108 109 110 111 112 113 114 115 116 117 118 119

        print res
        assert res == template

    def test_varReplace_nested(self):
        template = 'hello ${data.who}'
        vars = {
            'data': {
                'who': 'world'
            },
        }

120
        res = ansible.utils.varReplace(None, template, vars)
121 122 123 124 125 126 127 128 129 130 131 132

        assert res == 'hello world'

    def test_varReplace_nested_int(self):
        template = '$what ${data.who}'
        vars = {
            'data': {
                'who': 2
            },
            'what': 'hello',
        }

133
        res = ansible.utils.varReplace(None, template, vars)
134 135 136

        assert res == 'hello 2'

137 138 139 140 141 142
    def test_varReplace_unicode(self):
        template = 'hello $who'
        vars = {
            'who': u'wórld',
        }

143
        res = ansible.utils.varReplace(None, template, vars)
144 145 146

        assert res == u'hello wórld'

147 148 149 150 151 152
    def test_varReplace_list(self):
        template = 'hello ${data[1]}'
        vars = {
            'data': [ 'no-one', 'world' ]
        }

153
        res = ansible.utils.varReplace(None, template, vars)
154 155 156 157 158 159 160 161 162

        assert res == 'hello world'

    def test_varReplace_invalid_list(self):
        template = 'hello ${data[1}'
        vars = {
            'data': [ 'no-one', 'world' ]
        }

163
        res = ansible.utils.varReplace(None, template, vars)
164 165 166 167 168 169 170 171 172

        assert res == template

    def test_varReplace_list_oob(self):
        template = 'hello ${data[2]}'
        vars = {
            'data': [ 'no-one', 'world' ]
        }

173
        res = ansible.utils.varReplace(None, template, vars)
174 175 176 177 178 179 180 181 182

        assert res == template

    def test_varReplace_list_nolist(self):
        template = 'hello ${data[1]}'
        vars = {
            'data': { 'no-one': 0, 'world': 1 }
        }

183
        res = ansible.utils.varReplace(None, template, vars)
184 185 186 187 188 189 190 191 192

        assert res == template

    def test_varReplace_nested_list(self):
        template = 'hello ${data[1].msg[0]}'
        vars = {
            'data': [ 'no-one', {'msg': [ 'world'] } ]
        }

193
        res = ansible.utils.varReplace(None, template, vars)
194 195 196

        assert res == 'hello world'

197 198 199 200 201 202 203
    def test_varReplace_consecutive_vars(self):
        vars = {
            'foo': 'foo',
            'bar': 'bar',
        }

        template = '${foo}${bar}'
204
        res = ansible.utils.varReplace(None, template, vars)
205 206 207 208 209 210 211 212 213 214 215 216
        assert res == 'foobar'

    def test_varReplace_escape_dot(self):
        vars = {
            'hostvars': {
                'test.example.com': {
                    'foo': 'bar',
                },
            },
        }

        template = '${hostvars.{test.example.com}.foo}'
217
        res = ansible.utils.varReplace(None, template, vars)
218 219
        assert res == 'bar'

220 221 222 223 224 225 226 227 228 229
    def test_varReplace_list_join(self):
        vars = {
            'list': [
                'foo',
                'bar',
                'baz',
            ],
        }

        template = 'yum pkg=${list} state=installed'
230
        res = ansible.utils.varReplace(None, template, vars, expand_lists=True)
231 232
        assert res == 'yum pkg=foo,bar,baz state=installed'

233 234 235 236 237
    def test_varReplace_escaped_var(self):
        vars = {
            'foo': 'bar',
        }
        template = 'action \$foo'
238
        res = ansible.utils.varReplace(None, template, vars)
239 240
        assert res == 'action $foo'

241 242 243 244 245 246 247 248
    def test_varReplace_var_part(self):
        vars = {
            'foo': {
                'bar': 'result',
            },
            'key': 'bar',
        }
        template = 'test ${foo.$key}'
249
        res = ansible.utils.varReplace(None, template, vars)
250 251 252 253 254 255 256 257 258 259
        assert res == 'test result'

    def test_varReplace_var_partial_part(self):
        vars = {
            'foo': {
                'barbaz': 'result',
            },
            'key': 'bar',
        }
        template = 'test ${foo.${key}baz}'
260
        res = ansible.utils.varReplace(None, template, vars)
261 262
        assert res == 'test result'

263 264 265 266 267 268 269 270 271 272 273
    def test_varReplace_var_complex_var(self):
        vars = {
            'x': '$y',
            'y': {
                'foo': 'result',
            },
        }
        template = '${x.foo}'
        res = ansible.utils.template(None, template, vars)
        assert res == 'result'

274 275 276 277 278 279 280
    def test_template_varReplace_iterated(self):
        template = 'hello $who'
        vars = {
            'who': 'oh great $person',
            'person': 'one',
        }

281
        res = ansible.utils.template(None, template, vars)
282 283 284

        assert res == u'hello oh great one'

285
    def test_varReplace_include(self):
286
        template = 'hello $FILE(world) $LOOKUP(file, $filename)'
287

288
        res = ansible.utils.template("test", template, {'filename': 'world'}, expand_lists=True)
289

290
        assert res == u'hello world world'
291 292

    def test_varReplace_include_script(self):
293
        template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)'
294

295
        res = ansible.utils.template("test", template, {}, expand_lists=True)
296

297
        assert res == u'hello world world'
298

299
    #####################################
300
    ### template_ds function tests
301

302
    def test_template_ds_basic(self):
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        vars = {
            'data': {
                'var': [
                    'foo',
                    'bar',
                    'baz',
                ],
                'types': [
                    'str',
                    u'unicode',
                    1,
                    1L,
                    1.2,
                ],
                'alphas': '$alphas',
            },
            'alphas': [
                'abc',
                'def',
                'ghi',
            ],
        }

        template = '${data.var}'
327
        res = ansible.utils.template(None, template, vars)
328 329 330
        assert sorted(res) == sorted(vars['data']['var'])

        template = '${data.types}'
331
        res = ansible.utils.template(None, template, vars)
332 333 334
        assert sorted(res) == sorted(vars['data']['types'])

        template = '${data.alphas}'
335
        res = ansible.utils.template(None, template, vars)
336 337
        assert sorted(res) == sorted(vars['alphas'])

338
        template = '${data.nonexisting}'
339
        res = ansible.utils.template(None, template, vars)
340 341
        assert res == template

342
    #####################################
343 344 345 346 347 348 349
    ### Template function tests

    def test_template_basic(self):
        vars = {
            'who': 'world',
        }

Michael DeHaan committed
350
        res = ansible.utils.template_from_file("test", "template-basic", vars)
351 352 353 354 355 356 357 358

        assert res == 'hello world'

    def test_template_whitespace(self):
        vars = {
            'who': 'world',
        }

Michael DeHaan committed
359
        res = ansible.utils.template_from_file("test", "template-whitespace", vars)
360 361

        assert res == 'hello world\n'
362 363 364 365 366 367

    def test_template_unicode(self):
        vars = {
            'who': u'wórld',
        }

Michael DeHaan committed
368
        res = ansible.utils.template_from_file("test", "template-basic", vars)
369 370

        assert res == u'hello wórld'
Matt Goodall committed
371 372 373 374 375 376 377

    #####################################
    ### key-value parsing

    def test_parse_kv_basic(self):
        assert (ansible.utils.parse_kv('a=simple b="with space" c="this=that"') ==
                {'a': 'simple', 'b': 'with space', 'c': 'this=that'})