test_templates.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Unit tests for salt.utils.templates.py
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import sys
  8. import logging
  9. # Import Salt libs
  10. import salt.utils.templates
  11. # Import Salt Testing Libs
  12. from tests.support.unit import TestCase, skipIf
  13. log = logging.getLogger(__name__)
  14. ### Here we go!
  15. class RenderTestCase(TestCase):
  16. def setUp(self):
  17. # Default context for salt.utils.templates.render_*_tmpl to work
  18. self.context = {
  19. 'opts': {
  20. 'cachedir': '/D',
  21. '__cli': 'salt',
  22. },
  23. 'saltenv': None,
  24. }
  25. ### Tests for Jinja (whitespace-friendly)
  26. def test_render_jinja_sanity(self):
  27. tmpl = '''OK'''
  28. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  29. self.assertEqual(res, 'OK')
  30. def test_render_jinja_evaluate(self):
  31. tmpl = '''{{ "OK" }}'''
  32. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  33. self.assertEqual(res, 'OK')
  34. def test_render_jinja_evaluate_multi(self):
  35. tmpl = '''{% if 1 -%}OK{%- endif %}'''
  36. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  37. self.assertEqual(res, 'OK')
  38. def test_render_jinja_variable(self):
  39. tmpl = '''{{ var }}'''
  40. ctx = dict(self.context)
  41. ctx['var'] = 'OK'
  42. res = salt.utils.templates.render_jinja_tmpl(tmpl, ctx)
  43. self.assertEqual(res, 'OK')
  44. ### Tests for mako template
  45. def test_render_mako_sanity(self):
  46. tmpl = '''OK'''
  47. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  48. self.assertEqual(res, 'OK')
  49. def test_render_mako_evaluate(self):
  50. tmpl = '''${ "OK" }'''
  51. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  52. self.assertEqual(res, 'OK')
  53. def test_render_mako_evaluate_multi(self):
  54. tmpl = '''
  55. % if 1:
  56. OK
  57. % endif
  58. '''
  59. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  60. stripped = res.strip()
  61. self.assertEqual(stripped, 'OK')
  62. def test_render_mako_variable(self):
  63. tmpl = '''${ var }'''
  64. ctx = dict(self.context)
  65. ctx['var'] = 'OK'
  66. res = salt.utils.templates.render_mako_tmpl(tmpl, ctx)
  67. self.assertEqual(res, 'OK')
  68. ### Tests for wempy template
  69. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  70. def test_render_wempy_sanity(self):
  71. tmpl = '''OK'''
  72. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  73. self.assertEqual(res, 'OK')
  74. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  75. def test_render_wempy_evaluate(self):
  76. tmpl = '''{{="OK"}}'''
  77. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  78. self.assertEqual(res, 'OK')
  79. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  80. def test_render_wempy_evaluate_multi(self):
  81. tmpl = '''{{if 1:}}OK{{pass}}'''
  82. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  83. self.assertEqual(res, 'OK')
  84. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  85. def test_render_wempy_variable(self):
  86. tmpl = '''{{=var}}'''
  87. ctx = dict(self.context)
  88. ctx['var'] = 'OK'
  89. res = salt.utils.templates.render_wempy_tmpl(tmpl, ctx)
  90. self.assertEqual(res, 'OK')
  91. ### Tests for genshi template (xml-based)
  92. def test_render_genshi_sanity(self):
  93. tmpl = '''<RU>OK</RU>'''
  94. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  95. self.assertEqual(res, '<RU>OK</RU>')
  96. def test_render_genshi_evaluate(self):
  97. tmpl = '''<RU>${ "OK" }</RU>'''
  98. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  99. self.assertEqual(res, '<RU>OK</RU>')
  100. def test_render_genshi_evaluate_condition(self):
  101. tmpl = '''<RU xmlns:py="http://genshi.edgewall.org/" py:if="1">OK</RU>'''
  102. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  103. self.assertEqual(res, '<RU>OK</RU>')
  104. def test_render_genshi_variable(self):
  105. tmpl = '''<RU>$var</RU>'''
  106. ctx = dict(self.context)
  107. ctx['var'] = 'OK'
  108. res = salt.utils.templates.render_genshi_tmpl(tmpl, ctx)
  109. self.assertEqual(res, '<RU>OK</RU>')
  110. def test_render_genshi_variable_replace(self):
  111. tmpl = '''<RU xmlns:py="http://genshi.edgewall.org/" py:content="var">not ok</RU>'''
  112. ctx = dict(self.context)
  113. ctx['var'] = 'OK'
  114. res = salt.utils.templates.render_genshi_tmpl(tmpl, ctx)
  115. self.assertEqual(res, '<RU>OK</RU>')
  116. ### Tests for cheetah template (line-oriented and xml-friendly)
  117. def test_render_cheetah_sanity(self):
  118. tmpl = '''OK'''
  119. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  120. self.assertEqual(res, 'OK')
  121. def test_render_cheetah_evaluate(self):
  122. tmpl = '''<%="OK"%>'''
  123. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  124. self.assertEqual(res, 'OK')
  125. def test_render_cheetah_evaluate_xml(self):
  126. tmpl = '''
  127. <% if 1: %>
  128. OK
  129. <% pass %>
  130. '''
  131. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  132. stripped = res.strip()
  133. self.assertEqual(stripped, 'OK')
  134. def test_render_cheetah_evaluate_text(self):
  135. tmpl = '''
  136. #if 1
  137. OK
  138. #end if
  139. '''
  140. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  141. stripped = res.strip()
  142. self.assertEqual(stripped, 'OK')
  143. def test_render_cheetah_variable(self):
  144. tmpl = '''$var'''
  145. ctx = dict(self.context)
  146. ctx['var'] = 'OK'
  147. res = salt.utils.templates.render_cheetah_tmpl(tmpl, ctx)
  148. self.assertEqual(res.strip(), 'OK')