Files
MikrotikManager/backend/src/services/bgp-export-model.test.ts
T

33 lines
3.3 KiB
TypeScript

import assert from "node:assert/strict"
import { bgpExportConnection, bgpExportSources, bgpRedistributionMode, bgpTemplateReferences, nativeBgpExportValue, nativeBgpRows } from "./bgp-export-model.js"
assert.equal(bgpRedistributionMode({ ret: "" }), "disabled")
assert.equal(bgpRedistributionMode([]), null)
assert.deepEqual(bgpTemplateReferences([]), [])
assert.deepEqual(bgpTemplateReferences({ ret: "default" }), ["default"])
assert.deepEqual(bgpTemplateReferences({ ret: ["parent-a", "parent-b"] }), ["parent-a", "parent-b"])
for (const value of [{}, { ret: null }, { ret: "a,,b" }, { ret: "a,a" }, [{}]]) assert.throws(() => bgpTemplateReferences(value))
assert.equal(bgpRedistributionMode({ ret: "static" }), "static")
assert.equal(bgpRedistributionMode({ ret: "connected" }), "connected")
for (const value of [{}, { ret: null }, { ret: false }, { ret: "connected,static" }, { ret: "ospf" }]) assert.throws(() => bgpRedistributionMode(value))
assert.equal(nativeBgpExportValue("disabled"), "")
for (const id of ["all", "..", "*bad/id", "*bad?x", "*", "0"]) assert.throws(() => nativeBgpRows([{ ".id": id }]))
const connected = { ".id": "*c", "routing-table": "lab", "dst-address": "192.0.2.0/30", connect: "true", dynamic: "true", active: "true", "debug.ptr": "ignored" }
const configured = { ".id": "*s", "routing-table": "lab", "dst-address": "198.18.0.1/32", static: "true", dynamic: "false" }
const bgp = { ".id": "*b", "routing-table": "lab", bgp: "true", dynamic: "true" }
const outside = { ".id": "*x", "routing-table": "main", "dst-address": "invalid" }
let sources = bgpExportSources([outside, bgp, configured, connected], "lab", ["connected"], 4)
assert.equal(sources.length, 1); assert.equal(sources[0].active, true); assert.equal(sources[0].kind, "connected"); assert.equal("debug.ptr" in sources[0].proof, false)
sources = bgpExportSources([connected, configured], "lab", ["static"], 4)
assert.equal(sources[0].active, null); assert.equal(sources[0].kind, "static")
assert.equal(bgpExportSources([connected, configured], "lab", ["connected", "static"], 4).length, 2)
assert.equal(bgpExportSources([connected], "lab", ["disabled"], 4).length, 0)
for (const payload of [[connected, connected], [{ ...connected, dynamic: "false" }], [{ ...connected, connect: undefined }], [{ ...connected, "dst-address": "192.0.2.0/33" }], [{ ...connected, "routing-table": undefined }]]) assert.throws(() => bgpExportSources(payload, "lab", ["connected"], 4))
assert.equal(bgpExportSources([{ ...connected, "dst-address": "fd00::/64" }], "lab", ["connected"], 6).length, 1)
const connection = { ".id": "*peer", name: "lab-peer", dynamic: "false", disabled: "false", instance: "lab", vrf: "lab", "routing-table": "lab", inactive: "true" }
assert.equal(bgpExportConnection(connection, "static")["output.redistribute"], "static")
assert.equal("inactive" in bgpExportConnection(connection, "static"), false)
assert.equal(bgpExportConnection({ ...connection, disabled: "true" }, "static").disabled, "true")
for (const change of [{ disabled: undefined }, { vrf: undefined }, { instance: undefined }, { dynamic: "true" }, { dynamic: undefined }]) assert.throws(() => bgpExportConnection({ ...connection, ...change }, "static"))
console.log("BGP export sources: effective empty/connected/static values, explicit VRF/table, native origin provenance, v4/v6 prefixes, unknown and duplicate rejection, stable config versus session status")