test_pydsl.py 2.1 KB

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