1
0

test_postpone.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. self.schedule.opts['run_schedule_jobs_in_background'] = False
  37. def tearDown(self):
  38. self.schedule.reset()
  39. def test_postpone(self):
  40. '''
  41. verify that scheduled job is postponed until the specified time.
  42. '''
  43. job = {
  44. 'schedule': {
  45. 'job1': {
  46. 'function': 'test.ping',
  47. 'when': '11/29/2017 4pm',
  48. }
  49. }
  50. }
  51. # 11/29/2017 4pm
  52. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  53. # 5 minute delay
  54. delay = 300
  55. # Add job to schedule
  56. self.schedule.opts.update(job)
  57. # Postpone the job by 5 minutes
  58. self.schedule.postpone_job('job1', {'time': run_time.strftime('%Y-%m-%dT%H:%M:%S'),
  59. 'new_time': (run_time + datetime.timedelta(seconds=delay)).strftime('%Y-%m-%dT%H:%M:%S')})
  60. # Run at the original time
  61. self.schedule.eval(now=run_time)
  62. ret = self.schedule.job_status('job1')
  63. self.assertNotIn('_last_run', ret)
  64. # Run 5 minutes later
  65. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay))
  66. ret = self.schedule.job_status('job1')
  67. self.assertEqual(ret['_last_run'], run_time + datetime.timedelta(seconds=delay))
  68. # Run 6 minutes later
  69. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay + 1))
  70. ret = self.schedule.job_status('job1')
  71. self.assertEqual(ret['_last_run'], run_time + datetime.timedelta(seconds=delay))