forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestUtils.py
377 lines (285 loc) · 9.47 KB
/
TestUtils.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
import unittest
import ansible.utils
class TestUtils(unittest.TestCase):
#####################################
### varReplace function tests
def test_varReplace_simple(self):
template = 'hello $who'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_trailing_dollar(self):
template = '$what $who $'
vars = dict(what='hello', who='world')
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world $'
def test_varReplace_multiple(self):
template = '$what $who'
vars = {
'what': 'hello',
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_caps(self):
template = 'hello $whoVar'
vars = {
'whoVar': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
print res
assert res == 'hello world'
def test_varReplace_middle(self):
template = 'hello $who!'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world!'
def test_varReplace_alternative(self):
template = 'hello ${who}'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_almost_alternative(self):
template = 'hello $who}'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world}'
def test_varReplace_almost_alternative2(self):
template = 'hello ${who'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == template
def test_varReplace_alternative_greed(self):
template = 'hello ${who} }'
vars = {
'who': 'world',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world }'
def test_varReplace_notcomplex(self):
template = 'hello $mydata.who'
vars = {
'data': {
'who': 'world',
},
}
res = ansible.utils.varReplace(None, template, vars)
print res
assert res == template
def test_varReplace_nested(self):
template = 'hello ${data.who}'
vars = {
'data': {
'who': 'world'
},
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_nested_int(self):
template = '$what ${data.who}'
vars = {
'data': {
'who': 2
},
'what': 'hello',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello 2'
def test_varReplace_unicode(self):
template = 'hello $who'
vars = {
'who': u'wórld',
}
res = ansible.utils.varReplace(None, template, vars)
assert res == u'hello wórld'
def test_varReplace_list(self):
template = 'hello ${data[1]}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_invalid_list(self):
template = 'hello ${data[1}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(None, template, vars)
assert res == template
def test_varReplace_list_oob(self):
template = 'hello ${data[2]}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(None, template, vars)
assert res == template
def test_varReplace_list_nolist(self):
template = 'hello ${data[1]}'
vars = {
'data': { 'no-one': 0, 'world': 1 }
}
res = ansible.utils.varReplace(None, template, vars)
assert res == template
def test_varReplace_nested_list(self):
template = 'hello ${data[1].msg[0]}'
vars = {
'data': [ 'no-one', {'msg': [ 'world'] } ]
}
res = ansible.utils.varReplace(None, template, vars)
assert res == 'hello world'
def test_varReplace_consecutive_vars(self):
vars = {
'foo': 'foo',
'bar': 'bar',
}
template = '${foo}${bar}'
res = ansible.utils.varReplace(None, template, vars)
assert res == 'foobar'
def test_varReplace_escape_dot(self):
vars = {
'hostvars': {
'test.example.com': {
'foo': 'bar',
},
},
}
template = '${hostvars.{test.example.com}.foo}'
res = ansible.utils.varReplace(None, template, vars)
assert res == 'bar'
def test_varReplace_list_join(self):
vars = {
'list': [
'foo',
'bar',
'baz',
],
}
template = 'yum pkg=${list} state=installed'
res = ansible.utils.varReplace(None, template, vars, expand_lists=True)
assert res == 'yum pkg=foo,bar,baz state=installed'
def test_varReplace_escaped_var(self):
vars = {
'foo': 'bar',
}
template = 'action \$foo'
res = ansible.utils.varReplace(None, template, vars)
assert res == 'action $foo'
def test_varReplace_var_part(self):
vars = {
'foo': {
'bar': 'result',
},
'key': 'bar',
}
template = 'test ${foo.$key}'
res = ansible.utils.varReplace(None, template, vars)
assert res == 'test result'
def test_varReplace_var_partial_part(self):
vars = {
'foo': {
'barbaz': 'result',
},
'key': 'bar',
}
template = 'test ${foo.${key}baz}'
res = ansible.utils.varReplace(None, template, vars)
assert res == 'test result'
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'
def test_template_varReplace_iterated(self):
template = 'hello $who'
vars = {
'who': 'oh great $person',
'person': 'one',
}
res = ansible.utils.template(None, template, vars)
assert res == u'hello oh great one'
def test_varReplace_include(self):
template = 'hello $FILE(world) $LOOKUP(file, $filename)'
res = ansible.utils.template("test", template, {'filename': 'world'}, expand_lists=True)
assert res == u'hello world world'
def test_varReplace_include_script(self):
template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)'
res = ansible.utils.template("test", template, {}, expand_lists=True)
assert res == u'hello world world'
#####################################
### template_ds function tests
def test_template_ds_basic(self):
vars = {
'data': {
'var': [
'foo',
'bar',
'baz',
],
'types': [
'str',
u'unicode',
1,
1L,
1.2,
],
'alphas': '$alphas',
},
'alphas': [
'abc',
'def',
'ghi',
],
}
template = '${data.var}'
res = ansible.utils.template(None, template, vars)
assert sorted(res) == sorted(vars['data']['var'])
template = '${data.types}'
res = ansible.utils.template(None, template, vars)
assert sorted(res) == sorted(vars['data']['types'])
template = '${data.alphas}'
res = ansible.utils.template(None, template, vars)
assert sorted(res) == sorted(vars['alphas'])
template = '${data.nonexisting}'
res = ansible.utils.template(None, template, vars)
assert res == template
#####################################
### Template function tests
def test_template_basic(self):
vars = {
'who': 'world',
}
res = ansible.utils.template_from_file("test", "template-basic", vars)
assert res == 'hello world'
def test_template_whitespace(self):
vars = {
'who': 'world',
}
res = ansible.utils.template_from_file("test", "template-whitespace", vars)
assert res == 'hello world\n'
def test_template_unicode(self):
vars = {
'who': u'wórld',
}
res = ansible.utils.template_from_file("test", "template-basic", vars)
assert res == u'hello wórld'
#####################################
### 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'})