1
0

test_postpone.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. import copy
  5. import datetime
  6. import logging
  7. import os
  8. import dateutil.parser as dateutil_parser
  9. # Import Salt Testing libs
  10. from tests.support.case import ModuleCase
  11. from tests.support.mixins import SaltReturnAssertsMixin
  12. # Import Salt Testing Libs
  13. from tests.support.mock import MagicMock, patch
  14. import tests.integration as integration
  15. # Import Salt libs
  16. import salt.utils.schedule
  17. from salt.modules.test import ping as ping
  18. log = logging.getLogger(__name__)
  19. ROOT_DIR = os.path.join(integration.TMP, 'schedule-unit-tests')
  20. SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
  21. DEFAULT_CONFIG = salt.config.minion_config(None)
  22. DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
  23. DEFAULT_CONFIG['root_dir'] = ROOT_DIR
  24. DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
  25. DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
  26. DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
  27. class SchedulerPostponeTest(ModuleCase, SaltReturnAssertsMixin):
  28. '''
  29. Validate the pkg module
  30. '''
  31. def setUp(self):
  32. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  33. functions = {'test.ping': ping}
  34. self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
  35. self.schedule.opts['loop_interval'] = 1
  36. def tearDown(self):
  37. self.schedule.reset()
  38. def test_postpone(self):
  39. '''
  40. verify that scheduled job is postponed until the specified time.
  41. '''
  42. job = {
  43. 'schedule': {
  44. 'job1': {
  45. 'function': 'test.ping',
  46. 'when': '11/29/2017 4pm',
  47. }
  48. }
  49. }
  50. # 11/29/2017 4pm
  51. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  52. # 5 minute delay
  53. delay = 300
  54. # Add job to schedule
  55. self.schedule.opts.update(job)
  56. # Postpone the job by 5 minutes
  57. self.schedule.postpone_job('job1', {'time': run_time.strftime('%Y-%m-%dT%H:%M:%S'),
  58. 'new_time': (run_time + datetime.timedelta(seconds=delay)).strftime('%Y-%m-%dT%H:%M:%S')})
  59. # Run at the original time
  60. self.schedule.eval(now=run_time)
  61. ret = self.schedule.job_status('job1')
  62. self.assertNotIn('_last_run', ret)
  63. # Run 5 minutes later
  64. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay))
  65. ret = self.schedule.job_status('job1')
  66. self.assertEqual(ret['_last_run'], run_time + datetime.timedelta(seconds=delay))
  67. # Run 6 minutes later
  68. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay + 1))
  69. ret = self.schedule.job_status('job1')
  70. self.assertEqual(ret['_last_run'], run_time + datetime.timedelta(seconds=delay))