1
0

test_templates.py 8.0 KB

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