mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 17:50:20 +00:00
The `salt.utils.psutil_compat
` module has been deprecated
This commit is contained in:
parent
b9be2dec1b
commit
890140fba1
11 changed files with 101 additions and 192 deletions
1
changelog/66139.deprecated.md
Normal file
1
changelog/66139.deprecated.md
Normal file
|
@ -0,0 +1 @@
|
|||
The ``salt.utils.psutil_compat`` module has been deprecated and will be removed in Salt 3008. Please use the ``psutil`` module directly.
|
|
@ -8,13 +8,12 @@ import logging
|
|||
import salt.utils.beacons
|
||||
|
||||
try:
|
||||
import salt.utils.psutil_compat as psutil
|
||||
import psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
__virtualname__ = "network_info"
|
||||
|
@ -45,9 +44,10 @@ def _to_list(obj):
|
|||
|
||||
def __virtual__():
|
||||
if not HAS_PSUTIL:
|
||||
err_msg = "psutil not available"
|
||||
log.error("Unable to load %s beacon: %s", __virtualname__, err_msg)
|
||||
return False, err_msg
|
||||
return (
|
||||
False,
|
||||
f"Unable to load {__virtualname__} beacon: psutil library not installed",
|
||||
)
|
||||
return __virtualname__
|
||||
|
||||
|
||||
|
|
|
@ -6,23 +6,23 @@ import logging
|
|||
import salt.utils.beacons
|
||||
|
||||
try:
|
||||
import salt.utils.psutil_compat as psutil
|
||||
import psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
|
||||
|
||||
log = logging.getLogger(__name__) # pylint: disable=invalid-name
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
__virtualname__ = "ps"
|
||||
|
||||
|
||||
def __virtual__():
|
||||
if not HAS_PSUTIL:
|
||||
err_msg = "psutil library is missing."
|
||||
log.error("Unable to load %s beacon: %s", __virtualname__, err_msg)
|
||||
return False, err_msg
|
||||
return (
|
||||
False,
|
||||
f"Unable to load {__virtualname__} beacon: psutil library not installed",
|
||||
)
|
||||
return __virtualname__
|
||||
|
||||
|
||||
|
|
|
@ -40,8 +40,6 @@ from salt._logging import (
|
|||
try:
|
||||
import psutil
|
||||
|
||||
if not hasattr(psutil, "virtual_memory"):
|
||||
raise ImportError("Version of psutil too old.")
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
|
@ -88,7 +86,7 @@ def _gather_buffer_space():
|
|||
|
||||
Result is in bytes.
|
||||
"""
|
||||
if HAS_PSUTIL and psutil.version_info >= (0, 6, 0):
|
||||
if HAS_PSUTIL:
|
||||
# Oh good, we have psutil. This will be quick.
|
||||
total_mem = psutil.virtual_memory().total
|
||||
else:
|
||||
|
|
|
@ -79,21 +79,19 @@ from salt.utils.odict import OrderedDict
|
|||
from salt.utils.process import ProcessManager, SignalHandlingProcess, default_signals
|
||||
from salt.utils.zeromq import ZMQ_VERSION_INFO, zmq
|
||||
|
||||
HAS_PSUTIL = False
|
||||
try:
|
||||
import salt.utils.psutil_compat as psutil
|
||||
import psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
pass
|
||||
HAS_PSUTIL = False
|
||||
|
||||
HAS_RESOURCE = False
|
||||
try:
|
||||
import resource
|
||||
|
||||
HAS_RESOURCE = True
|
||||
except ImportError:
|
||||
pass
|
||||
HAS_RESOURCE = False
|
||||
|
||||
try:
|
||||
import salt.utils.win_functions
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
A salt interface to psutil, a system and process library.
|
||||
See http://code.google.com/p/psutil.
|
||||
|
||||
:depends: - psutil Python module, version 0.3.0 or later
|
||||
- python-utmp package (optional)
|
||||
:depends: - python-utmp package (optional)
|
||||
"""
|
||||
|
||||
|
||||
|
@ -15,15 +14,12 @@ import salt.utils.data
|
|||
import salt.utils.decorators.path
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
|
||||
# pylint: disable=import-error
|
||||
try:
|
||||
import salt.utils.psutil_compat as psutil
|
||||
import psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
PSUTIL2 = getattr(psutil, "version_info", ()) >= (2, 0)
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
# pylint: enable=import-error
|
||||
|
||||
|
||||
def __virtual__():
|
||||
|
@ -32,21 +28,7 @@ def __virtual__():
|
|||
False,
|
||||
"The ps module cannot be loaded: python module psutil not installed.",
|
||||
)
|
||||
|
||||
# Functions and attributes used in this execution module seem to have been
|
||||
# added as of psutil 0.3.0, from an inspection of the source code. Only
|
||||
# make this module available if the version of psutil is >= 0.3.0. Note
|
||||
# that this may need to be tweaked if we find post-0.3.0 versions which
|
||||
# also have problems running the functions in this execution module, but
|
||||
# most distributions have already moved to later versions (for example,
|
||||
# as of Dec. 2013 EPEL is on 0.6.1, Debian 7 is on 0.5.1, etc.).
|
||||
if psutil.version_info >= (0, 3, 0):
|
||||
return True
|
||||
return (
|
||||
False,
|
||||
"The ps execution module cannot be loaded: the psutil python module version {}"
|
||||
" is less than 0.3.0".format(psutil.version_info),
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def _get_proc_cmdline(proc):
|
||||
|
@ -56,7 +38,7 @@ def _get_proc_cmdline(proc):
|
|||
It's backward compatible with < 2.0 versions of psutil.
|
||||
"""
|
||||
try:
|
||||
return salt.utils.data.decode(proc.cmdline() if PSUTIL2 else proc.cmdline)
|
||||
return salt.utils.data.decode(proc.cmdline())
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
return []
|
||||
|
||||
|
@ -68,9 +50,7 @@ def _get_proc_create_time(proc):
|
|||
It's backward compatible with < 2.0 versions of psutil.
|
||||
"""
|
||||
try:
|
||||
return salt.utils.data.decode(
|
||||
proc.create_time() if PSUTIL2 else proc.create_time
|
||||
)
|
||||
return salt.utils.data.decode(proc.create_time())
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
return None
|
||||
|
||||
|
@ -82,7 +62,7 @@ def _get_proc_name(proc):
|
|||
It's backward compatible with < 2.0 versions of psutil.
|
||||
"""
|
||||
try:
|
||||
return salt.utils.data.decode(proc.name() if PSUTIL2 else proc.name)
|
||||
return salt.utils.data.decode(proc.name())
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
return []
|
||||
|
||||
|
@ -94,7 +74,7 @@ def _get_proc_status(proc):
|
|||
It's backward compatible with < 2.0 versions of psutil.
|
||||
"""
|
||||
try:
|
||||
return salt.utils.data.decode(proc.status() if PSUTIL2 else proc.status)
|
||||
return salt.utils.data.decode(proc.status())
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
return None
|
||||
|
||||
|
@ -106,7 +86,7 @@ def _get_proc_username(proc):
|
|||
It's backward compatible with < 2.0 versions of psutil.
|
||||
"""
|
||||
try:
|
||||
return salt.utils.data.decode(proc.username() if PSUTIL2 else proc.username)
|
||||
return salt.utils.data.decode(proc.username())
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, KeyError):
|
||||
return None
|
||||
|
||||
|
@ -442,9 +422,6 @@ def virtual_memory():
|
|||
|
||||
salt '*' ps.virtual_memory
|
||||
"""
|
||||
if psutil.version_info < (0, 6, 0):
|
||||
msg = "virtual_memory is only available in psutil 0.6.0 or greater"
|
||||
raise CommandExecutionError(msg)
|
||||
return dict(psutil.virtual_memory()._asdict())
|
||||
|
||||
|
||||
|
@ -464,9 +441,6 @@ def swap_memory():
|
|||
|
||||
salt '*' ps.swap_memory
|
||||
"""
|
||||
if psutil.version_info < (0, 6, 0):
|
||||
msg = "swap_memory is only available in psutil 0.6.0 or greater"
|
||||
raise CommandExecutionError(msg)
|
||||
return dict(psutil.swap_memory()._asdict())
|
||||
|
||||
|
||||
|
@ -530,9 +504,6 @@ def total_physical_memory():
|
|||
|
||||
salt '*' ps.total_physical_memory
|
||||
"""
|
||||
if psutil.version_info < (0, 6, 0):
|
||||
msg = "virtual_memory is only available in psutil 0.6.0 or greater"
|
||||
raise CommandExecutionError(msg)
|
||||
try:
|
||||
return psutil.virtual_memory().total
|
||||
except AttributeError:
|
||||
|
|
|
@ -5,7 +5,6 @@ minion.
|
|||
|
||||
:depends: - esky Python module for update functionality
|
||||
"""
|
||||
|
||||
import copy
|
||||
import fnmatch
|
||||
import logging
|
||||
|
@ -50,7 +49,12 @@ try:
|
|||
except ImportError:
|
||||
HAS_ESKY = False
|
||||
|
||||
# pylint: enable=import-error,no-name-in-module
|
||||
try:
|
||||
import psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
|
||||
# Fix a nasty bug with Win32 Python not supporting all of the standard signals
|
||||
try:
|
||||
|
@ -59,13 +63,6 @@ except AttributeError:
|
|||
salt_SIGKILL = signal.SIGTERM
|
||||
|
||||
|
||||
HAS_PSUTIL = True
|
||||
try:
|
||||
import salt.utils.psutil_compat
|
||||
except ImportError:
|
||||
HAS_PSUTIL = False
|
||||
|
||||
|
||||
__proxyenabled__ = ["*"]
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -1439,9 +1436,9 @@ def signal_job(jid, sig):
|
|||
if data["jid"] == jid:
|
||||
try:
|
||||
if HAS_PSUTIL:
|
||||
for proc in salt.utils.psutil_compat.Process(
|
||||
pid=data["pid"]
|
||||
).children(recursive=True):
|
||||
for proc in psutil.Process(pid=data["pid"]).children(
|
||||
recursive=True
|
||||
):
|
||||
proc.send_signal(sig)
|
||||
os.kill(int(data["pid"]), sig)
|
||||
if HAS_PSUTIL is False and "child_pids" in data:
|
||||
|
|
|
@ -10,3 +10,12 @@ Built off of http://grodola.blogspot.com/2014/01/psutil-20-porting.html
|
|||
"""
|
||||
|
||||
from psutil import * # pylint: disable=wildcard-import,unused-wildcard-import,3rd-party-module-not-gated
|
||||
|
||||
import salt.utils.versions
|
||||
|
||||
salt.utils.versions.warn_until(
|
||||
3008,
|
||||
"Please stop importing 'salt.utils.psutil_compat' and instead import "
|
||||
"'psutil' directly as there's no longer a need for a compatability "
|
||||
"layer. The 'salt.utils.psutil_compat' will go away on Salt {version}.",
|
||||
)
|
||||
|
|
|
@ -44,7 +44,7 @@ def test_empty_config():
|
|||
|
||||
def test_network_info_equal(stub_net_io_counters):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.net_io_counters",
|
||||
"psutil.net_io_counters",
|
||||
MagicMock(return_value=stub_net_io_counters),
|
||||
):
|
||||
config = [
|
||||
|
@ -90,7 +90,7 @@ def test_network_info_equal(stub_net_io_counters):
|
|||
|
||||
def test_network_info_greater_than(stub_net_io_counters):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.net_io_counters",
|
||||
"psutil.net_io_counters",
|
||||
MagicMock(return_value=stub_net_io_counters),
|
||||
):
|
||||
config = [
|
||||
|
|
|
@ -40,7 +40,7 @@ def test_empty_config():
|
|||
|
||||
def test_ps_running():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter", autospec=True, spec_set=True
|
||||
"psutil.process_iter", autospec=True, spec_set=True
|
||||
) as mock_process_iter:
|
||||
mock_process_iter.return_value = [
|
||||
FakeProcess(_name="salt-master", pid=3),
|
||||
|
@ -57,7 +57,7 @@ def test_ps_running():
|
|||
|
||||
def test_ps_not_running():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter", autospec=True, spec_set=True
|
||||
"psutil.process_iter", autospec=True, spec_set=True
|
||||
) as mock_process_iter:
|
||||
mock_process_iter.return_value = [
|
||||
FakeProcess(_name="salt-master", pid=3),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import time
|
||||
from collections import namedtuple
|
||||
|
||||
import psutil
|
||||
import pytest
|
||||
|
||||
import salt.modules.ps
|
||||
|
@ -10,8 +11,6 @@ import salt.utils.platform
|
|||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
from tests.support.mock import MagicMock, Mock, call, patch
|
||||
|
||||
psutil = pytest.importorskip("salt.utils.psutil_compat")
|
||||
|
||||
# TestCase Exceptions are tested in tests/unit/modules/test_ps.py
|
||||
|
||||
|
||||
|
@ -97,7 +96,7 @@ def test__status_when_process_is_found_with_matching_status_then_proc_info_shoul
|
|||
expected_result = [{"pid": 42, "name": "blerp"}]
|
||||
proc = sample_process
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
autospec=True,
|
||||
return_value=[
|
||||
proc
|
||||
|
@ -111,7 +110,7 @@ def test__status_when_process_is_found_with_matching_status_then_proc_info_shoul
|
|||
def test__status_when_no_matching_processes_then_no_results_should_be_returned():
|
||||
expected_result = []
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
autospec=True,
|
||||
return_value=[MagicMock(info={"status": "foo", "blerp": "whatever"})],
|
||||
):
|
||||
|
@ -125,7 +124,7 @@ def test__status_when_some_matching_processes_then_only_correct_info_should_be_r
|
|||
):
|
||||
expected_result = [{"name": "blerp", "pid": 42}]
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
autospec=True,
|
||||
return_value=[
|
||||
sample_process,
|
||||
|
@ -138,9 +137,6 @@ def test__status_when_some_matching_processes_then_only_correct_info_should_be_r
|
|||
assert actual_result == expected_result
|
||||
|
||||
|
||||
HAS_PSUTIL_VERSION = False
|
||||
|
||||
|
||||
STUB_CPU_TIMES = namedtuple("cputimes", "user nice system idle")(1, 2, 3, 4)
|
||||
STUB_CPU_TIMES_PERCPU = [
|
||||
namedtuple("cputimes", "user nice system idle")(1, 2, 3, 4),
|
||||
|
@ -205,9 +201,6 @@ def stub_user():
|
|||
)
|
||||
|
||||
|
||||
if psutil.version_info >= (0, 6, 0):
|
||||
HAS_PSUTIL_VERSION = True
|
||||
|
||||
STUB_PID_LIST = [0, 1, 2, 3]
|
||||
|
||||
try:
|
||||
|
@ -302,15 +295,15 @@ class DummyProcess:
|
|||
|
||||
@pytest.fixture
|
||||
def mocked_proc():
|
||||
mocked_proc = MagicMock("salt.utils.psutil_compat.Process")
|
||||
mocked_proc = MagicMock("psutil.Process")
|
||||
mocked_proc.name = Mock(return_value="test_mock_proc")
|
||||
mocked_proc.pid = Mock(return_value=9999999999)
|
||||
mocked_proc.cmdline = Mock(
|
||||
return_value=["test_mock_proc", "--arg", "--kwarg=value"]
|
||||
)
|
||||
|
||||
with patch("salt.utils.psutil_compat.Process.send_signal"), patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
with patch("psutil.Process.send_signal"), patch(
|
||||
"psutil.process_iter",
|
||||
MagicMock(return_value=[mocked_proc]),
|
||||
):
|
||||
yield mocked_proc
|
||||
|
@ -427,7 +420,7 @@ def test__get_proc_username():
|
|||
|
||||
|
||||
def test_get_pid_list():
|
||||
with patch("salt.utils.psutil_compat.pids", MagicMock(return_value=STUB_PID_LIST)):
|
||||
with patch("psutil.pids", MagicMock(return_value=STUB_PID_LIST)):
|
||||
assert STUB_PID_LIST == ps.get_pid_list()
|
||||
|
||||
|
||||
|
@ -435,12 +428,12 @@ def test_kill_pid():
|
|||
cmdline = ["echo", "питон"]
|
||||
top_proc = DummyProcess(cmdline=cmdline)
|
||||
|
||||
with patch("salt.utils.psutil_compat.Process") as mock_process:
|
||||
with patch("psutil.Process") as mock_process:
|
||||
mock_process.side_effect = psutil.NoSuchProcess(top_proc)
|
||||
ret = ps.kill_pid(0, signal=999)
|
||||
assert not ret
|
||||
|
||||
with patch("salt.utils.psutil_compat.Process") as send_signal_mock:
|
||||
with patch("psutil.Process") as send_signal_mock:
|
||||
ps.kill_pid(0, signal=999)
|
||||
assert send_signal_mock.call_args == call(0)
|
||||
|
||||
|
@ -467,7 +460,7 @@ def test_pkill(mocked_proc):
|
|||
|
||||
def test_pgrep(mocked_proc):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
MagicMock(return_value=[mocked_proc]),
|
||||
):
|
||||
assert mocked_proc.pid in (ps.pgrep(_get_proc_name(mocked_proc)) or [])
|
||||
|
@ -479,7 +472,7 @@ def test_pgrep(mocked_proc):
|
|||
|
||||
def test_pgrep_regex(mocked_proc):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
MagicMock(return_value=[mocked_proc]),
|
||||
):
|
||||
assert mocked_proc.pid in (
|
||||
|
@ -488,26 +481,22 @@ def test_pgrep_regex(mocked_proc):
|
|||
|
||||
|
||||
def test_cpu_percent():
|
||||
with patch("salt.utils.psutil_compat.cpu_percent", MagicMock(return_value=1)):
|
||||
with patch("psutil.cpu_percent", MagicMock(return_value=1)):
|
||||
assert ps.cpu_percent() == 1
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.cpu_percent", MagicMock(return_value=(1, 1, 1, 1))
|
||||
):
|
||||
with patch("psutil.cpu_percent", MagicMock(return_value=(1, 1, 1, 1))):
|
||||
assert ps.cpu_percent(per_cpu=True) == [1, 1, 1, 1]
|
||||
|
||||
with patch("salt.utils.psutil_compat.cpu_percent", MagicMock(return_value=1)):
|
||||
with patch("psutil.cpu_percent", MagicMock(return_value=1)):
|
||||
assert ps.cpu_percent(per_cpu=False) == 1
|
||||
|
||||
|
||||
def test_cpu_times():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.cpu_times", MagicMock(return_value=STUB_CPU_TIMES)
|
||||
):
|
||||
with patch("psutil.cpu_times", MagicMock(return_value=STUB_CPU_TIMES)):
|
||||
assert {"idle": 4, "nice": 2, "system": 3, "user": 1} == ps.cpu_times()
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.cpu_times",
|
||||
"psutil.cpu_times",
|
||||
MagicMock(return_value=STUB_CPU_TIMES_PERCPU),
|
||||
):
|
||||
assert [
|
||||
|
@ -518,21 +507,9 @@ def test_cpu_times():
|
|||
] == ps.cpu_times(per_cpu=True)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
HAS_PSUTIL_VERSION is False,
|
||||
reason="psutil 0.6.0 or greater is required for this test",
|
||||
)
|
||||
def test_virtual_memory():
|
||||
with patch("salt.modules.ps.psutil.version_info", (0, 5, 9)):
|
||||
with pytest.raises(CommandExecutionError) as exc:
|
||||
ps.virtual_memory()
|
||||
assert (
|
||||
exc.value.error
|
||||
== "virtual_memory is only available in psutil 0.6.0 or greater"
|
||||
)
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.virtual_memory",
|
||||
"psutil.virtual_memory",
|
||||
MagicMock(return_value=STUB_VIRT_MEM),
|
||||
):
|
||||
assert {
|
||||
|
@ -544,22 +521,9 @@ def test_virtual_memory():
|
|||
} == ps.virtual_memory()
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
HAS_PSUTIL_VERSION is False,
|
||||
reason="psutil 0.6.0 or greater is required for this test",
|
||||
)
|
||||
def test_swap_memory():
|
||||
|
||||
with patch("salt.modules.ps.psutil.version_info", (0, 5, 9)):
|
||||
with pytest.raises(CommandExecutionError) as exc:
|
||||
ps.swap_memory()
|
||||
assert (
|
||||
exc.value.error
|
||||
== "swap_memory is only available in psutil 0.6.0 or greater"
|
||||
)
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.swap_memory",
|
||||
"psutil.swap_memory",
|
||||
MagicMock(return_value=STUB_SWAP_MEM),
|
||||
):
|
||||
assert {
|
||||
|
@ -574,7 +538,7 @@ def test_swap_memory():
|
|||
|
||||
def test_disk_partitions():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_partitions",
|
||||
"psutil.disk_partitions",
|
||||
MagicMock(return_value=[STUB_DISK_PARTITION]),
|
||||
):
|
||||
assert {
|
||||
|
@ -587,7 +551,7 @@ def test_disk_partitions():
|
|||
|
||||
def test_disk_usage():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_usage",
|
||||
"psutil.disk_usage",
|
||||
MagicMock(return_value=STUB_DISK_USAGE),
|
||||
):
|
||||
assert {
|
||||
|
@ -600,11 +564,11 @@ def test_disk_usage():
|
|||
|
||||
def test_disk_partition_usage():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_partitions",
|
||||
"psutil.disk_partitions",
|
||||
MagicMock(return_value=[STUB_DISK_PARTITION]),
|
||||
):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_usage",
|
||||
"psutil.disk_usage",
|
||||
MagicMock(return_value=STUB_DISK_USAGE),
|
||||
):
|
||||
result = ps.disk_partition_usage()[0]
|
||||
|
@ -622,7 +586,7 @@ def test_disk_partition_usage():
|
|||
|
||||
def test_network_io_counters():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.net_io_counters",
|
||||
"psutil.net_io_counters",
|
||||
MagicMock(return_value=STUB_NETWORK_IO),
|
||||
):
|
||||
assert {
|
||||
|
@ -637,7 +601,7 @@ def test_network_io_counters():
|
|||
} == ps.network_io_counters()
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.net_io_counters",
|
||||
"psutil.net_io_counters",
|
||||
MagicMock(return_value=STUB_NETWORK_IO_PERNIC),
|
||||
):
|
||||
assert {
|
||||
|
@ -656,7 +620,7 @@ def test_network_io_counters():
|
|||
|
||||
def test_disk_io_counters():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_io_counters",
|
||||
"psutil.disk_io_counters",
|
||||
MagicMock(return_value=STUB_DISK_IO),
|
||||
):
|
||||
assert {
|
||||
|
@ -669,7 +633,7 @@ def test_disk_io_counters():
|
|||
} == ps.disk_io_counters()
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.disk_io_counters",
|
||||
"psutil.disk_io_counters",
|
||||
MagicMock(return_value=STUB_DISK_IO_PERDISK),
|
||||
):
|
||||
assert {
|
||||
|
@ -685,7 +649,7 @@ def test_disk_io_counters():
|
|||
|
||||
|
||||
def test_get_users(stub_user):
|
||||
with patch("salt.utils.psutil_compat.users", MagicMock(return_value=[stub_user])):
|
||||
with patch("psutil.users", MagicMock(return_value=[stub_user])):
|
||||
assert {
|
||||
"terminal": "ttys000",
|
||||
"started": 0.0,
|
||||
|
@ -708,8 +672,8 @@ def test_top():
|
|||
cmdline = ["echo", "питон"]
|
||||
top_proc = DummyProcess(cmdline=cmdline)
|
||||
|
||||
with patch("salt.utils.psutil_compat.pids", return_value=[1]):
|
||||
with patch("salt.utils.psutil_compat.Process") as mock_process:
|
||||
with patch("psutil.pids", return_value=[1]):
|
||||
with patch("psutil.Process") as mock_process:
|
||||
mock_process.side_effect = psutil.NoSuchProcess(top_proc)
|
||||
ret = ps.top(num_processes=1, interval=0)
|
||||
assert ret == []
|
||||
|
@ -750,8 +714,8 @@ def test_top():
|
|||
)
|
||||
zombie_mem_info = smem_info(0, 0, 0, 0, 0, 0, 0)
|
||||
|
||||
with patch("salt.utils.psutil_compat.pids", return_value=[1]):
|
||||
with patch("salt.utils.psutil_compat.Process", return_value=top_proc):
|
||||
with patch("psutil.pids", return_value=[1]):
|
||||
with patch("psutil.Process", return_value=top_proc):
|
||||
with patch.object(top_proc, "cpu_times") as mock_cpu_times:
|
||||
with patch.object(
|
||||
top_proc, "memory_info", return_value=zombie_mem_info, create=True
|
||||
|
@ -808,8 +772,8 @@ def test_top():
|
|||
assert ret[0]["mem"] == expected_mem
|
||||
assert ret[0]["cpu"] == expected_cpu
|
||||
|
||||
with patch("salt.utils.psutil_compat.pids", return_value=[1]):
|
||||
with patch("salt.utils.psutil_compat.Process", return_value=top_proc):
|
||||
with patch("psutil.pids", return_value=[1]):
|
||||
with patch("psutil.Process", return_value=top_proc):
|
||||
with patch.object(top_proc, "cpu_times") as mock_cpu_times:
|
||||
mock_cpu_times.side_effect = [
|
||||
top_proc._cpu_times,
|
||||
|
@ -818,8 +782,8 @@ def test_top():
|
|||
ret = ps.top(num_processes=1, interval=0)
|
||||
assert ret == []
|
||||
|
||||
with patch("salt.utils.psutil_compat.pids", return_value=[1]):
|
||||
with patch("salt.utils.psutil_compat.Process", return_value=top_proc):
|
||||
with patch("psutil.pids", return_value=[1]):
|
||||
with patch("psutil.Process", return_value=top_proc):
|
||||
with patch.object(top_proc, "cpu_times") as mock_cpu_times:
|
||||
with patch.object(
|
||||
top_proc, "memory_info", create=True
|
||||
|
@ -847,9 +811,9 @@ def test_top_zombie_process():
|
|||
processes[1].cpu_times = raise_exception
|
||||
|
||||
# Make sure psutil.pids only returns the above 3 pids
|
||||
with patch("salt.utils.psutil_compat.pids", return_value=pids):
|
||||
with patch("psutil.pids", return_value=pids):
|
||||
# Make sure we use our process list from above
|
||||
with patch("salt.utils.psutil_compat.Process", side_effect=processes):
|
||||
with patch("psutil.Process", side_effect=processes):
|
||||
result = ps.top(num_processes=1, interval=0)
|
||||
assert len(result) == 1
|
||||
|
||||
|
@ -862,15 +826,15 @@ def test_status_when_no_status_is_provided_then_raise_invocation_error():
|
|||
@pytest.mark.parametrize(
|
||||
"exc_type",
|
||||
(
|
||||
salt.utils.psutil_compat.AccessDenied(pid="9999", name="whatever"),
|
||||
salt.utils.psutil_compat.NoSuchProcess(pid="42"),
|
||||
psutil.AccessDenied(pid="9999", name="whatever"),
|
||||
psutil.NoSuchProcess(pid="42"),
|
||||
),
|
||||
)
|
||||
def test_status_when_access_denied_from_psutil_it_should_CommandExecutionError(
|
||||
exc_type,
|
||||
):
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
autospec=True,
|
||||
side_effect=exc_type,
|
||||
):
|
||||
|
@ -888,9 +852,9 @@ def test_status_when_no_filter_is_provided_then_raise_invocation_error():
|
|||
|
||||
def test_status_when_access_denied_from_psutil_then_raise_exception():
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.process_iter",
|
||||
"psutil.process_iter",
|
||||
autospec=True,
|
||||
return_value=salt.utils.psutil_compat.AccessDenied(pid="9999", name="whatever"),
|
||||
return_value=psutil.AccessDenied(pid="9999", name="whatever"),
|
||||
):
|
||||
with pytest.raises(Exception) as general_issue:
|
||||
actual_result = salt.modules.ps.status(status="fnord")
|
||||
|
@ -898,7 +862,7 @@ def test_status_when_access_denied_from_psutil_then_raise_exception():
|
|||
|
||||
## This is commented out pending discussion on https://github.com/saltstack/salt/commit/2e5c3162ef87cca8a2c7b12ade7c7e1b32028f0a
|
||||
# @skipIf(not HAS_UTMP, "The utmp module must be installed to run test_get_users_utmp()")
|
||||
# @patch('salt.utils.psutil_compat.get_users', new=MagicMock(return_value=None)) # This will force the function to use utmp
|
||||
# @patch('psutil.get_users', new=MagicMock(return_value=None)) # This will force the function to use utmp
|
||||
# def test_get_users_utmp():
|
||||
# pass
|
||||
|
||||
|
@ -1050,9 +1014,7 @@ def test_boot_time():
|
|||
Testing boot_time function in the ps module
|
||||
"""
|
||||
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.boot_time", MagicMock(return_value=1691593290.0)
|
||||
):
|
||||
with patch("psutil.boot_time", MagicMock(return_value=1691593290.0)):
|
||||
expected = 1691593290
|
||||
ret = ps.boot_time()
|
||||
assert ret == expected
|
||||
|
@ -1061,7 +1023,7 @@ def test_boot_time():
|
|||
ret = ps.boot_time(time_format="%m/%d/%Y")
|
||||
assert ret == expected
|
||||
|
||||
with patch("salt.utils.psutil_compat.boot_time") as mock_boot_time:
|
||||
with patch("psutil.boot_time") as mock_boot_time:
|
||||
mock_boot_time.side_effect = [AttributeError(), 1691593290.0]
|
||||
expected = 1691593290
|
||||
ret = ps.boot_time()
|
||||
|
@ -1073,13 +1035,13 @@ def test_num_cpus():
|
|||
Testing num_cpus function in the ps module
|
||||
"""
|
||||
|
||||
with patch("salt.utils.psutil_compat.cpu_count") as mock_cpu_count:
|
||||
with patch("psutil.cpu_count") as mock_cpu_count:
|
||||
mock_cpu_count.side_effect = AttributeError()
|
||||
with patch("salt.utils.psutil_compat.NUM_CPUS", create=True, new=5):
|
||||
with patch("psutil.NUM_CPUS", create=True, new=5):
|
||||
ret = ps.num_cpus()
|
||||
assert ret == 5
|
||||
|
||||
with patch("salt.utils.psutil_compat.cpu_count") as mock_cpu_count:
|
||||
with patch("psutil.cpu_count") as mock_cpu_count:
|
||||
mock_cpu_count.return_value = 5
|
||||
ret = ps.num_cpus()
|
||||
assert ret == 5
|
||||
|
@ -1089,26 +1051,17 @@ def test_total_physical_memory(stub_memory_usage):
|
|||
"""
|
||||
Testing total_physical_memory function in the ps module
|
||||
"""
|
||||
|
||||
with patch("salt.modules.ps.psutil.version_info", (0, 5, 9)):
|
||||
with pytest.raises(CommandExecutionError) as exc:
|
||||
ps.total_physical_memory()
|
||||
assert (
|
||||
exc.value.error
|
||||
== "virtual_memory is only available in psutil 0.6.0 or greater"
|
||||
)
|
||||
|
||||
with patch("salt.utils.psutil_compat.virtual_memory") as mock_total_physical_memory:
|
||||
with patch("psutil.virtual_memory") as mock_total_physical_memory:
|
||||
mock_total_physical_memory.side_effect = AttributeError()
|
||||
with patch(
|
||||
"salt.utils.psutil_compat.TOTAL_PHYMEM",
|
||||
"psutil.TOTAL_PHYMEM",
|
||||
create=True,
|
||||
new=stub_memory_usage.total,
|
||||
):
|
||||
ret = ps.total_physical_memory()
|
||||
assert ret == 15722012672
|
||||
|
||||
with patch("salt.utils.psutil_compat.virtual_memory") as mock_total_physical_memory:
|
||||
with patch("psutil.virtual_memory") as mock_total_physical_memory:
|
||||
mock_total_physical_memory.return_value = stub_memory_usage
|
||||
ret = ps.total_physical_memory()
|
||||
assert ret == 15722012672
|
||||
|
@ -1227,7 +1180,7 @@ def test_proc_info_access_denied():
|
|||
"""
|
||||
cmdline = ["echo", "питон"]
|
||||
dummy_proc = DummyProcess(cmdline=cmdline)
|
||||
with patch("salt.utils.psutil_compat.Process") as mock_process:
|
||||
with patch("psutil.Process") as mock_process:
|
||||
mock_process.side_effect = psutil.AccessDenied(dummy_proc)
|
||||
with pytest.raises(CommandExecutionError):
|
||||
salt.modules.ps.proc_info(pid=99, attrs=["username", "ppid"])
|
||||
|
@ -1240,7 +1193,7 @@ def test_proc_info_no_such_process():
|
|||
"""
|
||||
cmdline = ["echo", "питон"]
|
||||
dummy_proc = DummyProcess(cmdline=cmdline)
|
||||
with patch("salt.utils.psutil_compat.Process") as mock_process:
|
||||
with patch("psutil.Process") as mock_process:
|
||||
mock_process.side_effect = psutil.NoSuchProcess(dummy_proc)
|
||||
with pytest.raises(CommandExecutionError):
|
||||
salt.modules.ps.proc_info(pid=99, attrs=["username", "ppid"])
|
||||
|
@ -1251,8 +1204,7 @@ def test_proc_info_attribute_error():
|
|||
Testing proc_info function in the ps module
|
||||
when an AttributeError exception occurs
|
||||
"""
|
||||
cmdline = ["echo", "питон"]
|
||||
with patch("salt.utils.psutil_compat.Process") as mock_process:
|
||||
with patch("psutil.Process") as mock_process:
|
||||
mock_process.side_effect = AttributeError()
|
||||
with pytest.raises(CommandExecutionError):
|
||||
salt.modules.ps.proc_info(pid=99, attrs=["username", "ppid"])
|
||||
|
@ -1269,20 +1221,3 @@ def test__virtual__no_psutil():
|
|||
)
|
||||
result = ps.__virtual__()
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test__virtual__wrong_version():
|
||||
with patch("salt.modules.ps.psutil.version_info", (0, 2, 9)):
|
||||
expected = (
|
||||
False,
|
||||
"The ps execution module cannot be loaded: the psutil python module version {}"
|
||||
" is less than 0.3.0".format(psutil.version_info),
|
||||
)
|
||||
result = ps.__virtual__()
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test__virtual__correct_version():
|
||||
with patch("salt.modules.ps.psutil.version_info", (0, 3, 0)):
|
||||
result = ps.__virtual__()
|
||||
assert result
|
||||
|
|
Loading…
Add table
Reference in a new issue