test_cron.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the cron state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. log = logging.getLogger(__name__)
  9. # Import Salt Testing libs
  10. from tests.support.case import ModuleCase
  11. from tests.support.unit import skipIf
  12. # Import Salt libs
  13. import salt.utils.platform
  14. @skipIf(salt.utils.platform.is_windows(), 'minion is windows')
  15. class CronTest(ModuleCase):
  16. '''
  17. Validate the file state
  18. '''
  19. def setUp(self):
  20. '''
  21. Setup
  22. '''
  23. self.run_state('user.present', name='test_cron_user')
  24. self.user_primary_group = self.run_function('user.primary_group',
  25. name='test_cron_user')
  26. def tearDown(self):
  27. '''
  28. Teardown
  29. '''
  30. # Remove cron file
  31. self.run_function('cmd.run',
  32. cmd='crontab -u test_cron_user -r')
  33. # Delete user
  34. self.run_state('user.absent', name='test_cron_user')
  35. def test_46881(self):
  36. user_id = 'test_cron_user'
  37. _expected = {
  38. 'changes': {
  39. 'diff': '--- \n+++ \n@@ -1 +1,2 @@\n-\n+# Lines below here are managed by Salt, do not edit\n+@hourly touch /tmp/test-file\n',
  40. 'group': self.user_primary_group,
  41. 'user': user_id,
  42. },
  43. }
  44. ret = self.run_state(
  45. 'cron.file',
  46. name='salt://issue-46881/cron',
  47. user=user_id,
  48. )
  49. # There are several keys that do not really matter to this test.
  50. # We could just delete them, but then we lose their contents to
  51. # aid in debugging (see https://github.com/saltstack/salt/issues/52079)
  52. ignored_keys = (
  53. '__id__',
  54. '__sls__',
  55. '__run_num__',
  56. 'comment',
  57. 'duration',
  58. 'name',
  59. 'start_time',
  60. 'result',
  61. )
  62. id_ = 'cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file'
  63. for key in ignored_keys:
  64. _expected[key] = ret[id_].get(key)
  65. retchanges = ret[id_].get('changes', {}).get('attrs', None)
  66. if retchanges is not None:
  67. _expected['changes']['attrs'] = retchanges
  68. self.assertDictEqual(
  69. _expected,
  70. ret[id_],
  71. )