mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 00:55:19 +00:00
Updating documentation for wtmp, btmp, fixing service tests and updating various bits in the service beacon.
This commit is contained in:
parent
358707d84a
commit
ee3befbed8
@ -5,7 +5,7 @@ Beacon to fire events at failed login of users
|
||||
.. code-block:: yaml
|
||||
|
||||
beacons:
|
||||
btmp: {}
|
||||
btmp: []
|
||||
'''
|
||||
|
||||
# Import python libs
|
||||
@ -57,9 +57,9 @@ def validate(config):
|
||||
Validate the beacon configuration
|
||||
'''
|
||||
# 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 '
|
||||
'be a list of dictionaries.')
|
||||
'be a list.')
|
||||
return True, 'Valid beacon configuration'
|
||||
|
||||
|
||||
@ -71,7 +71,7 @@ def beacon(config):
|
||||
.. code-block:: yaml
|
||||
|
||||
beacons:
|
||||
btmp: {}
|
||||
btmp: []
|
||||
'''
|
||||
ret = []
|
||||
with salt.utils.files.fopen(BTMP, 'rb') as fp_:
|
||||
|
@ -32,13 +32,14 @@ def validate(config):
|
||||
return False, ('Configuration for service beacon'
|
||||
' requires services.')
|
||||
else:
|
||||
log.debug('config {}'.format(config))
|
||||
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 '
|
||||
'be a list of dictionaries.')
|
||||
else:
|
||||
for dict_item in config_item:
|
||||
if not isinstance(config_item, dict):
|
||||
for dict_item in _config['services'][config_item]:
|
||||
if not isinstance(_config['services'][config][dict_item], dict):
|
||||
return False, ('Configuration for service beacon '
|
||||
'must be a list of dictionaries.')
|
||||
|
||||
@ -55,7 +56,7 @@ def beacon(config):
|
||||
|
||||
beacons:
|
||||
service:
|
||||
- service:
|
||||
- services:
|
||||
salt-master:
|
||||
mysql:
|
||||
|
||||
@ -115,6 +116,7 @@ def beacon(config):
|
||||
service_config = _config['services'][service]
|
||||
|
||||
ret_dict[service] = {'running': __salt__['service.status'](service)}
|
||||
log.debug('ret {}'.format(ret_dict))
|
||||
ret_dict['service_name'] = service
|
||||
ret_dict['tag'] = service
|
||||
currtime = time.time()
|
||||
@ -122,25 +124,25 @@ def beacon(config):
|
||||
# If no options is given to the service, we fall back to the defaults
|
||||
# assign a False value to oncleanshutdown and onchangeonly. Those
|
||||
# key:values are then added to the service dictionary.
|
||||
if not service_config[service]:
|
||||
service_config[service] = {}
|
||||
if 'oncleanshutdown' not in service_config[service]:
|
||||
service_config[service]['oncleanshutdown'] = False
|
||||
if 'emitatstartup' not in service_config[service]:
|
||||
service_config[service]['emitatstartup'] = True
|
||||
if 'onchangeonly' not in service_config[service]:
|
||||
service_config[service]['onchangeonly'] = False
|
||||
if not service_config:
|
||||
service_config = {}
|
||||
if 'oncleanshutdown' not in service_config:
|
||||
service_config['oncleanshutdown'] = False
|
||||
if 'emitatstartup' not in service_config:
|
||||
service_config['emitatstartup'] = True
|
||||
if 'onchangeonly' not in service_config:
|
||||
service_config['onchangeonly'] = False
|
||||
|
||||
# We only want to report the nature of the shutdown
|
||||
# if the current running status is False
|
||||
# as well as if the config for the beacon asks for it
|
||||
if 'uncleanshutdown' in service_config[service] and not ret_dict[service]['running']:
|
||||
filename = service_config[service]['uncleanshutdown']
|
||||
if 'uncleanshutdown' in service_config and not ret_dict[service]['running']:
|
||||
filename = service_config['uncleanshutdown']
|
||||
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:
|
||||
LAST_STATUS[service] = ret_dict[service]
|
||||
if not service_config[service]['emitatstartup']:
|
||||
if not service_config['emitatstartup']:
|
||||
continue
|
||||
else:
|
||||
ret.append(ret_dict)
|
||||
|
@ -5,7 +5,7 @@ Beacon to fire events at login of users as registered in the wtmp file
|
||||
.. code-block:: yaml
|
||||
|
||||
beacons:
|
||||
wtmp: {}
|
||||
wtmp: []
|
||||
'''
|
||||
|
||||
# Import Python libs
|
||||
@ -60,8 +60,8 @@ def __validate__(config):
|
||||
Validate the beacon configuration
|
||||
'''
|
||||
# Configuration for wtmp beacon should be a list of dicts
|
||||
if not isinstance(config, dict):
|
||||
return False, ('Configuration for wtmp beacon must be a dictionary.')
|
||||
if not isinstance(config, list):
|
||||
return False, ('Configuration for wtmp beacon must be a list.')
|
||||
return True, 'Valid beacon configuration'
|
||||
|
||||
|
||||
@ -73,7 +73,7 @@ def beacon(config):
|
||||
.. code-block:: yaml
|
||||
|
||||
beacons:
|
||||
wtmp: {}
|
||||
wtmp: []
|
||||
'''
|
||||
ret = []
|
||||
with salt.utils.files.fopen(WTMP, 'rb') as fp_:
|
||||
|
@ -10,7 +10,7 @@ from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
|
||||
# Salt libs
|
||||
import salt.beacons.service as service
|
||||
import salt.beacons.service as service_beacon
|
||||
|
||||
PATCH_OPTS = dict(autospec=True, spec_set=True)
|
||||
|
||||
@ -25,7 +25,7 @@ class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
||||
|
||||
def setup_loader_modules(self):
|
||||
return {
|
||||
service: {
|
||||
service_beacon: {
|
||||
'__context__': {},
|
||||
'__salt__': {},
|
||||
}
|
||||
@ -34,41 +34,41 @@ class ServiceBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
||||
def test_non_list_config(self):
|
||||
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.'))
|
||||
|
||||
def test_empty_config(self):
|
||||
config = [{}]
|
||||
|
||||
ret = service.validate(config)
|
||||
ret = service_beacon.validate(config)
|
||||
|
||||
self.assertEqual(ret, (False, 'Configuration for ps '
|
||||
'beacon requires processes.'))
|
||||
self.assertEqual(ret, (False, 'Configuration for service '
|
||||
'beacon requires services.'))
|
||||
|
||||
def test_service_running(self):
|
||||
with patch.dict(service.__salt__,
|
||||
{'service.status': TEMPERATURE_MOCK}):
|
||||
config = [{'processes': {'salt-master': 'running'}}]
|
||||
with patch.dict(service_beacon.__salt__,
|
||||
{'service.status': MagicMock(return_value=True)}):
|
||||
config = [{'services': {'salt-master': {}}}]
|
||||
|
||||
ret = service.validate(config)
|
||||
ret = service_beacon.validate(config)
|
||||
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
ret = service.beacon(config)
|
||||
self.assertEqual(ret, [{'salt-master': 'Running'}])
|
||||
ret = service_beacon.beacon(config)
|
||||
self.assertEqual(ret, [{'service_name': 'salt-master',
|
||||
'salt-master': {'running': True}}])
|
||||
|
||||
def test_service_not_running(self):
|
||||
with patch('psutil.process_iter', **PATCH_OPTS) as mock_process_iter:
|
||||
mock_process_iter.return_value = [FakeProcess(cmdline=['salt-master'], pid=3),
|
||||
FakeProcess(cmdline=['salt-minion'], pid=4)]
|
||||
config = [{'processes': {'mysql': 'stopped'}}]
|
||||
with patch.dict(service_beacon.__salt__,
|
||||
{'service.status': MagicMock(return_value=False)}):
|
||||
config = [{'services': {'salt-master': {}}}]
|
||||
|
||||
ret = service.validate(config)
|
||||
ret = service_beacon.validate(config)
|
||||
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
ret = service.beacon(config)
|
||||
self.assertEqual(ret, [{'mysql': 'Stopped'}])
|
||||
|
||||
ret = service_beacon.beacon(config)
|
||||
self.assertEqual(ret, [{'service_name': 'salt-master',
|
||||
'salt-master': {'running': False}}])
|
||||
|
Loading…
Reference in New Issue
Block a user