Skip to content

Commit

Permalink
Migrate tests/unit/utils/parsers/test_log_parsers.py to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
MKLeb committed Oct 5, 2023
1 parent a932628 commit 3d7a2e8
Show file tree
Hide file tree
Showing 3 changed files with 854 additions and 1,283 deletions.
79 changes: 79 additions & 0 deletions tests/pytests/unit/utils/parsers/test_daemon_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
Tests the PIDfile deletion in the DaemonMixIn.
"""

import logging

import pytest

import salt.utils.parsers
from tests.support.mock import ANY, MagicMock, patch


@pytest.fixture
def daemon_mixin():
mixin = salt.utils.parsers.DaemonMixIn()
mixin.config = {}
mixin.config["pidfile"] = "/some/fake.pid"
return mixin


def test_pid_file_deletion(daemon_mixin):
"""
PIDfile deletion without exception.
"""
with patch("os.unlink", MagicMock()) as unlink_mock:
with patch("os.path.isfile", MagicMock(return_value=True)):
with patch("salt.utils.parsers.log", MagicMock()) as log_mock:
daemon_mixin._mixin_before_exit()
assert unlink_mock.call_count == 1
log_mock.info.assert_not_called()
log_mock.debug.assert_not_called()


def test_pid_deleted_oserror_as_root(daemon_mixin):
"""
PIDfile deletion with exception, running as root.
"""
with patch("os.unlink", MagicMock(side_effect=OSError())) as unlink_mock:
with patch("os.path.isfile", MagicMock(return_value=True)):
with patch("salt.utils.parsers.log", MagicMock()) as log_mock:
if salt.utils.platform.is_windows():
patch_args = (
"salt.utils.win_functions.is_admin",
MagicMock(return_value=True),
)
else:
patch_args = ("os.getuid", MagicMock(return_value=0))

with patch(*patch_args):
daemon_mixin._mixin_before_exit()
assert unlink_mock.call_count == 1
log_mock.info.assert_called_with(
"PIDfile(%s) could not be deleted: %s",
format(daemon_mixin.config["pidfile"], ""),
ANY,
exc_info_on_loglevel=logging.DEBUG,
)


def test_pid_deleted_oserror_as_non_root(daemon_mixin):
"""
PIDfile deletion with exception, running as non-root.
"""
with patch("os.unlink", MagicMock(side_effect=OSError())) as unlink_mock:
with patch("os.path.isfile", MagicMock(return_value=True)):
with patch("salt.utils.parsers.log", MagicMock()) as log_mock:
if salt.utils.platform.is_windows():
patch_args = (
"salt.utils.win_functions.is_admin",
MagicMock(return_value=False),
)
else:
patch_args = ("os.getuid", MagicMock(return_value=1000))

with patch(*patch_args):
daemon_mixin._mixin_before_exit()
assert unlink_mock.call_count == 1
log_mock.info.assert_not_called()
log_mock.debug.assert_not_called()
Loading

0 comments on commit 3d7a2e8

Please sign in to comment.