~harlowja/cloud-init/changeable-templates

« back to all changes in this revision

Viewing changes to tests/unittests/test_templating.py

  • Committer: Joshua Harlow
  • Date: 2014-03-08 03:33:41 UTC
  • Revision ID: harlowja@yahoo-inc.com-20140308033341-nfkhdben0e2hijf1
Add some basic template tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vi: ts=4 expandtab
 
2
#
 
3
#    Copyright (C) 2014 Yahoo! Inc.
 
4
#
 
5
#    Author: Joshua Harlow <harlowja@yahoo-inc.com>
 
6
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
from tests.unittests import helpers as test_helpers
 
20
 
 
21
 
 
22
from cloudinit import templater
 
23
 
 
24
 
 
25
class TestTemplates(test_helpers.TestCase):
 
26
    def test_detection(self):
 
27
        blob = "## template:cheetah"
 
28
 
 
29
        (template_type, contents) = templater.detect_template(blob)
 
30
        self.assertEqual("cheetah", template_type)
 
31
        self.assertEqual("", contents.strip())
 
32
 
 
33
        blob = "blahblah $blah"
 
34
        (template_type, contents) = templater.detect_template(blob)
 
35
        self.assertEqual("cheetah", template_type)
 
36
        self.assertEquals(blob, contents)
 
37
 
 
38
        blob = '##template:something-new'
 
39
        self.assertRaises(ValueError, templater.detect_template, blob)
 
40
 
 
41
    def test_render_cheetah(self):
 
42
        blob = '''## template:cheetah
 
43
$a,$b'''
 
44
        c = templater.render_string(blob, {"a": 1, "b": 2})
 
45
        self.assertEquals("1,2", c)
 
46
 
 
47
    def test_render_jinja(self):
 
48
        blob = '''## template:jinja
 
49
{{a}},{{b}}'''
 
50
        c = templater.render_string(blob, {"a": 1, "b": 2})
 
51
        self.assertEquals("1,2", c)
 
52
 
 
53
    def test_render_default(self):
 
54
        blob = '''$a,$b'''
 
55
        c = templater.render_string(blob, {"a": 1, "b": 2})
 
56
        self.assertEquals("1,2", c)