test_env.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 pytest
  9. import salt.utils.files
  10. from tests.support.case import ModuleCase
  11. from tests.support.mixins import SaltReturnAssertsMixin
  12. from tests.support.runtests import RUNTIME_VARS
  13. @pytest.mark.windows_whitelisted
  14. class EnvTestCase(ModuleCase, SaltReturnAssertsMixin):
  15. def setUp(self):
  16. self.state_name = "test_sdb_env"
  17. self.state_file_name = self.state_name + ".sls"
  18. self.state_file_set_var = os.path.join(
  19. RUNTIME_VARS.BASE_FILES, self.state_file_name
  20. )
  21. with salt.utils.files.fopen(self.state_file_set_var, "w") as wfh:
  22. wfh.write(
  23. textwrap.dedent(
  24. """\
  25. set some env var:
  26. cmd.run:
  27. - name: echo {{ salt['sdb.set']('sdb://osenv/foo', 'bar') }}
  28. - order: 1
  29. {% if salt['sdb.get']('sdb://osenv/foo') == 'bar' %}
  30. always-changes-and-succeeds:
  31. test.succeed_with_changes:
  32. - name: foo
  33. {% else %}
  34. always-changes-and-fails:
  35. test.fail_with_changes:
  36. - name: foo
  37. {% endif %}
  38. """
  39. )
  40. )
  41. def tearDown(self):
  42. os.remove(self.state_file_set_var)
  43. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  44. def test_env_module_sets_key(self):
  45. state_key = "test_|-always-changes-and-succeeds_|-foo_|-succeed_with_changes"
  46. ret = self.run_function("state.sls", [self.state_name])
  47. self.assertTrue(ret[state_key]["result"])