mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Updating various documentation and beacons. Adding unit tests for the beacon module.
This commit is contained in:
parent
61eb8b37db
commit
474b6dbdf8
6 changed files with 135 additions and 7 deletions
|
@ -37,8 +37,9 @@ class Beacon(object):
|
|||
.. code_block:: yaml
|
||||
beacons:
|
||||
inotify:
|
||||
- /etc/fstab: {}
|
||||
- /var/cache/foo: {}
|
||||
- files:
|
||||
- /etc/fstab: {}
|
||||
- /var/cache/foo: {}
|
||||
'''
|
||||
ret = []
|
||||
b_config = copy.deepcopy(config)
|
||||
|
@ -205,6 +206,8 @@ class Beacon(object):
|
|||
# Fire the complete event back along with the list of beacons
|
||||
evt = salt.utils.event.get_event('minion', opts=self.opts)
|
||||
b_conf = self.functions['config.merge']('beacons')
|
||||
if not isinstance(self.opts['beacons'], dict):
|
||||
self.opts['beacons'] = {}
|
||||
self.opts['beacons'].update(b_conf)
|
||||
evt.fire_event({'complete': True, 'beacons': self.opts['beacons']},
|
||||
tag='/salt/minion/minion_beacons_list_complete')
|
||||
|
|
|
@ -38,7 +38,7 @@ def validate(config):
|
|||
Validate the beacon configuration
|
||||
'''
|
||||
if not isinstance(config, list):
|
||||
return False, ('Configuration for rest_example beacon must be a list.')
|
||||
return False, ('Configuration for proxy_example beacon must be a list.')
|
||||
return True, 'Valid beacon configuration'
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ __virtualname__ = 'ps'
|
|||
|
||||
def __virtual__():
|
||||
if not HAS_PSUTIL:
|
||||
return (False, 'cannot load network_info beacon: psutil not available')
|
||||
return (False, 'cannot load ps beacon: psutil not available')
|
||||
return __virtualname__
|
||||
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ def _get_loc():
|
|||
return __context__[LOC_KEY]
|
||||
|
||||
|
||||
def __validate__(config):
|
||||
def validate(config):
|
||||
'''
|
||||
Validate the beacon configuration
|
||||
'''
|
||||
|
|
|
@ -81,7 +81,7 @@ def add(name, beacon_data, **kwargs):
|
|||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' beacons.add ps "{'salt-master': 'stopped', 'apache2': 'stopped'}"
|
||||
salt '*' beacons.add ps "[{'salt-master': 'stopped', 'apache2': 'stopped'}]"
|
||||
|
||||
'''
|
||||
ret = {'comment': 'Failed to add beacon {0}.'.format(name),
|
||||
|
@ -125,6 +125,7 @@ def add(name, beacon_data, **kwargs):
|
|||
res = __salt__['event.fire']({'name': name, 'beacon_data': beacon_data, 'func': 'add'}, 'manage_beacons')
|
||||
if res:
|
||||
event_ret = eventer.get_event(tag='/salt/minion/minion_beacon_add_complete', wait=30)
|
||||
log.debug('=== event_ret {} ==='.format(event_ret))
|
||||
if event_ret and event_ret['complete']:
|
||||
beacons = event_ret['beacons']
|
||||
if name in beacons and beacons[name] == beacon_data:
|
||||
|
@ -149,7 +150,7 @@ def modify(name, beacon_data, **kwargs):
|
|||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' beacons.modify ps "{'salt-master': 'stopped', 'apache2': 'stopped'}"
|
||||
salt '*' beacons.modify ps "[{'salt-master': 'stopped', 'apache2': 'stopped'}]"
|
||||
'''
|
||||
|
||||
ret = {'comment': '',
|
||||
|
@ -252,6 +253,7 @@ def delete(name, **kwargs):
|
|||
if res:
|
||||
event_ret = eventer.get_event(tag='/salt/minion/minion_beacon_delete_complete', wait=30)
|
||||
if event_ret and event_ret['complete']:
|
||||
log.debug('== event_ret {} =='.format(event_ret))
|
||||
beacons = event_ret['beacons']
|
||||
if name not in beacons:
|
||||
ret['result'] = True
|
||||
|
|
123
tests/unit/modules/test_beacons.py
Normal file
123
tests/unit/modules/test_beacons.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
||||
'''
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
import os
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.paths import TMP
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
MagicMock,
|
||||
patch,
|
||||
NO_MOCK,
|
||||
NO_MOCK_REASON
|
||||
)
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.modules.beacons as beacons
|
||||
from salt.utils.event import SaltEvent
|
||||
|
||||
SOCK_DIR = os.path.join(TMP, 'test-socks')
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
class BeaconsTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.beacons
|
||||
'''
|
||||
def setup_loader_modules(self):
|
||||
return {beacons: {}}
|
||||
|
||||
def test_delete(self):
|
||||
'''
|
||||
Test deleting a beacon.
|
||||
'''
|
||||
comm1 = 'Deleted beacon: ps.'
|
||||
event_returns = [
|
||||
{'complete': True,
|
||||
'tag': '/salt/minion/minion_beacons_delete_complete',
|
||||
'beacons': {}},
|
||||
]
|
||||
|
||||
with patch.dict(beacons.__opts__, {'beacons': {'ps': [{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]}, 'sock_dir': SOCK_DIR}):
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.dict(beacons.__salt__, {'event.fire': mock}):
|
||||
with patch.object(SaltEvent, 'get_event', side_effect=event_returns):
|
||||
self.assertDictEqual(beacons.delete('ps'),
|
||||
{'comment': comm1, 'result': True})
|
||||
|
||||
def test_add(self):
|
||||
'''
|
||||
Test adding a beacon
|
||||
'''
|
||||
comm1 = 'Added beacon: ps.'
|
||||
event_returns = [{'complete': True,
|
||||
'tag': '/salt/minion/minion_beacons_list_complete',
|
||||
'beacons': {}},
|
||||
{'complete': True,
|
||||
'tag': '/salt/minion/minion_beacon_add_complete',
|
||||
'beacons': {'ps': [{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]}}]
|
||||
|
||||
with patch.dict(beacons.__opts__, {'beacons': {}, 'sock_dir': SOCK_DIR}):
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.dict(beacons.__salt__, {'event.fire': mock}):
|
||||
with patch.object(SaltEvent, 'get_event', side_effect=event_returns):
|
||||
self.assertDictEqual(beacons.add('ps', [{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]),
|
||||
{'comment': comm1, 'result': True})
|
||||
|
||||
def test_save(self):
|
||||
'''
|
||||
Test saving beacons.
|
||||
'''
|
||||
comm1 = 'Beacons saved to //tmp/beacons.conf.'
|
||||
with patch.dict(beacons.__opts__, {'config_dir': '', 'beacons': {},
|
||||
'default_include': '/tmp/',
|
||||
'sock_dir': SOCK_DIR}):
|
||||
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.dict(beacons.__salt__, {'event.fire': mock}):
|
||||
_ret_value = {'complete': True, 'beacons': {}}
|
||||
with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
|
||||
self.assertDictEqual(beacons.save(),
|
||||
{'comment': comm1, 'result': True})
|
||||
|
||||
def test_disable(self):
|
||||
'''
|
||||
Test disabling beacons
|
||||
'''
|
||||
comm1 = 'Disabled beacons on minion.'
|
||||
event_returns = [{'complete': True,
|
||||
'tag': '/salt/minion/minion_beacons_disabled_complete',
|
||||
'beacons': {'enabled': False,
|
||||
'ps': [{'processes': {'salt-master': 'stopped',
|
||||
'apache2': 'stopped'}}]}}]
|
||||
|
||||
with patch.dict(beacons.__opts__, {'beacons': {}, 'sock_dir': SOCK_DIR}):
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.dict(beacons.__salt__, {'event.fire': mock}):
|
||||
with patch.object(SaltEvent, 'get_event', side_effect=event_returns):
|
||||
self.assertDictEqual(beacons.disable(),
|
||||
{'comment': comm1, 'result': True})
|
||||
|
||||
def test_enable(self):
|
||||
'''
|
||||
Test enabling beacons
|
||||
'''
|
||||
comm1 = 'Enabled beacons on minion.'
|
||||
event_returns = [{'complete': True,
|
||||
'tag': '/salt/minion/minion_beacon_enabled_complete',
|
||||
'beacons': {'enabled': True,
|
||||
'ps': [{'processes': {'salt-master': 'stopped',
|
||||
'apache2': 'stopped'}}]}}]
|
||||
|
||||
with patch.dict(beacons.__opts__, {'beacons': {}, 'sock_dir': SOCK_DIR}):
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.dict(beacons.__salt__, {'event.fire': mock}):
|
||||
with patch.object(SaltEvent, 'get_event', side_effect=event_returns):
|
||||
self.assertDictEqual(beacons.enable(),
|
||||
{'comment': comm1, 'result': True})
|
Loading…
Add table
Reference in a new issue