Updating documentation for wtmp, btmp, fixing service tests and updating various bits in the service beacon.

This commit is contained in:
Gareth J. Greenaway 2017-07-13 12:36:35 -07:00
parent 358707d84a
commit ee3befbed8
4 changed files with 47 additions and 45 deletions

View File

@ -5,7 +5,7 @@ Beacon to fire events at failed login of users
.. code-block:: yaml .. code-block:: yaml
beacons: beacons:
btmp: {} btmp: []
''' '''
# Import python libs # Import python libs
@ -57,9 +57,9 @@ def validate(config):
Validate the beacon configuration Validate the beacon configuration
''' '''
# Configuration for load beacon should be a list of dicts # Configuration for load beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, list):
return False, ('Configuration for btmp beacon must ' return False, ('Configuration for btmp beacon must '
'be a list of dictionaries.') 'be a list.')
return True, 'Valid beacon configuration' return True, 'Valid beacon configuration'
@ -71,7 +71,7 @@ def beacon(config):
.. code-block:: yaml .. code-block:: yaml
beacons: beacons:
btmp: {} btmp: []
''' '''
ret = [] ret = []
with salt.utils.files.fopen(BTMP, 'rb') as fp_: with salt.utils.files.fopen(BTMP, 'rb') as fp_:

View File

@ -32,13 +32,14 @@ def validate(config):
return False, ('Configuration for service beacon' return False, ('Configuration for service beacon'
' requires services.') ' requires services.')
else: else:
log.debug('config {}'.format(config))
for config_item in _config['services']: for config_item in _config['services']:
if not isinstance(config_item, dict): if not isinstance(_config['services'][config_item], dict):
return False, ('Configuration for service beacon must ' return False, ('Configuration for service beacon must '
'be a list of dictionaries.') 'be a list of dictionaries.')
else: else:
for dict_item in config_item: for dict_item in _config['services'][config_item]:
if not isinstance(config_item, dict): if not isinstance(_config['services'][config][dict_item], dict):
return False, ('Configuration for service beacon ' return False, ('Configuration for service beacon '
'must be a list of dictionaries.') 'must be a list of dictionaries.')
@ -55,7 +56,7 @@ def beacon(config):
beacons: beacons:
service: service:
- service: - services:
salt-master: salt-master:
mysql: mysql:
@ -115,6 +116,7 @@ def beacon(config):
service_config = _config['services'][service] service_config = _config['services'][service]
ret_dict[service] = {'running': __salt__['service.status'](service)} ret_dict[service] = {'running': __salt__['service.status'](service)}
log.debug('ret {}'.format(ret_dict))
ret_dict['service_name'] = service ret_dict['service_name'] = service
ret_dict['tag'] = service ret_dict['tag'] = service
currtime = time.time() currtime = time.time()
@ -122,25 +124,25 @@ def beacon(config):
# If no options is given to the service, we fall back to the defaults # If no options is given to the service, we fall back to the defaults
# assign a False value to oncleanshutdown and onchangeonly. Those # assign a False value to oncleanshutdown and onchangeonly. Those
# key:values are then added to the service dictionary. # key:values are then added to the service dictionary.
if not service_config[service]: if not service_config:
service_config[service] = {} service_config = {}
if 'oncleanshutdown' not in service_config[service]: if 'oncleanshutdown' not in service_config:
service_config[service]['oncleanshutdown'] = False service_config['oncleanshutdown'] = False
if 'emitatstartup' not in service_config[service]: if 'emitatstartup' not in service_config:
service_config[service]['emitatstartup'] = True service_config['emitatstartup'] = True
if 'onchangeonly' not in service_config[service]: if 'onchangeonly' not in service_config:
service_config[service]['onchangeonly'] = False service_config['onchangeonly'] = False
# We only want to report the nature of the shutdown # We only want to report the nature of the shutdown
# if the current running status is False # if the current running status is False
# as well as if the config for the beacon asks for it # as well as if the config for the beacon asks for it
if 'uncleanshutdown' in service_config[service] and not ret_dict[service]['running']: if 'uncleanshutdown' in service_config and not ret_dict[service]['running']:
filename = service_config[service]['uncleanshutdown'] filename = service_config['uncleanshutdown']
ret_dict[service]['uncleanshutdown'] = True if os.path.exists(filename) else False ret_dict[service]['uncleanshutdown'] = True if os.path.exists(filename) else False
if 'onchangeonly' in service_config[service] and service_config[service]['onchangeonly'] is True: if 'onchangeonly' in service_config and service_config['onchangeonly'] is True:
if service not in LAST_STATUS: if service not in LAST_STATUS:
LAST_STATUS[service] = ret_dict[service] LAST_STATUS[service] = ret_dict[service]
if not service_config[service]['emitatstartup']: if not service_config['emitatstartup']:
continue continue
else: else:
ret.append(ret_dict) ret.append(ret_dict)

View File

@ -5,7 +5,7 @@ Beacon to fire events at login of users as registered in the wtmp file
.. code-block:: yaml .. code-block:: yaml
beacons: beacons:
wtmp: {} wtmp: []
''' '''
# Import Python libs # Import Python libs
@ -60,8 +60,8 @@ def __validate__(config):
Validate the beacon configuration Validate the beacon configuration
''' '''
# Configuration for wtmp beacon should be a list of dicts # Configuration for wtmp beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, list):
return False, ('Configuration for wtmp beacon must be a dictionary.') return False, ('Configuration for wtmp beacon must be a list.')
return True, 'Valid beacon configuration' return True, 'Valid beacon configuration'
@ -73,7 +73,7 @@ def beacon(config):
.. code-block:: yaml .. code-block:: yaml
beacons: beacons:
wtmp: {} wtmp: []
''' '''
ret = [] ret = []
with salt.utils.files.fopen(WTMP, 'rb') as fp_: with salt.utils.files.fopen(WTMP, 'rb') as fp_:

View File

@ -10,7 +10,7 @@ from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
from tests.support.mixins import LoaderModuleMockMixin from tests.support.mixins import LoaderModuleMockMixin
# Salt libs # Salt libs
import salt.beacons.service as service import salt.beacons.service as service_beacon
PATCH_OPTS = dict(autospec=True, spec_set=True) PATCH_OPTS = dict(autospec=True, spec_set=True)
@ -25,7 +25,7 @@ class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self): def setup_loader_modules(self):
return { return {
service: { service_beacon: {
'__context__': {}, '__context__': {},
'__salt__': {}, '__salt__': {},
} }
@ -34,41 +34,41 @@ class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
def test_non_list_config(self): def test_non_list_config(self):
config = {} config = {}
ret = service.validate(config) ret = service_beacon.validate(config)
self.assertEqual(ret, (False, 'Configuration for ps beacon must' self.assertEqual(ret, (False, 'Configuration for service beacon must'
' be a list.')) ' be a list.'))
def test_empty_config(self): def test_empty_config(self):
config = [{}] config = [{}]
ret = service.validate(config) ret = service_beacon.validate(config)
self.assertEqual(ret, (False, 'Configuration for ps ' self.assertEqual(ret, (False, 'Configuration for service '
'beacon requires processes.')) 'beacon requires services.'))
def test_service_running(self): def test_service_running(self):
with patch.dict(service.__salt__, with patch.dict(service_beacon.__salt__,
{'service.status': TEMPERATURE_MOCK}): {'service.status': MagicMock(return_value=True)}):
config = [{'processes': {'salt-master': 'running'}}] config = [{'services': {'salt-master': {}}}]
ret = service.validate(config) ret = service_beacon.validate(config)
self.assertEqual(ret, (True, 'Valid beacon configuration')) self.assertEqual(ret, (True, 'Valid beacon configuration'))
ret = service.beacon(config) ret = service_beacon.beacon(config)
self.assertEqual(ret, [{'salt-master': 'Running'}]) self.assertEqual(ret, [{'service_name': 'salt-master',
'salt-master': {'running': True}}])
def test_service_not_running(self): def test_service_not_running(self):
with patch('psutil.process_iter', **PATCH_OPTS) as mock_process_iter: with patch.dict(service_beacon.__salt__,
mock_process_iter.return_value = [FakeProcess(cmdline=['salt-master'], pid=3), {'service.status': MagicMock(return_value=False)}):
FakeProcess(cmdline=['salt-minion'], pid=4)] config = [{'services': {'salt-master': {}}}]
config = [{'processes': {'mysql': 'stopped'}}]
ret = service.validate(config) ret = service_beacon.validate(config)
self.assertEqual(ret, (True, 'Valid beacon configuration')) self.assertEqual(ret, (True, 'Valid beacon configuration'))
ret = service.beacon(config) ret = service_beacon.beacon(config)
self.assertEqual(ret, [{'mysql': 'Stopped'}]) self.assertEqual(ret, [{'service_name': 'salt-master',
'salt-master': {'running': False}}])