test_idem.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Tests for the idem state
  3. """
  4. import tempfile
  5. from contextlib import contextmanager
  6. import pytest
  7. import salt.utils.idem as idem
  8. import salt.utils.path
  9. pytestmark = pytest.mark.skipif(not idem.HAS_POP[0], reason=idem.HAS_POP[1])
  10. @pytest.mark.skipif(not salt.utils.path.which("idem"), reason="idem is not installed")
  11. @contextmanager
  12. def test_state(salt_call_cli):
  13. with tempfile.NamedTemporaryFile(suffix=".sls", delete=True, mode="w+") as fh:
  14. sls_succeed_without_changes = """
  15. state_name:
  16. test.succeed_without_changes:
  17. - name: idem_test
  18. - foo: bar
  19. """
  20. fh.write(sls_succeed_without_changes)
  21. fh.flush()
  22. ret = salt_call_cli.run(
  23. "--local", "state.single", "idem.state", sls=fh.name, name="idem_test"
  24. )
  25. parent = ret.json["idem_|-idem_test_|-idem_test_|-state"]
  26. assert parent["result"] is True, parent["comment"]
  27. sub_state_ret = parent["sub_state_run"][0]
  28. assert sub_state_ret["result"] is True
  29. assert sub_state_ret["name"] == "idem_test"
  30. assert "Success!" in sub_state_ret["comment"]
  31. def test_bad_state(salt_call_cli):
  32. bad_sls = "non-existant-file.sls"
  33. ret = salt_call_cli.run(
  34. "--local", "state.single", "idem.state", sls=bad_sls, name="idem_bad_test"
  35. )
  36. parent = ret.json["idem_|-idem_bad_test_|-idem_bad_test_|-state"]
  37. assert parent["result"] is False
  38. assert "SLS ref {} did not resolve to a file".format(bad_sls) == parent["comment"]
  39. assert not parent["sub_state_run"]