Files
NODEDC_MISSION_CORE/tests/test_b2_dual_sim.py
T

132 lines
5.2 KiB
Python

"""Both-SIM permission is not a promise of priority or automatic failover."""
import hashlib
import json
import pytest
from test_equipment_network import context, full_core_context # noqa: F401
from test_vps_host import ( # noqa: F401
full_context,
reach_sim_restart,
sim_context,
sim_packet,
store,
)
from k1link.device_plugins.arnavi_b2 import profiles
from k1link.device_plugins.arnavi_b2.usb_protocol import Configuration, Setting
from k1link.fleet import equipment_network as network
from k1link.fleet.independent_equipment import EquipmentError
@pytest.mark.parametrize("flags", [0, 1 << 10, 1 << 20, (1 << 10) | (1 << 20)])
def test_both_permission_preserves_apns_passwords_and_other_flags(flags):
before = Configuration(
(
Setting(72, (flags | 2072).to_bytes(4, "big")),
Setting(67, b"private.one"),
Setting(89, b"private.two"),
Setting(90, b"user"),
Setting(91, b"synthetic"),
)
)
after = profiles.prepare(before, {"connectivity": {"strategy": "both"}})
assert after.integer(72, 4) == 2072
assert after.records[1:] == before.records[1:]
with pytest.raises(ValueError):
profiles.Policy(connectivity={"strategy": "both", "apn": "conflict"})
def both_context(source):
command, body, before, target, wire, now = source
# Existing exclusive target becomes the predecessor; remove its prohibition.
body.pop("sim_slot")
body.update(
sim_policy="both",
expected_sha256=hashlib.sha256(target.encode()).hexdigest(),
target_configuration=before.encode().hex(),
)
return command, body, target, before, wire, now
@pytest.mark.parametrize("slot", [1, 2])
def test_both_requires_readback_then_fresh_gprs_on_new_session(sim_context, slot): # noqa: F811
command, body, before, target, wire, now = both_context(sim_context)
reach_sim_restart((command, body, before, target, wire, now))
assert command.take(body["imei"], "live")[-3:-1] == b"\x01\x07"
now[0] += 10
command.receive(body["imei"], sim_packet(now[0], slot=slot), "live")
assert command.get(body["id"])["state"] == "dispatched"
command.session_started[body["imei"], "new"] = now[0] - 1700000000
now[0] += 1
command.receive(body["imei"], sim_packet(now[0], slot=slot, gsm=3), "new")
assert command.get(body["id"])["state"] == "dispatched"
command.receive(body["imei"], sim_packet(now[0], slot=slot), "new")
result = command.get(body["id"])
assert result["state"] == "sim_selected" and result["active_sim"] == slot
assert result["sim_policy"] == "both"
@pytest.mark.parametrize("bad", ["slot", "flags", "policy"])
def test_vps_rejects_contradictory_both_request(sim_context, bad): # noqa: F811
command, body, before, _, _, _ = both_context(sim_context)
if bad == "slot":
body["sim_slot"] = 1
elif bad == "flags":
body["target_configuration"] = before.encode().hex()
else:
body["sim_policy"] = "preferred"
with pytest.raises(ValueError):
command.submit(body)
@pytest.mark.parametrize("slot", [1, 2])
def test_core_both_receipt_accepts_either_real_slot_and_no_configuration_only_success(
full_core_context, # noqa: F811
monkeypatch,
slot,
):
equipment, hosts, row, config = full_core_context
row["settings"]["connectivity"] = {"strategy": "both"}
with equipment.db() as db:
db.execute("UPDATE instances SET body=? WHERE id=?", (json.dumps(row), row["id"]))
for host in hosts.rows():
host["status"]["version"] = "0.1.37"
hosts.save(host)
sent = []
def send(host, method, path, body=None):
sent.append(body)
return {"id": body["id"], "kind": body["kind"], "state": "queued", "durable": True}
monkeypatch.setattr(network, "request", send)
op = network.start(hosts, row["id"], 1, select_sim=True)["network_operation"]
assert op["sim_policy"] == sent[0]["sim_policy"] == "both"
assert "sim_slot" not in sent[0]
receipt = {"id": op["id"], "kind": "select-sim", "state": "verified"}
with pytest.raises(ValueError, match="not SIM selection"):
network.record(hosts, row["id"], op["id"], receipt)
for invalid in (0, 3, True):
with pytest.raises(ValueError, match="mismatch"):
network.record(
hosts,
row["id"],
op["id"],
{**receipt, "state": "sim_selected", "active_sim": invalid},
)
network.record(
hosts, row["id"], op["id"], {**receipt, "state": "sim_selected", "active_sim": slot}
)
assert equipment.listing()[0]["network_operation"]["active_sim"] == slot
def test_core_both_is_explicit_and_requires_compatible_vps(full_core_context): # noqa: F811
equipment, hosts, row, _ = full_core_context
row["settings"]["connectivity"] = {"strategy": "both"}
assert network.plan(equipment, row, full=True)["kind"] == "configure-all"
assert network.plan(equipment, row, full=True, select_sim=True)["kind"] == "select-sim"
with equipment.db() as db:
db.execute("UPDATE instances SET body=? WHERE id=?", (json.dumps(row), row["id"]))
with pytest.raises(EquipmentError, match="обеих SIM"):
network.start(hosts, row["id"], 1, select_sim=True)