3
# Copyright (C) 2014 Yahoo! Inc.
5
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
from __future__ import print_function
21
from . import helpers as test_helpers
24
from cloudinit import templater
29
Cheetah # make pyflakes happy, as Cheetah is not used here
34
class TestTemplates(test_helpers.TestCase):
35
def test_render_basic(self):
36
in_data = textwrap.dedent("""
41
in_data = in_data.strip()
42
expected_data = textwrap.dedent("""
47
out_data = templater.basic_render(in_data, {'b': 2})
48
self.assertEqual(expected_data.strip(), out_data)
50
@test_helpers.skipIf(not HAS_CHEETAH, 'cheetah renderer not available')
51
def test_detection(self):
52
blob = "## template:cheetah"
54
(template_type, renderer, contents) = templater.detect_template(blob)
55
self.assertIn("cheetah", template_type)
56
self.assertEqual("", contents.strip())
58
blob = "blahblah $blah"
59
(template_type, renderer, contents) = templater.detect_template(blob)
60
self.assertIn("cheetah", template_type)
61
self.assertEqual(blob, contents)
63
blob = '##template:something-new'
64
self.assertRaises(ValueError, templater.detect_template, blob)
66
def test_render_cheetah(self):
67
blob = '''## template:cheetah
69
c = templater.render_string(blob, {"a": 1, "b": 2})
70
self.assertEqual("1,2", c)
72
def test_render_jinja(self):
73
blob = '''## template:jinja
75
c = templater.render_string(blob, {"a": 1, "b": 2})
76
self.assertEqual("1,2", c)
78
def test_render_default(self):
80
c = templater.render_string(blob, {"a": 1, "b": 2})
81
self.assertEqual("1,2", c)
83
def test_render_basic_deeper(self):
84
hn = 'myfoohost.yahoo.com'
85
expected_data = "h=%s\nc=d\n" % hn
86
in_data = "h=$hostname.canonical_name\nc=d\n"
92
out_data = templater.render_string(in_data, params)
93
self.assertEqual(expected_data, out_data)
95
def test_render_basic_no_parens(self):
97
in_data = "h=$hostname\nc=d\n"
98
expected_data = "h=%s\nc=d\n" % hn
99
out_data = templater.basic_render(in_data, {'hostname': hn})
100
self.assertEqual(expected_data, out_data)
102
def test_render_basic_parens(self):
104
in_data = "h = ${hostname}\nc=d\n"
105
expected_data = "h = %s\nc=d\n" % hn
106
out_data = templater.basic_render(in_data, {'hostname': hn})
107
self.assertEqual(expected_data, out_data)
109
def test_render_basic2(self):
112
in_data = "deb $mirror $codename-updates main contrib non-free"
113
ex_data = "deb %s %s-updates main contrib non-free" % (mirror,
116
out_data = templater.basic_render(in_data,
118
'codename': codename})
119
self.assertEqual(ex_data, out_data)