feat(vps): ship durable ARNavi host with reproducible install and recovery
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
"""SIGKILL during full-image verification; isolated synthetic B2 and SQLite."""
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from ndc_vps import VERSION
|
||||
from ndc_vps.commands import frame
|
||||
from ndc_vps.usb_protocol import Configuration, Setting
|
||||
from ndc_vps.web_protocol import decode_settings_reply
|
||||
from qualify_faults import IMEI, Harness
|
||||
|
||||
|
||||
async def web(harness, opcode, payload=b""):
|
||||
data = int(IMEI).to_bytes(8, "little") + bytes.fromhex("0001e7020000")
|
||||
data += bytes([opcode]) + payload
|
||||
body = b"[\x01" + len(data).to_bytes(2, "big") + data + b"]"
|
||||
reader, writer = await asyncio.open_connection("127.0.0.1", harness.ports["tracker"])
|
||||
try:
|
||||
writer.write(
|
||||
b"POST /c/index.php HTTP/1.1\r\n"
|
||||
+ f"Content-Length: {len(body)}\r\n\r\n".encode()
|
||||
+ body
|
||||
)
|
||||
await writer.drain()
|
||||
try:
|
||||
header = await asyncio.wait_for(reader.readuntil(b"\r\n\r\n"), 3)
|
||||
except asyncio.IncompleteReadError as error:
|
||||
assert not error.partial
|
||||
return None
|
||||
except ConnectionResetError:
|
||||
return None # Linux abortive close when no settings image is authorized.
|
||||
size = next(
|
||||
int(line.split(b":")[1])
|
||||
for line in header.split(b"\r\n")
|
||||
if line.lower().startswith(b"content-length:")
|
||||
)
|
||||
return await asyncio.wait_for(reader.readexactly(size), 3)
|
||||
finally:
|
||||
writer.transport.abort()
|
||||
|
||||
|
||||
async def stage(harness, body, expected):
|
||||
pending = await harness.get(body)
|
||||
await asyncio.sleep(max(0, pending.get("not_before", 0) - time.time()) + 0.05)
|
||||
# Fresh telemetry wakes the sender after stage pacing, as in production.
|
||||
data = int(time.time()).to_bytes(4, "little") + b"\x63\0\0\0\0"
|
||||
harness.peer[1].write(b"[\x01\x01\x05\0" + data + bytes([sum(data) % 256]) + b"]")
|
||||
await harness.peer[1].drain()
|
||||
assert await asyncio.wait_for(harness.peer[0].readexactly(4), 3) == b"{\0\x01}"
|
||||
assert await harness.frame() == frame("123456*SERV*1." + str(expected))
|
||||
|
||||
|
||||
async def qualify():
|
||||
start = time.monotonic()
|
||||
result = {
|
||||
"session_id": uuid.uuid4().hex,
|
||||
"started_utc": datetime.now(UTC).isoformat(),
|
||||
"started_monotonic": start,
|
||||
"version": VERSION,
|
||||
"physical_devices_used": False,
|
||||
"production_state_used": False,
|
||||
}
|
||||
before = Configuration(
|
||||
tuple(
|
||||
Setting(n, v)
|
||||
for n, v in (
|
||||
(3, b"example.org"),
|
||||
(4, b"\x26\xc2"),
|
||||
(5, b"123456"),
|
||||
(8, b"\0"),
|
||||
(70, b"example.org"),
|
||||
(71, b"\x26\xc2"),
|
||||
(25, b"\0\x0f"),
|
||||
(222, b"opaque"),
|
||||
)
|
||||
)
|
||||
)
|
||||
target = Configuration(
|
||||
tuple(Setting(r.number, b"\0\x10" if r.number == 25 else r.value) for r in before.records)
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="ndc-vps-recovery-") as name:
|
||||
harness = Harness(Path(name))
|
||||
await harness.start()
|
||||
try:
|
||||
await harness.connect()
|
||||
body = harness.body(
|
||||
kind="configure-all",
|
||||
password="123456",
|
||||
not_after=time.time() + 150,
|
||||
expected_sha256=hashlib.sha256(before.encode()).hexdigest(),
|
||||
target_configuration=target.encode().hex(),
|
||||
)
|
||||
accepted = await harness.submit(body)
|
||||
assert await harness.frame() == frame("123456*SERV*1.9")
|
||||
assert await web(harness, 5, before.encode()) == bytes.fromhex("5b00030005015d")
|
||||
await stage(harness, body, 8)
|
||||
assert decode_settings_reply(await web(harness, 2)).encode() == target.encode()
|
||||
await harness.restart() # image received, readback not yet dispatched
|
||||
pending = await harness.get(body)
|
||||
assert pending["state"] == "queued" and pending["recovery_count"] == 1
|
||||
assert pending["expires_at"] == accepted["expires_at"]
|
||||
await harness.connect()
|
||||
assert await web(harness, 2) is None # never re-serve the settings image
|
||||
await stage(harness, body, 9)
|
||||
await harness.restart() # readback sent, but no result yet
|
||||
pending = await harness.submit(body) # same immutable operation
|
||||
assert pending["state"] == "queued" and pending["recovery_count"] == 2
|
||||
assert pending["expires_at"] == accepted["expires_at"]
|
||||
await harness.connect()
|
||||
assert await web(harness, 2) is None
|
||||
await stage(harness, body, 9)
|
||||
# B2-side reconnect after a read dispatch, with the VPS still alive.
|
||||
await harness.connect()
|
||||
pending = await harness.get(body)
|
||||
assert pending["expires_at"] == accepted["expires_at"]
|
||||
await stage(harness, body, 9)
|
||||
assert await web(harness, 2) is None
|
||||
assert await web(harness, 5, target.encode()) == bytes.fromhex("5b00030005015d")
|
||||
final = await harness.get(body)
|
||||
assert final["state"] == "verified" and final["recovery_verification"]
|
||||
assert final["configuration"] == target.encode().hex()
|
||||
await harness.restart()
|
||||
assert await harness.get(body) == final
|
||||
await harness.connect()
|
||||
await harness.no_frame()
|
||||
assert len(harness.frames) == 5
|
||||
assert harness.frames.count(frame("123456*SERV*1.8")) == 1
|
||||
with harness.store.db() as db:
|
||||
assert db.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
|
||||
assert db.execute("SELECT count(*) FROM commands").fetchone()[0] == 1
|
||||
result.update(
|
||||
ok=True,
|
||||
crashes_during_operation=2,
|
||||
image_writes=1,
|
||||
independent_readbacks_sent=3,
|
||||
tracker_reconnects_during_verification=1,
|
||||
original_deadline_preserved=True,
|
||||
durable_final_result=True,
|
||||
command_rows=1,
|
||||
)
|
||||
finally:
|
||||
await harness.stop()
|
||||
result.update(
|
||||
completed_utc=datetime.now(UTC).isoformat(), elapsed_seconds=time.monotonic() - start
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.umask(0o077)
|
||||
print(json.dumps(asyncio.run(asyncio.wait_for(qualify(), 80)), indent=2))
|
||||
Reference in New Issue
Block a user