Merge pull request #62862 from agraul/fix/extend-exclude-interaction

Fix extend+exclude interaction
This commit is contained in:
Gareth J. Greenaway 2022-10-15 07:57:04 -07:00 committed by GitHub
commit b59e7ef86e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 171 additions and 1 deletions

1
changelog/62082.fixed Normal file
View file

@ -0,0 +1 @@
Ignore extend declarations in sls files that are excluded.

View file

@ -1685,6 +1685,25 @@ class State:
else:
name = ids[0][0]
sls_excludes = []
# excluded sls are plain list items or dicts with an "sls" key
for exclude in high.get("__exclude__", []):
if isinstance(exclude, str):
sls_excludes.append(exclude)
elif exclude.get("sls"):
sls_excludes.append(exclude["sls"])
if body.get("__sls__") in sls_excludes:
log.debug(
"Cannot extend ID '%s' in '%s:%s' because '%s:%s' is excluded.",
name,
body.get("__env__", "base"),
body.get("__sls__", "base"),
body.get("__env__", "base"),
body.get("__sls__", "base"),
)
continue
for state, run in body.items():
if state.startswith("__"):
continue

View file

@ -3,10 +3,12 @@
"""
import logging
import textwrap
import pytest # pylint: disable=unused-import
import salt.state
from salt.utils.odict import OrderedDict
log = logging.getLogger(__name__)
@ -181,7 +183,7 @@ def test_find_sls_ids_with_exclude(highstate, state_tree_dir):
with pytest.helpers.temp_file(
"slsfile1.sls", slsfile1, sls_dir
), pytest.helpers.temp_file(
"slsfile1.sls", slsfile1, sls_dir
"slsfile2.sls", slsfile2, sls_dir
), pytest.helpers.temp_file(
"stateB.sls", stateB, sls_dir
), pytest.helpers.temp_file(
@ -197,3 +199,151 @@ def test_find_sls_ids_with_exclude(highstate, state_tree_dir):
high, _ = highstate.render_highstate(matches)
ret = salt.state.find_sls_ids("issue-47182.stateA.newer", high)
assert ret == [("somestuff", "cmd")]
def test_dont_extend_in_excluded_sls_file(highstate, state_tree_dir):
"""
See https://github.com/saltstack/salt/issues/62082#issuecomment-1245461333
"""
top_sls = textwrap.dedent(
"""\
base:
'*':
- test1
- exclude
"""
)
exclude_sls = textwrap.dedent(
"""\
exclude:
- sls: test2
"""
)
test1_sls = textwrap.dedent(
"""\
include:
- test2
test1:
cmd.run:
- name: echo test1
"""
)
test2_sls = textwrap.dedent(
"""\
extend:
test1:
cmd.run:
- name: echo "override test1 in test2"
test2-id:
cmd.run:
- name: echo test2
"""
)
sls_dir = str(state_tree_dir)
with pytest.helpers.temp_file(
"top.sls", top_sls, sls_dir
), pytest.helpers.temp_file(
"test1.sls", test1_sls, sls_dir
), pytest.helpers.temp_file(
"test2.sls", test2_sls, sls_dir
), pytest.helpers.temp_file(
"exclude.sls", exclude_sls, sls_dir
):
# manually compile the high data, error checking is not needed in this
# test case.
top = highstate.get_top()
matches = highstate.top_matches(top)
high, _ = highstate.render_highstate(matches)
# high is mutated by call_high and the different "pipeline steps"
assert high == OrderedDict(
[
(
"__extend__",
[
{
"test1": OrderedDict(
[
("__sls__", "test2"),
("__env__", "base"),
(
"cmd",
[
OrderedDict(
[
(
"name",
'echo "override test1 in test2"',
)
]
),
"run",
],
),
]
)
}
],
),
(
"test1",
OrderedDict(
[
(
"cmd",
[
OrderedDict([("name", "echo test1")]),
"run",
{"order": 10001},
],
),
("__sls__", "test1"),
("__env__", "base"),
]
),
),
(
"test2-id",
OrderedDict(
[
(
"cmd",
[
OrderedDict([("name", "echo test2")]),
"run",
{"order": 10000},
],
),
("__sls__", "test2"),
("__env__", "base"),
]
),
),
("__exclude__", [OrderedDict([("sls", "test2")])]),
]
)
highstate.state.call_high(high)
# assert that the extend declaration was not applied
assert high == OrderedDict(
[
(
"test1",
OrderedDict(
[
(
"cmd",
[
OrderedDict([("name", "echo test1")]),
"run",
{"order": 10001},
],
),
("__sls__", "test1"),
("__env__", "base"),
]
),
)
]
)