1
0

test_skip.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. import copy
  5. import logging
  6. import os
  7. import dateutil.parser as dateutil_parser
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.mixins import SaltReturnAssertsMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.runtests import RUNTIME_VARS
  13. # Import Salt libs
  14. import salt.utils.schedule
  15. from salt.modules.test import ping
  16. log = logging.getLogger(__name__)
  17. ROOT_DIR = os.path.join(RUNTIME_VARS.TMP, 'schedule-unit-tests')
  18. SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
  19. DEFAULT_CONFIG = salt.config.minion_config(None)
  20. DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
  21. DEFAULT_CONFIG['root_dir'] = ROOT_DIR
  22. DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
  23. DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
  24. DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
  25. class SchedulerSkipTest(ModuleCase, SaltReturnAssertsMixin):
  26. '''
  27. Validate the pkg module
  28. '''
  29. def setUp(self):
  30. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  31. functions = {'test.ping': ping}
  32. self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
  33. self.schedule.opts['loop_interval'] = 1
  34. def tearDown(self):
  35. self.schedule.reset()
  36. def test_skip(self):
  37. '''
  38. verify that scheduled job is skipped at the specified time
  39. '''
  40. job_name = 'test_skip'
  41. job = {
  42. 'schedule': {
  43. job_name: {
  44. 'function': 'test.ping',
  45. 'when': ['11/29/2017 4pm', '11/29/2017 5pm'],
  46. }
  47. }
  48. }
  49. # Add job to schedule
  50. self.schedule.opts.update(job)
  51. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  52. self.schedule.skip_job(job_name, {'time': run_time.strftime('%Y-%m-%dT%H:%M:%S'),
  53. 'time_fmt': '%Y-%m-%dT%H:%M:%S'})
  54. # Run 11/29/2017 at 4pm
  55. self.schedule.eval(now=run_time)
  56. ret = self.schedule.job_status(job_name)
  57. self.assertNotIn('_last_run', ret)
  58. self.assertEqual(ret['_skip_reason'], 'skip_explicit')
  59. self.assertEqual(ret['_skipped_time'], run_time)
  60. # Run 11/29/2017 at 5pm
  61. run_time = dateutil_parser.parse('11/29/2017 5:00pm')
  62. self.schedule.eval(now=run_time)
  63. ret = self.schedule.job_status(job_name)
  64. self.assertEqual(ret['_last_run'], run_time)
  65. def test_skip_during_range(self):
  66. '''
  67. verify that scheduled job is skipped during the specified range
  68. '''
  69. job_name = 'test_skip_during_range'
  70. job = {
  71. 'schedule': {
  72. job_name: {
  73. 'function': 'test.ping',
  74. 'hours': '1',
  75. 'skip_during_range': {
  76. 'start': '11/29/2017 2pm',
  77. 'end': '11/29/2017 3pm'
  78. },
  79. }
  80. }
  81. }
  82. # Add job to schedule
  83. self.schedule.opts.update(job)
  84. # eval at 1:30pm to prime.
  85. run_time = dateutil_parser.parse('11/29/2017 1:30pm')
  86. self.schedule.eval(now=run_time)
  87. ret = self.schedule.job_status(job_name)
  88. # eval at 2:30pm, will not run during range.
  89. run_time = dateutil_parser.parse('11/29/2017 2:30pm')
  90. self.schedule.eval(now=run_time)
  91. ret = self.schedule.job_status(job_name)
  92. self.assertNotIn('_last_run', ret)
  93. self.assertEqual(ret['_skip_reason'], 'in_skip_range')
  94. self.assertEqual(ret['_skipped_time'], run_time)
  95. # eval at 3:30pm, will run.
  96. run_time = dateutil_parser.parse('11/29/2017 3:30pm')
  97. self.schedule.eval(now=run_time)
  98. ret = self.schedule.job_status(job_name)
  99. self.assertEqual(ret['_last_run'], run_time)
  100. def test_skip_during_range_invalid_datestring(self):
  101. '''
  102. verify that scheduled job is not not and returns the right error string
  103. '''
  104. run_time = dateutil_parser.parse('11/29/2017 2:30pm')
  105. job_name1 = 'skip_during_range_invalid_datestring1'
  106. job1 = {
  107. 'schedule': {
  108. job_name1: {
  109. 'function': 'test.ping',
  110. 'hours': '1',
  111. '_next_fire_time': run_time,
  112. 'skip_during_range': {
  113. 'start': '25pm',
  114. 'end': '3pm'
  115. },
  116. }
  117. }
  118. }
  119. job_name2 = 'skip_during_range_invalid_datestring2'
  120. job2 = {
  121. 'schedule': {
  122. job_name2: {
  123. 'function': 'test.ping',
  124. 'hours': '1',
  125. '_next_fire_time': run_time,
  126. 'skip_during_range': {
  127. 'start': '2pm',
  128. 'end': '25pm'
  129. },
  130. }
  131. }
  132. }
  133. # Add job1 to schedule
  134. self.schedule.opts.update(job1)
  135. # Eval
  136. self.schedule.eval(now=run_time)
  137. # Check the first job
  138. ret = self.schedule.job_status(job_name1)
  139. _expected = ('Invalid date string for start in '
  140. 'skip_during_range. Ignoring '
  141. 'job {0}.').format(job_name1)
  142. self.assertEqual(ret['_error'], _expected)
  143. # Clear out schedule
  144. self.schedule.opts['schedule'] = {}
  145. # Add job2 to schedule
  146. self.schedule.opts.update(job2)
  147. # Eval
  148. self.schedule.eval(now=run_time)
  149. # Check the second job
  150. ret = self.schedule.job_status(job_name2)
  151. _expected = ('Invalid date string for end in '
  152. 'skip_during_range. Ignoring '
  153. 'job {0}.').format(job_name2)
  154. self.assertEqual(ret['_error'], _expected)
  155. def test_skip_during_range_global(self):
  156. '''
  157. verify that scheduled job is skipped during the specified range
  158. '''
  159. job_name = 'skip_during_range_global'
  160. job = {
  161. 'schedule': {
  162. 'skip_during_range': {
  163. 'start': '11/29/2017 2:00pm',
  164. 'end': '11/29/2017 3:00pm'
  165. },
  166. job_name: {
  167. 'function': 'test.ping',
  168. 'hours': '1',
  169. }
  170. }
  171. }
  172. # Add job to schedule
  173. self.schedule.opts.update(job)
  174. # eval at 1:30pm to prime.
  175. run_time = dateutil_parser.parse('11/29/2017 1:30pm')
  176. self.schedule.eval(now=run_time)
  177. ret = self.schedule.job_status(job_name)
  178. # eval at 2:30pm, will not run during range.
  179. run_time = dateutil_parser.parse('11/29/2017 2:30pm')
  180. self.schedule.eval(now=run_time)
  181. ret = self.schedule.job_status(job_name)
  182. self.assertNotIn('_last_run', ret)
  183. self.assertEqual(ret['_skip_reason'], 'in_skip_range')
  184. self.assertEqual(ret['_skipped_time'], run_time)
  185. # eval at 3:30pm, will run.
  186. run_time = dateutil_parser.parse('11/29/2017 3:30pm')
  187. self.schedule.eval(now=run_time)
  188. ret = self.schedule.job_status(job_name)
  189. self.assertEqual(ret['_last_run'], run_time)
  190. def test_run_after_skip_range(self):
  191. '''
  192. verify that scheduled job is skipped during the specified range
  193. '''
  194. job_name = 'skip_run_after_skip_range'
  195. job = {
  196. 'schedule': {
  197. job_name: {
  198. 'function': 'test.ping',
  199. 'when': '11/29/2017 2:30pm',
  200. 'run_after_skip_range': True,
  201. 'skip_during_range': {
  202. 'start': '11/29/2017 2pm',
  203. 'end': '11/29/2017 3pm'
  204. },
  205. }
  206. }
  207. }
  208. # Add job to schedule
  209. self.schedule.opts.update(job)
  210. # eval at 2:30pm, will not run during range.
  211. run_time = dateutil_parser.parse('11/29/2017 2:30pm')
  212. self.schedule.eval(now=run_time)
  213. ret = self.schedule.job_status(job_name)
  214. self.assertNotIn('_last_run', ret)
  215. self.assertEqual(ret['_skip_reason'], 'in_skip_range')
  216. self.assertEqual(ret['_skipped_time'], run_time)
  217. # eval at 3:00:01pm, will run.
  218. run_time = dateutil_parser.parse('11/29/2017 3:00:01pm')
  219. self.schedule.eval(now=run_time)
  220. ret = self.schedule.job_status(job_name)
  221. self.assertEqual(ret['_last_run'], run_time)
  222. def test_run_seconds_skip(self):
  223. '''
  224. verify that scheduled job is skipped during the specified range
  225. '''
  226. job_name = 'run_seconds_skip'
  227. job = {
  228. 'schedule': {
  229. job_name: {
  230. 'function': 'test.ping',
  231. 'seconds': '10',
  232. }
  233. }
  234. }
  235. # Add job to schedule
  236. self.schedule.opts.update(job)
  237. # eval at 2:00pm, to prime the scheduler
  238. run_time = dateutil_parser.parse('11/29/2017 2:00pm')
  239. self.schedule.eval(now=run_time)
  240. ret = self.schedule.job_status(job_name)
  241. # eval at 2:00:10pm
  242. run_time = dateutil_parser.parse('11/29/2017 2:00:10pm')
  243. self.schedule.eval(now=run_time)
  244. ret = self.schedule.job_status(job_name)
  245. # Skip at 2:00:20pm
  246. run_time = dateutil_parser.parse('11/29/2017 2:00:20pm')
  247. self.schedule.skip_job(job_name, {'time': run_time.strftime('%Y-%m-%dT%H:%M:%S'),
  248. 'time_fmt': '%Y-%m-%dT%H:%M:%S'})
  249. self.schedule.eval(now=run_time)
  250. ret = self.schedule.job_status(job_name)
  251. self.assertIn('_next_fire_time', ret)
  252. self.assertEqual(ret['_skip_reason'], 'skip_explicit')
  253. self.assertEqual(ret['_skipped_time'], run_time)
  254. # Run at 2:00:30pm
  255. run_time = dateutil_parser.parse('11/29/2017 2:00:30pm')
  256. self.schedule.eval(now=run_time)
  257. ret = self.schedule.job_status(job_name)
  258. self.assertIn('_last_run', ret)