test_cron.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. Tests for the cron state
  3. """
  4. import logging
  5. import subprocess
  6. import pytest
  7. import salt.utils.platform
  8. from tests.support.helpers import slowTest
  9. log = logging.getLogger(__name__)
  10. @pytest.fixture
  11. def cron_account():
  12. with pytest.helpers.create_account() as system_account:
  13. try:
  14. yield system_account
  15. finally:
  16. command = ["crontab", "-u", system_account.username, "-r"]
  17. if salt.utils.platform.is_freebsd():
  18. command.append("-f")
  19. subprocess.run(command, check=False)
  20. @slowTest
  21. @pytest.mark.skip_on_windows
  22. @pytest.mark.skip_if_not_root
  23. @pytest.mark.skip_if_binaries_missing("crontab")
  24. def test_managed(cron_account, salt_cli, salt_minion, base_env_state_tree_root_dir):
  25. """
  26. file.managed
  27. """
  28. cron_contents = "# Lines below here are managed by Salt, do not edit\n@hourly touch /tmp/test-file\n"
  29. expected = "--- \n+++ \n@@ -1 +1,2 @@\n-\n+# Lines below here are managed by Salt, do not edit\n+@hourly touch /tmp/test-file\n"
  30. with pytest.helpers.temp_file(
  31. "issue-46881/cron", cron_contents, base_env_state_tree_root_dir
  32. ):
  33. ret = salt_cli.run(
  34. "state.single",
  35. "cron.file",
  36. name="salt://issue-46881/cron",
  37. user=cron_account.username,
  38. minion_tgt=salt_minion.id,
  39. )
  40. assert ret.exitcode == 0, ret
  41. state = ret.json["cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file"]
  42. assert "changes" in state
  43. assert "diff" in state["changes"]
  44. assert state["changes"]["diff"] == expected