mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add read_proc_file tests
This commit is contained in:
parent
fe00470219
commit
0401be21ed
2 changed files with 75 additions and 1 deletions
|
@ -111,7 +111,7 @@ def _read_proc_file(path, opts):
|
|||
log.debug("Unable to remove proc file %s.", path)
|
||||
return None
|
||||
if not isinstance(data, dict):
|
||||
# Invalid serial object
|
||||
# Invalid payload object
|
||||
return None
|
||||
if not salt.utils.process.os_is_running(data["pid"]):
|
||||
# The process is no longer running, clear out the file and
|
||||
|
|
|
@ -1,9 +1,28 @@
|
|||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.payload
|
||||
import salt.utils.master
|
||||
from tests.support.mock import mock_open, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def proc_file(tmp_path):
|
||||
path = str(tmp_path / "20240208071139934305")
|
||||
data = {
|
||||
"fun": "runner.state.orch",
|
||||
"jid": "20240208071139934305",
|
||||
"user": "vagrant",
|
||||
"fun_args": ["test_orch", {"orchestration_jid": "20240208071139934305"}],
|
||||
"_stamp": "2024-02-08T07:11:40.336362",
|
||||
"pid": 99999999,
|
||||
}
|
||||
with open(path, "wb") as fp: # pylint: disable=resource-leakage
|
||||
fp.write(salt.payload.dumps(data))
|
||||
return path, data
|
||||
|
||||
|
||||
@pytest.mark.skip_on_platforms(linux=True)
|
||||
def test_check_cmdline_returns_true():
|
||||
assert salt.utils.master._check_cmdline({"pid": 99999999}) is True
|
||||
|
@ -51,3 +70,58 @@ def test_check_cmdline_proc_vanishes():
|
|||
"salt.utils.files.fopen", side_effect=OSError
|
||||
):
|
||||
assert salt.utils.master._check_cmdline({"pid": 99999999}) is False
|
||||
|
||||
|
||||
def test_read_proc_file_empty():
|
||||
with patch("salt.utils.files.fopen", mock_open(read_data=b"")), patch(
|
||||
"os.remove"
|
||||
) as mock_remove:
|
||||
path = "/var/cache/salt/minion/proc/20240208040218919013"
|
||||
assert salt.utils.master._read_proc_file(path, {}) is None
|
||||
mock_remove.assert_called_with(path)
|
||||
|
||||
|
||||
def test_read_proc_file_empty_error():
|
||||
with patch("salt.utils.files.fopen", mock_open(read_data=b"")), patch(
|
||||
"os.remove", side_effect=OSError
|
||||
):
|
||||
path = "/var/cache/salt/minion/proc/20240208040218919013"
|
||||
assert salt.utils.master._read_proc_file(path, {}) is None
|
||||
|
||||
|
||||
def test_read_proc_file_not_dict():
|
||||
with patch("salt.utils.files.fopen", mock_open(read_data=b"x")), patch(
|
||||
"salt.payload.loads", return_value=[]
|
||||
):
|
||||
path = "/var/cache/salt/minion/proc/20240208040218919013"
|
||||
assert salt.utils.master._read_proc_file(path, {}) is None
|
||||
|
||||
|
||||
def test_read_proc_file_not_running(proc_file):
|
||||
assert os.path.isfile(proc_file[0])
|
||||
assert salt.utils.master._read_proc_file(proc_file[0], {}) is None
|
||||
assert os.path.exists(proc_file[0]) is False
|
||||
|
||||
|
||||
def test_read_proc_file_running(proc_file):
|
||||
with patch("salt.utils.process.os_is_running", return_value=True), patch(
|
||||
"salt.utils.master._check_cmdline", return_value=True
|
||||
):
|
||||
assert salt.utils.master._read_proc_file(proc_file[0], {}) == proc_file[1]
|
||||
assert os.path.exists(proc_file[0]) is True
|
||||
|
||||
|
||||
def test_read_proc_file_running_not_salt(proc_file):
|
||||
with patch("salt.utils.process.os_is_running", return_value=True), patch(
|
||||
"salt.utils.master._check_cmdline", return_value=False
|
||||
):
|
||||
assert salt.utils.master._read_proc_file(proc_file[0], {}) is None
|
||||
assert os.path.exists(proc_file[0]) is False
|
||||
|
||||
|
||||
def test_read_proc_file_running_not_salt_error(proc_file):
|
||||
with patch("salt.utils.process.os_is_running", return_value=True), patch(
|
||||
"salt.utils.master._check_cmdline", return_value=False
|
||||
), patch("os.remove", side_effect=OSError):
|
||||
assert salt.utils.master._read_proc_file(proc_file[0], {}) is None
|
||||
assert os.path.exists(proc_file[0]) is True
|
||||
|
|
Loading…
Add table
Reference in a new issue