Add state.test to SSH wrapper

(cherry picked from commit 82f90e2f15)
This commit is contained in:
jeanluc 2023-11-06 12:37:23 +01:00 committed by Daniel Wozniak
parent 6c62792c73
commit 512f61d573
4 changed files with 94 additions and 0 deletions

1
changelog/61100.fixed.md Normal file
View file

@ -0,0 +1 @@
Fixed state.test does not work with salt-ssh

View file

@ -1317,3 +1317,18 @@ def single(fun, name, test=None, **kwargs):
# If for some reason the json load fails, return the stdout
return stdout
def test(*args, **kwargs):
"""
.. versionadded:: 3001
Alias for `state.apply` with the kwarg `test` forced to `True`.
This is a nicety to avoid the need to type out `test=True` and the possibility of
a typo causing changes you do not intend.
"""
kwargs["test"] = True
ret = apply_(*args, **kwargs)
return ret

View file

@ -25,3 +25,71 @@ def _reap_stray_processes():
with reap_stray_processes():
# Run test
yield
@pytest.fixture(scope="module")
def state_tree(base_env_state_tree_root_dir):
top_file = """
{%- from "map.jinja" import abc with context %}
base:
'localhost':
- basic
'127.0.0.1':
- basic
"""
map_file = """
{%- set abc = "def" %}
"""
state_file = """
{%- from "map.jinja" import abc with context %}
Ok with {{ abc }}:
test.succeed_with_changes
"""
top_tempfile = pytest.helpers.temp_file(
"top.sls", top_file, base_env_state_tree_root_dir
)
map_tempfile = pytest.helpers.temp_file(
"map.jinja", map_file, base_env_state_tree_root_dir
)
state_tempfile = pytest.helpers.temp_file(
"test.sls", state_file, base_env_state_tree_root_dir
)
with top_tempfile, map_tempfile, state_tempfile:
yield
@pytest.fixture(scope="module")
def state_tree_dir(base_env_state_tree_root_dir):
"""
State tree with files to test salt-ssh
when the map.jinja file is in another directory
"""
top_file = """
{%- from "test/map.jinja" import abc with context %}
base:
'localhost':
- test
'127.0.0.1':
- test
"""
map_file = """
{%- set abc = "def" %}
"""
state_file = """
{%- from "test/map.jinja" import abc with context %}
Ok with {{ abc }}:
test.succeed_without_changes
"""
top_tempfile = pytest.helpers.temp_file(
"top.sls", top_file, base_env_state_tree_root_dir
)
map_tempfile = pytest.helpers.temp_file(
"test/map.jinja", map_file, base_env_state_tree_root_dir
)
state_tempfile = pytest.helpers.temp_file(
"test.sls", state_file, base_env_state_tree_root_dir
)
with top_tempfile, map_tempfile, state_tempfile:
yield

View file

@ -101,3 +101,13 @@ def test_state_high(salt_ssh_cli):
"""
ret = salt_ssh_cli.run("state.high", '{"echo blah": {"cmd": ["run"]}}')
assert ret.data["cmd_|-echo blah_|-echo blah_|-run"]["changes"]["stdout"] == "blah"
def test_state_test(salt_ssh_cli, state_tree):
ret = salt_ssh_cli.run("state.test", "test")
assert ret.returncode == 0
assert ret.data
assert (
ret.data["test_|-Ok with def_|-Ok with def_|-succeed_with_changes"]["result"]
is None
)