Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert relative paths to absolute as their associated sls file is processed #61659

Merged
1 change: 1 addition & 0 deletions changelog/54179.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert relative paths to absolute when rendering relative state files
3 changes: 2 additions & 1 deletion salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4131,7 +4131,8 @@ def render_state(self, sls, saltenv, mods, matches, local=False, context=None):
log.error(msg)
errors.append(msg)
continue
inc_sls = ".".join(p_comps[:-level_count] + [include])
# relative paths become abolute paths, otherwise issue allowing for both
inc_sls = os.sep.join(p_comps[:-level_count] + [include])

if env_key != xenv_key:
if matches is None:
Expand Down
293 changes: 293 additions & 0 deletions tests/pytests/integration/states/test_highstate_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
"""
Tests for the highstate state relative and absolute paths
"""
import logging

import pytest

log = logging.getLogger(__name__)


pytestmark = [
pytest.mark.slow_test,
pytest.mark.windows_whitelisted,
]


@pytest.fixture(scope="module")
def pillar_tree(salt_master, salt_minion, salt_call_cli):
top_file = """
base:
'{}':
- basic
""".format(
salt_minion.id
)
basic_pillar_file = """
monty: python
companions:
three:
- liz
- jo
- sarah jane
"""
top_tempfile = salt_master.pillar_tree.base.temp_file("top.sls", top_file)
basic_tempfile = salt_master.pillar_tree.base.temp_file(
"basic.sls", basic_pillar_file
)

try:
with top_tempfile, basic_tempfile:
ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
assert ret.exitcode == 0
assert ret.json is True
yield
finally:
# Refresh pillar again to cleaup the temp pillar
ret = salt_call_cli.run("saltutil.refresh_pillar", wait=True)
assert ret.exitcode == 0
assert ret.json is True


def test_rel_abs_path_ids(
salt_master,
salt_call_cli,
pillar_tree,
tmp_path,
salt_minion,
):
"""
This tests for any regressions for this issue:
/~https://github.com/saltstack/salt/issues/54179
"""
init_sls_name = "tmp_dir/init"
init_sls_contents = """
include:
- .bug
- tmp_dir/include
"""

include_sls_name = "tmp_dir/include"
include_sls_contents = """
include:
- tmp_dir/bug
"""

include_bug_sls_name = "tmp_dir/include_bug"
include_bug_sls_contents = """
include_bug:
test.succeed_without_changes:
- name: include_bug
- require:
- id: bug
"""

bug_sls_name = "tmp_dir/bug"
bug_sls_contents = """
bug:
test.succeed_without_changes:
- name: bug
"""

init_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(init_sls_name), init_sls_contents
)

include_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_sls_name), include_sls_contents
)

include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_bug_sls_name), include_bug_sls_contents
)

bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(bug_sls_name), bug_sls_contents
)

with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
assert ret.exitcode == 0


def test_rel_rel_path_ids(
salt_master,
salt_call_cli,
pillar_tree,
tmp_path,
salt_minion,
):
"""
This tests for any regressions for this issue:
/~https://github.com/saltstack/salt/issues/54179
"""
init_sls_name = "tmp_dir/init"
init_sls_contents = """
include:
- .bug
- .include
"""

include_sls_name = "tmp_dir/include"
include_sls_contents = """
include:
- tmp_dir/bug
"""

include_bug_sls_name = "tmp_dir/include_bug"
include_bug_sls_contents = """
include_bug:
test.succeed_without_changes:
- name: include_bug
- require:
- id: bug
"""

bug_sls_name = "tmp_dir/bug"
bug_sls_contents = """
bug:
test.succeed_without_changes:
- name: bug
"""

init_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(init_sls_name), init_sls_contents
)

include_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_sls_name), include_sls_contents
)

include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_bug_sls_name), include_bug_sls_contents
)

bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(bug_sls_name), bug_sls_contents
)

with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
assert ret.exitcode == 0


def test_abs_rel_path_ids(
salt_master,
salt_call_cli,
pillar_tree,
tmp_path,
salt_minion,
):
"""
This tests for any regressions for this issue:
/~https://github.com/saltstack/salt/issues/54179
"""
init_sls_name = "tmp_dir/init"
init_sls_contents = """
include:
- tmp_dir/bug
- .include
"""

include_sls_name = "tmp_dir/include"
include_sls_contents = """
include:
- tmp_dir/bug
"""

include_bug_sls_name = "tmp_dir/include_bug"
include_bug_sls_contents = """
include_bug:
test.succeed_without_changes:
- name: include_bug
- require:
- id: bug
"""

bug_sls_name = "tmp_dir/bug"
bug_sls_contents = """
bug:
test.succeed_without_changes:
- name: bug
"""

init_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(init_sls_name), init_sls_contents
)

include_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_sls_name), include_sls_contents
)

include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_bug_sls_name), include_bug_sls_contents
)

bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(bug_sls_name), bug_sls_contents
)

with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
assert ret.exitcode == 0


def test_abs_abs_path_ids(
salt_master,
salt_call_cli,
pillar_tree,
tmp_path,
salt_minion,
):
"""
This tests for any regressions for this issue:
/~https://github.com/saltstack/salt/issues/54179
"""
init_sls_name = "tmp_dir/init"
init_sls_contents = """
include:
- tmp_dir/bug
- tmp_dir/include
"""

include_sls_name = "tmp_dir/include"
include_sls_contents = """
include:
- tmp_dir/bug
"""

include_bug_sls_name = "tmp_dir/include_bug"
include_bug_sls_contents = """
include_bug:
test.succeed_without_changes:
- name: include_bug
- require:
- id: bug
"""

bug_sls_name = "tmp_dir/bug"
bug_sls_contents = """
bug:
test.succeed_without_changes:
- name: bug
"""

init_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(init_sls_name), init_sls_contents
)

include_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_sls_name), include_sls_contents
)

include_bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(include_bug_sls_name), include_bug_sls_contents
)

bug_sls_tempfile = salt_master.state_tree.base.temp_file(
"{}.sls".format(bug_sls_name), bug_sls_contents
)

with init_sls_tempfile, include_sls_tempfile, include_bug_sls_tempfile, bug_sls_tempfile:
ret = salt_call_cli.run("--local", "state.apply", "tmp_dir")
assert ret.exitcode == 0