Skip to content

Commit

Permalink
fix comparisons and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfranzon committed Feb 7, 2025
1 parent 2030fa9 commit 0f2cefa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/fractal_healthcheck/checks/implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,16 @@ def disk_usage(
try:
return CheckResult(
log=f"The usage of {mountpoint} is {usage_perc}%, while the threashold is {max_perc_usage}%",
success=usage_perc > max_perc_usage,
success=max_perc_usage > usage_perc,
)
except Exception as e:
return CheckResult(exception=e, success=False)


def memory_usage() -> CheckResult:
def memory_usage(max_memory_usage: int = 75) -> CheckResult:
"""
Memory usage, via psutil.virtual_memory
"""
MAX_MEMORY_USAGE = 75
try:
mem_usage = psutil.virtual_memory()

Expand All @@ -150,8 +149,8 @@ def memory_usage() -> CheckResult:
"Percent": f"{mem_usage_percent}%",
}
return CheckResult(
log=f"The memory usage is {mem_usage_percent}%, while the threashold is {MAX_MEMORY_USAGE}%\n {json.dumps(log, indent=2)}",
success=mem_usage_percent > MAX_MEMORY_USAGE,
log=f"The memory usage is {mem_usage_percent}%, while the threashold is {max_memory_usage}%\n {json.dumps(log, indent=2)}",
success=max_memory_usage > mem_usage_percent,
)
except Exception as e:
return CheckResult(exception=e, success=False)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_implementations/test_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def mock_disk_usage(_):
monkeypatch.setattr(psutil, "disk_usage", mock_disk_usage)
result = disk_usage("/mock")
assert result.success is True
assert "lower than 85%" in result.log
assert "The usage of /mock is 50%, while the threashold is 85%" in result.log


def test_disk_usage_high(monkeypatch):
Expand All @@ -105,7 +105,7 @@ def mock_disk_usage(_):
monkeypatch.setattr(psutil, "disk_usage", mock_disk_usage)
result = disk_usage("/mock")
assert result.success is False
assert "higher than 85%" in result.log
assert "The usage of /mock is 90%, while the threashold is 85%" in result.log


def test_memory_usage():
Expand Down

0 comments on commit 0f2cefa

Please sign in to comment.