Skip to content

Commit

Permalink
Fixing a scenario when you call the modify function in the schedule f…
Browse files Browse the repository at this point in the history
…unction but do not specify a function. So long as the job exists, we should pull the existing function in from the current job to pass along to build_schedule_item.
  • Loading branch information
garethgreenaway committed Nov 4, 2019
1 parent 9bbbd36 commit a2d45a6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions salt/modules/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ def modify(name, **kwargs):
return ret

_current = current_schedule[name]

if 'function' not in kwargs:
kwargs['function'] = _current.get('function')

if '_seconds' in _current:
_current['seconds'] = _current['_seconds']
del _current['_seconds']
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/modules/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import logging
import os

# Import Salt Testing Libs
Expand All @@ -22,6 +23,7 @@
import salt.modules.schedule as schedule
from salt.utils.event import SaltEvent

log = logging.getLogger(__name__)
SOCK_DIR = os.path.join(TMP, 'test-socks')

JOB1 = {'function': 'test.ping', 'maxrunning': 1, 'name': 'job1',
Expand Down Expand Up @@ -360,3 +362,79 @@ def test_copy(self):
{'comment': comm3,
'minions': ['minion1'],
'result': True})

# 'modify' function tests: 1

def test_modify(self):
'''
Test if modifying job to the schedule.
'''
job1 = {'function': 'salt', 'seconds': 3600}

comm1 = 'Modified job: job1 in schedule.'
diff1 = ('--- \n+++ \n@@ -1,3 +1,6 @@\n '
'enabled:True\n function:salt\n'
'-seconds:3600\n+jid_include:True\n'
'+maxrunning:1\n+name:job1\n'
'+seconds:60\n')

diff4 = ('--- \n+++ \n@@ -1,3 +1,5 @@\n '
'enabled:True\n-function:salt\n'
'-seconds:3600\n+function:test.version\n'
'+jid_include:True\n+maxrunning:1\n'
'+name:job1\n')

expected1 = {'comment': comm1,
'changes': {'diff': diff1},
'result': True}

comm2 = 'Error: Unable to use "seconds", "minutes", "hours", ' \
'or "days" with "when" option.'
expected2 = {'comment': comm2,
'changes': {},
'result': False}

comm3 = 'Unable to use "when" and "cron" options together. Ignoring.'
expected3 = {'comment': comm3,
'changes': {},
'result': False}

comm4 = 'Job: job1 would be modified in schedule.'
expected4 = {'comment': comm4,
'changes': {'diff': diff4},
'result': True}

comm5 = 'Job job2 does not exist in schedule.'
expected5 = {'comment': comm5,
'changes': {},
'result': False}

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': {'job1': job1}}
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
ret = schedule.modify('job1', seconds='60')
self.assertDictEqual(ret, expected1)

_ret_value = {'complete': True, 'schedule': {'job1': job1}}
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
ret = schedule.modify('job1', function='test.ping',
seconds=3600, when='2400')
self.assertDictEqual(ret, expected2)

_ret_value = {'complete': True, 'schedule': {'job1': job1}}
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
ret = schedule.modify('job1', function='test.ping',
when='2400', cron='2')
self.assertDictEqual(ret, expected3)

_ret_value = {'complete': True, 'schedule': {'job1': job1}}
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
ret = schedule.modify('job1', function='test.version', test=True)
self.assertDictEqual(ret, expected4)

_ret_value = {'complete': True, 'schedule': {}}
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
ret = schedule.modify('job2', function='test.version', test=True)
self.assertDictEqual(ret, expected5)

0 comments on commit a2d45a6

Please sign in to comment.