Skip to content

Commit

Permalink
Fix pillar serialization for jinja saltstack#60083
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz authored and Joe Eacott committed Jun 8, 2021
1 parent 59d2ffd commit 1938a82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from salt.exceptions import CommandExecutionError, SaltInvocationError, SaltRenderError
from salt.ext import six
from salt.features import features
from salt.loader_context import NamedLoaderContext
from salt.utils.decorators.jinja import JinjaFilter, JinjaGlobal, JinjaTest
from salt.utils.odict import OrderedDict
from salt.utils.versions import LooseVersion
Expand Down Expand Up @@ -477,7 +478,10 @@ def opt_jinja_env_helper(opts, optname):
decoded_context = {}
for key, value in context.items():
if not isinstance(value, str):
decoded_context[key] = value
if isinstance(value, NamedLoaderContext):
decoded_context[key] = value.value()
else:
decoded_context[key] = value
continue

try:
Expand All @@ -490,7 +494,6 @@ def opt_jinja_env_helper(opts, optname):
SLS_ENCODING,
)
decoded_context[key] = salt.utils.data.decode(value)

try:
template = jinja_env.from_string(tmplstr)
template.globals.update(decoded_context)
Expand Down
Empty file.
37 changes: 37 additions & 0 deletions tests/pytests/integration/utils/test_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Tests for the templates utils
"""
import os

import pytest


def test_issue_60083(
salt_call_cli, tmp_path, base_env_state_tree_root_dir,
):
"""
Validate that we can serialize pillars to json in states.
Issue #60083
"""
target_path = tmp_path / "issue-60083-target.txt"
assert not os.path.exists(target_path)
sls_name = "issue-60083"
sls_contents = """
{{ pillar["target-path"] }}:
file.managed:
- contents: |
{{ pillar|json }}
"""
sls_tempfile = pytest.helpers.temp_file(
"{}.sls".format(sls_name), sls_contents, base_env_state_tree_root_dir
)
with sls_tempfile: # , issue_50221_ext_pillar_tempfile:
ret = salt_call_cli.run(
"state.apply", sls_name, pillar={"target-path": str(target_path)}
)
assert ret.stdout.find("Jinja error") == -1
assert ret.json
keys = list(ret.json.keys())
assert len(keys) == 1
key = keys[0]
assert ret.json[key]["changes"]["diff"] == "New file"

0 comments on commit 1938a82

Please sign in to comment.