feat(system): add worker and VPS catalogs with guarded local removal
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
from threading import RLock
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from k1link.fleet.server_catalog import ServerCatalog, address
|
||||
from k1link.web import server_catalog_api as api
|
||||
from k1link.web.fleet_api import local_operator
|
||||
|
||||
|
||||
def service(kind="wireguard", host="203.0.113.10", key="wg-one"):
|
||||
return dict(kind=kind, id=key, endpoint=f"https://{host}:19444", name=kind, state="ready")
|
||||
|
||||
|
||||
def test_projection_keeps_service_identity_and_has_no_credentials(tmp_path):
|
||||
s = ServerCatalog(tmp_path)
|
||||
original = [service(), service("arnavi", key="arnavi-one"), service(host="203.0.113.20")]
|
||||
rows = s.listing(original)
|
||||
assert len(rows) == 2
|
||||
assert len(rows[0]["services"]) == 2
|
||||
assert rows[0]["services"] == original[:2]
|
||||
assert ServerCatalog(tmp_path).listing(original) == rows
|
||||
assert s.listing([]) == [] # discovery does not create orphan durable entries
|
||||
|
||||
|
||||
def test_metadata_persists_without_renaming_services_and_revision_is_enforced(tmp_path):
|
||||
s = ServerCatalog(tmp_path)
|
||||
services = [service()]
|
||||
row = s.listing(services)[0]
|
||||
changed = s.save("Primary", row["address"], services, row["id"], 1)
|
||||
assert changed["revision"] == 2
|
||||
assert changed["services"] == services
|
||||
assert ServerCatalog(tmp_path).find(row["id"], services)["name"] == "Primary"
|
||||
with pytest.raises(ValueError, match="изменились"):
|
||||
s.save("Stale", row["address"], services, row["id"], 1)
|
||||
with pytest.raises(ValueError, match="переподключения"):
|
||||
s.save("Moved", "203.0.113.99", services, row["id"], 2)
|
||||
with pytest.raises(ValueError, match="уже есть"):
|
||||
s.save("Duplicate", row["address"], services)
|
||||
|
||||
|
||||
def test_create_and_edit_empty_server_preserve_id(tmp_path):
|
||||
s = ServerCatalog(tmp_path)
|
||||
row = s.save("Test", "Server.Example.COM.", [])
|
||||
assert row["address"] == "server.example.com"
|
||||
assert row["services"] == []
|
||||
changed = s.save("Renamed", "new.example.com", [], row["id"], 1)
|
||||
assert changed["id"] == row["id"]
|
||||
assert s.listing([service(host="new.example.com")])[0]["services"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
[
|
||||
"https://server.example.com",
|
||||
"server.example.com:443",
|
||||
"user@host.com",
|
||||
"999.999.1.1",
|
||||
"",
|
||||
"host; rm -rf /",
|
||||
],
|
||||
)
|
||||
def test_invalid_address(value):
|
||||
with pytest.raises(ValueError):
|
||||
address(value)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path):
|
||||
hosts = SimpleNamespace(listing=Mock(return_value=[]), pair=Mock())
|
||||
profiles = SimpleNamespace(listing=Mock(return_value=[]), import_file=Mock())
|
||||
fleet = SimpleNamespace(root=tmp_path, vps_hosts=hosts, network_profiles=profiles, lock=RLock())
|
||||
app = FastAPI()
|
||||
app.include_router(api.router)
|
||||
app.dependency_overrides[local_operator] = lambda: fleet
|
||||
with TestClient(app) as c:
|
||||
yield c, fleet, app
|
||||
|
||||
|
||||
def test_api_new_and_stale_update(client):
|
||||
c, fleet, _ = client
|
||||
r = c.post("/api/v1/system/servers", json=dict(name="Test", address="test.example.com"))
|
||||
assert r.status_code == 200
|
||||
key = r.json()["id"]
|
||||
payload = dict(name="Edited", address="test.example.com", expected_revision=1)
|
||||
assert c.put(f"/api/v1/system/servers/{key}", json=payload).status_code == 200
|
||||
assert c.put(f"/api/v1/system/servers/{key}", json=payload).status_code == 409
|
||||
assert c.get("/api/v1/system/servers").json()["items"][0]["name"] == "Edited"
|
||||
fleet.vps_hosts.pair.assert_not_called()
|
||||
fleet.network_profiles.import_file.assert_not_called()
|
||||
|
||||
|
||||
def test_health_only_checks_the_selected_hosts_existing_bindings(client, monkeypatch):
|
||||
from k1link.web import server_health
|
||||
|
||||
c, fleet, _ = client
|
||||
wg = dict(
|
||||
id="wg-one", server_id="wg-one", name="WG",
|
||||
endpoint="https://one.example:19444", state="ready",
|
||||
)
|
||||
other = {**wg, "id": "wg-two", "server_id": "wg-two", "endpoint": "https://two.example:19444"}
|
||||
fleet.network_profiles.listing.return_value = [wg, other]
|
||||
fleet.network_profiles.lock = RLock()
|
||||
fleet.network_profiles.profiles = {"one": wg, "two": other}
|
||||
fleet.network_profiles.verify_status = Mock()
|
||||
fleet.vps_hosts.rows = Mock(return_value=[])
|
||||
probe = Mock(return_value={"state": "online", "checked_at": 100})
|
||||
monkeypatch.setattr(server_health, "probe", probe)
|
||||
key = next(
|
||||
x["id"] for x in c.get("/api/v1/system/servers").json()["items"]
|
||||
if x["address"] == "one.example"
|
||||
)
|
||||
result = c.get(f"/api/v1/system/servers/{key}/health")
|
||||
assert result.status_code == 200
|
||||
assert result.json()["items"] == [
|
||||
dict(id="wg-one", kind="wireguard", state="online", checked_at=100)
|
||||
]
|
||||
assert probe.call_args.args[1]["server_id"] == "wg-one"
|
||||
assert c.get("/api/v1/system/servers/unknown/health").status_code == 409
|
||||
fleet.network_profiles.import_file.assert_not_called()
|
||||
fleet.vps_hosts.pair.assert_not_called()
|
||||
|
||||
|
||||
def test_import_rejects_wrong_host_before_admission(client, monkeypatch):
|
||||
c, fleet, _ = client
|
||||
key = c.post(
|
||||
"/api/v1/system/servers", json=dict(name="Test", address="test.example.com")
|
||||
).json()["id"]
|
||||
monkeypatch.setattr(
|
||||
api, "parse_file", lambda _: dict(endpoint="https://other.example.com:19444")
|
||||
)
|
||||
assert (
|
||||
c.post(
|
||||
f"/api/v1/system/servers/{key}/services",
|
||||
json=dict(kind="wireguard", document="invitation", expected_revision=1),
|
||||
).status_code
|
||||
== 409
|
||||
)
|
||||
fleet.network_profiles.import_file.assert_not_called()
|
||||
|
||||
|
||||
def test_successful_admission_is_visible_without_second_persistence(client, monkeypatch):
|
||||
c, fleet, _ = client
|
||||
key = c.post(
|
||||
"/api/v1/system/servers", json=dict(name="Test", address="test.example.com")
|
||||
).json()["id"]
|
||||
monkeypatch.setattr(
|
||||
api, "parse_file", lambda _: dict(endpoint="https://test.example.com:19444")
|
||||
)
|
||||
row = dict(id="wg-one", name="Test", endpoint="https://test.example.com:19444", state="ready")
|
||||
fleet.network_profiles.import_file.side_effect = lambda *_: setattr(
|
||||
fleet.network_profiles.listing, "return_value", [row]
|
||||
)
|
||||
payload = dict(kind="wireguard", document="invitation", expected_revision=1)
|
||||
r = c.post(f"/api/v1/system/servers/{key}/services", json=payload)
|
||||
assert r.status_code == 200
|
||||
assert r.json()["services"][0]["id"] == "wg-one"
|
||||
assert c.get("/api/v1/system/servers").json()["items"][0]["services"][0]["id"] == "wg-one"
|
||||
|
||||
|
||||
def test_unavailable_service_store_does_not_look_empty(client):
|
||||
c, fleet, _ = client
|
||||
fleet.vps_hosts = None
|
||||
assert c.get("/api/v1/system/servers").status_code == 503
|
||||
|
||||
|
||||
def test_local_operator_required(client):
|
||||
c, _, app = client
|
||||
app.dependency_overrides.clear()
|
||||
assert c.post(
|
||||
"/api/v1/system/servers", json=dict(name="Test", address="test.example.com")
|
||||
).status_code in (401, 403, 503)
|
||||
@@ -0,0 +1,45 @@
|
||||
import asyncio
|
||||
from unittest.mock import Mock
|
||||
|
||||
from k1link.web import server_health as health
|
||||
|
||||
|
||||
def test_probe_is_read_only_and_rejects_wrong_identity(monkeypatch):
|
||||
request = Mock(return_value={"schema": "missioncore.vps-host/v1", "host_id": "other"})
|
||||
monkeypatch.setattr(health, "request", request)
|
||||
row = {"id": "ours"}
|
||||
assert health.probe("arnavi", row, Mock())["state"] == "unavailable"
|
||||
request.assert_called_once_with(row, "GET", "/v1/status")
|
||||
request.return_value["host_id"] = "ours"
|
||||
assert health.probe("arnavi", row, Mock())["state"] == "online"
|
||||
request.side_effect = TimeoutError()
|
||||
assert health.probe("arnavi", row, Mock())["state"] == "unavailable"
|
||||
|
||||
|
||||
def test_wireguard_requires_existing_identity_validator(monkeypatch):
|
||||
monkeypatch.setattr(health, "request", Mock(return_value={"schema": "wrong"}))
|
||||
verify = Mock(side_effect=ValueError("mismatch"))
|
||||
assert health.probe("wireguard", {}, verify)["state"] == "unavailable"
|
||||
verify.assert_called_once()
|
||||
|
||||
|
||||
def test_concurrent_checks_share_bounded_cache_and_binding_change_invalidates(monkeypatch):
|
||||
probe = Mock(return_value={"state": "online", "checked_at": 100})
|
||||
monkeypatch.setattr(health, "probe", probe)
|
||||
|
||||
async def scenario():
|
||||
service = health.ServerHealth()
|
||||
row = {"id": "one", "endpoint": "https://example:19443", "token": "old"}
|
||||
replies = await asyncio.gather(*(service.observe("arnavi", row, Mock()) for _ in range(10)))
|
||||
assert all(r["state"] == "online" for r in replies)
|
||||
assert probe.call_count == 1
|
||||
await service.observe("arnavi", row, Mock())
|
||||
assert probe.call_count == 1
|
||||
await service.observe("arnavi", {**row, "token": "changed"}, Mock())
|
||||
assert probe.call_count == 2
|
||||
service.cache = {k: (0, v) for k, (_, v) in service.cache.items()}
|
||||
probe.return_value = {"state": "unavailable", "checked_at": 101}
|
||||
assert (await service.observe("arnavi", row, Mock()))["state"] == "unavailable"
|
||||
assert not service.pending
|
||||
|
||||
asyncio.run(scenario())
|
||||
@@ -0,0 +1,287 @@
|
||||
import copy
|
||||
import json
|
||||
from threading import RLock
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from k1link.compute_connections.store import Connection, NetworkProfile, Registry, WorkerBinding
|
||||
from k1link.fleet.independent_equipment import EquipmentStore
|
||||
from k1link.fleet.network_profiles import NetworkProfiles
|
||||
from k1link.fleet.server_removal import ServerRemoval
|
||||
from k1link.fleet.vps_hosts import VPSHosts
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup(tmp_path, monkeypatch):
|
||||
from k1link.fleet import server_removal
|
||||
|
||||
fleet = SimpleNamespace(root=tmp_path, lock=RLock(), listeners={})
|
||||
fleet.network_profiles = NetworkProfiles(fleet)
|
||||
fleet.vps_hosts = VPSHosts(tmp_path, "core-test", EquipmentStore(tmp_path, {}))
|
||||
vehicles = []
|
||||
fleet.rows = lambda: copy.deepcopy(vehicles)
|
||||
fleet.save = lambda row: vehicles.__setitem__(
|
||||
next(i for i, v in enumerate(vehicles) if v["id"] == row["id"]), copy.deepcopy(row)
|
||||
)
|
||||
ensure, helper = Mock(), Mock(return_value={"removed": True})
|
||||
monkeypatch.setattr(server_removal.network_client, "ensure", ensure)
|
||||
monkeypatch.setattr(server_removal.network_client, "request", helper)
|
||||
manager = ServerRemoval(fleet, tmp_path / "system")
|
||||
host = manager.catalog.save("Chosen VPS", "one.example", [])
|
||||
return manager, host, vehicles, ensure, helper
|
||||
|
||||
|
||||
def seed(manager, vehicles):
|
||||
for name, address in [("one", "one.example"), ("two", "two.example")]:
|
||||
manager.networks.profiles[name] = dict(
|
||||
server_id=name,
|
||||
endpoint=f"https://{address}:19444",
|
||||
name=name,
|
||||
state="ready",
|
||||
subnet="10.203.1.0/24" if name == "one" else "10.203.2.0/24",
|
||||
token="private-" + name,
|
||||
)
|
||||
manager.hosts.save(
|
||||
dict(
|
||||
id=name,
|
||||
endpoint=f"https://{address}:19443",
|
||||
revision=1,
|
||||
fingerprint="fingerprint-" + name,
|
||||
token="arnavi-" + name,
|
||||
state="paired",
|
||||
vehicle_ids=["vehicle"],
|
||||
)
|
||||
)
|
||||
manager.networks.save()
|
||||
vehicles.append(
|
||||
dict(
|
||||
id="vehicle",
|
||||
name="Vehicle",
|
||||
network_active="tailscale",
|
||||
network_paths={"one": {"core": "10.203.1.2"}, "two": {"core": "10.203.2.2"}},
|
||||
)
|
||||
)
|
||||
worker = WorkerBinding(
|
||||
contour_id="worker",
|
||||
host_key_sha256="SHA256:" + "a" * 43,
|
||||
transport_id="transport",
|
||||
active_connection_id="path-two",
|
||||
connections=(
|
||||
Connection(
|
||||
id="path-one", profile_id="profile-one", endpoint="one.example", peer_id="peer"
|
||||
),
|
||||
Connection(
|
||||
id="path-two", profile_id="profile-two", endpoint="two.example", peer_id="peer"
|
||||
),
|
||||
),
|
||||
)
|
||||
profiles = tuple(
|
||||
NetworkProfile(id="profile-" + name, name=name, provider="wireguard", network_id=name)
|
||||
for name in ("one", "two")
|
||||
)
|
||||
with manager.connections.locked():
|
||||
manager.connections._write(Registry(profiles=profiles, workers=(worker,)))
|
||||
|
||||
|
||||
def test_empty_server_delete_and_stale_name(setup):
|
||||
manager, host, _, ensure, helper = setup
|
||||
first = manager.preview(host)
|
||||
changed = manager.catalog.save("Changed", host["address"], [], host["id"], 1)
|
||||
with pytest.raises(ValueError, match="изменились"):
|
||||
manager.remove(changed, first["confirmation"])
|
||||
manager.remove(changed, manager.preview(changed)["confirmation"])
|
||||
assert manager.catalog.listing([]) == []
|
||||
ensure.assert_not_called()
|
||||
helper.assert_not_called()
|
||||
|
||||
|
||||
def test_cascade_keeps_other_vps_devices_and_active_worker_connection(setup):
|
||||
manager, host, vehicles, ensure, helper = setup
|
||||
seed(manager, vehicles)
|
||||
unrelated = copy.deepcopy(manager.networks.profiles["two"])
|
||||
listener = Mock()
|
||||
manager.fleet.listeners["10.203.1.2"] = listener
|
||||
with manager.hosts.db() as db:
|
||||
db.execute("INSERT INTO packets VALUES('one',1,'dev-one',1,?)", (b"packet",))
|
||||
db.execute("INSERT INTO packets VALUES('two',1,'dev-two',1,?)", (b"keep",))
|
||||
db.execute("INSERT INTO links VALUES('dev-one','one',1)")
|
||||
db.execute("INSERT INTO positions VALUES('dev-one','{}')")
|
||||
plan = manager.preview(host)
|
||||
assert plan["blocked"] == []
|
||||
assert plan["services"] == ["ARNavi", "WireGuard"]
|
||||
assert plan["worker_connections"] == plan["vehicle_connections"] == 1
|
||||
assert manager.remove(host, plan["confirmation"])["removed"]
|
||||
helper.assert_called_once_with("forget", "one")
|
||||
ensure.assert_called_once()
|
||||
listener.shutdown.assert_called_once()
|
||||
listener.server_close.assert_called_once()
|
||||
assert manager.networks.profiles == {"two": unrelated}
|
||||
assert [r["id"] for r in manager.hosts.rows()] == ["two"]
|
||||
assert list(vehicles[0]["network_paths"]) == ["two"]
|
||||
registry = manager.connections.read()
|
||||
assert [p.id for p in registry.profiles] == ["profile-two"]
|
||||
worker = registry.workers[0]
|
||||
assert worker.active_connection_id == "path-two"
|
||||
assert [c.id for c in worker.connections] == ["path-two"]
|
||||
assert worker.revision == 1
|
||||
assert manager.catalog.listing([]) == []
|
||||
with manager.hosts.db() as db:
|
||||
assert db.execute("SELECT host FROM packets").fetchall() == [("two",)]
|
||||
assert db.execute("SELECT * FROM links").fetchall() == []
|
||||
assert db.execute("SELECT * FROM positions").fetchall() == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("consumer", ["worker", "board", "unknown-board", "command", "outbox"])
|
||||
def test_active_or_pending_consumers_block_before_any_mutation(setup, consumer):
|
||||
manager, host, vehicles, ensure, helper = setup
|
||||
seed(manager, vehicles)
|
||||
if consumer == "worker":
|
||||
manager.connections.select("worker", "path-one", expected_revision=0)
|
||||
if consumer == "board":
|
||||
vehicles[0]["network_active"] = "one"
|
||||
if consumer == "unknown-board":
|
||||
vehicles[0].pop("network_active")
|
||||
if consumer == "command":
|
||||
with manager.hosts.equipment.db() as db:
|
||||
db.execute(
|
||||
"INSERT INTO command_journal(id,instance,state,body) "
|
||||
"VALUES('cmd','dev','queued',?)",
|
||||
(json.dumps({"host_id": "one"}),),
|
||||
)
|
||||
if consumer == "outbox":
|
||||
with manager.hosts.equipment.db() as db:
|
||||
db.execute(
|
||||
"INSERT INTO network_outbox(id,instance,host,target,body) "
|
||||
"VALUES('cmd','dev','two','one','{}')"
|
||||
)
|
||||
before = manager.connections.path.read_bytes()
|
||||
plan = manager.preview(host)
|
||||
assert plan["blocked"]
|
||||
with pytest.raises(ValueError):
|
||||
manager.remove(host, plan["confirmation"])
|
||||
assert manager.connections.path.read_bytes() == before
|
||||
assert len(manager.hosts.rows()) == len(manager.networks.profiles) == 2
|
||||
ensure.assert_not_called()
|
||||
helper.assert_not_called()
|
||||
|
||||
|
||||
def test_new_service_invalidates_confirmation(setup):
|
||||
manager, host, vehicles, _, helper = setup
|
||||
old = manager.preview(host)
|
||||
seed(manager, vehicles)
|
||||
with pytest.raises(ValueError, match="изменились"):
|
||||
manager.remove(host, old["confirmation"])
|
||||
helper.assert_not_called()
|
||||
|
||||
|
||||
def test_helper_failure_preserves_connections_and_retryable_server(setup):
|
||||
manager, host, vehicles, _, helper = setup
|
||||
seed(manager, vehicles)
|
||||
helper.side_effect = OSError("helper unavailable")
|
||||
before = manager.connections.path.read_bytes()
|
||||
with pytest.raises(OSError):
|
||||
manager.remove(host, manager.preview(host)["confirmation"])
|
||||
assert manager.catalog.find(host["id"], [])["name"] == host["name"]
|
||||
assert manager.connections.path.read_bytes() == before
|
||||
assert len(manager.hosts.rows()) == len(manager.networks.profiles) == 2
|
||||
|
||||
|
||||
def test_interrupted_profile_write_is_retryable_and_never_reports_success(setup, monkeypatch):
|
||||
manager, host, vehicles, _, _ = setup
|
||||
seed(manager, vehicles)
|
||||
save = manager.networks.save
|
||||
monkeypatch.setattr(manager.networks, "save", Mock(side_effect=OSError("write failed")))
|
||||
with pytest.raises(OSError):
|
||||
manager.remove(host, manager.preview(host)["confirmation"])
|
||||
assert "one" in manager.networks.profiles
|
||||
assert len(manager.hosts.rows()) == 2
|
||||
assert manager.catalog.find(host["id"], [])
|
||||
monkeypatch.setattr(manager.networks, "save", save)
|
||||
assert manager.remove(host, manager.preview(host)["confirmation"])["removed"]
|
||||
assert "one" not in manager.networks.profiles
|
||||
assert [r["id"] for r in manager.hosts.rows()] == ["two"]
|
||||
|
||||
|
||||
def test_api_requires_fresh_confirmation_and_local_operator(setup):
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from k1link.web import server_catalog_api as api
|
||||
from k1link.web.fleet_api import local_operator
|
||||
|
||||
manager, host, _, ensure, helper = setup
|
||||
app = FastAPI()
|
||||
app.state.system_connections_root = manager.connections.path.parent
|
||||
app.include_router(api.router)
|
||||
app.dependency_overrides[local_operator] = lambda: manager.fleet
|
||||
path = "/api/v1/system/servers/" + host["id"]
|
||||
with TestClient(app) as client:
|
||||
assert client.request("DELETE", path, json={"expected_revision": 1}).status_code == 422
|
||||
plan = client.post(path + "/removal", json={"expected_revision": 1})
|
||||
assert plan.status_code == 200
|
||||
body = dict(expected_revision=1, confirmation=plan.json()["confirmation"])
|
||||
assert (
|
||||
client.request("DELETE", path, json={**body, "confirmation": "0" * 64}).status_code
|
||||
== 409
|
||||
)
|
||||
assert client.request("DELETE", path, json=body).status_code == 200
|
||||
assert client.get("/api/v1/system/servers").json()["items"] == []
|
||||
app.dependency_overrides.clear()
|
||||
assert client.request("DELETE", path, json=body).status_code in (401, 403, 503)
|
||||
ensure.assert_not_called()
|
||||
helper.assert_not_called()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def helper_manager(tmp_path):
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"removal_helper", Path(__file__).resolve().parents[1] / "apps/wireguard-client/helper.py"
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
manager = module.Manager(tmp_path)
|
||||
one, two = "wg_" + "1" * 32, "wg_" + "2" * 32
|
||||
row = dict(
|
||||
private_key="private", public_key="public", config=None, interface=None, pending=None
|
||||
)
|
||||
manager.profiles = {one: copy.deepcopy(row), two: copy.deepcopy(row)}
|
||||
manager.persist()
|
||||
return manager, one, two
|
||||
|
||||
|
||||
def test_helper_forgets_only_selected_profile_and_is_idempotent(helper_manager):
|
||||
manager, one, two = helper_manager
|
||||
manager.down = Mock()
|
||||
manager.prepare = Mock(side_effect=AssertionError("Must not create a profile"))
|
||||
assert manager.dispatch(dict(action="forget", server_id=one))["removed"]
|
||||
assert list(json.loads(manager.path.read_text())) == [two]
|
||||
assert manager.dispatch(dict(action="forget", server_id=one))["removed"]
|
||||
manager.down.assert_called_once()
|
||||
manager.prepare.assert_not_called()
|
||||
|
||||
|
||||
def test_helper_refuses_pending_or_foreign_tunnel_and_retains_profile(helper_manager):
|
||||
manager, one, two = helper_manager
|
||||
manager.down = Mock(side_effect=ValueError("Foreign interface"))
|
||||
manager.profiles[one]["pending"] = {"id": "operation"}
|
||||
with pytest.raises(ValueError, match="pending"):
|
||||
manager.dispatch(dict(action="forget", server_id=one))
|
||||
manager.down.assert_not_called()
|
||||
manager.profiles[one]["pending"] = None
|
||||
with pytest.raises(ValueError, match="Foreign"):
|
||||
manager.dispatch(dict(action="forget", server_id=one))
|
||||
assert set(manager.profiles) == {one, two}
|
||||
|
||||
|
||||
def test_helper_failed_persistence_retains_retryable_profile(helper_manager):
|
||||
manager, one, two = helper_manager
|
||||
manager.down = Mock()
|
||||
manager.persist = Mock(side_effect=OSError("full disk"))
|
||||
with pytest.raises(OSError):
|
||||
manager.dispatch(dict(action="forget", server_id=one))
|
||||
assert set(manager.profiles) == {one, two}
|
||||
Reference in New Issue
Block a user