mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Moving the minion_blackout code to the start of the try...except. Adding a test to ensure we get a log.exception when minion_blackout is True.
This commit is contained in:
parent
2bf8fa5362
commit
9f422b2a06
2 changed files with 58 additions and 15 deletions
|
@ -583,6 +583,7 @@ class Schedule(object):
|
|||
'''
|
||||
Execute this method in a multiprocess or thread
|
||||
'''
|
||||
log.debug('=== calling handle_func with data %s ===', data)
|
||||
if salt.utils.platform.is_windows() \
|
||||
or self.opts.get('transport') == 'zeromq':
|
||||
# Since function references can't be pickled and pickling
|
||||
|
@ -631,6 +632,22 @@ class Schedule(object):
|
|||
|
||||
# TODO: Make it readable! Splt to funcs, remove nested try-except-finally sections.
|
||||
try:
|
||||
|
||||
minion_blackout_violation = False
|
||||
if self.opts['pillar'].get('minion_blackout', False):
|
||||
whitelist = self.opts['pillar'].get('minion_blackout_whitelist', [])
|
||||
# this minion is blacked out. Only allow saltutil.refresh_pillar and the whitelist
|
||||
if func != 'saltutil.refresh_pillar' and func not in whitelist:
|
||||
minion_blackout_violation = True
|
||||
elif self.opts['grains'].get('minion_blackout', False):
|
||||
whitelist = self.opts['grains'].get('minion_blackout_whitelist', [])
|
||||
if func != 'saltutil.refresh_pillar' and func not in whitelist:
|
||||
minion_blackout_violation = True
|
||||
if minion_blackout_violation:
|
||||
raise SaltInvocationError('Minion in blackout mode. Set \'minion_blackout\' '
|
||||
'to False in pillar or grains to resume operations. Only '
|
||||
'saltutil.refresh_pillar allowed in blackout mode.')
|
||||
|
||||
ret['pid'] = os.getpid()
|
||||
|
||||
if not self.standalone:
|
||||
|
@ -710,21 +727,6 @@ class Schedule(object):
|
|||
|
||||
self.functions.pack['__context__']['retcode'] = 0
|
||||
|
||||
minion_blackout_violation = False
|
||||
if self.opts['pillar'].get('minion_blackout', False):
|
||||
whitelist = self.opts['pillar'].get('minion_blackout_whitelist', [])
|
||||
# this minion is blacked out. Only allow saltutil.refresh_pillar and the whitelist
|
||||
if func != 'saltutil.refresh_pillar' and func not in whitelist:
|
||||
minion_blackout_violation = True
|
||||
elif self.opts['grains'].get('minion_blackout', False):
|
||||
whitelist = self.opts['grains'].get('minion_blackout_whitelist', [])
|
||||
if func != 'saltutil.refresh_pillar' and func not in whitelist:
|
||||
minion_blackout_violation = True
|
||||
if minion_blackout_violation:
|
||||
raise SaltInvocationError('Minion in blackout mode. Set \'minion_blackout\' '
|
||||
'to False in pillar or grains to resume operations. Only '
|
||||
'saltutil.refresh_pillar allowed in blackout mode.')
|
||||
|
||||
ret['return'] = self.functions[func](*args, **kwargs)
|
||||
|
||||
if not self.standalone:
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import copy
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
|
@ -26,6 +27,7 @@ except ImportError:
|
|||
_CRON_SUPPORTED = False
|
||||
# pylint: enable=import-error
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
ROOT_DIR = os.path.join(integration.TMP, 'schedule-unit-tests')
|
||||
SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
|
||||
|
@ -331,3 +333,42 @@ class ScheduleTestCase(TestCase):
|
|||
self.schedule.eval()
|
||||
self.assertTrue(self.schedule.opts['schedule']['testjob']['_splay'] >
|
||||
self.schedule.opts['schedule']['testjob']['_next_fire_time'])
|
||||
|
||||
def test_handle_func_schedule_minion_blackout(self):
|
||||
'''
|
||||
Tests eval if the schedule from pillar is not a dictionary
|
||||
'''
|
||||
self.schedule.opts.update({'pillar': {'schedule': {}}})
|
||||
self.schedule.opts.update({'grains': {'minion_blackout': True}})
|
||||
|
||||
self.schedule.opts.update(
|
||||
{'schedule': {'testjob': {'function': 'test.true',
|
||||
'seconds': 60}}})
|
||||
data = {'function': 'test.true',
|
||||
'_next_scheduled_fire_time': datetime.datetime(2018,
|
||||
11,
|
||||
21,
|
||||
14,
|
||||
9,
|
||||
53,
|
||||
903438),
|
||||
'run': True,
|
||||
'name': 'testjob',
|
||||
'seconds': 60,
|
||||
'_splay': None,
|
||||
'_seconds': 60,
|
||||
'jid_include': True,
|
||||
'maxrunning': 1,
|
||||
'_next_fire_time': datetime.datetime(2018,
|
||||
11,
|
||||
21,
|
||||
14,
|
||||
8,
|
||||
53,
|
||||
903438)}
|
||||
|
||||
with patch.object(salt.utils.schedule, 'log') as log_mock:
|
||||
with patch('salt.utils.process.daemonize'), \
|
||||
patch('sys.platform', 'linux2'):
|
||||
self.schedule.handle_func(False, 'test.ping', data)
|
||||
self.assertTrue(log_mock.exception.called)
|
||||
|
|
Loading…
Add table
Reference in a new issue