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 ae98e37 commit 0e942f2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 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
42 changes: 42 additions & 0 deletions tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,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 0e942f2

Please sign in to comment.