diff --git a/salt/utils/schedule.py b/salt/utils/schedule.py index 6d1a8311e52..3f7f26141e8 100644 --- a/salt/utils/schedule.py +++ b/salt/utils/schedule.py @@ -210,7 +210,7 @@ def _check_max_running(self, func, data, opts, now): # dict we treat it like it was there and is True # Check if we're able to run - if not data['run']: + if 'run' not in data or not data['run']: return data if 'jid_include' not in data or data['jid_include']: jobcount = 0 @@ -459,7 +459,10 @@ def run_job(self, name): if 'name' not in data: data['name'] = name - log.info('Running Job: %s', name) + + # Assume run should be True until we check max_running + if 'run' not in data: + data['run'] = True if not self.standalone: data = self._check_max_running(func, @@ -468,8 +471,8 @@ def run_job(self, name): datetime.datetime.now()) # Grab run, assume True - run = data.get('run', True) - if run: + if data.get('run'): + log.info('Running Job: %s', name) self._run_job(func, data) def enable_schedule(self): diff --git a/tests/integration/scheduler/test_run_job.py b/tests/integration/scheduler/test_run_job.py new file mode 100644 index 00000000000..c8cdcb6b24b --- /dev/null +++ b/tests/integration/scheduler/test_run_job.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- + +# Import Python libs +from __future__ import absolute_import +import copy +import logging +import os + +# Import Salt Testing libs +from tests.support.case import ModuleCase +from tests.support.mixins import SaltReturnAssertsMixin + +# Import Salt Testing Libs +from tests.support.mock import MagicMock, patch +import tests.integration as integration + +# Import Salt libs +import salt.utils.schedule +import salt.utils.platform + +from salt.modules.test import ping as ping + +try: + import croniter # pylint: disable=W0611 + HAS_CRONITER = True +except ImportError: + HAS_CRONITER = False + +log = logging.getLogger(__name__) +ROOT_DIR = os.path.join(integration.TMP, 'schedule-unit-tests') +SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks') + +DEFAULT_CONFIG = salt.config.minion_config(None) +DEFAULT_CONFIG['conf_dir'] = ROOT_DIR +DEFAULT_CONFIG['root_dir'] = ROOT_DIR +DEFAULT_CONFIG['sock_dir'] = SOCK_DIR +DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki') +DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache') + + +class SchedulerRunJobTest(ModuleCase, SaltReturnAssertsMixin): + ''' + Validate the pkg module + ''' + def setUp(self): + with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)): + functions = {'test.ping': ping} + self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={}) + self.schedule.opts['loop_interval'] = 1 + + def tearDown(self): + self.schedule.reset() + + def test_run_job(self): + ''' + verify that scheduled job runs + ''' + job_name = 'test_run_job' + job = { + 'schedule': { + job_name: { + 'function': 'test.ping', + } + } + } + # Add the job to the scheduler + self.schedule.opts.update(job) + + # Run job + self.schedule.run_job(job_name) + ret = self.schedule.job_status(job_name) + expected = {'function': 'test.ping', 'run': True, 'name': 'test_run_job'} + self.assertEqual(ret, expected) diff --git a/tests/unit/modules/test_schedule.py b/tests/unit/modules/test_schedule.py index f3e68f4b284..9fb01e94ec7 100644 --- a/tests/unit/modules/test_schedule.py +++ b/tests/unit/modules/test_schedule.py @@ -150,14 +150,14 @@ def test_run_job(self): ''' Test if it run a scheduled job on the minion immediately. ''' - with patch.dict(schedule.__opts__, {'schedule': {}, 'sock_dir': SOCK_DIR}): + with patch.dict(schedule.__opts__, {'schedule': {'job1': JOB1}, 'sock_dir': SOCK_DIR}): mock = MagicMock(return_value=True) with patch.dict(schedule.__salt__, {'event.fire': mock}): - _ret_value = {'complete': True, 'schedule': {}} + _ret_value = {'complete': True, 'schedule': {'job1': JOB1}} with patch.object(SaltEvent, 'get_event', return_value=_ret_value): self.assertDictEqual(schedule.run_job('job1'), - {'comment': 'Job job1 does not exist.', - 'result': False}) + {'comment': 'Scheduling Job job1 on minion.', + 'result': True}) # 'enable_job' function tests: 1 diff --git a/tests/unit/test_module_names.py b/tests/unit/test_module_names.py index 1efcb5869e8..c4109c09165 100644 --- a/tests/unit/test_module_names.py +++ b/tests/unit/test_module_names.py @@ -152,6 +152,7 @@ def test_module_name_source_match(self): 'integration.scheduler.test_skip', 'integration.scheduler.test_maxrunning', 'integration.scheduler.test_helpers', + 'integration.scheduler.test_run_job', 'integration.shell.test_spm', 'integration.shell.test_cp', 'integration.shell.test_syndic',