99 lines
3.8 KiB
TypeScript
99 lines
3.8 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { hasManagedCommentPrefix, isOwnedRecursiveComment, managedRecursiveComment, stripManagedRecursiveComment } from "../managed-markers.js"
|
|
import {
|
|
planBgpInApply,
|
|
planRecursiveApply,
|
|
unmanagedRouteIds,
|
|
} from "./config-apply-plan.js"
|
|
import {
|
|
canonicalFilterRules,
|
|
fingerprintPayload,
|
|
} from "./config-revisions.js"
|
|
import { mapRosManagedRoutes } from "./config-apply-plan.js"
|
|
|
|
{
|
|
const fp1 = fingerprintPayload(canonicalFilterRules([
|
|
{ community: "65001:100", action: "route", gateway: "10.0.0.1", gatewayTunnelId: "gre1", description: "a" },
|
|
]))
|
|
const fp2 = fingerprintPayload(canonicalFilterRules([
|
|
{ community: "65001:100", action: "route", gateway: "10.0.0.1", gatewayTunnelId: "gre1", description: "a" },
|
|
]))
|
|
const fp3 = fingerprintPayload(canonicalFilterRules([
|
|
{ community: "65001:100", action: "route", gateway: "10.0.0.2", gatewayTunnelId: "gre1", description: "a" },
|
|
]))
|
|
assert.equal(fp1, fp2)
|
|
assert.notEqual(fp1, fp3)
|
|
}
|
|
|
|
{
|
|
const existing = [
|
|
{ ".id": "*1", chain: "bgp-in", comment: "MikrotikManager: msk", rule: "if (true) { accept; }" },
|
|
{ ".id": "*2", chain: "bgp-in", comment: "legacy", rule: "if (bgp-communities includes 1:1) { reject; }" },
|
|
{ ".id": "*3", chain: "bgp-out", comment: "MikrotikManager: other", rule: "if (bgp-communities includes 1:1) { accept; }" },
|
|
]
|
|
const patch = planBgpInApply(existing, 3)
|
|
assert.equal(patch.action, "patch")
|
|
assert.equal(patch.managedId, "*1")
|
|
assert.deepEqual(patch.conflictIds, ["*2"])
|
|
|
|
const create = planBgpInApply(existing.filter((r) => r[".id"] !== "*1"), 1)
|
|
assert.equal(create.action, "create")
|
|
assert.equal(create.managedId, undefined)
|
|
|
|
const del = planBgpInApply(existing, 0)
|
|
assert.equal(del.action, "delete")
|
|
assert.equal(del.managedId, "*1")
|
|
|
|
const noop = planBgpInApply([], 0)
|
|
assert.equal(noop.action, "noop")
|
|
assert.equal(planBgpInApply([{ ".id": "*case", chain: "BGP-IN", comment: "MikrotikManager: other", rule: "bgp-communities" }], 1).action, "create")
|
|
assert.deepEqual(planBgpInApply([{ ".id": "*case", chain: "BGP-IN", rule: "bgp-communities" }], 1).conflictIds, [])
|
|
}
|
|
|
|
{
|
|
const routes = [
|
|
{ ".id": "*10", comment: "MikrotikManager:recursive via de", static: "true", "dst-address": "8.8.8.8/32", gateway: "1.1.1.1" },
|
|
{ ".id": "*11", comment: "user static", static: "true", "dst-address": "1.1.1.1/32", gateway: "9.9.9.9" },
|
|
{ ".id": "*12", comment: "recursive: old", static: "true", "dst-address": "9.9.9.9/32", gateway: "1.1.1.1" },
|
|
]
|
|
const plan = planRecursiveApply(routes)
|
|
assert.deepEqual(plan.deleteIds, ["*10", "*12"])
|
|
assert.deepEqual(unmanagedRouteIds(routes), ["*11"])
|
|
}
|
|
|
|
{
|
|
assert.equal(stripManagedRecursiveComment("MikrotikManager:recursive via de"), "via de")
|
|
assert.equal(stripManagedRecursiveComment("recursive: old"), "old")
|
|
assert.equal(managedRecursiveComment("MikrotikManager:recursive via de"), "MikrotikManager:recursive via de")
|
|
assert.equal(isOwnedRecursiveComment("MikrotikManager:recursive via de"), true)
|
|
assert.equal(isOwnedRecursiveComment("recursive: x"), true)
|
|
assert.equal(isOwnedRecursiveComment("user static"), false)
|
|
assert.equal(hasManagedCommentPrefix("MikrotikManager: msk"), true)
|
|
}
|
|
|
|
{
|
|
const mapped = mapRosManagedRoutes([
|
|
{
|
|
".id": "*1",
|
|
static: "true",
|
|
"dst-address": "10.9.9.2/32",
|
|
gateway: "1.2.3.4",
|
|
comment: "MikrotikManager:recursive hop-de",
|
|
distance: "1",
|
|
},
|
|
{
|
|
".id": "*2",
|
|
static: "true",
|
|
"dst-address": "10.9.9.3/32",
|
|
gateway: "1.2.3.4",
|
|
comment: "not ours",
|
|
distance: "1",
|
|
},
|
|
])
|
|
assert.equal(mapped.length, 1)
|
|
assert.equal(mapped[0]?.dstAddress, "10.9.9.2/32")
|
|
assert.equal(mapped[0]?.comment, "hop-de")
|
|
}
|
|
|
|
console.log("config-apply-plan.test.ts: ok")
|