test_template.py 3.9 KB

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