Skip to content

Commit

Permalink
Fix problematic tests and allow smooth tests executions on containers
Browse files Browse the repository at this point in the history
* Align boto imports in tests with Salt modules

Some modules `import boto` to set log levels. The related tests don't
import `boto`. This can cause a problem when `boto` is not available.

Tests are skipped when HAS_BOTO in the test_boto_*.py is False. Not
trying to `import boto` can leave HAS_BOTO=True in the test file even
though HAS_BOTO is False on the application side. In this case, tests
are not skipped and fail.

* Fix mock order in test_dig (test_network.py)

`salt.utils.path.which` should not be mocked before `network.__utils__`. The
Salt loader calls `salt.utils.network.linux_interfaces`, which needs the real
`salt.utils.path.which`.

* Fix mock calls

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
(cherry picked from commit 3506e7f)

* Skip venafiapi test if vcert module not available

The same HAS_VCERT check is done in the runner module.

* Moving tests/integration/modules/test_cmdmod.py to pytest, Gareth J Greenaway original author

(cherry picked from commit 2c1040b)

* The repo.saltproject.io `index.html` file changed it's contents. Fix tests.

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
(cherry picked from commit 672f658)

* Skip hwclock test when executed inside a container

* Skip git pillar tests when executed inside a container

These tests require a Git repository container, which is hard to correctly set
up when executing the tests in the container in GH Actions.

Using --network host can help, but there was still an error (the git repos were
empty) when I tried to set this up.

* Skip test requiring systemd inside container

* Exclude tests for hgfs if missing hglib

* Skip and fix tests when running on containers

* Fix some failing test causing problem in SUSE environments

* Skip more tests when running on containers

* Use skipif instead of skip_if as it seems not behaving equally

* Skip more tests that cannot run in a container

* Remove SSH test which doesn't make sense after lock mechanism

* Fix failing boto tests

* Skip unmaintained tests upstream around zcbuildout

* Skip some tests that does not run well in GH Actions

---------

Co-authored-by: Pedro Algarvio <palgarvio@vmware.com>
Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com>
Co-authored-by: Alexander Graul <agraul@suse.com>
  • Loading branch information
4 people committed Oct 29, 2024
1 parent e86e373 commit 583c813
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 28 deletions.
3 changes: 3 additions & 0 deletions tests/integration/modules/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import pytest
import os

from tests.support.case import ModuleCase

Expand All @@ -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):
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/pillar/test_git_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
https://github.com/unbit/uwsgi/commit/ac1e354
"""

import os
import random
import string
import sys
Expand Down Expand Up @@ -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."),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/cache/test_consul.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import socket
import time

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/cache/test_mysql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
44 changes: 36 additions & 8 deletions tests/pytests/functional/modules/test_cmdmod.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import random
import sys
from contextlib import contextmanager

import pytest

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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.
Expand All @@ -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"]
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/pytests/functional/modules/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
4 changes: 4 additions & 0 deletions tests/pytests/functional/states/rabbitmq/test_vhost.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import os

import pytest

Expand All @@ -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"),
]


Expand Down
21 changes: 3 additions & 18 deletions tests/pytests/functional/states/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions tests/pytests/integration/cli/test_syndic_eauth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json
import logging
import os
import pathlib
import tempfile
import time

import pytest
Expand All @@ -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"),
]


Expand Down
Loading

0 comments on commit 583c813

Please sign in to comment.