1
0

test_yamlex.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import skipIf, TestCase
  7. # Import Salt libs
  8. import salt.state
  9. from salt.config import minion_config
  10. from salt.template import compile_template_str
  11. import salt.serializers.yamlex as yamlex
  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(template,
  29. _state.rend,
  30. _state.opts['renderer'],
  31. _state.opts['renderer_blacklist'],
  32. _state.opts['renderer_whitelist'])
  33. class RendererTests(TestCase, RendererMixin, LoaderModuleMockMixin):
  34. def setup_loader_modules(self):
  35. return {yamlex: {}}
  36. @skipIf(not yamlex.available, SKIP_MESSAGE % 'yamlex')
  37. def test_basic(self):
  38. sls_obj = self.render(basic_template)
  39. assert sls_obj == {'foo': 'bar'}, sls_obj
  40. @skipIf(not yamlex.available, SKIP_MESSAGE % 'yamlex')
  41. def test_complex(self):
  42. sls_obj = self.render(complex_template)
  43. assert sls_obj == {
  44. 'placeholder': {
  45. 'foo': {
  46. 'foo': 42,
  47. 'bar': None,
  48. 'baz': 'inga'
  49. }
  50. }
  51. }, sls_obj