Debian Codegraph @

tests/test_process_identity.py

raw
1 from __future__ import annotations
2
3 import os
4 import sys
5 from errno import ENODEV, EPERM
6 from typing import TYPE_CHECKING, Final
7
8 import pytest
9
10 from filelock._identity import host_name, owner_is_stale, process_alive, process_start_token
11
12 if TYPE_CHECKING:
13 from pytest_mock import MockerFixture
14
15 _DEAD_PID: Final[int] = 2**22 + 1
16 _POSIX_ONLY: Final[pytest.MarkDecorator] = pytest.mark.skipif(sys.platform == "win32", reason="posix kill semantics")
17 #: NetBSD and other POSIX platforms without a proven start-time source expose no token, so an owner carries none there.
18 _NEEDS_START_TOKEN: Final[pytest.MarkDecorator] = pytest.mark.skipif(
19 process_start_token(os.getpid()) is None, reason="this platform exposes no process start-time source"
20 )
21
22
23 @pytest.mark.parametrize(
24 ("raw", "expected"),
25 [
26 pytest.param("build-01.example.com", "build-01.example.com", id="plain"),
27 pytest.param("build 01", "build?2001", id="space"),
28 pytest.param("build\n01", "build?0a01", id="newline"),
29 pytest.param("wörks", "w?c3?b6rks", id="non-ascii"),
30 pytest.param("who?", "who?3f", id="escape-character"),
31 pytest.param("b\udcffd", "b?ffd", id="undecodable-byte"),
32 pytest.param("x" * 300, "x" * 253, id="over-long"),
33 pytest.param("ä" * 200, "?c3?a4" * 42, id="over-long-escaped"),
34 pytest.param("", "?", id="empty"),
35 ],
36 )
37 def test_host_name_escapes_out_of_grammar_bytes(raw: str, expected: str, mocker: MockerFixture) -> None:
38 mocker.patch("filelock._identity.socket.gethostname", return_value=raw)
39 assert host_name() == expected
40
41
42 def test_process_alive_true_for_self() -> None:
43 assert process_alive(os.getpid()) is True
44
45
46 def test_process_alive_false_for_dead() -> None:
47 assert process_alive(_DEAD_PID) is False
48
49
50 @_POSIX_ONLY
51 def test_process_alive_reads_permission_denied_as_alive(mocker: MockerFixture) -> None: # pragma: win32 no cover
52 mocker.patch("filelock._identity.os.kill", side_effect=OSError(EPERM, "operation not permitted"))
53 assert process_alive(_DEAD_PID) is True
54
55
56 @_POSIX_ONLY
57 def test_process_alive_reraises_unexpected_errno(mocker: MockerFixture) -> None: # pragma: win32 no cover
58 mocker.patch("filelock._identity.os.kill", side_effect=OSError(ENODEV, "no such device"))
59 with pytest.raises(OSError, match="no such device"): # pragma: win32 no cover
60 process_alive(_DEAD_PID)
61
62
63 @_NEEDS_START_TOKEN
64 def test_process_start_token_is_int_for_self() -> None:
65 assert isinstance(process_start_token(os.getpid()), int)
66
67
68 def test_process_start_token_none_for_dead() -> None:
69 assert process_start_token(_DEAD_PID) is None
70
71
72 @pytest.mark.skipif(sys.platform != "darwin", reason="macOS sysctl probe")
73 def test_darwin_sysctl_probe_failure_reads_no_token(mocker: MockerFixture) -> None: # pragma: darwin cover
74 mocker.patch("filelock._identity._LIBC.sysctl", return_value=-1)
75 assert process_start_token(os.getpid()) is None
76
77
78 def test_owner_is_stale_foreign_host_never_reclaimed() -> None:
79 assert owner_is_stale(os.getpid(), "another-host.example.com", 1) is False
80
81
82 def test_owner_is_stale_dead_process_reclaimed() -> None:
83 assert owner_is_stale(_DEAD_PID, host_name(), 1) is True
84
85
86 def test_owner_is_stale_live_process_without_token_held() -> None:
87 assert owner_is_stale(os.getpid(), host_name(), None) is False
88
89
90 def test_owner_is_stale_live_process_matching_token_held() -> None:
91 assert owner_is_stale(os.getpid(), host_name(), process_start_token(os.getpid())) is False
92
93
94 @_NEEDS_START_TOKEN
95 def test_owner_is_stale_live_process_mismatched_token_reclaimed() -> None:
96 token = process_start_token(os.getpid())
97 assert token is not None
98 assert owner_is_stale(os.getpid(), host_name(), token + 1) is True
99
100
101 def test_owner_is_stale_live_process_unreadable_token_held(mocker: MockerFixture) -> None:
102 # A live PID whose current start token cannot be read is indistinguishable from the recorded owner, so it holds.
103 mocker.patch("filelock._identity.process_start_token", return_value=None)
104 assert owner_is_stale(os.getpid(), host_name(), 987654) is False