Files
MikrotikManager/backend/src/routes/ospf-catalog.test.ts
T

74 lines
7.0 KiB
TypeScript

import assert from "node:assert/strict"
import Fastify from "fastify"
import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zod"
import { createOspfRoutes, parseBfdSessions, parseBfdCatalogSessions, parseCatalogNeighbors, parseCatalogInterfaces, parseCatalogInstances, parseCatalogRoutes } from "./ospf.js"
import { emptyOspfCatalog, pollOspfCatalog, readOspfCatalogRaw, type OspfCatalogCollections } from "../services/ospf-catalog-poll.js"
import { servers } from "../db/schema.js"
const devices = [{ id: 1, name: "Первый" }, { id: 2, name: "Второй" }]
const row = { id: "*1", serverId: 1 }
const collections = { ...emptyOspfCatalog(), neighbors: [row] } as OspfCatalogCollections
const read = async (device: { id: number }) => { if (device.id === 2) throw new Error("Ошибка BFD тест"); return collections }
const mixed = await pollOspfCatalog(devices, read)
assert.deepEqual(mixed, { ...collections, failures: [{ serverId: 2, serverName: "Второй", error: "Ошибка BFD тест" }] })
assert.deepEqual(await pollOspfCatalog(devices, async () => emptyOspfCatalog()), { ...emptyOspfCatalog(), failures: [] })
assert.equal((await pollOspfCatalog(devices, async () => { throw new Error("Отказ") })).failures.length, 2)
for (const rows of [[{ ...row, serverId: 2 }], [row, row], [{ ...row, id: " " }]]) {
const result = await pollOspfCatalog([devices[0]], async () => ({ ...emptyOspfCatalog(), routes: rows } as OspfCatalogCollections))
assert.equal(result.routes.length, 0)
assert.equal(result.failures.length, 1)
}
const client = { getOspfNeighbors: async () => [], getOspfAreas: async () => [], getOspfInterfaceTemplates: async () => [], getOspfInstances: async () => [], getBfdSessions: async () => [], getIpRoutes: async () => [] }
assert.equal((await readOspfCatalogRaw(client)).bfdSessions.length, 0)
for (const section of ["getBfdSessions", "getIpRoutes"] as const) await assert.rejects(() => readOspfCatalogRaw({ ...client, [section]: async () => { throw new Error("Отказ " + section) } }), /Отказ/)
const app = Fastify()
app.setValidatorCompiler(validatorCompiler)
app.setSerializerCompiler(serializerCompiler)
await app.register(createOspfRoutes({ listEnabledServers: async () => devices as typeof servers.$inferSelect[], readCatalog: read }), { prefix: "/api" })
const response = await app.inject({ method: "GET", url: "/api/ospf/catalog" })
assert.equal(response.statusCode, 200)
assert.deepEqual(response.json(), mixed)
await app.close()
console.log("OSPF catalog: partial/empty/failed/foreign/duplicate and strict BFD/routes, HTTP passed")
const bfdServer = { id: 1, name: "Первый", host: "192.0.2.1", site: "", country: "RU" } as typeof servers.$inferSelect
const rawBfd = (state?: string) => [{ ".id": "*1", "local-address": "192.0.2.1%ether1", "remote-address": "192.0.2.2", ...(state === undefined ? {} : { state }) }]
for (const state of [undefined, "", " "]) assert.equal(parseBfdSessions(bfdServer, rawBfd(state), true)[0].state, "Unknown")
assert.equal(parseBfdSessions(bfdServer, rawBfd("down"), true)[0].state, "Down")
assert.equal(parseBfdSessions(bfdServer, rawBfd("up"), true)[0].state, "Up")
assert.equal(parseBfdSessions(bfdServer, rawBfd(), false)[0].state, "Down")
console.log("OSPF strict BFD missing/blank/known states and legacy compatibility passed")
const missingBfd = parseBfdCatalogSessions(bfdServer, rawBfd("up"))[0]
for (const field of ["multiplier", "txInterval", "rxInterval", "holdTime", "packetsRx", "packetsTx", "stateChanges"] as const) assert.equal(missingBfd[field], null)
const zeroBfd = parseBfdCatalogSessions(bfdServer, [{ ...rawBfd("up")[0], multiplier: "0", "actual-tx-interval": "0ms", "required-min-rx": "1us", "hold-time": "1s200ms", "packets-rx": "0", "packets-tx": "9", "state-changes": "0" }])[0]
assert.equal(zeroBfd.multiplier, 0); assert.equal(zeroBfd.txInterval, 0); assert.equal(zeroBfd.rxInterval, 0.001); assert.equal(zeroBfd.holdTime, 1200); assert.equal(zeroBfd.packetsRx, 0); assert.equal(zeroBfd.packetsTx, 9); assert.equal(zeroBfd.stateChanges, 0)
assert.equal(parseBfdCatalogSessions(bfdServer, [{ ...rawBfd("up")[0], "desired-tx-interval": "200ms" }])[0].txInterval, null)
const neighborSource = { ".id": "*1", address: "192.0.2.2", "router-id": "192.0.2.2", instance: "default", area: "backbone", interface: "ether1", state: "full" }
assert.equal(parseCatalogNeighbors(bfdServer, [neighborSource], new Map())[0].priority, null)
const neighborZero = parseCatalogNeighbors(bfdServer, [{ ...neighborSource, priority: "0", "state-changes": "0" }], new Map())[0]
assert.equal(neighborZero.priority, 0); assert.equal(neighborZero.stateChanges, 0)
for (const priority of ["junk", "1junk", "256", "-1"]) assert.equal(parseCatalogNeighbors(bfdServer, [{ ...neighborSource, priority }], new Map())[0].priority, null)
console.log("OSPF strict values: missing/zero/microseconds/compound, no desired-as-actual and priority bounds passed")
const rawIface = { ".id": "*1", area: "backbone", interfaces: "ether1", disabled: "false", inactive: "false" }
const missingIface = parseCatalogInterfaces(bfdServer, [rawIface], [], [], new Map())[0]
for (const field of ["cost", "priority", "type", "helloInterval", "deadInterval", "useBfd"] as const) assert.equal(missingIface[field], null)
const zeroIface = parseCatalogInterfaces(bfdServer, [{ ...rawIface, cost: "0", priority: "0", type: "ptp", "hello-interval": "0s", "dead-interval": "1s500ms", "use-bfd": "no" }], [], [], new Map())[0]
assert.equal(zeroIface.cost, 0); assert.equal(zeroIface.priority, 0); assert.equal(zeroIface.helloInterval, 0); assert.equal(zeroIface.deadInterval, 1.5); assert.equal(zeroIface.useBfd, false)
assert.equal(parseCatalogInterfaces(bfdServer, [{ ...rawIface, disabled: "junk", cost: "10x" }], [], [], new Map())[0].disabled, null)
const rawInstance = { ".id": "*1", name: "default", disabled: "false", inactive: "false" }
const missingInstance = parseCatalogInstances(bfdServer, [rawInstance])[0]
assert.equal(missingInstance.version, null); assert.equal(missingInstance.routerId, null)
assert.equal(parseCatalogInstances(bfdServer, [{ ...rawInstance, version: "3", "router-id": "main" }])[0].version, 3)
const rawRoute = { ".id": "*1", "dst-address": "192.0.2.0/24", ospf: "true", distance: "110" }
assert.equal(parseCatalogRoutes(bfdServer, [rawRoute])[0].cost, null)
assert.equal(parseCatalogRoutes(bfdServer, [{ ...rawRoute, "ospf-metric": "0" }])[0].cost, 0)
assert.equal(parseCatalogRoutes(bfdServer, [{ ...rawRoute, "ospf-metric": "42" }])[0].cost, 42)
assert.equal(parseCatalogRoutes(bfdServer, [{ ...rawRoute, "ospf-metric": "42x" }])[0].cost, null)
console.log("OSPF strict interfaces/instances/routes: absent/zero/fractional/flags, no distance-as-cost passed")
assert.equal(parseBfdCatalogSessions(bfdServer, rawBfd("up"))[0].multihop, null)
assert.equal(parseBfdCatalogSessions(bfdServer, [{ ...rawBfd("up")[0], multihop: "false" }])[0].multihop, false)
assert.equal(parseCatalogRoutes(bfdServer, [rawRoute])[0].type, "Unknown")
assert.equal(parseCatalogRoutes(bfdServer, [{ ...rawRoute, "ospf-type": "intra-area" }])[0].type, "O")