test_template.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email: `Mike Place <mp@saltstack.com>`
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt libs
  8. from salt import template
  9. from salt.ext.six.moves import StringIO
  10. from tests.support.mock import MagicMock
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase
  13. class TemplateTestCase(TestCase):
  14. render_dict = {
  15. "jinja": "fake_jinja_func",
  16. "json": "fake_json_func",
  17. "mako": "fake_make_func",
  18. }
  19. def test_compile_template_bad_type(self):
  20. """
  21. Test to ensure that unsupported types cannot be passed to the template compiler
  22. """
  23. ret = template.compile_template(["1", "2", "3"], None, None, None, None)
  24. self.assertDictEqual(ret, {})
  25. def test_compile_template_preserves_windows_newlines(self):
  26. """
  27. Test to ensure that a file with Windows newlines, when rendered by a
  28. template renderer, does not eat the CR character.
  29. """
  30. def _get_rend(renderer, value):
  31. """
  32. We need a new MagicMock each time since we're dealing with StringIO
  33. objects which are read like files.
  34. """
  35. return {renderer: MagicMock(return_value=StringIO(value))}
  36. input_data_windows = "foo\r\nbar\r\nbaz\r\n"
  37. input_data_non_windows = input_data_windows.replace("\r\n", "\n")
  38. renderer = "test"
  39. blacklist = whitelist = []
  40. ret = template.compile_template(
  41. ":string:",
  42. _get_rend(renderer, input_data_non_windows),
  43. renderer,
  44. blacklist,
  45. whitelist,
  46. input_data=input_data_windows,
  47. ).read()
  48. # Even though the mocked renderer returned a string without the windows
  49. # newlines, the compiled template should still have them.
  50. self.assertEqual(ret, input_data_windows)
  51. # Now test that we aren't adding them in unnecessarily.
  52. ret = template.compile_template(
  53. ":string:",
  54. _get_rend(renderer, input_data_non_windows),
  55. renderer,
  56. blacklist,
  57. whitelist,
  58. input_data=input_data_non_windows,
  59. ).read()
  60. self.assertEqual(ret, input_data_non_windows)
  61. # Finally, ensure that we're not unnecessarily replacing the \n with
  62. # \r\n in the event that the renderer returned a string with the
  63. # windows newlines intact.
  64. ret = template.compile_template(
  65. ":string:",
  66. _get_rend(renderer, input_data_windows),
  67. renderer,
  68. blacklist,
  69. whitelist,
  70. input_data=input_data_windows,
  71. ).read()
  72. self.assertEqual(ret, input_data_windows)
  73. def test_check_render_pipe_str(self):
  74. """
  75. Check that all renderers specified in the pipe string are available.
  76. """
  77. ret = template.check_render_pipe_str("jinja|json", self.render_dict, None, None)
  78. self.assertIn(("fake_jinja_func", ""), ret)
  79. self.assertIn(("fake_json_func", ""), ret)
  80. self.assertNotIn(("OBVIOUSLY_NOT_HERE", ""), ret)
  81. def test_check_renderer_blacklisting(self):
  82. """
  83. Check that all renderers specified in the pipe string are available.
  84. """
  85. ret = template.check_render_pipe_str(
  86. "jinja|json", self.render_dict, ["jinja"], None
  87. )
  88. self.assertListEqual([("fake_json_func", "")], ret)
  89. ret = template.check_render_pipe_str(
  90. "jinja|json", self.render_dict, None, ["jinja"]
  91. )
  92. self.assertListEqual([("fake_jinja_func", "")], ret)
  93. ret = template.check_render_pipe_str(
  94. "jinja|json", self.render_dict, ["jinja"], ["jinja"]
  95. )
  96. self.assertListEqual([], ret)
  97. ret = template.check_render_pipe_str(
  98. "jinja|json", self.render_dict, ["jinja"], ["jinja", "json"]
  99. )
  100. self.assertListEqual([("fake_json_func", "")], ret)