1
0

test_yamlex.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import salt.serializers.yamlex as yamlex
  5. # Import Salt libs
  6. import salt.state
  7. from salt.config import minion_config
  8. from salt.template import compile_template_str
  9. # Import Salt Testing libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase, skipIf
  12. basic_template = """#!yamlex
  13. foo: bar
  14. """
  15. complex_template = """#!yamlex
  16. placeholder: {foo: !aggregate {foo: 42}}
  17. placeholder: {foo: !aggregate {bar: null}}
  18. placeholder: {foo: !aggregate {baz: inga}}
  19. """
  20. SKIP_MESSAGE = "%s is unavailable, do prerequisites have been met?"
  21. class RendererMixin(object):
  22. def render(self, template, opts=None):
  23. _config = minion_config(None)
  24. _config["file_client"] = "local"
  25. if opts:
  26. _config.update(opts)
  27. _state = salt.state.State(_config)
  28. return compile_template_str(
  29. template,
  30. _state.rend,
  31. _state.opts["renderer"],
  32. _state.opts["renderer_blacklist"],
  33. _state.opts["renderer_whitelist"],
  34. )
  35. class RendererTests(TestCase, RendererMixin, LoaderModuleMockMixin):
  36. def setup_loader_modules(self):
  37. return {yamlex: {}}
  38. @skipIf(not yamlex.available, SKIP_MESSAGE % "yamlex")
  39. def test_basic(self):
  40. sls_obj = self.render(basic_template)
  41. assert sls_obj == {"foo": "bar"}, sls_obj
  42. @skipIf(not yamlex.available, SKIP_MESSAGE % "yamlex")
  43. def test_complex(self):
  44. sls_obj = self.render(complex_template)
  45. assert sls_obj == {
  46. "placeholder": {"foo": {"foo": 42, "bar": None, "baz": "inga"}}
  47. }, sls_obj