test_env.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test case for env sdb module
  4. '''
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import textwrap
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.paths import FILES
  11. from tests.support.mixins import SaltReturnAssertsMixin
  12. # Import salt libs
  13. import salt.utils.files
  14. STATE_DIR = os.path.join(FILES, 'file', 'base')
  15. class EnvTestCase(ModuleCase, SaltReturnAssertsMixin):
  16. def setUp(self):
  17. self.state_name = 'test_sdb_env'
  18. self.state_file_name = self.state_name + '.sls'
  19. self.state_file_set_var = os.path.join(STATE_DIR, self.state_file_name)
  20. with salt.utils.files.fopen(self.state_file_set_var, 'w') as wfh:
  21. wfh.write(textwrap.dedent('''\
  22. set some env var:
  23. cmd.run:
  24. - name: echo {{ salt['sdb.set']('sdb://osenv/foo', 'bar') }}
  25. - order: 1
  26. {% if salt['sdb.get']('sdb://osenv/foo') == 'bar' %}
  27. always-changes-and-succeeds:
  28. test.succeed_with_changes:
  29. - name: foo
  30. {% else %}
  31. always-changes-and-fails:
  32. test.fail_with_changes:
  33. - name: foo
  34. {% endif %}
  35. '''))
  36. def tearDown(self):
  37. os.remove(self.state_file_set_var)
  38. def test_env_module_sets_key(self):
  39. state_key = 'test_|-always-changes-and-succeeds_|-foo_|-succeed_with_changes'
  40. ret = self.run_function('state.sls', [self.state_name])
  41. self.assertTrue(ret[state_key]['result'])