From 52e5d263c5a61defb7b8a637d7dfb6aadede7f31 Mon Sep 17 00:00:00 2001 From: Adam Mendlik Date: Sun, 24 Nov 2024 19:19:09 -0700 Subject: [PATCH] Generate SLS context for non-SLS templates Rather than logging a warning, just populate some of the context variables with appropriate values. --- changelog/67067.changed.md | 1 + salt/utils/templates.py | 12 +++++- .../utils/templates/test_wrap_tmpl_func.py | 42 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 changelog/67067.changed.md diff --git a/changelog/67067.changed.md b/changelog/67067.changed.md new file mode 100644 index 000000000000..3df2a8187057 --- /dev/null +++ b/changelog/67067.changed.md @@ -0,0 +1 @@ +Remove warning when running `slsutil.renderer` on non-SLS files diff --git a/salt/utils/templates.py b/salt/utils/templates.py index d9204cf71545..d4e42ecca424 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -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 "" diff --git a/tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py b/tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py index 9c093a263004..275bf2db0cb9 100644 --- a/tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py +++ b/tests/pytests/unit/utils/templates/test_wrap_tmpl_func.py @@ -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", + )