test_error.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.unit import skipIf
  13. from tests.support.runtests import RUNTIME_VARS
  14. # Import Salt libs
  15. import salt.utils.schedule
  16. from salt.modules.test import ping
  17. try:
  18. import croniter # pylint: disable=W0611
  19. HAS_CRONITER = True
  20. except ImportError:
  21. HAS_CRONITER = False
  22. log = logging.getLogger(__name__)
  23. ROOT_DIR = os.path.join(RUNTIME_VARS.TMP, 'schedule-unit-tests')
  24. SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
  25. DEFAULT_CONFIG = salt.config.minion_config(None)
  26. DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
  27. DEFAULT_CONFIG['root_dir'] = ROOT_DIR
  28. DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
  29. DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
  30. DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
  31. class SchedulerErrorTest(ModuleCase, SaltReturnAssertsMixin):
  32. '''
  33. Validate the pkg module
  34. '''
  35. def setUp(self):
  36. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  37. functions = {'test.ping': ping}
  38. self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
  39. self.schedule.opts['loop_interval'] = 1
  40. self.schedule.opts['grains']['whens'] = {'tea time': '11/29/2017 12:00pm'}
  41. def tearDown(self):
  42. self.schedule.reset()
  43. @skipIf(not HAS_CRONITER, 'Cannot find croniter python module')
  44. def test_eval_cron_invalid(self):
  45. '''
  46. verify that scheduled job runs
  47. '''
  48. job = {
  49. 'schedule': {
  50. 'job1': {
  51. 'function': 'test.ping',
  52. 'cron': '0 16 29 13 *'
  53. }
  54. }
  55. }
  56. # Add the job to the scheduler
  57. self.schedule.opts.update(job)
  58. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  59. with patch('croniter.croniter.get_next', MagicMock(return_value=run_time)):
  60. self.schedule.eval(now=run_time)
  61. ret = self.schedule.job_status('job1')
  62. self.assertEqual(ret['_error'],
  63. 'Invalid cron string. Ignoring job job1.')
  64. def test_eval_when_invalid_date(self):
  65. '''
  66. verify that scheduled job does not run
  67. and returns the right error
  68. '''
  69. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  70. job = {
  71. 'schedule': {
  72. 'job1': {
  73. 'function': 'test.ping',
  74. 'when': '13/29/2017 1:00pm',
  75. }
  76. }
  77. }
  78. # Add the job to the scheduler
  79. self.schedule.opts.update(job)
  80. # Evaluate 1 second before the run time
  81. self.schedule.eval(now=run_time)
  82. ret = self.schedule.job_status('job1')
  83. self.assertEqual(ret['_error'],
  84. 'Invalid date string 13/29/2017 1:00pm. Ignoring job job1.')
  85. def test_eval_whens_grain_not_dict(self):
  86. '''
  87. verify that scheduled job does not run
  88. and returns the right error
  89. '''
  90. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  91. job = {
  92. 'schedule': {
  93. 'job1': {
  94. 'function': 'test.ping',
  95. 'when': 'tea time',
  96. }
  97. }
  98. }
  99. self.schedule.opts['grains']['whens'] = ['tea time']
  100. # Add the job to the scheduler
  101. self.schedule.opts.update(job)
  102. # Evaluate 1 second before the run time
  103. self.schedule.eval(now=run_time)
  104. ret = self.schedule.job_status('job1')
  105. self.assertEqual(ret['_error'],
  106. 'Grain "whens" must be a dict. Ignoring job job1.')
  107. def test_eval_once_invalid_datestring(self):
  108. '''
  109. verify that scheduled job does not run
  110. and returns the right error
  111. '''
  112. job = {
  113. 'schedule': {
  114. 'job1': {
  115. 'function': 'test.ping',
  116. 'once': '2017-13-13T13:00:00',
  117. }
  118. }
  119. }
  120. run_time = dateutil_parser.parse('12/13/2017 1:00pm')
  121. # Add the job to the scheduler
  122. self.schedule.opts.update(job)
  123. # Evaluate 1 second at the run time
  124. self.schedule.eval(now=run_time)
  125. ret = self.schedule.job_status('job1')
  126. _expected = ('Date string could not be parsed: '
  127. '2017-13-13T13:00:00, %Y-%m-%dT%H:%M:%S. '
  128. 'Ignoring job job1.')
  129. self.assertEqual(ret['_error'], _expected)
  130. def test_eval_skip_during_range_invalid_date(self):
  131. '''
  132. verify that scheduled job does not run
  133. and returns the right error
  134. '''
  135. job = {
  136. 'schedule': {
  137. 'job1': {
  138. 'function': 'test.ping',
  139. 'hours': 1,
  140. 'skip_during_range': {'start': '1:00pm', 'end': '25:00pm'}
  141. }
  142. }
  143. }
  144. # Add the job to the scheduler
  145. self.schedule.opts.update(job)
  146. # eval at 3:00pm to prime, simulate minion start up.
  147. run_time = dateutil_parser.parse('11/29/2017 3:00pm')
  148. self.schedule.eval(now=run_time)
  149. ret = self.schedule.job_status('job1')
  150. # eval at 4:00pm to prime
  151. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  152. self.schedule.eval(now=run_time)
  153. ret = self.schedule.job_status('job1')
  154. _expected = ('Invalid date string for end in '
  155. 'skip_during_range. Ignoring '
  156. 'job job1.')
  157. self.assertEqual(ret['_error'], _expected)
  158. def test_eval_skip_during_range_end_before_start(self):
  159. '''
  160. verify that scheduled job does not run
  161. and returns the right error
  162. '''
  163. job = {
  164. 'schedule': {
  165. 'job1': {
  166. 'function': 'test.ping',
  167. 'hours': 1,
  168. 'skip_during_range': {'start': '1:00pm', 'end': '12:00pm'}
  169. }
  170. }
  171. }
  172. # Add the job to the scheduler
  173. self.schedule.opts.update(job)
  174. # eval at 3:00pm to prime, simulate minion start up.
  175. run_time = dateutil_parser.parse('11/29/2017 3:00pm')
  176. self.schedule.eval(now=run_time)
  177. ret = self.schedule.job_status('job1')
  178. # eval at 4:00pm to prime
  179. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  180. self.schedule.eval(now=run_time)
  181. ret = self.schedule.job_status('job1')
  182. _expected = ('schedule.handle_func: Invalid '
  183. 'range, end must be larger than '
  184. 'start. Ignoring job job1.')
  185. self.assertEqual(ret['_error'], _expected)
  186. def test_eval_skip_during_range_not_dict(self):
  187. '''
  188. verify that scheduled job does not run
  189. and returns the right error
  190. '''
  191. job = {
  192. 'schedule': {
  193. 'job1': {
  194. 'function': 'test.ping',
  195. 'hours': 1,
  196. 'skip_during_range': ['start', '1:00pm', 'end', '12:00pm']
  197. }
  198. }
  199. }
  200. # Add the job to the scheduler
  201. self.schedule.opts.update(job)
  202. # eval at 3:00pm to prime, simulate minion start up.
  203. run_time = dateutil_parser.parse('11/29/2017 3:00pm')
  204. self.schedule.eval(now=run_time)
  205. ret = self.schedule.job_status('job1')
  206. # eval at 4:00pm to prime
  207. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  208. self.schedule.eval(now=run_time)
  209. ret = self.schedule.job_status('job1')
  210. _expected = ('schedule.handle_func: Invalid, '
  211. 'range must be specified as a '
  212. 'dictionary. Ignoring job job1.')
  213. self.assertEqual(ret['_error'], _expected)