test_pydsl.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import shutil
  6. import textwrap
  7. # Import Salt Testing libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import destructiveTest
  10. # Import Salt libs
  11. import salt.utils.files
  12. import salt.utils.platform
  13. import salt.utils.stringutils
  14. class PyDSLRendererIncludeTestCase(ModuleCase):
  15. def setUp(self):
  16. self.directory_created = False
  17. if salt.utils.platform.is_windows():
  18. if not os.path.isdir('\\tmp'):
  19. os.mkdir('\\tmp')
  20. self.directory_created = True
  21. def tearDown(self):
  22. if salt.utils.platform.is_windows():
  23. if self.directory_created:
  24. shutil.rmtree('\\tmp')
  25. @destructiveTest
  26. def test_rendering_includes(self):
  27. '''
  28. This test is currently hard-coded to /tmp to work-around a seeming
  29. inability to load custom modules inside the pydsl renderers. This
  30. is a FIXME.
  31. '''
  32. self.run_function('state.sls', ['pydsl.aaa'])
  33. expected = textwrap.dedent('''\
  34. X1
  35. X2
  36. X3
  37. Y1 extended
  38. Y2 extended
  39. Y3
  40. hello red 1
  41. hello green 2
  42. hello blue 3
  43. ''')
  44. # Windows adds `linefeed` in addition to `newline`. There's also an
  45. # unexplainable space before the `linefeed`...
  46. if salt.utils.platform.is_windows():
  47. expected = 'X1 \r\n' \
  48. 'X2 \r\n' \
  49. 'X3 \r\n' \
  50. 'Y1 extended \r\n' \
  51. 'Y2 extended \r\n' \
  52. 'Y3 \r\n' \
  53. 'hello red 1 \r\n' \
  54. 'hello green 2 \r\n' \
  55. 'hello blue 3 \r\n'
  56. try:
  57. with salt.utils.files.fopen('/tmp/output', 'r') as f:
  58. ret = salt.utils.stringutils.to_unicode(f.read())
  59. finally:
  60. os.remove('/tmp/output')
  61. self.assertEqual(sorted(ret), sorted(expected))