fix: correct PermissionError handling in is_process_alive, add corrupt PID test
This commit is contained in:
@@ -47,8 +47,10 @@ def is_process_alive(pid: int) -> bool:
|
|||||||
try:
|
try:
|
||||||
os.kill(pid, 0)
|
os.kill(pid, 0)
|
||||||
return True
|
return True
|
||||||
except (ProcessLookupError, PermissionError):
|
except ProcessLookupError:
|
||||||
return False
|
return False
|
||||||
|
except PermissionError:
|
||||||
|
return True # process exists but owned by another user
|
||||||
|
|
||||||
|
|
||||||
def check_stale(config_dir: Path, name: str) -> bool:
|
def check_stale(config_dir: Path, name: str) -> bool:
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timezone, timedelta
|
||||||
from unittest.mock import patch
|
|
||||||
from autossh_mgr.process import (
|
from autossh_mgr.process import (
|
||||||
write_pid_file, read_pid_file, delete_pid_file,
|
write_pid_file, read_pid_file, delete_pid_file,
|
||||||
is_process_alive, check_stale, get_status, format_uptime,
|
is_process_alive, check_stale, get_status, format_uptime,
|
||||||
TunnelStatus,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -95,3 +93,11 @@ def test_format_uptime_hours():
|
|||||||
def test_format_uptime_days():
|
def test_format_uptime_days():
|
||||||
started = datetime.now(timezone.utc) - timedelta(days=1, hours=5)
|
started = datetime.now(timezone.utc) - timedelta(days=1, hours=5)
|
||||||
assert format_uptime(started) == "1d 5h"
|
assert format_uptime(started) == "1d 5h"
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_pid_file_corrupt(config_dir):
|
||||||
|
import click
|
||||||
|
pid_path = config_dir / "pids" / "web-service.pid"
|
||||||
|
pid_path.write_text("only-one-line\n")
|
||||||
|
with pytest.raises(click.ClickException, match="Corrupt PID file"):
|
||||||
|
read_pid_file(config_dir, "web-service")
|
||||||
|
|||||||
Reference in New Issue
Block a user