|
20 | 20 | import salt.modules.schedule as schedule
|
21 | 21 | from salt.utils.event import SaltEvent
|
22 | 22 |
|
| 23 | + |
23 | 24 | JOB1 = {'function': 'test.ping', 'maxrunning': 1, 'name': 'job1',
|
24 | 25 | 'jid_include': True, 'enabled': True}
|
25 | 26 |
|
@@ -360,3 +361,79 @@ def test_copy(self):
|
360 | 361 | {'comment': comm3,
|
361 | 362 | 'minions': ['minion1'],
|
362 | 363 | '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