test_templates.py 7.7 KB

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