Skip to content

Commit 0ae1f74

Browse files
authored
Merge pull request saltstack#55196 from garethgreenaway/schedule_modify_no_function_fix
[master] Fix to modify function in schedule moule
2 parents 4fe84b6 + 8b20cde commit 0ae1f74

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

salt/modules/schedule.py

+4
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ def modify(name, **kwargs):
515515
return ret
516516

517517
_current = current_schedule[name]
518+
519+
if 'function' not in kwargs:
520+
kwargs['function'] = _current.get('function')
521+
518522
if '_seconds' in _current:
519523
_current['seconds'] = _current['_seconds']
520524
del _current['_seconds']

tests/unit/modules/test_schedule.py

+77
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import salt.modules.schedule as schedule
2121
from salt.utils.event import SaltEvent
2222

23+
2324
JOB1 = {'function': 'test.ping', 'maxrunning': 1, 'name': 'job1',
2425
'jid_include': True, 'enabled': True}
2526

@@ -360,3 +361,79 @@ def test_copy(self):
360361
{'comment': comm3,
361362
'minions': ['minion1'],
362363
'result': True})
364+
365+
# 'modify' function tests: 1
366+
367+
def test_modify(self):
368+
'''
369+
Test if modifying job to the schedule.
370+
'''
371+
job1 = {'function': 'salt', 'seconds': 3600}
372+
373+
comm1 = 'Modified job: job1 in schedule.'
374+
diff1 = ('--- \n+++ \n@@ -1,3 +1,6 @@\n '
375+
'enabled:True\n function:salt\n'
376+
'-seconds:3600\n+jid_include:True\n'
377+
'+maxrunning:1\n+name:job1\n'
378+
'+seconds:60\n')
379+
380+
diff4 = ('--- \n+++ \n@@ -1,3 +1,5 @@\n '
381+
'enabled:True\n-function:salt\n'
382+
'-seconds:3600\n+function:test.version\n'
383+
'+jid_include:True\n+maxrunning:1\n'
384+
'+name:job1\n')
385+
386+
expected1 = {'comment': comm1,
387+
'changes': {'diff': diff1},
388+
'result': True}
389+
390+
comm2 = 'Error: Unable to use "seconds", "minutes", "hours", ' \
391+
'or "days" with "when" option.'
392+
expected2 = {'comment': comm2,
393+
'changes': {},
394+
'result': False}
395+
396+
comm3 = 'Unable to use "when" and "cron" options together. Ignoring.'
397+
expected3 = {'comment': comm3,
398+
'changes': {},
399+
'result': False}
400+
401+
comm4 = 'Job: job1 would be modified in schedule.'
402+
expected4 = {'comment': comm4,
403+
'changes': {'diff': diff4},
404+
'result': True}
405+
406+
comm5 = 'Job job2 does not exist in schedule.'
407+
expected5 = {'comment': comm5,
408+
'changes': {},
409+
'result': False}
410+
411+
with patch.dict(schedule.__opts__, {'schedule': {'job1': job1}, 'sock_dir': self.sock_dir}):
412+
mock = MagicMock(return_value=True)
413+
with patch.dict(schedule.__salt__, {'event.fire': mock}):
414+
_ret_value = {'complete': True, 'schedule': {'job1': job1}}
415+
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
416+
ret = schedule.modify('job1', seconds='60')
417+
self.assertDictEqual(ret, expected1)
418+
419+
_ret_value = {'complete': True, 'schedule': {'job1': job1}}
420+
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
421+
ret = schedule.modify('job1', function='test.ping',
422+
seconds=3600, when='2400')
423+
self.assertDictEqual(ret, expected2)
424+
425+
_ret_value = {'complete': True, 'schedule': {'job1': job1}}
426+
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
427+
ret = schedule.modify('job1', function='test.ping',
428+
when='2400', cron='2')
429+
self.assertDictEqual(ret, expected3)
430+
431+
_ret_value = {'complete': True, 'schedule': {'job1': job1}}
432+
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
433+
ret = schedule.modify('job1', function='test.version', test=True)
434+
self.assertDictEqual(ret, expected4)
435+
436+
_ret_value = {'complete': True, 'schedule': {}}
437+
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
438+
ret = schedule.modify('job2', function='test.version', test=True)
439+
self.assertDictEqual(ret, expected5)

0 commit comments

Comments
 (0)