test_schedule.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import copy
  8. import datetime
  9. import logging
  10. import os
  11. # Import Salt Testing Libs
  12. from tests.support.unit import skipIf, TestCase
  13. from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
  14. import tests.integration as integration
  15. # Import Salt Libs
  16. import salt.config
  17. from salt.utils.schedule import Schedule
  18. # pylint: disable=import-error,unused-import
  19. try:
  20. import croniter
  21. _CRON_SUPPORTED = True
  22. except ImportError:
  23. _CRON_SUPPORTED = False
  24. # pylint: enable=import-error
  25. log = logging.getLogger(__name__)
  26. # pylint: disable=too-many-public-methods,invalid-name
  27. @skipIf(NO_MOCK, NO_MOCK_REASON)
  28. class ScheduleTestCase(TestCase):
  29. '''
  30. Unit tests for salt.utils.schedule module
  31. '''
  32. @classmethod
  33. def setUpClass(cls):
  34. root_dir = os.path.join(integration.TMP, 'schedule-unit-tests')
  35. default_config = salt.config.minion_config(None)
  36. default_config['conf_dir'] = default_config['root_dir'] = root_dir
  37. default_config['sock_dir'] = os.path.join(root_dir, 'test-socks')
  38. default_config['pki_dir'] = os.path.join(root_dir, 'pki')
  39. default_config['cachedir'] = os.path.join(root_dir, 'cache')
  40. cls.default_config = default_config
  41. @classmethod
  42. def tearDownClass(cls):
  43. delattr(cls, 'default_config')
  44. def setUp(self):
  45. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  46. self.schedule = Schedule(copy.deepcopy(self.default_config), {}, returners={})
  47. self.addCleanup(delattr, self, 'schedule')
  48. # delete_job tests
  49. def test_delete_job_exists(self):
  50. '''
  51. Tests ensuring the job exists and deleting it
  52. '''
  53. self.schedule.opts.update({'schedule': {'foo': 'bar'}, 'pillar': {}})
  54. self.assertIn('foo', self.schedule.opts['schedule'])
  55. self.schedule.delete_job('foo')
  56. self.assertNotIn('foo', self.schedule.opts['schedule'])
  57. def test_delete_job_in_pillar(self):
  58. '''
  59. Tests ignoring deletion job from pillar
  60. '''
  61. self.schedule.opts.update({'pillar': {'schedule': {'foo': 'bar'}}, 'schedule': {}})
  62. self.assertIn('foo', self.schedule.opts['pillar']['schedule'])
  63. self.schedule.delete_job('foo')
  64. self.assertIn('foo', self.schedule.opts['pillar']['schedule'])
  65. def test_delete_job_intervals(self):
  66. '''
  67. Tests removing job from intervals
  68. '''
  69. self.schedule.opts.update({'pillar': {}, 'schedule': {}})
  70. self.schedule.intervals = {'foo': 'bar'}
  71. self.schedule.delete_job('foo')
  72. self.assertNotIn('foo', self.schedule.intervals)
  73. def test_delete_job_prefix(self):
  74. '''
  75. Tests ensuring jobs exists and deleting them by prefix
  76. '''
  77. self.schedule.opts.update({'schedule': {'foobar': 'bar', 'foobaz': 'baz', 'fooboo': 'boo'},
  78. 'pillar': {}})
  79. ret = copy.deepcopy(self.schedule.opts)
  80. del ret['schedule']['foobar']
  81. del ret['schedule']['foobaz']
  82. self.schedule.delete_job_prefix('fooba')
  83. self.assertEqual(self.schedule.opts, ret)
  84. def test_delete_job_prefix_in_pillar(self):
  85. '''
  86. Tests ignoring deletion jobs by prefix from pillar
  87. '''
  88. self.schedule.opts.update({'pillar': {'schedule': {'foobar': 'bar', 'foobaz': 'baz', 'fooboo': 'boo'}},
  89. 'schedule': {}})
  90. ret = copy.deepcopy(self.schedule.opts)
  91. self.schedule.delete_job_prefix('fooba')
  92. self.assertEqual(self.schedule.opts, ret)
  93. # add_job tests
  94. def test_add_job_data_not_dict(self):
  95. '''
  96. Tests if data is a dictionary
  97. '''
  98. data = 'foo'
  99. self.assertRaises(ValueError, Schedule.add_job, self.schedule, data)
  100. def test_add_job_multiple_jobs(self):
  101. '''
  102. Tests if more than one job is scheduled at a time
  103. '''
  104. data = {'key1': 'value1', 'key2': 'value2'}
  105. self.assertRaises(ValueError, Schedule.add_job, self.schedule, data)
  106. def test_add_job(self):
  107. '''
  108. Tests adding a job to the schedule
  109. '''
  110. data = {'foo': {'bar': 'baz'}}
  111. ret = copy.deepcopy(self.schedule.opts)
  112. ret.update({'schedule': {'foo': {'bar': 'baz', 'enabled': True},
  113. 'hello': {'world': 'peace', 'enabled': True}},
  114. 'pillar': {}})
  115. self.schedule.opts.update({'schedule': {'hello': {'world': 'peace', 'enabled': True}},
  116. 'pillar': {}})
  117. Schedule.add_job(self.schedule, data)
  118. self.assertEqual(self.schedule.opts, ret)
  119. # enable_job tests
  120. def test_enable_job(self):
  121. '''
  122. Tests enabling a job
  123. '''
  124. self.schedule.opts.update({'schedule': {'name': {'enabled': 'foo'}}})
  125. Schedule.enable_job(self.schedule, 'name')
  126. self.assertTrue(self.schedule.opts['schedule']['name']['enabled'])
  127. def test_enable_job_pillar(self):
  128. '''
  129. Tests ignoring enable a job from pillar
  130. '''
  131. self.schedule.opts.update({'pillar': {'schedule': {'name': {'enabled': False}}}})
  132. Schedule.enable_job(self.schedule, 'name', persist=False)
  133. self.assertFalse(self.schedule.opts['pillar']['schedule']['name']['enabled'])
  134. # disable_job tests
  135. def test_disable_job(self):
  136. '''
  137. Tests disabling a job
  138. '''
  139. self.schedule.opts.update({'schedule': {'name': {'enabled': 'foo'}}, 'pillar': {}})
  140. Schedule.disable_job(self.schedule, 'name')
  141. self.assertFalse(self.schedule.opts['schedule']['name']['enabled'])
  142. def test_disable_job_pillar(self):
  143. '''
  144. Tests ignoring disable a job in pillar
  145. '''
  146. self.schedule.opts.update({'pillar': {'schedule': {'name': {'enabled': True}}}, 'schedule': {}})
  147. Schedule.disable_job(self.schedule, 'name', persist=False)
  148. self.assertTrue(self.schedule.opts['pillar']['schedule']['name']['enabled'])
  149. # modify_job tests
  150. def test_modify_job(self):
  151. '''
  152. Tests modifying a job in the scheduler
  153. '''
  154. schedule = {'foo': 'bar'}
  155. self.schedule.opts.update({'schedule': {'name': 'baz'}, 'pillar': {}})
  156. ret = copy.deepcopy(self.schedule.opts)
  157. ret.update({'schedule': {'name': {'foo': 'bar'}}})
  158. Schedule.modify_job(self.schedule, 'name', schedule)
  159. self.assertEqual(self.schedule.opts, ret)
  160. def test_modify_job_not_exists(self):
  161. '''
  162. Tests modifying a job in the scheduler if jobs not exists
  163. '''
  164. schedule = {'foo': 'bar'}
  165. self.schedule.opts.update({'schedule': {}, 'pillar': {}})
  166. ret = copy.deepcopy(self.schedule.opts)
  167. ret.update({'schedule': {'name': {'foo': 'bar'}}})
  168. Schedule.modify_job(self.schedule, 'name', schedule)
  169. self.assertEqual(self.schedule.opts, ret)
  170. def test_modify_job_pillar(self):
  171. '''
  172. Tests ignoring modification of job from pillar
  173. '''
  174. schedule = {'foo': 'bar'}
  175. self.schedule.opts.update({'schedule': {}, 'pillar': {'schedule': {'name': 'baz'}}})
  176. ret = copy.deepcopy(self.schedule.opts)
  177. Schedule.modify_job(self.schedule, 'name', schedule, persist=False)
  178. self.assertEqual(self.schedule.opts, ret)
  179. maxDiff = None
  180. # enable_schedule tests
  181. def test_enable_schedule(self):
  182. '''
  183. Tests enabling the scheduler
  184. '''
  185. self.schedule.opts.update({'schedule': {'enabled': 'foo'}, 'pillar': {}})
  186. Schedule.enable_schedule(self.schedule)
  187. self.assertTrue(self.schedule.opts['schedule']['enabled'])
  188. # disable_schedule tests
  189. def test_disable_schedule(self):
  190. '''
  191. Tests disabling the scheduler
  192. '''
  193. self.schedule.opts.update({'schedule': {'enabled': 'foo'}, 'pillar': {}})
  194. Schedule.disable_schedule(self.schedule)
  195. self.assertFalse(self.schedule.opts['schedule']['enabled'])
  196. # reload tests
  197. def test_reload_update_schedule_key(self):
  198. '''
  199. Tests reloading the schedule from saved schedule where both the
  200. saved schedule and self.schedule.opts contain a schedule key
  201. '''
  202. saved = {'schedule': {'foo': 'bar'}}
  203. ret = copy.deepcopy(self.schedule.opts)
  204. ret.update({'schedule': {'foo': 'bar', 'hello': 'world'}})
  205. self.schedule.opts.update({'schedule': {'hello': 'world'}})
  206. Schedule.reload(self.schedule, saved)
  207. self.assertEqual(self.schedule.opts, ret)
  208. def test_reload_update_schedule_no_key(self):
  209. '''
  210. Tests reloading the schedule from saved schedule that does not
  211. contain a schedule key but self.schedule.opts does
  212. '''
  213. saved = {'foo': 'bar'}
  214. ret = copy.deepcopy(self.schedule.opts)
  215. ret.update({'schedule': {'foo': 'bar', 'hello': 'world'}})
  216. self.schedule.opts.update({'schedule': {'hello': 'world'}})
  217. Schedule.reload(self.schedule, saved)
  218. self.assertEqual(self.schedule.opts, ret)
  219. def test_reload_no_schedule_in_opts(self):
  220. '''
  221. Tests reloading the schedule from saved schedule that does not
  222. contain a schedule key and neither does self.schedule.opts
  223. '''
  224. saved = {'foo': 'bar'}
  225. ret = copy.deepcopy(self.schedule.opts)
  226. ret['schedule'] = {'foo': 'bar'}
  227. self.schedule.opts.pop('schedule', None)
  228. Schedule.reload(self.schedule, saved)
  229. self.assertEqual(self.schedule.opts, ret)
  230. def test_reload_schedule_in_saved_but_not_opts(self):
  231. '''
  232. Tests reloading the schedule from saved schedule that contains
  233. a schedule key, but self.schedule.opts does not
  234. '''
  235. saved = {'schedule': {'foo': 'bar'}}
  236. ret = copy.deepcopy(self.schedule.opts)
  237. ret['schedule'] = {'foo': 'bar'}
  238. self.schedule.opts.pop('schedule', None)
  239. Schedule.reload(self.schedule, saved)
  240. self.assertEqual(self.schedule.opts, ret)
  241. # eval tests
  242. def test_eval_schedule_is_not_dict(self):
  243. '''
  244. Tests eval if the schedule is not a dictionary
  245. '''
  246. self.schedule.opts.update({'schedule': '', 'pillar': {'schedule': {}}})
  247. self.assertRaises(ValueError, Schedule.eval, self.schedule)
  248. def test_eval_schedule_is_not_dict_in_pillar(self):
  249. '''
  250. Tests eval if the schedule from pillar is not a dictionary
  251. '''
  252. self.schedule.opts.update({'schedule': {}, 'pillar': {'schedule': ''}})
  253. self.assertRaises(ValueError, Schedule.eval, self.schedule)
  254. def test_eval_schedule_time(self):
  255. '''
  256. Tests eval if the schedule setting time is in the future
  257. '''
  258. self.schedule.opts.update({'pillar': {'schedule': {}}})
  259. self.schedule.opts.update({'schedule': {'testjob': {'function': 'test.true', 'seconds': 60}}})
  260. now = datetime.datetime.now()
  261. self.schedule.eval()
  262. self.assertTrue(self.schedule.opts['schedule']['testjob']['_next_fire_time'] > now)
  263. def test_eval_schedule_time_eval(self):
  264. '''
  265. Tests eval if the schedule setting time is in the future plus splay
  266. '''
  267. self.schedule.opts.update({'pillar': {'schedule': {}}})
  268. self.schedule.opts.update(
  269. {'schedule': {'testjob': {'function': 'test.true', 'seconds': 60, 'splay': 5}}})
  270. now = datetime.datetime.now()
  271. self.schedule.eval()
  272. self.assertTrue(self.schedule.opts['schedule']['testjob']['_splay'] - now > datetime.timedelta(seconds=60))
  273. @skipIf(not _CRON_SUPPORTED, 'croniter module not installed')
  274. def test_eval_schedule_cron(self):
  275. '''
  276. Tests eval if the schedule is defined with cron expression
  277. '''
  278. self.schedule.opts.update({'pillar': {'schedule': {}}})
  279. self.schedule.opts.update({'schedule': {'testjob': {'function': 'test.true', 'cron': '* * * * *'}}})
  280. now = datetime.datetime.now()
  281. self.schedule.eval()
  282. self.assertTrue(self.schedule.opts['schedule']['testjob']['_next_fire_time'] > now)
  283. @skipIf(not _CRON_SUPPORTED, 'croniter module not installed')
  284. def test_eval_schedule_cron_splay(self):
  285. '''
  286. Tests eval if the schedule is defined with cron expression plus splay
  287. '''
  288. self.schedule.opts.update({'pillar': {'schedule': {}}})
  289. self.schedule.opts.update(
  290. {'schedule': {'testjob': {'function': 'test.true', 'cron': '* * * * *', 'splay': 5}}})
  291. self.schedule.eval()
  292. self.assertTrue(self.schedule.opts['schedule']['testjob']['_splay'] >
  293. self.schedule.opts['schedule']['testjob']['_next_fire_time'])
  294. def test_handle_func_schedule_minion_blackout(self):
  295. '''
  296. Tests eval if the schedule from pillar is not a dictionary
  297. '''
  298. self.schedule.opts.update({'pillar': {'schedule': {}}})
  299. self.schedule.opts.update({'grains': {'minion_blackout': True}})
  300. self.schedule.opts.update(
  301. {'schedule': {'testjob': {'function': 'test.true',
  302. 'seconds': 60}}})
  303. data = {'function': 'test.true',
  304. '_next_scheduled_fire_time': datetime.datetime(2018,
  305. 11,
  306. 21,
  307. 14,
  308. 9,
  309. 53,
  310. 903438),
  311. 'run': True,
  312. 'name': 'testjob',
  313. 'seconds': 60,
  314. '_splay': None,
  315. '_seconds': 60,
  316. 'jid_include': True,
  317. 'maxrunning': 1,
  318. '_next_fire_time': datetime.datetime(2018,
  319. 11,
  320. 21,
  321. 14,
  322. 8,
  323. 53,
  324. 903438)}
  325. with patch.object(salt.utils.schedule, 'log') as log_mock:
  326. with patch('salt.utils.process.daemonize'), \
  327. patch('sys.platform', 'linux2'):
  328. self.schedule.handle_func(False, 'test.ping', data)
  329. self.assertTrue(log_mock.exception.called)