Merge pull request #50347 from garethgreenaway/41342_beacon_state_module_fixes

[2018.3] Fixes to beacon state module
This commit is contained in:
Nicole Thomas 2018-11-05 09:19:40 -05:00 committed by GitHub
commit 3022b7db5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 3 deletions

View file

@ -428,3 +428,9 @@ class Beacon(object):
tag='/salt/minion/minion_beacon_disabled_complete')
return True
def reset(self):
'''
Reset the beacons to defaults
'''
self.opts['beacons'] = {}

View file

@ -2214,6 +2214,8 @@ class Minion(MinionBase):
self.beacons.list_available_beacons()
elif func == 'validate_beacon':
self.beacons.validate_beacon(name, beacon_data)
elif func == 'reset':
self.beacons.reset()
def environ_setenv(self, tag, data):
'''

View file

@ -134,7 +134,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),
@ -207,7 +207,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': '',
@ -571,3 +571,38 @@ def disable_beacon(name, **kwargs):
# Effectively a no-op, since we can't really return without an event system
ret['comment'] = 'Event module not available. Beacon disable job failed.'
return ret
def reset(**kwargs):
'''
Resest beacon configuration on the minion
CLI Example:
.. code-block:: bash
salt '*' beacons.reset
'''
ret = {'comment': [],
'result': True}
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Beacons would be reset.'
else:
try:
eventer = salt.utils.event.get_event('minion', opts=__opts__)
res = __salt__['event.fire']({'func': 'reset'}, 'manage_beacons')
if res:
event_ret = eventer.get_event(tag='/salt/minion/minion_beacon_reset_complete', wait=30)
if event_ret and event_ret['complete']:
ret['result'] = True
ret['comment'] = 'Beacon configuration reset.'
else:
ret['result'] = False
ret['comment'] = event_ret['comment']
return ret
except KeyError:
# Effectively a no-op, since we can't really return without an event system
ret['comment'] = 'Event module not available. Beacon disable job failed.'
return ret

View file

@ -34,6 +34,12 @@ Management of the Salt beacons
'''
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt libs
from salt.ext import six
import logging
log = logging.getLogger(__name__)
def present(name,
save=False,
@ -54,7 +60,7 @@ def present(name,
'comment': []}
current_beacons = __salt__['beacons.list'](return_yaml=False)
beacon_data = [kwargs]
beacon_data = [{k: v} for k, v in six.iteritems(kwargs)]
if name in current_beacons:

View file

@ -31,6 +31,9 @@ class BeaconsAddDeleteTest(ModuleCase):
if os.path.isfile(self.beacons_config_file_path):
os.unlink(self.beacons_config_file_path)
# Reset beacons
self.run_function('beacons.reset')
def test_add_and_delete(self):
'''
Test adding and deleting a beacon
@ -81,6 +84,9 @@ class BeaconsTest(ModuleCase):
self.run_function('beacons.delete', ['ps'])
self.run_function('beacons.save')
# Reset beacons
self.run_function('beacons.reset')
def test_disable(self):
'''
Test disabling beacons

View file

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the beacon states
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Testing Libs
from tests.support.case import ModuleCase
from tests.support.helpers import destructiveTest
from tests.support.mixins import SaltReturnAssertsMixin
import logging
log = logging.getLogger(__name__)
@destructiveTest
class BeaconStateTestCase(ModuleCase, SaltReturnAssertsMixin):
'''
Test beacon states
'''
def setUp(self):
'''
'''
self.run_function('beacons.reset')
def tearDown(self):
self.run_function('beacons.reset')
def test_present_absent(self):
kwargs = {'/': '38%', 'interval': 5}
ret = self.run_state(
'beacon.present',
name='diskusage',
**kwargs
)
self.assertSaltTrueReturn(ret)
ret = self.run_function('beacons.list', return_yaml=False)
self.assertTrue('diskusage' in ret)
self.assertTrue({'interval': 5} in ret['diskusage'])
self.assertTrue({'/': '38%'} in ret['diskusage'])
ret = self.run_state(
'beacon.absent',
name='diskusage',
)
self.assertSaltTrueReturn(ret)
ret = self.run_function('beacons.list', return_yaml=False)
self.assertEqual(ret, {'beacons': {}})