test_run_job.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Salt Testing libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.mixins import SaltReturnAssertsMixin
  10. # Import Salt Testing Libs
  11. from tests.support.mock import MagicMock, patch
  12. import tests.integration as integration
  13. # Import Salt libs
  14. import salt.utils.schedule
  15. import salt.utils.platform
  16. from salt.modules.test import ping as 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(integration.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 SchedulerRunJobTest(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. def tearDown(self):
  41. self.schedule.reset()
  42. def test_run_job(self):
  43. '''
  44. verify that scheduled job runs
  45. '''
  46. job_name = 'test_run_job'
  47. job = {
  48. 'schedule': {
  49. job_name: {
  50. 'function': 'test.ping',
  51. }
  52. }
  53. }
  54. # Add the job to the scheduler
  55. self.schedule.opts.update(job)
  56. # Run job
  57. self.schedule.run_job(job_name)
  58. ret = self.schedule.job_status(job_name)
  59. expected = {'function': 'test.ping', 'run': True, 'name': 'test_run_job'}
  60. self.assertEqual(ret, expected)