1
0

test_postpone.py 2.8 KB

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