From 1247f3a4c4b2203ffb04089834f81ad96f1a1353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 3 Oct 2022 17:32:18 +0200 Subject: [PATCH] Make test_state.py use separate container This makes it detect cases where remote/local paths gets confused (like in #60003 or #62043). Without using a container, remote paths works locally too, so tests do not detect an issue. Rename test_ssh_setup.py to test_ssh_setup_and_state.py to match its new content. pytest.mark.skip_on_windows is dropped as test_ssh_setup.py already has necessary mark (dockerd present). --- ...h_setup.py => test_ssh_setup_and_state.py} | 120 +++++++++++++++++ tests/pytests/integration/ssh/test_state.py | 126 ------------------ 2 files changed, 120 insertions(+), 126 deletions(-) rename tests/pytests/integration/ssh/{test_ssh_setup.py => test_ssh_setup_and_state.py} (69%) delete mode 100644 tests/pytests/integration/ssh/test_state.py diff --git a/tests/pytests/integration/ssh/test_ssh_setup.py b/tests/pytests/integration/ssh/test_ssh_setup_and_state.py similarity index 69% rename from tests/pytests/integration/ssh/test_ssh_setup.py rename to tests/pytests/integration/ssh/test_ssh_setup_and_state.py index 0524dda967fc..f6e308504672 100644 --- a/tests/pytests/integration/ssh/test_ssh_setup.py +++ b/tests/pytests/integration/ssh/test_ssh_setup_and_state.py @@ -1,6 +1,7 @@ """ Integration tests for salt-ssh py_versions """ +import json import logging import os import signal @@ -245,3 +246,122 @@ def test_setup(salt_ssh_cli, ssh_container_name, ssh_sub_container_name, ssh_pas ), "The command wasn't actually terminated. Took {} seconds.".format( round(stop - start, 2) ) + + +@pytest.fixture(scope="module") +def state_tree(base_env_state_tree_root_dir): + top_file = """ + 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_without_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.mark.slow_test +def test_state_with_import(salt_ssh_cli, state_tree): + """ + verify salt-ssh can use imported map files in states + """ + ret = salt_ssh_cli.run("state.sls", "test") + assert ret.returncode == 0 + assert ret.data + + +@pytest.fixture +def nested_state_tree(base_env_state_tree_root_dir, tmp_path): + top_file = """ + base: + 'localhost': + - basic + '127.0.0.1': + - basic + """ + state_file = """ + /{}/file.txt: + file.managed: + - source: salt://foo/file.jinja + - template: jinja + """.format( + tmp_path + ) + file_jinja = """ + {% from 'foo/map.jinja' import comment %}{{ comment }} + """ + map_file = """ + {% set comment = "blah blah" %} + """ + statedir = base_env_state_tree_root_dir / "foo" + 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, statedir) + file_tempfile = pytest.helpers.temp_file("file.jinja", file_jinja, statedir) + state_tempfile = pytest.helpers.temp_file("init.sls", state_file, statedir) + + with top_tempfile, map_tempfile, state_tempfile, file_tempfile: + yield + + +@pytest.mark.slow_test +def test_state_with_import_from_dir(salt_ssh_cli, nested_state_tree): + """ + verify salt-ssh can use imported map files in states + """ + ret = salt_ssh_cli.run( + "--extra-filerefs=salt://foo/map.jinja", "state.apply", "foo" + ) + assert ret.returncode == 0 + assert ret.data + + +@pytest.mark.slow_test +def test_state_low(salt_ssh_cli): + """ + test state.low with salt-ssh + """ + ret = salt_ssh_cli.run( + "state.low", '{"state": "cmd", "fun": "run", "name": "echo blah"}' + ) + assert ( + json.loads(ret.stdout)["localhost"]["cmd_|-echo blah_|-echo blah_|-run"][ + "changes" + ]["stdout"] + == "blah" + ) + + +@pytest.mark.slow_test +def test_state_high(salt_ssh_cli): + """ + test state.high with salt-ssh + """ + ret = salt_ssh_cli.run("state.high", '{"echo blah": {"cmd": ["run"]}}') + assert ( + json.loads(ret.stdout)["localhost"]["cmd_|-echo blah_|-echo blah_|-run"][ + "changes" + ]["stdout"] + == "blah" + ) diff --git a/tests/pytests/integration/ssh/test_state.py b/tests/pytests/integration/ssh/test_state.py deleted file mode 100644 index c4e7a2f53942..000000000000 --- a/tests/pytests/integration/ssh/test_state.py +++ /dev/null @@ -1,126 +0,0 @@ -import json - -import pytest - -pytestmark = [ - pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"), -] - - -@pytest.fixture(scope="module") -def state_tree(base_env_state_tree_root_dir): - top_file = """ - 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_without_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.mark.slow_test -def test_state_with_import(salt_ssh_cli, state_tree): - """ - verify salt-ssh can use imported map files in states - """ - ret = salt_ssh_cli.run("state.sls", "test") - assert ret.returncode == 0 - assert ret.data - - -@pytest.fixture -def nested_state_tree(base_env_state_tree_root_dir, tmp_path): - top_file = """ - base: - 'localhost': - - basic - '127.0.0.1': - - basic - """ - state_file = """ - /{}/file.txt: - file.managed: - - source: salt://foo/file.jinja - - template: jinja - """.format( - tmp_path - ) - file_jinja = """ - {% from 'foo/map.jinja' import comment %}{{ comment }} - """ - map_file = """ - {% set comment = "blah blah" %} - """ - statedir = base_env_state_tree_root_dir / "foo" - 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, statedir) - file_tempfile = pytest.helpers.temp_file("file.jinja", file_jinja, statedir) - state_tempfile = pytest.helpers.temp_file("init.sls", state_file, statedir) - - with top_tempfile, map_tempfile, state_tempfile, file_tempfile: - yield - - -@pytest.mark.slow_test -def test_state_with_import_from_dir(salt_ssh_cli, nested_state_tree): - """ - verify salt-ssh can use imported map files in states - """ - ret = salt_ssh_cli.run( - "--extra-filerefs=salt://foo/map.jinja", "state.apply", "foo" - ) - assert ret.returncode == 0 - assert ret.data - - -@pytest.mark.slow_test -def test_state_low(salt_ssh_cli): - """ - test state.low with salt-ssh - """ - ret = salt_ssh_cli.run( - "state.low", '{"state": "cmd", "fun": "run", "name": "echo blah"}' - ) - assert ( - json.loads(ret.stdout)["localhost"]["cmd_|-echo blah_|-echo blah_|-run"][ - "changes" - ]["stdout"] - == "blah" - ) - - -@pytest.mark.slow_test -def test_state_high(salt_ssh_cli): - """ - test state.high with salt-ssh - """ - ret = salt_ssh_cli.run("state.high", '{"echo blah": {"cmd": ["run"]}}') - assert ( - json.loads(ret.stdout)["localhost"]["cmd_|-echo blah_|-echo blah_|-run"][ - "changes" - ]["stdout"] - == "blah" - )