1
0

test_run_job.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from tests.support.mock import MagicMock, patch
  11. from tests.support.runtests import RUNTIME_VARS
  12. # Import Salt libs
  13. import salt.utils.schedule
  14. import salt.utils.platform
  15. from salt.modules.test import ping
  16. try:
  17. import croniter # pylint: disable=W0611
  18. HAS_CRONITER = True
  19. except ImportError:
  20. HAS_CRONITER = False
  21. log = logging.getLogger(__name__)
  22. ROOT_DIR = os.path.join(RUNTIME_VARS.TMP, 'schedule-unit-tests')
  23. SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
  24. DEFAULT_CONFIG = salt.config.minion_config(None)
  25. DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
  26. DEFAULT_CONFIG['root_dir'] = ROOT_DIR
  27. DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
  28. DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
  29. DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
  30. class SchedulerRunJobTest(ModuleCase, SaltReturnAssertsMixin):
  31. '''
  32. Validate the pkg module
  33. '''
  34. def setUp(self):
  35. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  36. functions = {'test.ping': ping}
  37. self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
  38. self.schedule.opts['loop_interval'] = 1
  39. def tearDown(self):
  40. self.schedule.reset()
  41. def test_run_job(self):
  42. '''
  43. verify that scheduled job runs
  44. '''
  45. job_name = 'test_run_job'
  46. job = {
  47. 'schedule': {
  48. job_name: {
  49. 'function': 'test.ping',
  50. }
  51. }
  52. }
  53. # Add the job to the scheduler
  54. self.schedule.opts.update(job)
  55. # Run job
  56. self.schedule.run_job(job_name)
  57. ret = self.schedule.job_status(job_name)
  58. expected = {'function': 'test.ping', 'run': True, 'name': 'test_run_job'}
  59. self.assertEqual(ret, expected)