Some tests require TCPPubClient to be mocked.

This prevents the tests from hanging on Windows, and slowing down on Linux.

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2023-10-10 20:45:56 +01:00
parent 0445f9ea39
commit 0971af9331
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
8 changed files with 40 additions and 6 deletions

View file

@ -1,6 +1,10 @@
import asyncio
import pytest
import salt.config
import salt.transport.tcp
from tests.support.mock import MagicMock, patch
@pytest.fixture
@ -53,3 +57,14 @@ def syndic_opts(tmp_path):
opts[name] = str(dirpath)
opts["log_file"] = "logs/syndic.log"
return opts
@pytest.fixture
def mocked_tcp_pub_client():
transport = MagicMock(spec=salt.transport.tcp.TCPPubClient)
transport.connect = MagicMock()
future = asyncio.Future()
transport.connect.return_value = future
future.set_result(True)
with patch("salt.transport.tcp.TCPPubClient", transport):
yield

View file

@ -1212,6 +1212,7 @@ def test_get_pillar_errors(pillar: PillarPair, expected_errors):
)
@pytest.mark.usefixtures("mocked_tcp_pub_client")
def test_event():
"""
test state.event runner

View file

@ -13,6 +13,10 @@ from tests.support.mock import MagicMock, call, mock_open, patch
log = logging.getLogger(__name__)
pytestmark = [
pytest.mark.usefixtures("mocked_tcp_pub_client"),
]
@pytest.fixture
def configure_loader_modules(minion_opts):
@ -111,7 +115,7 @@ def test_save():
# Test that beacons contents are written to config file.
_expected = {
"comment": "Beacons saved to {}.".format(_beacon_conf_file),
"comment": f"Beacons saved to {_beacon_conf_file}.",
"result": True,
}
with patch("salt.utils.files.fopen", mock_open(read_data="")) as fopen_mock:
@ -131,7 +135,7 @@ def test_save():
# Test that when beacons is empty then an empty config file is written.
_expected = {
"comment": "Beacons saved to {}.".format(_beacon_conf_file),
"comment": f"Beacons saved to {_beacon_conf_file}.",
"result": True,
}
with patch("salt.utils.files.fopen", mock_open(read_data="")) as fopen_mock:

View file

@ -15,6 +15,10 @@ from tests.support.mock import MagicMock, call, mock_open, patch
log = logging.getLogger(__name__)
pytestmark = [
pytest.mark.usefixtures("mocked_tcp_pub_client"),
]
@pytest.fixture
def job1():

View file

@ -14,6 +14,10 @@ from tests.support.mock import MagicMock, patch
log = logging.getLogger(__name__)
pytestmark = [
pytest.mark.usefixtures("mocked_tcp_pub_client"),
]
class MockEvent:
"""

View file

@ -10,6 +10,10 @@ import salt.states.file as filestate
from salt.utils.event import SaltEvent
from tests.support.mock import MagicMock, patch
pytestmark = [
pytest.mark.usefixtures("mocked_tcp_pub_client"),
]
@pytest.fixture
def configure_loader_modules():

View file

@ -328,6 +328,7 @@ def test_fulfills_version_spec(installed_versions, operator, version, expected_r
)
@pytest.mark.usefixtures("mocked_tcp_pub_client")
def test_mod_beacon(tmp_path):
"""
Test to create a beacon based on a pkg

View file

@ -478,7 +478,7 @@ def test_dead_with_missing_service():
ret = service.dead(name=name)
assert ret == {
"changes": {},
"comment": "The named service {} is not available".format(name),
"comment": f"The named service {name} is not available",
"result": True,
"name": name,
}
@ -587,6 +587,7 @@ def test_mod_watch():
assert service.mod_watch("salt", "stack") == ret[1]
@pytest.mark.usefixtures("mocked_tcp_pub_client")
def test_mod_beacon(tmp_path):
"""
Test to create a beacon based on a service
@ -708,7 +709,7 @@ def test_running_with_reload(minion_opts):
service_name = "Spooler"
if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
pytest.skip("{} is not installed".format(cmd_name))
pytest.skip(f"{cmd_name} is not installed")
pre_srv_enabled = (
True if service_name in modules["service.get_enabled"]() else False
@ -732,9 +733,9 @@ def test_running_with_reload(minion_opts):
result = service.running(name=service_name, enable=True, reload=False)
if salt.utils.platform.is_windows():
comment = "Started service {}".format(service_name)
comment = f"Started service {service_name}"
else:
comment = "Service {} has been enabled, and is running".format(service_name)
comment = f"Service {service_name} has been enabled, and is running"
expected = {
"changes": {service_name: True},
"comment": comment,