diff --git a/tests/integration/modules/test_timezone.py b/tests/integration/modules/test_timezone.py index 3af9bf628eca..5a2ed6dde40d 100644 --- a/tests/integration/modules/test_timezone.py +++ b/tests/integration/modules/test_timezone.py @@ -5,6 +5,7 @@ """ import pytest +import os from tests.support.case import ModuleCase @@ -16,6 +17,8 @@ HAS_TZLOCAL = False +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" +@pytest.mark.skipif(INSIDE_CONTAINER, reason="No hwclock in a container") class TimezoneLinuxModuleTest(ModuleCase): def setUp(self): """ diff --git a/tests/integration/pillar/test_git_pillar.py b/tests/integration/pillar/test_git_pillar.py index e390da7aece9..2a21296712ee 100644 --- a/tests/integration/pillar/test_git_pillar.py +++ b/tests/integration/pillar/test_git_pillar.py @@ -63,6 +63,7 @@ https://github.com/unbit/uwsgi/commit/ac1e354 """ +import os import random import string import sys @@ -100,9 +101,11 @@ except Exception: # pylint: disable=broad-except HAS_PYGIT2 = False +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" pytestmark = [ SKIP_INITIAL_PHOTONOS_FAILURES, pytest.mark.skip_on_platforms(windows=True, darwin=True), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Communication problems between containers."), ] diff --git a/tests/pytests/functional/cache/test_consul.py b/tests/pytests/functional/cache/test_consul.py index 0a42913b6c29..14f9fa340d0b 100644 --- a/tests/pytests/functional/cache/test_consul.py +++ b/tests/pytests/functional/cache/test_consul.py @@ -1,4 +1,5 @@ import logging +import os import socket import time @@ -13,10 +14,13 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.skip_on_fips_enabled_platform, pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing("dockerd"), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/cache/test_mysql.py b/tests/pytests/functional/cache/test_mysql.py index c283872c08c9..e15fc732a4a8 100644 --- a/tests/pytests/functional/cache/test_mysql.py +++ b/tests/pytests/functional/cache/test_mysql.py @@ -1,4 +1,5 @@ import logging +import os import pytest @@ -11,9 +12,12 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing("dockerd"), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/modules/test_cmdmod.py b/tests/pytests/functional/modules/test_cmdmod.py index f8dbb7e11348..42427c2b1f4a 100644 --- a/tests/pytests/functional/modules/test_cmdmod.py +++ b/tests/pytests/functional/modules/test_cmdmod.py @@ -1,6 +1,7 @@ import os import random import sys +from contextlib import contextmanager import pytest @@ -109,6 +110,20 @@ def test_run(cmdmod, grains): assert cmdmod.run('echo "a=b" | sed -e s/=/:/g', python_shell=True) == "a:b" +@contextmanager +def _ensure_user_exists(name, usermod): + if name in usermod.info(name).values(): + # User already exists; don't touch + yield + else: + # Need to create user for test + usermod.add(name) + try: + yield + finally: + usermod.delete(name, remove=True) + + @pytest.mark.slow_test def test_stdout(cmdmod): """ @@ -295,6 +310,8 @@ def test_tty(cmdmod): assert "Success" in ret +@pytest.mark.skip_on_windows +@pytest.mark.skip_if_binaries_missing("which") def test_which(cmdmod): """ cmd.which @@ -314,6 +331,9 @@ def test_which(cmdmod): assert cmd_which.rstrip().lower() == cmd_run.rstrip().lower() + +@pytest.mark.skip_on_windows +@pytest.mark.skip_if_binaries_missing("which") def test_which_bin(cmdmod): """ cmd.which_bin @@ -426,7 +446,8 @@ def test_cwd_runas(cmdmod, usermod, runas_usr, tmp_path): cwd_normal = cmdmod.run_stdout(cmd, cwd=tmp_cwd).rstrip("\n") assert tmp_cwd == cwd_normal - cwd_runas = cmdmod.run_stdout(cmd, cwd=tmp_cwd, runas=runas_usr).rstrip("\n") + with _ensure_user_exists(runas_usr, usermod): + cwd_runas = cmdmod.run_stdout(cmd, cwd=tmp_cwd, runas=runas_usr).rstrip("\n") assert tmp_cwd == cwd_runas @@ -439,7 +460,8 @@ def test_runas_env(cmdmod, usermod, runas_usr): cmd.run should be able to change working directory correctly, whether or not runas is in use. """ - user_path = cmdmod.run_stdout('printf %s "$PATH"', runas=runas_usr) + with _ensure_user_exists(runas_usr, usermod): + user_path = cmdmod.run_stdout('printf %s "$PATH"', runas=runas_usr) # XXX: Not sure of a better way. Environment starts out with # /bin:/usr/bin and should be populated by path helper and the bash # profile. @@ -462,11 +484,14 @@ def test_runas_complex_command_bad_cwd(cmdmod, usermod, runas_usr, tmp_path): """ tmp_cwd = str(tmp_path) os.chmod(tmp_cwd, 0o700) - cmd_result = cmdmod.run_all( - 'pwd; pwd; : $(echo "You have failed the test" >&2)', - cwd=tmp_cwd, - runas=runas_usr, - ) + + with _ensure_user_exists(runas_usr, usermod): + cmd_result = cmdmod.run_all( + 'pwd; pwd; : $(echo "You have failed the test" >&2)', + cwd=tmp_cwd, + runas=runas_usr, + ) + assert "" == cmd_result["stdout"] assert "You have failed the test" not in cmd_result["stderr"] assert 0 != cmd_result["retcode"] @@ -481,10 +506,12 @@ def test_runas(cmdmod, usermod, runas_usr): """ Ensure that the env is the runas user's """ - out = cmdmod.run("env", runas=runas_usr).splitlines() + with _ensure_user_exists(runas_usr, usermod): + out = cmdmod.run("env", runas=runas_usr).splitlines() assert f"USER={runas_usr}" in out +@pytest.mark.skip_if_binaries_missing("sleep", reason="sleep cmd not installed") def test_timeout(cmdmod): """ cmd.run trigger timeout @@ -501,6 +528,7 @@ def test_timeout(cmdmod): assert "Timed out" in out +@pytest.mark.skip_if_binaries_missing("sleep", reason="sleep cmd not installed") def test_timeout_success(cmdmod): """ cmd.run sufficient timeout to succeed diff --git a/tests/pytests/functional/modules/test_system.py b/tests/pytests/functional/modules/test_system.py index 07f34e8a5164..57e68a5bbe5a 100644 --- a/tests/pytests/functional/modules/test_system.py +++ b/tests/pytests/functional/modules/test_system.py @@ -10,9 +10,12 @@ import salt.utils.files +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.skip_unless_on_linux, pytest.mark.slow_test, + pytest.mark.skipif(INSIDE_CONTAINER, reason="No systemd in container."), ] log = logging.getLogger(__name__) diff --git a/tests/pytests/functional/states/rabbitmq/test_cluster.py b/tests/pytests/functional/states/rabbitmq/test_cluster.py index f8b4bdc225e9..210b22a2360c 100644 --- a/tests/pytests/functional/states/rabbitmq/test_cluster.py +++ b/tests/pytests/functional/states/rabbitmq/test_cluster.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -13,11 +14,14 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/rabbitmq/test_plugin.py b/tests/pytests/functional/states/rabbitmq/test_plugin.py index e1b686e33659..f11914905363 100644 --- a/tests/pytests/functional/states/rabbitmq/test_plugin.py +++ b/tests/pytests/functional/states/rabbitmq/test_plugin.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -14,11 +15,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/rabbitmq/test_policy.py b/tests/pytests/functional/states/rabbitmq/test_policy.py index e5cee97cbc84..7ccf6a522e03 100644 --- a/tests/pytests/functional/states/rabbitmq/test_policy.py +++ b/tests/pytests/functional/states/rabbitmq/test_policy.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -14,11 +15,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/rabbitmq/test_upstream.py b/tests/pytests/functional/states/rabbitmq/test_upstream.py index cfdad35aba6b..c7bcf3b0d44b 100644 --- a/tests/pytests/functional/states/rabbitmq/test_upstream.py +++ b/tests/pytests/functional/states/rabbitmq/test_upstream.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -13,11 +14,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/rabbitmq/test_user.py b/tests/pytests/functional/states/rabbitmq/test_user.py index 2f9b22d28d26..31723df7be8a 100644 --- a/tests/pytests/functional/states/rabbitmq/test_user.py +++ b/tests/pytests/functional/states/rabbitmq/test_user.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -13,11 +14,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/rabbitmq/test_vhost.py b/tests/pytests/functional/states/rabbitmq/test_vhost.py index a648d41854f6..d6ac6901a250 100644 --- a/tests/pytests/functional/states/rabbitmq/test_vhost.py +++ b/tests/pytests/functional/states/rabbitmq/test_vhost.py @@ -3,6 +3,7 @@ """ import logging +import os import pytest @@ -13,11 +14,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing( "docker", "dockerd", reason="Docker not installed" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/functional/states/test_pkg.py b/tests/pytests/functional/states/test_pkg.py index 9337eab23386..29fe757db250 100644 --- a/tests/pytests/functional/states/test_pkg.py +++ b/tests/pytests/functional/states/test_pkg.py @@ -82,22 +82,7 @@ def PKG_CAP_TARGETS(grains): _PKG_CAP_TARGETS = [] if grains["os_family"] == "Suse": if grains["os"] == "SUSE": - _PKG_CAP_TARGETS = [("perl(YAML)", "perl-YAML")] - # sudo zypper install 'perl(YAML)' - # Loading repository data... - # Reading installed packages... - # 'perl(YAML)' not found in package names. Trying capabilities. - # Resolving package dependencies... - # - # The following NEW package is going to be installed: - # perl-YAML - # - # 1 new package to install. - # Overall download size: 85.3 KiB. Already cached: 0 B. After the operation, additional 183.3 KiB will be used. - # Continue? [y/n/v/...? shows all options] (y): - - # So, it just doesn't work here? skip it for now - _PKG_CAP_TARGETS.clear() + _PKG_CAP_TARGETS = [("perl(Error)", "perl-Error")] if not _PKG_CAP_TARGETS: pytest.skip("Capability not provided") return _PKG_CAP_TARGETS @@ -892,8 +877,8 @@ def test_pkg_cap_003_installed_multipkg_with_version( This is a destructive test as it installs and then removes two packages """ target, realpkg = PKG_CAP_TARGETS[0] - version = latest_version(target) - realver = latest_version(realpkg) + version = modules.pkg.version(target) + realver = modules.pkg.version(realpkg) # If this condition is False, we need to find new targets. # This needs to be able to test successful installation of packages. diff --git a/tests/pytests/integration/cli/test_syndic_eauth.py b/tests/pytests/integration/cli/test_syndic_eauth.py index e1d159cdf919..868f596b118e 100644 --- a/tests/pytests/integration/cli/test_syndic_eauth.py +++ b/tests/pytests/integration/cli/test_syndic_eauth.py @@ -1,5 +1,8 @@ import json import logging +import os +import pathlib +import tempfile import time import pytest @@ -10,9 +13,12 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.core_test, pytest.mark.timeout_unless_on_windows(600), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/integration/daemons/test_memory_leak.py b/tests/pytests/integration/daemons/test_memory_leak.py index 8461edeb5585..92984acbe809 100644 --- a/tests/pytests/integration/daemons/test_memory_leak.py +++ b/tests/pytests/integration/daemons/test_memory_leak.py @@ -1,4 +1,5 @@ import multiprocessing +import os import time import psutil @@ -20,6 +21,8 @@ ), ] +GITHUB_ACTIONS = bool(os.getenv("GITHUB_ACTIONS", False)) + @pytest.fixture def file_add_delete_sls(tmp_path, salt_master): @@ -51,6 +54,9 @@ def file_add_delete_sls(tmp_path, salt_master): # This test is fundimentally flawed. Needs to be re-factored to test the memory # consuption of the minoin process not system wide memory. @pytest.mark.skip(reason="Flawed test") +@pytest.mark.skip_on_darwin(reason="MacOS is a spawning platform, won't work") +@pytest.mark.skipif(GITHUB_ACTIONS, reason="Test is failing in GitHub Actions") +@pytest.mark.flaky(max_runs=4) def test_memory_leak(salt_cli, salt_minion, file_add_delete_sls): max_usg = None diff --git a/tests/pytests/integration/ssh/test_log.py b/tests/pytests/integration/ssh/test_log.py index de498b8d64c1..686ff4a3634e 100644 --- a/tests/pytests/integration/ssh/test_log.py +++ b/tests/pytests/integration/ssh/test_log.py @@ -3,6 +3,7 @@ """ import logging +import os import time import pytest @@ -12,12 +13,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" log = logging.getLogger(__name__) pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing("dockerd"), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/integration/ssh/test_master.py b/tests/pytests/integration/ssh/test_master.py index 5a81fcb8d0e2..94f4a2093fca 100644 --- a/tests/pytests/integration/ssh/test_master.py +++ b/tests/pytests/integration/ssh/test_master.py @@ -2,6 +2,8 @@ Simple Smoke Tests for Connected SSH minions """ +import os + import pytest from saltfactories.utils.functional import StateResult @@ -10,7 +12,10 @@ pytest.mark.skip_on_windows(reason="salt-ssh not available on Windows"), ] +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + +@pytest.mark.skipif(INSIDE_CONTAINER, reason="No systemd in container.") @pytest.mark.skip_if_not_root def test_service(salt_ssh_cli, grains): service = "cron" diff --git a/tests/pytests/integration/ssh/test_py_versions.py b/tests/pytests/integration/ssh/test_py_versions.py index 749aab061cc7..b77aba0bb7f7 100644 --- a/tests/pytests/integration/ssh/test_py_versions.py +++ b/tests/pytests/integration/ssh/test_py_versions.py @@ -3,6 +3,7 @@ """ import logging +import os import socket import time @@ -13,12 +14,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" log = logging.getLogger(__name__) pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing("dockerd"), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/integration/ssh/test_ssh_setup.py b/tests/pytests/integration/ssh/test_ssh_setup.py index 6d6ebbeb4d0c..94182d03aecf 100644 --- a/tests/pytests/integration/ssh/test_ssh_setup.py +++ b/tests/pytests/integration/ssh/test_ssh_setup.py @@ -18,12 +18,14 @@ pytest.importorskip("docker") +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" log = logging.getLogger(__name__) pytestmark = [ pytest.mark.slow_test, pytest.mark.skip_if_binaries_missing("dockerd"), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/scenarios/compat/test_with_versions.py b/tests/pytests/scenarios/compat/test_with_versions.py index 60c486864a84..1187158f3d39 100644 --- a/tests/pytests/scenarios/compat/test_with_versions.py +++ b/tests/pytests/scenarios/compat/test_with_versions.py @@ -6,6 +6,7 @@ """ import logging +import os import pathlib import pytest @@ -20,6 +21,8 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.skip("GREAT MODULE MIGRATION"), @@ -28,6 +31,7 @@ pytest.mark.skipif( salt.utils.platform.is_photonos() is True, reason="Skip on PhotonOS" ), + pytest.mark.skipif(INSIDE_CONTAINER, reason="Cannot run in a container"), ] diff --git a/tests/pytests/scenarios/failover/multimaster/test_failover_master.py b/tests/pytests/scenarios/failover/multimaster/test_failover_master.py index e996469789c4..4e0bb49f6fd6 100644 --- a/tests/pytests/scenarios/failover/multimaster/test_failover_master.py +++ b/tests/pytests/scenarios/failover/multimaster/test_failover_master.py @@ -14,7 +14,10 @@ log = logging.getLogger(__name__) +GITHUB_ACTIONS = bool(os.getenv("GITHUB_ACTIONS", False)) + +@pytest.mark.skipif(GITHUB_ACTIONS, reason="Test is failing in GitHub Actions") def test_pki(salt_mm_failover_master_1, salt_mm_failover_master_2, caplog): """ Verify https://docs.saltproject.io/en/latest/topics/tutorials/multimaster_pki.html diff --git a/tests/pytests/scenarios/setup/test_install.py b/tests/pytests/scenarios/setup/test_install.py index 5953a9640c5d..a4494ce08f73 100644 --- a/tests/pytests/scenarios/setup/test_install.py +++ b/tests/pytests/scenarios/setup/test_install.py @@ -19,11 +19,16 @@ log = logging.getLogger(__name__) +INSIDE_CONTAINER = os.getenv("HOSTNAME", "") == "salt-test-container" + pytestmark = [ pytest.mark.core_test, pytest.mark.windows_whitelisted, pytest.mark.skip_initial_onedir_failure, pytest.mark.skip_if_binaries_missing(*KNOWN_BINARY_NAMES, check_all=False), + pytest.mark.skipif( + INSIDE_CONTAINER, reason="No gcc and python3-devel in container." + ), ] diff --git a/tests/pytests/unit/utils/event/test_event.py b/tests/pytests/unit/utils/event/test_event.py index a91ba88f2162..a5c423fae1da 100644 --- a/tests/pytests/unit/utils/event/test_event.py +++ b/tests/pytests/unit/utils/event/test_event.py @@ -46,6 +46,36 @@ def _assert_got_event(evt, data, msg=None, expected_failure=False): assert data[key] != evt[key] +def test_master_event(sock_dir): + with salt.utils.event.MasterEvent(str(sock_dir), listen=False) as me: + assert me.puburi == str(sock_dir / "master_event_pub.ipc") + assert me.pulluri == str(sock_dir / "master_event_pull.ipc") + + +def test_minion_event(sock_dir): + opts = dict(id="foo", sock_dir=str(sock_dir)) + id_hash = hashlib.sha256(salt.utils.stringutils.to_bytes(opts["id"])).hexdigest()[ + :10 + ] + with salt.utils.event.MinionEvent(opts, listen=False) as me: + assert me.puburi == str(sock_dir / f"minion_event_{id_hash}_pub.ipc") + assert me.pulluri == str(sock_dir / f"minion_event_{id_hash}_pull.ipc") + + +def test_minion_event_tcp_ipc_mode(): + opts = dict(id="foo", ipc_mode="tcp") + with salt.utils.event.MinionEvent(opts, listen=False) as me: + assert me.puburi == 4510 + assert me.pulluri == 4511 + + +def test_minion_event_no_id(sock_dir): + with salt.utils.event.MinionEvent(dict(sock_dir=str(sock_dir)), listen=False) as me: + id_hash = hashlib.sha256(salt.utils.stringutils.to_bytes("")).hexdigest()[:10] + assert me.puburi == str(sock_dir / f"minion_event_{id_hash}_pub.ipc") + assert me.pulluri == str(sock_dir / f"minion_event_{id_hash}_pull.ipc") + + @pytest.mark.slow_test def test_event_single(sock_dir): """Test a single event is received""" diff --git a/tests/unit/modules/test_network.py b/tests/unit/modules/test_network.py index 34b06250fc6a..9eef9a02f584 100644 --- a/tests/unit/modules/test_network.py +++ b/tests/unit/modules/test_network.py @@ -153,9 +153,11 @@ def test_dig(self): """ Test for Performs a DNS lookup with dig """ - with patch("salt.utils.path.which", MagicMock(return_value="dig")), patch.dict( + with patch.dict( network.__utils__, {"network.sanitize_host": MagicMock(return_value="A")} - ), patch.dict(network.__salt__, {"cmd.run": MagicMock(return_value="A")}): + ), patch("salt.utils.path.which", MagicMock(return_value="dig")), patch.dict( + network.__salt__, {"cmd.run": MagicMock(return_value="A")} + ): self.assertEqual(network.dig("host"), "A") def test_arp(self):