GitHub Actions commited on
Commit ·
6a14ec5
1
Parent(s): 27be63b
fix(tests,identity): order-independent event loop + Windows key permissions
Browse filesconftest autouse fixture provisions a fresh current event loop per test (Python 3.13 asyncio.run resets the current loop to None, causing order-dependent failures). Four test_coverage_boost concurrency tests now build gather() inside an async wrapper. keys.py POSIX 0o600 check is gated behind os.name==posix; stat.S_IMODE returns 0o666 on NTFS (not an error), so the guard wrongly rejected valid keys on Windows. POSIX enforcement unchanged.
- hearthnet/identity/keys.py +5 -4
- tests/conftest.py +28 -0
- tests/test_coverage_boost.py +20 -8
hearthnet/identity/keys.py
CHANGED
|
@@ -262,16 +262,17 @@ def load(keys_dir: Path) -> KeyPair:
|
|
| 262 |
pub_path = keys_dir / "device.pub"
|
| 263 |
if not priv_path.exists() or not pub_path.exists():
|
| 264 |
raise IdentityError("keys_missing", reason=f"Key files not found in {keys_dir}")
|
| 265 |
-
# Check permissions on POSIX
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
| 267 |
mode = oct(stat.S_IMODE(priv_path.stat().st_mode))
|
| 268 |
if not mode.endswith("600") and not mode.endswith("400"):
|
| 269 |
raise IdentityError(
|
| 270 |
"keys_permissions",
|
| 271 |
reason=f"Private key {priv_path} has unsafe permissions {mode}",
|
| 272 |
)
|
| 273 |
-
except AttributeError:
|
| 274 |
-
pass # Windows
|
| 275 |
try:
|
| 276 |
sk_b64 = priv_path.read_text().strip()
|
| 277 |
padding = 4 - len(sk_b64) % 4
|
|
|
|
| 262 |
pub_path = keys_dir / "device.pub"
|
| 263 |
if not priv_path.exists() or not pub_path.exists():
|
| 264 |
raise IdentityError("keys_missing", reason=f"Key files not found in {keys_dir}")
|
| 265 |
+
# Check permissions on POSIX only. POSIX mode bits are not meaningful on
|
| 266 |
+
# Windows (NTFS files commonly report 0o666 regardless of ACLs), so the
|
| 267 |
+
# check would raise false positives there. stat.S_IMODE does not raise on
|
| 268 |
+
# Windows, so an explicit os.name guard is required.
|
| 269 |
+
if os.name == "posix":
|
| 270 |
mode = oct(stat.S_IMODE(priv_path.stat().st_mode))
|
| 271 |
if not mode.endswith("600") and not mode.endswith("400"):
|
| 272 |
raise IdentityError(
|
| 273 |
"keys_permissions",
|
| 274 |
reason=f"Private key {priv_path} has unsafe permissions {mode}",
|
| 275 |
)
|
|
|
|
|
|
|
| 276 |
try:
|
| 277 |
sk_b64 = priv_path.read_text().strip()
|
| 278 |
padding = 4 - len(sk_b64) % 4
|
tests/conftest.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
|
|
|
| 3 |
import sys
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
|
|
|
|
|
| 6 |
ROOT = Path(__file__).resolve().parents[1]
|
| 7 |
if str(ROOT) not in sys.path:
|
| 8 |
sys.path.insert(0, str(ROOT))
|
|
@@ -15,3 +18,28 @@ try:
|
|
| 15 |
nest_asyncio.apply()
|
| 16 |
except ImportError:
|
| 17 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
import asyncio
|
| 4 |
import sys
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
ROOT = Path(__file__).resolve().parents[1]
|
| 10 |
if str(ROOT) not in sys.path:
|
| 11 |
sys.path.insert(0, str(ROOT))
|
|
|
|
| 18 |
nest_asyncio.apply()
|
| 19 |
except ImportError:
|
| 20 |
pass
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@pytest.fixture(autouse=True)
|
| 24 |
+
def _ensure_current_event_loop():
|
| 25 |
+
"""Guarantee every test starts with an open current event loop.
|
| 26 |
+
|
| 27 |
+
Python 3.13 no longer auto-creates an event loop on demand, and
|
| 28 |
+
``asyncio.run()`` resets the current loop to ``None`` when it exits. Many
|
| 29 |
+
sync tests in this suite call ``asyncio.get_event_loop().run_until_complete``
|
| 30 |
+
or build coroutines via ``asyncio.gather(...)`` outside a running loop, both
|
| 31 |
+
of which require a current loop to exist. Without this fixture those tests
|
| 32 |
+
fail or pass purely depending on the order in which test files happen to
|
| 33 |
+
run. Setting a fresh loop per test makes the suite order-independent.
|
| 34 |
+
"""
|
| 35 |
+
loop = asyncio.new_event_loop()
|
| 36 |
+
asyncio.set_event_loop(loop)
|
| 37 |
+
try:
|
| 38 |
+
yield
|
| 39 |
+
finally:
|
| 40 |
+
try:
|
| 41 |
+
loop.close()
|
| 42 |
+
finally:
|
| 43 |
+
# Leave an open loop set as current for any teardown/collection
|
| 44 |
+
# work that runs between this test and the next one's setup.
|
| 45 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
tests/test_coverage_boost.py
CHANGED
|
@@ -206,8 +206,11 @@ class TestConcurrentOperations:
|
|
| 206 |
(1, 0),
|
| 207 |
{"input": {"recipient": f"user-{idx}", "body": f"message {idx}"}, "params": {}}
|
| 208 |
)
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
assert len(results) == 5
|
| 212 |
|
| 213 |
def test_concurrent_embeddings(self, node):
|
|
@@ -221,8 +224,11 @@ class TestConcurrentOperations:
|
|
| 221 |
)
|
| 222 |
except Exception:
|
| 223 |
return {"skipped": True}
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
| 226 |
assert len(results) == 3
|
| 227 |
|
| 228 |
def test_concurrent_rag_operations(self, node):
|
|
@@ -237,8 +243,11 @@ class TestConcurrentOperations:
|
|
| 237 |
"text": f"Content {idx}"
|
| 238 |
}}
|
| 239 |
)
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
| 242 |
assert len(results) == 5
|
| 243 |
|
| 244 |
|
|
@@ -311,8 +320,11 @@ class TestErrorRecovery:
|
|
| 311 |
)
|
| 312 |
except BusError:
|
| 313 |
return {"error": f"expected {idx}"}
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
| 316 |
assert len(results) == 5
|
| 317 |
|
| 318 |
|
|
|
|
| 206 |
(1, 0),
|
| 207 |
{"input": {"recipient": f"user-{idx}", "body": f"message {idx}"}, "params": {}}
|
| 208 |
)
|
| 209 |
+
|
| 210 |
+
async def _all():
|
| 211 |
+
return await asyncio.gather(*[task(i) for i in range(5)])
|
| 212 |
+
|
| 213 |
+
results = _run(_all())
|
| 214 |
assert len(results) == 5
|
| 215 |
|
| 216 |
def test_concurrent_embeddings(self, node):
|
|
|
|
| 224 |
)
|
| 225 |
except Exception:
|
| 226 |
return {"skipped": True}
|
| 227 |
+
|
| 228 |
+
async def _all():
|
| 229 |
+
return await asyncio.gather(*[task(i) for i in range(3)])
|
| 230 |
+
|
| 231 |
+
results = _run(_all())
|
| 232 |
assert len(results) == 3
|
| 233 |
|
| 234 |
def test_concurrent_rag_operations(self, node):
|
|
|
|
| 243 |
"text": f"Content {idx}"
|
| 244 |
}}
|
| 245 |
)
|
| 246 |
+
|
| 247 |
+
async def _all():
|
| 248 |
+
return await asyncio.gather(*[task(i) for i in range(5)])
|
| 249 |
+
|
| 250 |
+
results = _run(_all())
|
| 251 |
assert len(results) == 5
|
| 252 |
|
| 253 |
|
|
|
|
| 320 |
)
|
| 321 |
except BusError:
|
| 322 |
return {"error": f"expected {idx}"}
|
| 323 |
+
|
| 324 |
+
async def _all():
|
| 325 |
+
return await asyncio.gather(*[task(i) for i in range(5)])
|
| 326 |
+
|
| 327 |
+
results = _run(_all())
|
| 328 |
assert len(results) == 5
|
| 329 |
|
| 330 |
|