Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to scheduler when job without a time element is run with schedule.run_job #54799

8 changes: 6 additions & 2 deletions salt/utils/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,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']:
garethgreenaway marked this conversation as resolved.
Show resolved Hide resolved
return data
if 'jid_include' not in data or data['jid_include']:
jobcount = 0
Expand Down Expand Up @@ -463,7 +463,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,
Expand All @@ -474,6 +477,7 @@ def run_job(self, name):
# Grab run, assume True
run = data.get('run', True)
if run:
garethgreenaway marked this conversation as resolved.
Show resolved Hide resolved
log.info('Running Job: %s', name)
self._run_job(func, data)

def enable_schedule(self):
Expand Down
73 changes: 73 additions & 0 deletions tests/integration/scheduler/test_run_job.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions tests/unit/modules/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_module_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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',
Expand Down