diff --git a/salt/pillar/__init__.py b/salt/pillar/__init__.py index 754f9ef391bf..4592ac0c7bbc 100644 --- a/salt/pillar/__init__.py +++ b/salt/pillar/__init__.py @@ -763,8 +763,6 @@ def render_pstate(self, sls, saltenv, mods, defaults=None): else: # render included state(s) include_states = [] - - matched_pstates = [] for sub_sls in state.pop('include'): if isinstance(sub_sls, dict): sub_sls, v = next(six.iteritems(sub_sls)) @@ -772,45 +770,43 @@ def render_pstate(self, sls, saltenv, mods, defaults=None): key = v.get('key', None) else: key = None - try: - matched_pstates.extend(fnmatch.filter( + matched_pstates = fnmatch.filter( self.avail[saltenv], sub_sls.lstrip('.').replace('/', '.'), - )) + ) except KeyError: errors.extend( ['No matching pillar environment for environment ' '\'{0}\' found'.format(saltenv)] ) - - for sub_sls in set(matched_pstates): - if sub_sls not in mods: - nstate, mods, err = self.render_pstate( - sub_sls, - saltenv, - mods, - defaults - ) - if nstate: - if key: - # If key is x:y, convert it to {x: {y: nstate}} - for key_fragment in reversed(key.split(":")): - nstate = { - key_fragment: nstate - } - if not self.opts.get('pillar_includes_override_sls', False): - include_states.append(nstate) - else: - state = merge( - state, - nstate, - self.merge_strategy, - self.opts.get('renderer', 'yaml'), - self.opts.get('pillar_merge_lists', False)) - if err: - errors += err - + matched_pstates = [sub_sls] + for m_sub_sls in matched_pstates: + if m_sub_sls not in mods: + nstate, mods, err = self.render_pstate( + m_sub_sls, + saltenv, + mods, + defaults + ) + if nstate: + if key: + # If key is x:y, convert it to {x: {y: nstate}} + for key_fragment in reversed(key.split(":")): + nstate = { + key_fragment: nstate + } + if not self.opts.get('pillar_includes_override_sls', False): + include_states.append(nstate) + else: + state = merge( + state, + nstate, + self.merge_strategy, + self.opts.get('renderer', 'yaml'), + self.opts.get('pillar_merge_lists', False)) + if err: + errors += err if not self.opts.get('pillar_includes_override_sls', False): # merge included state(s) with the current state # merged last to ensure that its values are diff --git a/tests/filename_map.yml b/tests/filename_map.yml index 02e622cee106..3f4dc01cb370 100644 --- a/tests/filename_map.yml +++ b/tests/filename_map.yml @@ -192,6 +192,7 @@ salt/output/*: salt/pillar/__init__.py: - integration.minion.test_pillar + - integration.pillar.test_pillar_include salt/(cli/run\.py|runner\.py): - integration.shell.test_runner diff --git a/tests/integration/files/pillar/base/glob_include.sls b/tests/integration/files/pillar/base/glob_include.sls new file mode 100644 index 000000000000..4950242df7be --- /dev/null +++ b/tests/integration/files/pillar/base/glob_include.sls @@ -0,0 +1,2 @@ +include: + - 'glob_include*' diff --git a/tests/integration/files/pillar/base/glob_include_a.sls b/tests/integration/files/pillar/base/glob_include_a.sls new file mode 100644 index 000000000000..226c294cf9e5 --- /dev/null +++ b/tests/integration/files/pillar/base/glob_include_a.sls @@ -0,0 +1,2 @@ +glob-a: + - 'Entry A' diff --git a/tests/integration/files/pillar/base/glob_include_b.sls b/tests/integration/files/pillar/base/glob_include_b.sls new file mode 100644 index 000000000000..257e261283ff --- /dev/null +++ b/tests/integration/files/pillar/base/glob_include_b.sls @@ -0,0 +1,2 @@ +glob-b: + - 'Entry B' diff --git a/tests/integration/files/pillar/base/include-a.sls b/tests/integration/files/pillar/base/include-a.sls new file mode 100644 index 000000000000..1df26dbad0da --- /dev/null +++ b/tests/integration/files/pillar/base/include-a.sls @@ -0,0 +1,2 @@ +a: + - 'Entry A' diff --git a/tests/integration/files/pillar/base/include-b.sls b/tests/integration/files/pillar/base/include-b.sls new file mode 100644 index 000000000000..de1612327a16 --- /dev/null +++ b/tests/integration/files/pillar/base/include-b.sls @@ -0,0 +1,2 @@ +b: + - 'Entry B' diff --git a/tests/integration/files/pillar/base/include.sls b/tests/integration/files/pillar/base/include.sls new file mode 100644 index 000000000000..cae2926c21ba --- /dev/null +++ b/tests/integration/files/pillar/base/include.sls @@ -0,0 +1,5 @@ +include: + - include-a: + key: element:a + - include-b: + key: element:b diff --git a/tests/integration/files/pillar/base/top.sls b/tests/integration/files/pillar/base/top.sls index 0bcfdfb9bd7c..c7c2a8342c6a 100644 --- a/tests/integration/files/pillar/base/top.sls +++ b/tests/integration/files/pillar/base/top.sls @@ -3,6 +3,8 @@ base: - generic - blackout - sdb + - include + - glob_include 'sub_minion': - sdb - generic diff --git a/tests/integration/pillar/test_pillar_include.py b/tests/integration/pillar/test_pillar_include.py new file mode 100644 index 000000000000..1451aee13cd4 --- /dev/null +++ b/tests/integration/pillar/test_pillar_include.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +''' +Pillar include tests +''' +from __future__ import absolute_import, unicode_literals + +from tests.support.case import ModuleCase + + +class PillarIncludeTest(ModuleCase): + + def test_pillar_include(self): + ''' + Test pillar include + ''' + ret = self.minion_run('pillar.items') + assert 'a' in ret['element'] + assert ret['element']['a'] == {'a': ['Entry A']} + assert 'b' in ret['element'] + assert ret['element']['b'] == {'b': ['Entry B']} + + def test_pillar_glob_include(self): + ''' + Test pillar include via glob pattern + ''' + ret = self.minion_run('pillar.items') + assert 'glob-a' in ret + assert ret['glob-a'] == ['Entry A'] + assert 'glob-b' in ret + assert ret['glob-b'] == ['Entry B'] diff --git a/tests/unit/test_module_names.py b/tests/unit/test_module_names.py index 1efcb5869e8f..87348d7885cd 100644 --- a/tests/unit/test_module_names.py +++ b/tests/unit/test_module_names.py @@ -142,6 +142,7 @@ def test_module_name_source_match(self): 'integration.netapi.rest_tornado.test_app', 'integration.netapi.rest_cherrypy.test_app_pam', 'integration.output.test_output', + 'integration.pillar.test_pillar_include', 'integration.proxy.test_shell', 'integration.proxy.test_simple', 'integration.reactor.test_reactor',