Merge pull request #38342 from scthi/bugfix-ext-pillar-nodegroups

Bugfix ext pillar nodegroups
This commit is contained in:
Mike Place 2016-12-22 09:47:42 -07:00 committed by GitHub
commit bbc149c67f
3 changed files with 30 additions and 11 deletions

View file

@ -22,7 +22,7 @@ class Cache(object):
:param cache:
The name of the cache driver to use. This is the name of the python
module of the `salt.cache` package. Defult is `localfs`.
module of the `salt.cache` package. Default is `localfs`.
:param serial:
The module of `salt.serializers` package that should be used by the cache

View file

@ -40,29 +40,35 @@ Configuring Nodegroups Pillar
from __future__ import absolute_import
# Import Salt libs
from salt.minion import Matcher
from salt.utils.minions import CkMinions
# Import 3rd-party libs
import salt.ext.six as six
__version__ = '0.0.1'
__version__ = '0.0.2'
def ext_pillar(minion_id, pillar, pillar_name=None):
'''
A salt external pillar which provides the list of nodegroups of which the minion is a memeber.
A salt external pillar which provides the list of nodegroups of which the minion is a member.
:param minion_id: provided by salt, but not used by nodegroups ext_pillar
:param minion_id: used for compound matching nodegroups
:param pillar: provided by salt, but not used by nodegroups ext_pillar
:param pillar_name: optional name to use for the pillar, defaults to 'nodegroups'
:return: a dictionary which is included by the salt master in the pillars returned to the minion
'''
pillar_name = pillar_name or 'nodegroups'
m = Matcher(__opts__)
all_nodegroups = __opts__['nodegroups']
nodegroups_minion_is_in = []
ckminions = None
for nodegroup_name in six.iterkeys(all_nodegroups):
if m.nodegroup_match(nodegroup_name, all_nodegroups):
ckminions = ckminions or CkMinions(__opts__)
match = ckminions.check_minions(
all_nodegroups[nodegroup_name],
'compound')
if minion_id in match:
nodegroups_minion_is_in.append(nodegroup_name)
return {pillar_name: nodegroups_minion_is_in}

View file

@ -6,6 +6,7 @@ from __future__ import absolute_import
# Import Salt Testing libs
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import patch, MagicMock
ensure_in_syspath('../../')
@ -15,25 +16,37 @@ from salt.pillar import nodegroups
fake_minion_id = 'fake_id'
fake_pillar = {}
fake_nodegroups = {
'a': fake_minion_id,
'b': 'nodegroup_b',
'groupA': fake_minion_id,
'groupB': 'another_minion_id',
}
fake_opts = {
'cache': 'localfs',
'nodegroups': fake_nodegroups,
'id': fake_minion_id
}
fake_opts = {'nodegroups': fake_nodegroups, 'id': fake_minion_id}
fake_pillar_name = 'fake_pillar_name'
nodegroups.__opts__ = fake_opts
def side_effect(group_sel, t):
if group_sel.find(fake_minion_id) != -1:
return [fake_minion_id, ]
return ['another_minion_id', ]
class NodegroupsPillarTestCase(TestCase):
'''
Tests for salt.pillar.nodegroups
'''
@patch('salt.utils.minions.CkMinions.check_minions',
MagicMock(side_effect=side_effect))
def _runner(self, expected_ret, pillar_name=None):
pillar_name = pillar_name or fake_pillar_name
actual_ret = nodegroups.ext_pillar(fake_minion_id, fake_pillar, pillar_name=pillar_name)
self.assertDictEqual(actual_ret, expected_ret)
def test_succeeds(self):
ret = {fake_pillar_name: ['a', ]}
ret = {fake_pillar_name: ['groupA', ]}
self._runner(ret)