Multiple test fixes

This commit is contained in:
Daniel A. Wozniak 2025-01-08 15:40:50 -07:00 committed by Daniel Wozniak
parent faecc6d48c
commit ee33df82eb
5 changed files with 58 additions and 8 deletions

View file

@ -52,6 +52,7 @@ class ShadowModuleTest(ModuleCase):
@pytest.mark.destructive_test
@pytest.mark.slow_test
@pytest.mark.skip_if_binaries_missing("passwd")
def test_del_password(self):
"""
Test shadow.del_password
@ -61,8 +62,9 @@ class ShadowModuleTest(ModuleCase):
# Correct Functionality
self.assertTrue(self.run_function("shadow.del_password", [self._test_user]))
self.assertEqual(
self.run_function("shadow.info", [self._test_user])["passwd"], ""
self.assertIn(
self.run_function("shadow.info", [self._test_user])["passwd"],
["", "!", "!!"],
)
# User does not exist

View file

@ -3,6 +3,7 @@ Tests for the service state
"""
import os
import subprocess
import pytest
@ -21,6 +22,16 @@ STOPPED = False
RUNNING = True
def _check_systemctl():
if not hasattr(_check_systemctl, "memo"):
proc = subprocess.run(["systemctl"], capture_output=True, check=False)
_check_systemctl.memo = (
b"Failed to get D-Bus connection: No such file or directory" in proc.stderr
or b"Failed to connect to bus: No such file or directory" in proc.stderr
)
return _check_systemctl.memo
@pytest.fixture
def service_name(grains, modules):
# For local testing purposes
@ -86,6 +97,7 @@ def check_service_status(exp_return, modules, service_name):
@pytest.mark.slow_test
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_running(service_name, modules, states):
"""
test service.running state module
@ -105,6 +117,7 @@ def test_service_running(service_name, modules, states):
@pytest.mark.slow_test
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_dead(service_name, modules, states):
"""
test service.dead state module
@ -119,6 +132,7 @@ def test_service_dead(service_name, modules, states):
@pytest.mark.slow_test
@pytest.mark.skipif(_check_systemctl(), reason="systemctl degraded")
def test_service_dead_init_delay(service_name, modules, states):
"""
test service.dead state module

View file

@ -3,6 +3,7 @@
"""
import logging
import subprocess
import pytest
@ -16,6 +17,16 @@ from tests.support.mock import MagicMock, patch
log = logging.getLogger(__name__)
def _check_systemctl():
if not hasattr(_check_systemctl, "memo"):
proc = subprocess.run(["systemctl"], capture_output=True, check=False)
_check_systemctl.memo = (
b"Failed to get D-Bus connection: No such file or directory" in proc.stderr
or b"Failed to connect to bus: No such file or directory" in proc.stderr
)
return _check_systemctl.memo
def func(name):
"""
Mock func method
@ -678,6 +689,7 @@ def test_mod_beacon(tmp_path):
assert ret == expected
@pytest.mark.skipif(_check_systemctl(), reason="systemctl is in a degraded state")
@pytest.mark.skip_on_darwin(reason="service.running is currently failing on OSX")
@pytest.mark.skip_if_not_root
@pytest.mark.destructive_test

View file

@ -1431,12 +1431,17 @@ def test_isportopen_false():
assert ret is False
def test_isportopen():
if salt.utils.platform.is_windows():
port = "135"
else:
port = "22"
ret = network.isportopen("127.0.0.1", port)
@pytest.fixture
def openport_22233():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 22233))
s.listen(5)
yield
s.close()
def test_isportopen(openport_22233):
ret = network.isportopen("127.0.0.1", 22233)
assert ret == 0

View file

@ -2,6 +2,8 @@
:codeauthor: Rupesh Tare <rupesht@saltstack.com>
"""
import subprocess
import pytest
import salt.modules.localemod as localemod
@ -11,6 +13,15 @@ from tests.support.mock import MagicMock, Mock, patch
from tests.support.unit import TestCase
def _check_localectl():
if not hasattr(_check_localectl, "memo"):
proc = subprocess.run(["localectl"], check=False, capture_output=True)
_check_localectl.memo = (
b"Failed to connect to bus: No such file or directory" in proc.stderr
)
return _check_localectl.memo
class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
"""
Test cases for salt.modules.localemod
@ -55,6 +66,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
):
assert localemod.list_avail() == ["A", "B"]
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
@patch(
"salt.modules.localemod.__salt__",
@ -87,6 +99,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
assert "data" in out["x11_model"]
assert out["x11_model"]["data"] == "pc105"
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
@patch(
"salt.modules.localemod.__salt__",
@ -165,6 +178,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
assert 'Unable to find "localectl"' in str(exc_info.value)
assert not localemod.log.debug.called
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
@patch(
"salt.modules.localemod.__salt__",
@ -175,6 +189,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
localemod._localectl_status()
assert 'Unable to parse result of "localectl"' in str(exc_info.value)
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
@patch(
"salt.modules.localemod.__salt__",
@ -185,6 +200,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
localemod._localectl_status()
assert 'Unable to parse result of "localectl"' in str(exc_info.value)
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
@patch(
"salt.modules.localemod.__salt__",
@ -831,6 +847,7 @@ class LocalemodTestCase(TestCase, LoaderModuleMockMixin):
):
assert localemod.gen_locale("en_US.UTF-8", verbose=True) == ret
@pytest.mark.skipif(_check_localectl, reason="localectl is in degraded state")
@patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/localctl"))
def test_parse_localectl(self):
localectl_out = (