test_cron.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. Tests for the cron state
  3. """
  4. import logging
  5. import pprint
  6. import salt.utils.platform
  7. from tests.support.case import ModuleCase
  8. from tests.support.helpers import skip_if_binaries_missing, slowTest
  9. from tests.support.unit import skipIf
  10. log = logging.getLogger(__name__)
  11. @skipIf(salt.utils.platform.is_windows(), "minion is windows")
  12. @skip_if_binaries_missing("crontab")
  13. class CronTest(ModuleCase):
  14. """
  15. Validate the file state
  16. """
  17. def setUp(self):
  18. """
  19. Setup
  20. """
  21. ret = self.run_state("user.present", name="test_cron_user")
  22. assert ret
  23. def tearDown(self):
  24. """
  25. Teardown
  26. """
  27. # Remove cron file
  28. if salt.utils.platform.is_freebsd():
  29. self.run_function("cmd.run", cmd="crontab -u test_cron_user -rf")
  30. else:
  31. self.run_function("cmd.run", cmd="crontab -u test_cron_user -r")
  32. # Delete user
  33. self.run_state("user.absent", name="test_cron_user")
  34. @slowTest
  35. def test_managed(self):
  36. """
  37. file.managed
  38. """
  39. ret = self.run_state(
  40. "cron.file", name="salt://issue-46881/cron", user="test_cron_user"
  41. )
  42. assert ret
  43. self.assertIn(
  44. "cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file",
  45. ret,
  46. msg="Assertion failed. run_state retuned: {}".format(pprint.pformat(ret)),
  47. )
  48. state = ret["cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file"]
  49. self.assertIn(
  50. "changes",
  51. state,
  52. msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
  53. )
  54. self.assertIn(
  55. "diff",
  56. state["changes"],
  57. msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
  58. )
  59. 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"
  60. self.assertEqual(
  61. expected,
  62. state["changes"]["diff"],
  63. msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
  64. )