test_template.py 3.8 KB

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