Skip to content

Commit

Permalink
Generate SLS context for non-SLS templates
Browse files Browse the repository at this point in the history
Rather than logging a warning, just populate some of the context
variables with appropriate values.
  • Loading branch information
amendlik committed Nov 25, 2024
1 parent 9233e1c commit c95098e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
12 changes: 10 additions & 2 deletions salt/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,16 @@ def generate_sls_context(tmplpath, sls):
elif template.endswith(f"{slspath}/init.sls"):
template = template[-(9 + len(slspath)) :]
else:
# Something went wrong
log.warning("Failed to determine proper template path")
# It is not an SLS file being processed,
template = sls
sls_context.update(
dict(
tplpath=tmplpath,
tplfile=template,
tpldir=str(pathlib.Path(sls).parents[0]),
)
)
return sls_context

slspath = template.rsplit("/", 1)[0] if "/" in template else ""

Expand Down
46 changes: 45 additions & 1 deletion tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def _test_generated_sls_context(tmplpath, sls, **expected):
tmplpath = f"C:{tmplpath}"
expected["tplpath"] = tmplpath
actual = generate_sls_context(tmplpath, sls)
assert {key: actual[key] for key in expected if key in actual} == actual
for key, value in expected.items():
assert key in actual
assert actual[key] == expected[key]


def test_sls_context_call(tmp_path):
Expand Down Expand Up @@ -216,3 +218,45 @@ def test_generate_sls_context__backslash_in_path():
sls_path="foo",
slspath="foo",
)


def test_generate_sls_context__non_sls_root():
"""generate_sls_context - Non-SLS template in the root directory
(Issue #56410)
"""
_test_generated_sls_context(
"jinja.yaml",
"jinja.yaml",
tplpath="jinja.yaml",
tplfile="jinja.yaml",
tpldir=".",
)


def test_generate_sls_context__non_sls_one_level():
"""generate_sls_context - Non-SLS template with one-level directory
(Issue #56410)
"""
_test_generated_sls_context(
"one/jinja.yaml",
"one/jinja.yaml",
tplpath="one/jinja.yaml",
tplfile="one/jinja.yaml",
tpldir="one",
)


def test_generate_sls_context__non_sls_two_level():
"""generate_sls_context - Non-SLS template with two-level directory
(Issue #56410)
"""
_test_generated_sls_context(
"one/two/jinja.yaml",
"one/two/jinja.yaml",
tplpath="one/two/jinja.yaml",
tplfile="one/two/jinja.yaml",
tpldir="one/two",
)

0 comments on commit c95098e

Please sign in to comment.