105 lines
6.9 KiB
TypeScript
105 lines
6.9 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { MikrotikClient } from "./mikrotik.js"
|
|
import { readOspfStateForServer, readOspfMcpStateForServer, readBfdStateForServer } from "./ospf-state-read.js"
|
|
import type { ServerRow } from "../modules/servers/repository/servers-repository.js"
|
|
import { buildObservedAreaMap } from "./ospf-read-model.js"
|
|
|
|
const server = { id: 1, name: "Router", enabled: true } as ServerRow
|
|
const fromServer = MikrotikClient.fromServer
|
|
let calls = 0, fail = false, duplicate = false, knownArea = false, invalidV6 = false
|
|
let bfdUnavailable = false, v4Unavailable = false
|
|
const read = async () => { calls++; if (fail) throw new Error("fixture unavailable") }
|
|
const client = {
|
|
getOspfNeighbors: async () => { await read(); return [{ ".id": "*n", area: "area1", priority: "0" }] },
|
|
getOspfAreas: async () => { await read(); return [{ ".id": "*a", name: "area1", ...(knownArea ? { "area-id": "0.0.0.0", instance: "*i" } : {}) }] },
|
|
getOspfInterfaceTemplates: async () => { await read(); return [{ ".id": "*t", area: "area1" }] },
|
|
getOspfInstances: async () => { await read(); const row = { ".id": "*i", name: "ospf1" }; return duplicate ? [row, row] : [row] },
|
|
getBfdSessions: async () => { await read(); if (bfdUnavailable) throw new Error("BFD unavailable"); return [{ ".id": "*b", "local-address": "192.0.2.1%ether1", "remote-address": "192.0.2.2", "packets-rx": "0", "desired-tx-interval": "200ms" }] },
|
|
getIpRoutes: async () => { await read(); if (v4Unavailable) throw new Error("IPv4 unavailable"); return [] }, getIpv6Routes: async () => { await read(); return invalidV6 ? [{ "dst-address": "fd00::/64" }] : [] },
|
|
get: async (path: string) => {
|
|
if (path.startsWith("/routing/ospf/lsa?")) {
|
|
assert.doesNotMatch(path, /body/)
|
|
return lsaRows
|
|
}
|
|
assert.match(path, /^\/routing\/ospf\/interface\?\.proplist=/)
|
|
assert.doesNotMatch(path, /auth-key/)
|
|
if (runtimeRows === null) throw new Error("fixture menu unavailable")
|
|
return runtimeRows
|
|
},
|
|
}
|
|
const runtimeRow = { ".id": "*r", interface: "gre-1", state: "ptp", priority: "0", passive: "false", "auth-key": "fixture-secret" }
|
|
let runtimeRows: unknown = [runtimeRow]
|
|
const lsaRow = { ".id": "*lsa", id: "0.0.0.1", age: "invalid", body: "fixture-body" }
|
|
let lsaRows = [lsaRow]
|
|
try {
|
|
MikrotikClient.fromServer = () => client as unknown as MikrotikClient
|
|
const state = await readOspfStateForServer(server)
|
|
assert.equal(calls, 7)
|
|
assert.equal(state.legacy.interfaces[0].cost, 10); assert.equal(state.observed.interfaces[0].cost, null)
|
|
assert.equal(state.legacy.instances[0].version, 2); assert.equal(state.observed.instances[0].version, null)
|
|
assert.equal(state.legacy.neighbors[0].priority, 1); assert.equal(state.observed.neighbors[0].priority, 0)
|
|
assert.equal(state.legacy.bfdSessions[0].state, "Down"); assert.equal(state.observed.bfdSessions[0].state, "Unknown")
|
|
assert.equal(state.legacy.bfdSessions[0].txInterval, 200); assert.equal(state.observed.bfdSessions[0].txInterval, null)
|
|
assert.equal(state.observed.bfdSessions[0].packetsRx, 0)
|
|
assert.equal(state.observed.areas[0].areaId, null); assert.equal(state.legacy.areas[0].areaId, "0.0.0.0")
|
|
assert.equal(state.observed.areas[0].disabled, null)
|
|
assert.equal(state.observed.interfaces[0].areaId, null); assert.equal(state.observed.neighbors[0].areaId, null)
|
|
assert.equal(state.observed.interfaces[0].instance, null); assert.equal(state.observed.neighbors[0].state, "Unknown")
|
|
const bfd = await readBfdStateForServer(server)
|
|
assert.equal(calls, 8); assert.deepEqual(bfd.observed, state.observed.bfdSessions)
|
|
await assert.rejects(readOspfStateForServer({ ...server, enabled: false }), /выключен/)
|
|
await assert.rejects(readBfdStateForServer({ ...server, enabled: false }), /выключен/); assert.equal(calls, 8)
|
|
knownArea = true
|
|
const linked = await readOspfStateForServer(server)
|
|
assert.equal(linked.observed.interfaces[0].instance, "ospf1"); assert.equal(linked.observed.interfaces[0].areaId, "0.0.0.0")
|
|
const ambiguous = buildObservedAreaMap([{ ".id": "*a", name: "same", instance: "i1", "area-id": "0.0.0.0" }, { ".id": "*b", name: "same", instance: "i2", "area-id": "0.0.0.1" }])
|
|
assert.equal(ambiguous.has("same"), false); assert.equal(ambiguous.get("*a"), "0.0.0.0"); assert.equal(ambiguous.get("*b"), "0.0.0.1")
|
|
invalidV6 = true; await assert.rejects(readOspfStateForServer(server), /native ID/); invalidV6 = false
|
|
duplicate = true; await assert.rejects(readOspfStateForServer(server), /идентификаторы/)
|
|
duplicate = false
|
|
const mcpState = await readOspfMcpStateForServer(server)
|
|
assert.equal(Object.hasOwn(mcpState, "lsas"), false)
|
|
const lsa = (await readOspfMcpStateForServer(server, undefined, true)).lsas?.[0]
|
|
assert.equal(lsa?.linkStateId, "0.0.0.1"); assert.equal(lsa?.ageSeconds, null)
|
|
assert.equal(Object.hasOwn(lsa!, "body"), false)
|
|
lsaRows = [lsaRow, lsaRow]
|
|
assert.equal((await readOspfMcpStateForServer(server, undefined, true)).lsas, null)
|
|
assert.equal(mcpState.interfaces[0].id, "*t")
|
|
assert.deepEqual(mcpState.runtimeInterfaces, [{ id: "*r", serverId: 1, interface: "gre-1", address: null, area: null, type: null, state: "ptp", dr: null, bdr: null, cost: null, priority: 0, dynamic: null, disabled: null, passive: false, useBfd: null }])
|
|
runtimeRows = []
|
|
assert.deepEqual((await readOspfMcpStateForServer(server)).runtimeInterfaces, [])
|
|
for (const missing of [null, [runtimeRow, runtimeRow]]) {
|
|
runtimeRows = missing
|
|
const partial = await readOspfMcpStateForServer(server)
|
|
assert.equal(partial.runtimeInterfaces, null)
|
|
assert.equal(partial.neighbors[0].id, "*n")
|
|
}
|
|
runtimeRows = [runtimeRow]
|
|
bfdUnavailable = true
|
|
const partialBfd = await readOspfMcpStateForServer(server)
|
|
assert.equal(partialBfd.bfdSessions, null)
|
|
assert.equal(partialBfd.neighbors[0].id, "*n")
|
|
assert.equal(partialBfd.runtimeInterfaces?.[0].id, "*r")
|
|
assert.deepEqual(partialBfd.routes, [])
|
|
await assert.rejects(readBfdStateForServer(server), /BFD unavailable/)
|
|
await assert.rejects(readOspfStateForServer(server), /BFD unavailable/)
|
|
bfdUnavailable = false
|
|
for (const family of [4, 6]) {
|
|
v4Unavailable = family === 4
|
|
invalidV6 = family === 6
|
|
const partialRoutes = await readOspfMcpStateForServer(server)
|
|
assert.equal(partialRoutes.routes, null)
|
|
assert.equal(partialRoutes.bfdSessions?.[0].id, "*b")
|
|
assert.equal(partialRoutes.neighbors[0].id, "*n")
|
|
}
|
|
invalidV6 = false
|
|
const abort = new AbortController()
|
|
abort.abort()
|
|
const beforeAbort = calls
|
|
await assert.rejects(readOspfMcpStateForServer(server, abort.signal), { name: "AbortError" })
|
|
assert.equal(calls, beforeAbort)
|
|
duplicate = false; fail = true; await assert.rejects(readOspfStateForServer(server), /fixture unavailable/)
|
|
await assert.rejects(readBfdStateForServer(server), /fixture unavailable/)
|
|
} finally { MikrotikClient.fromServer = fromServer }
|
|
console.log("Shared OSPF/BFD state: one poll, legacy parity, nullable observations/known zero, areas, BFD-only source, disabled/duplicate/failure passed")
|