1
0

test_templates.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 os
  8. import sys
  9. import logging
  10. # Import Salt libs
  11. import salt.utils.templates
  12. import salt.utils.files
  13. # Import Salt Testing Libs
  14. from tests.support.helpers import with_tempdir
  15. from tests.support.unit import TestCase, skipIf
  16. log = logging.getLogger(__name__)
  17. ### Here we go!
  18. class RenderTestCase(TestCase):
  19. def setUp(self):
  20. # Default context for salt.utils.templates.render_*_tmpl to work
  21. self.context = {
  22. 'opts': {
  23. 'cachedir': '/D',
  24. '__cli': 'salt',
  25. },
  26. 'saltenv': None,
  27. }
  28. ### Tests for Jinja (whitespace-friendly)
  29. def test_render_jinja_sanity(self):
  30. tmpl = '''OK'''
  31. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  32. self.assertEqual(res, 'OK')
  33. def test_render_jinja_evaluate(self):
  34. tmpl = '''{{ "OK" }}'''
  35. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  36. self.assertEqual(res, 'OK')
  37. def test_render_jinja_evaluate_multi(self):
  38. tmpl = '''{% if 1 -%}OK{%- endif %}'''
  39. res = salt.utils.templates.render_jinja_tmpl(tmpl, dict(self.context))
  40. self.assertEqual(res, 'OK')
  41. def test_render_jinja_variable(self):
  42. tmpl = '''{{ var }}'''
  43. ctx = dict(self.context)
  44. ctx['var'] = 'OK'
  45. res = salt.utils.templates.render_jinja_tmpl(tmpl, ctx)
  46. self.assertEqual(res, 'OK')
  47. ### Tests for mako template
  48. def test_render_mako_sanity(self):
  49. tmpl = '''OK'''
  50. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  51. self.assertEqual(res, 'OK')
  52. def test_render_mako_evaluate(self):
  53. tmpl = '''${ "OK" }'''
  54. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  55. self.assertEqual(res, 'OK')
  56. def test_render_mako_evaluate_multi(self):
  57. tmpl = '''
  58. % if 1:
  59. OK
  60. % endif
  61. '''
  62. res = salt.utils.templates.render_mako_tmpl(tmpl, dict(self.context))
  63. stripped = res.strip()
  64. self.assertEqual(stripped, 'OK')
  65. def test_render_mako_variable(self):
  66. tmpl = '''${ var }'''
  67. ctx = dict(self.context)
  68. ctx['var'] = 'OK'
  69. res = salt.utils.templates.render_mako_tmpl(tmpl, ctx)
  70. self.assertEqual(res, 'OK')
  71. ### Tests for wempy template
  72. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  73. def test_render_wempy_sanity(self):
  74. tmpl = '''OK'''
  75. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  76. self.assertEqual(res, 'OK')
  77. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  78. def test_render_wempy_evaluate(self):
  79. tmpl = '''{{="OK"}}'''
  80. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  81. self.assertEqual(res, 'OK')
  82. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  83. def test_render_wempy_evaluate_multi(self):
  84. tmpl = '''{{if 1:}}OK{{pass}}'''
  85. res = salt.utils.templates.render_wempy_tmpl(tmpl, dict(self.context))
  86. self.assertEqual(res, 'OK')
  87. @skipIf(sys.version_info > (3,), 'The wempy module is currently unsupported under Python3')
  88. def test_render_wempy_variable(self):
  89. tmpl = '''{{=var}}'''
  90. ctx = dict(self.context)
  91. ctx['var'] = 'OK'
  92. res = salt.utils.templates.render_wempy_tmpl(tmpl, ctx)
  93. self.assertEqual(res, 'OK')
  94. ### Tests for genshi template (xml-based)
  95. def test_render_genshi_sanity(self):
  96. tmpl = '''<RU>OK</RU>'''
  97. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  98. self.assertEqual(res, '<RU>OK</RU>')
  99. def test_render_genshi_evaluate(self):
  100. tmpl = '''<RU>${ "OK" }</RU>'''
  101. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  102. self.assertEqual(res, '<RU>OK</RU>')
  103. def test_render_genshi_evaluate_condition(self):
  104. tmpl = '''<RU xmlns:py="http://genshi.edgewall.org/" py:if="1">OK</RU>'''
  105. res = salt.utils.templates.render_genshi_tmpl(tmpl, dict(self.context))
  106. self.assertEqual(res, '<RU>OK</RU>')
  107. def test_render_genshi_variable(self):
  108. tmpl = '''<RU>$var</RU>'''
  109. ctx = dict(self.context)
  110. ctx['var'] = 'OK'
  111. res = salt.utils.templates.render_genshi_tmpl(tmpl, ctx)
  112. self.assertEqual(res, '<RU>OK</RU>')
  113. def test_render_genshi_variable_replace(self):
  114. tmpl = '''<RU xmlns:py="http://genshi.edgewall.org/" py:content="var">not ok</RU>'''
  115. ctx = dict(self.context)
  116. ctx['var'] = 'OK'
  117. res = salt.utils.templates.render_genshi_tmpl(tmpl, ctx)
  118. self.assertEqual(res, '<RU>OK</RU>')
  119. ### Tests for cheetah template (line-oriented and xml-friendly)
  120. def test_render_cheetah_sanity(self):
  121. tmpl = '''OK'''
  122. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  123. self.assertEqual(res, 'OK')
  124. def test_render_cheetah_evaluate(self):
  125. tmpl = '''<%="OK"%>'''
  126. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  127. self.assertEqual(res, 'OK')
  128. def test_render_cheetah_evaluate_xml(self):
  129. tmpl = '''
  130. <% if 1: %>
  131. OK
  132. <% pass %>
  133. '''
  134. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  135. stripped = res.strip()
  136. self.assertEqual(stripped, 'OK')
  137. def test_render_cheetah_evaluate_text(self):
  138. tmpl = '''
  139. #if 1
  140. OK
  141. #end if
  142. '''
  143. res = salt.utils.templates.render_cheetah_tmpl(tmpl, dict(self.context))
  144. stripped = res.strip()
  145. self.assertEqual(stripped, 'OK')
  146. def test_render_cheetah_variable(self):
  147. tmpl = '''$var'''
  148. ctx = dict(self.context)
  149. ctx['var'] = 'OK'
  150. res = salt.utils.templates.render_cheetah_tmpl(tmpl, ctx)
  151. self.assertEqual(res.strip(), 'OK')
  152. class MockRender(object):
  153. def __call__(self, tplstr, context, tmplpath=None):
  154. self.tplstr = tplstr
  155. self.context = context
  156. self.tmplpath = tmplpath
  157. return tplstr
  158. class WrapRenderTestCase(TestCase):
  159. @with_tempdir()
  160. def test_wrap_issue_56119_a(self, tempdir):
  161. slsfile = os.path.join(tempdir, 'foo')
  162. with salt.utils.files.fopen(slsfile, 'w') as fp:
  163. fp.write('{{ slspath }}')
  164. context = {'opts': {}, 'saltenv': 'base', 'sls': 'foo.bar'}
  165. render = MockRender()
  166. wrapped = salt.utils.templates.wrap_tmpl_func(render)
  167. res = wrapped(
  168. slsfile,
  169. context=context,
  170. tmplpath='/tmp/foo/bar/init.sls'
  171. )
  172. assert render.context['slspath'] == 'foo/bar', render.context['slspath']
  173. assert render.context['tpldir'] == 'foo/bar', render.context['tpldir']
  174. @with_tempdir()
  175. def test_wrap_issue_56119_b(self, tempdir):
  176. slsfile = os.path.join(tempdir, 'foo')
  177. with salt.utils.files.fopen(slsfile, 'w') as fp:
  178. fp.write('{{ slspath }}')
  179. context = {'opts': {}, 'saltenv': 'base', 'sls': 'foo.bar.bang'}
  180. render = MockRender()
  181. wrapped = salt.utils.templates.wrap_tmpl_func(render)
  182. res = wrapped(
  183. slsfile,
  184. context=context,
  185. tmplpath='/tmp/foo/bar/bang.sls'
  186. )
  187. assert render.context['slspath'] == 'foo/bar', render.context['slspath']
  188. assert render.context['tpldir'] == 'foo/bar', render.context['tpldir']