-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
tests/pytests/integration/ssh/state/test_retcode_render_module_exception.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Verify salt-ssh stops state execution and fails with a retcode > 0 | ||
when a state rendering fails because an execution module throws an exception. | ||
""" | ||
|
||
import pytest | ||
|
||
from salt.defaults.exitcodes import EX_AGGREGATE | ||
|
||
pytestmark = [ | ||
pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"), | ||
pytest.mark.slow_test, | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def state_tree_render_module_exception(base_env_state_tree_root_dir): | ||
top_file = """ | ||
base: | ||
'localhost': | ||
- fail_module_exception | ||
'127.0.0.1': | ||
- fail_module_exception | ||
""" | ||
state_file = r""" | ||
This should fail being rendered: | ||
test.show_notification: | ||
- text: {{ salt["disk.usage"]("c") | yaml_dquote }} | ||
""" | ||
top_tempfile = pytest.helpers.temp_file( | ||
"top.sls", top_file, base_env_state_tree_root_dir | ||
) | ||
state_tempfile = pytest.helpers.temp_file( | ||
"fail_module_exception.sls", state_file, base_env_state_tree_root_dir | ||
) | ||
with top_tempfile, state_tempfile: | ||
yield | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args,retcode", | ||
( | ||
(("state.sls", "fail_module_exception"), EX_AGGREGATE), | ||
(("state.highstate",), EX_AGGREGATE), | ||
(("state.sls_id", "foo", "fail_module_exception"), EX_AGGREGATE), | ||
(("state.show_sls", "fail_module_exception"), EX_AGGREGATE), | ||
(("state.show_low_sls", "fail_module_exception"), EX_AGGREGATE), | ||
(("state.show_highstate",), EX_AGGREGATE), | ||
# state.show_lowstate exits with 0 for non-ssh as well | ||
(("state.show_lowstate",), 0), | ||
(("state.top", "top.sls"), EX_AGGREGATE), | ||
), | ||
) | ||
def test_it(salt_ssh_cli, args, retcode): | ||
ret = salt_ssh_cli.run(*args) | ||
|
||
assert ret.returncode == retcode | ||
assert isinstance(ret.data, list) | ||
assert ret.data | ||
assert isinstance(ret.data[0], str) | ||
# While these three should usually follow each other, there | ||
# can be warnings in between that would break such a logic. | ||
assert "Rendering SLS 'base:fail_module_exception' failed:" in ret.data[0] | ||
assert "Problem running salt function in Jinja template:" in ret.data[0] | ||
assert ( | ||
"Error running 'disk.usage': Invalid flag passed to disk.usage" in ret.data[0] | ||
) |
91 changes: 91 additions & 0 deletions
91
tests/pytests/integration/ssh/state/test_retcode_state_run_remote_exception.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Verify salt-ssh does not report success when it cannot parse | ||
the return value. | ||
""" | ||
|
||
import pytest | ||
|
||
from salt.defaults.exitcodes import EX_AGGREGATE | ||
|
||
pytestmark = [ | ||
pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"), | ||
pytest.mark.slow_test, | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def state_tree_remote_exception_mod(salt_run_cli, base_env_state_tree_root_dir): | ||
module_contents = r""" | ||
import os | ||
import sys | ||
def __virtual__(): | ||
return "whoops" | ||
def do_stuff(name): | ||
print("Hi there, nice that you're trying to make me do stuff.", file=sys.stderr) | ||
print("Traceback (most recent call last):", file=sys.stderr) | ||
print(' File "/dont/treat/me/like/that.py" in buzz_off', file=sys.stderr) | ||
print("ComplianceError: 'Outlaw detected'", file=sys.stderr) | ||
sys.stderr.flush() | ||
os._exit(1) | ||
""" | ||
module_dir = base_env_state_tree_root_dir / "_states" | ||
module_tempfile = pytest.helpers.temp_file("whoops.py", module_contents, module_dir) | ||
try: | ||
with module_tempfile: | ||
ret = salt_run_cli.run("saltutil.sync_states") | ||
assert ret.returncode == 0 | ||
assert "states.whoops" in ret.data | ||
yield | ||
finally: | ||
ret = salt_run_cli.run("saltutil.sync_states") | ||
assert ret.returncode == 0 | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def state_tree_remote_exception( | ||
base_env_state_tree_root_dir, state_tree_remote_exception_mod | ||
): | ||
top_file = """ | ||
base: | ||
'localhost': | ||
- remote_stacktrace | ||
'127.0.0.1': | ||
- remote_stacktrace | ||
""" | ||
state_file = r""" | ||
This should be detected as failure: | ||
whoops.do_stuff | ||
""" | ||
top_tempfile = pytest.helpers.temp_file( | ||
"top.sls", top_file, base_env_state_tree_root_dir | ||
) | ||
state_tempfile = pytest.helpers.temp_file( | ||
"remote_stacktrace.sls", state_file, base_env_state_tree_root_dir | ||
) | ||
with top_tempfile, state_tempfile: | ||
yield | ||
|
||
|
||
@pytest.mark.slow_test | ||
@pytest.mark.usefixtures("state_tree_remote_exception") | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
("state.sls", "remote_stacktrace"), | ||
("state.highstate",), | ||
("state.sls_id", "This should be detected as failure", "remote_stacktrace"), | ||
("state.top", "top.sls"), | ||
("state.single", "whoops.do_stuff", "now"), | ||
), | ||
) | ||
def test_it(salt_ssh_cli, args): | ||
ret = salt_ssh_cli.run(*args) | ||
|
||
assert ret.returncode == EX_AGGREGATE | ||
assert ret.data | ||
assert isinstance(ret.data, str) | ||
assert "ComplianceError: 'Outlaw detected'" in ret.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters