Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore partitions with access errors. #246

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions agentops/host_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import psutil
import socket
from .helpers import get_agentops_version
from .log_config import logger
import importlib.metadata
import os
import sys
Expand Down Expand Up @@ -115,14 +116,19 @@ def get_disk_details():
partitions = psutil.disk_partitions()
disk_info = {}
for partition in partitions:
usage = psutil.disk_usage(partition.mountpoint)
disk_info[partition.device] = {
"Mountpoint": partition.mountpoint,
"Total": f"{usage.total / (1024**3):.2f} GB",
"Used": f"{usage.used / (1024**3):.2f} GB",
"Free": f"{usage.free / (1024**3):.2f} GB",
"Percentage": f"{usage.percent}%",
}
try:
usage = psutil.disk_usage(partition.mountpoint)
disk_info[partition.device] = {
"Mountpoint": partition.mountpoint,
"Total": f"{usage.total / (1024**3):.2f} GB",
"Used": f"{usage.used / (1024**3):.2f} GB",
"Free": f"{usage.free / (1024**3):.2f} GB",
"Percentage": f"{usage.percent}%",
}
except OSError as inaccessible:
# Skip inaccessible partitions, such as removable drives with no media
logger.debug("Mountpoint %s inaccessible: %s", partition.mountpoint, inaccessible)

return disk_info


Expand Down
43 changes: 43 additions & 0 deletions tests/test_host_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from unittest.mock import patch
from agentops import host_env

# noinspection PyProtectedMember
from psutil._common import sdiskpart, sdiskusage


def mock_partitions():
return [
sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4',
opts='rw,relatime', maxfile=0, maxpath=0),
sdiskpart(device='z:\\', mountpoint='z:\\', fstype='ntfs',
opts='rw,relatime', maxfile=0, maxpath=0),
]


def mock_disk_usage(partition):
if partition == '/':
return sdiskusage(total=(1024**3), used=0, free=(1024**3), percent=100)
else:
raise PermissionError("Device access exception should have been caught")


class TestHostEnv:
@patch('psutil.disk_partitions', new=lambda: [mock_partitions()[0]])
@patch('psutil.disk_usage', new=mock_disk_usage)
def test_disk_info(self):
self.assert_disk_info()

@patch('psutil.disk_partitions', new=mock_partitions)
@patch('psutil.disk_usage', new=mock_disk_usage)
def test_disk_info_skips_oserror(self):
self.assert_disk_info()

def assert_disk_info(self):
disk_info = host_env.get_disk_details()
assert list(disk_info.keys()) == ["/dev/sda1"]
sda1 = disk_info["/dev/sda1"]
assert sda1["Mountpoint"] == "/"
assert sda1["Total"] == "1.00 GB"
assert sda1["Used"] == "0.00 GB"
assert sda1["Free"] == "1.00 GB"
assert sda1["Percentage"] == "100%"
Loading