Skip to content

Commit

Permalink
Merge pull request #52156 from waynew/8875-pillar-relative-includes
Browse files Browse the repository at this point in the history
Allow pillar relative includes
  • Loading branch information
dwoz authored Mar 12, 2019
2 parents d5d9e72 + 1820a3e commit 96b407a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 5 deletions.
17 changes: 14 additions & 3 deletions salt/pillar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def compile_pillar(self):
return ret_pillar

def destroy(self):
if self._closing:
if hasattr(self, '_closing') and self._closing:
return

self._closing = True
Expand Down Expand Up @@ -712,7 +712,8 @@ def render_pstate(self, sls, saltenv, mods, defaults=None):
defaults = {}
err = ''
errors = []
fn_ = self.client.get_state(sls, saltenv).get('dest', False)
state_data = self.client.get_state(sls, saltenv)
fn_ = state_data.get('dest', False)
if not fn_:
if sls in self.ignored_pillars.get(saltenv, []):
log.debug('Skipping ignored and missing SLS \'%s\' in '
Expand Down Expand Up @@ -795,7 +796,17 @@ def render_pstate(self, sls, saltenv, mods, defaults=None):
key = None

try:
matched_pstates += fnmatch.filter(self.avail[saltenv], sub_sls)
if sub_sls.startswith('.'):
if state_data.get('source', '').endswith('/init.sls'):
include_parts = sls.split('.')
else:
include_parts = sls.split('.')[:-1]
sub_sls = '.'.join(include_parts+[sub_sls[1:]])
matches = fnmatch.filter(
self.avail[saltenv],
sub_sls,
)
matched_pstates.extend(matches)
except KeyError:
errors.extend(
['No matching pillar environment for environment '
Expand Down
145 changes: 143 additions & 2 deletions tests/unit/test_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
'''

# Import python libs
from __future__ import absolute_import
from __future__ import absolute_import, print_function
import os
import shutil
import tempfile
import textwrap

# Import Salt Testing libs
from tests.support.runtests import RUNTIME_VARS
Expand All @@ -19,10 +21,12 @@
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch

# Import salt libs
import salt.exceptions
import salt.fileclient
import salt.pillar
import salt.utils.stringutils
import salt.exceptions

from salt.utils.files import fopen


class MockFileclient(object):
Expand Down Expand Up @@ -725,6 +729,143 @@ def _setup_test_include_sls(self, tempdir):
'test.sub_wildcard_1': {'path': '', 'dest': sub_wildcard_1_sls.name},
}

@with_tempdir()
def test_relative_include(self, tempdir):
join = os.path.join
with fopen(join(tempdir, 'top.sls'), 'w') as f:
print(
textwrap.dedent('''
base:
'*':
- includer
- simple_includer
- includes.with.more.depth
'''),
file=f,
)
includer_dir = join(tempdir, 'includer')
os.makedirs(includer_dir)
with fopen(join(includer_dir, 'init.sls'), 'w') as f:
print(
textwrap.dedent('''
include:
- .this
- includer.that
'''),
file=f,
)
with fopen(join(includer_dir, 'this.sls'), 'w') as f:
print(
textwrap.dedent('''
this:
is all good
'''),
file=f,
)
with fopen(join(includer_dir, 'that.sls'), 'w') as f:
print(
textwrap.dedent('''
that:
is also all good
'''),
file=f,
)

with fopen(join(tempdir, 'simple_includer.sls'), 'w') as simpleincluder:
print(
textwrap.dedent('''
include:
- .simple
- super_simple
'''),
file=simpleincluder,
)
with fopen(join(tempdir, 'simple.sls'), 'w') as f:
print(
textwrap.dedent('''
simple:
simon
'''),
file=f,
)
with fopen(join(tempdir, 'super_simple.sls'), 'w') as f:
print(
textwrap.dedent('''
super simple:
a caveman
'''),
file=f,
)

depth_dir = join(tempdir, 'includes', 'with', 'more')
os.makedirs(depth_dir)
with fopen(join(depth_dir, 'depth.sls'), 'w') as f:
print(
textwrap.dedent('''
include:
- .ramble
- includes.with.more.doors
mordor:
has dark depths
'''),
file=f,
)

with fopen(join(depth_dir, 'ramble.sls'), 'w') as f:
print(
textwrap.dedent('''
found:
my precious
'''),
file=f,
)

with fopen(join(depth_dir, 'doors.sls'), 'w') as f:
print(
textwrap.dedent('''
mojo:
bad risin'
'''),
file=f,
)
opts = {
'optimization_order': [0, 1, 2],
'renderer': 'yaml',
'renderer_blacklist': [],
'renderer_whitelist': [],
'state_top': 'top.sls',
'pillar_roots': {'base': [tempdir]},
'extension_modules': '',
'saltenv': 'base',
'file_roots': [],
'file_ignore_regex': None,
'file_ignore_glob': None,
}
grains = {
'os': 'Ubuntu',
'os_family': 'Debian',
'oscodename': 'raring',
'osfullname': 'Ubuntu',
'osrelease': '13.04',
'kernel': 'Linux',
}
pillar = salt.pillar.Pillar(opts, grains, 'minion', 'base')
# Make sure that confirm_top.confirm_top returns True
pillar.matchers['confirm_top.confirm_top'] = lambda *x, **y: True

# Act
compiled_pillar = pillar.compile_pillar()

# Assert
self.assertEqual(compiled_pillar['this'], 'is all good')
self.assertEqual(compiled_pillar['that'], 'is also all good')
self.assertEqual(compiled_pillar['simple'], 'simon')
self.assertEqual(compiled_pillar['super simple'], 'a caveman')
self.assertEqual(compiled_pillar['mordor'], 'has dark depths')
self.assertEqual(compiled_pillar['found'], 'my precious')
self.assertEqual(compiled_pillar['mojo'], "bad risin'")


@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('salt.transport.client.ReqChannel.factory', MagicMock())
Expand Down

0 comments on commit 96b407a

Please sign in to comment.