99 lines
7.1 KiB
TypeScript
99 lines
7.1 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { analyzeEcmpPreflight, duplicateEcmpAdjacency, ecmpAdjacency, preflightObservation, preflightPaths, type PreflightObservation, type PreflightSource } from "./ecmp-preflight.js"
|
|
|
|
function fixture(): Record<PreflightSource, PreflightObservation> {
|
|
const result = Object.fromEntries(Object.keys(preflightPaths("ipv4")).map(key => [key, { rows: [], error: null }])) as Record<PreflightSource, PreflightObservation>
|
|
result.routes.rows = [1, 2].map(index => ({ ".id": `*${index}`, "dst-address": "192.0.2.0/24", "routing-table": "main", gateway: `192.0.2.${index}`, "immediate-gw": `192.0.2.${index}%gre${index}`, disabled: "false", active: index === 1 ? "true" : "false", distance: String(index * 10) }))
|
|
result.settings.rows = [{ "rp-filter": "no", "ipv4-multipath-hash-policy": "l3", "multipath-hash-policy": "l4" }]
|
|
result.tables.rows = [{ ".id": "*main", name: "main", fib: "true" }]
|
|
result.connectionTracking.rows = [{ enabled: "auto" }]
|
|
return result
|
|
}
|
|
test("preflight observes unequal/inactive candidates without claiming packet/path acceptance", () => {
|
|
const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], fixture())
|
|
assert.equal(result.configurationObserved, true); assert.equal(result.routeSummary[1].active, false)
|
|
assert.equal(result.decision, "review-required"); assert.equal(result.independentPaths, "not-proven"); assert.equal(result.networkValidation, "not-performed")
|
|
assert.equal(result.hashPolicy, "l3"); assert.deepEqual(result.findings, [])
|
|
})
|
|
test("preflight flags missing, ambiguous or inactive FIB without accepting unrelated tables", () => {
|
|
for (const tables of [
|
|
[],
|
|
[{ ".id": "*main", name: "main" }],
|
|
[{ ".id": "*main", name: "main", fib: "" }],
|
|
[{ ".id": "*main", name: "main", fib: "false" }],
|
|
[{ ".id": "*main", name: "main", fib: "true", disabled: "true" }],
|
|
[{ ".id": "*main", name: "main", fib: "true", invalid: "true" }],
|
|
[{ ".id": "*main", name: "main", fib: "true" }, { ".id": "*alias", name: "main", fib: "true" }],
|
|
]) {
|
|
const sources = fixture()
|
|
sources.tables.rows = [...tables, { ".id": "*other", name: "other", fib: "true" }]
|
|
const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources)
|
|
assert.equal(result.configurationObserved, false)
|
|
assert.deepEqual(result.findings.find(item => item.code === "FIB_TABLE_UNCONFIRMED")?.ids, ["*1", "*2"])
|
|
}
|
|
})
|
|
test("NAT, FastTrack, marks, notrack, strict RPF and shared egress require explicit review", () => {
|
|
const sources = fixture()
|
|
sources.filter.rows = [{ ".id": "*fast", action: "fasttrack-connection", disabled: "false" }, { ".id": "*disabled", action: "fasttrack-connection", disabled: "true" }]
|
|
sources.nat.rows = [{ ".id": "*nat", action: "masquerade", disabled: "false" }]
|
|
sources.mangle.rows = [{ ".id": "*mark", action: "mark-routing", disabled: "false" }]
|
|
sources.raw.rows = [{ ".id": "*notrack", action: "notrack", disabled: "false" }]
|
|
sources.settings.rows[0]["rp-filter"] = "strict"
|
|
sources.connectionTracking.rows[0].enabled = "no"
|
|
sources.routes.rows[1]["immediate-gw"] = "192.0.2.2%gre1"
|
|
const result = analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources)
|
|
for (const code of ["NAT_AFFINITY_REVIEW", "FASTTRACK_REVIEW", "POLICY_ROUTING_REVIEW", "NOTRACK_REVIEW", "STRICT_RPF_REVIEW", "SHARED_EGRESS_INTERFACE", "CONNECTION_TRACKING_DISABLED"]) assert.ok(result.findings.some(item => item.code === code), code)
|
|
assert.deepEqual(result.findings.find(item => item.code === "FASTTRACK_REVIEW")?.ids, ["*fast"])
|
|
})
|
|
test("failed/malformed/omitted evidence is unknown, never an empty healthy catalog", () => {
|
|
for (const raw of [null, {}, [null], [{ ".id": "*1" }, { ".id": "*1" }], [{ name: "missing-id" }]]) assert.ok(preflightObservation(raw).error)
|
|
assert.ok(preflightObservation([], true).error); assert.ok(preflightObservation([{}, {}], true).error)
|
|
const sources = fixture()
|
|
sources.nat = { rows: [], error: "unavailable" }; delete sources.settings.rows[0]["ipv4-multipath-hash-policy"]
|
|
sources.raw.rows = [{ ".id": "*raw", action: "notrack" }]
|
|
const result = analyzeEcmpPreflight("ipv4", ["*1", "*missing"], sources)
|
|
assert.equal(result.configurationObserved, false)
|
|
for (const code of ["OBSERVATION_UNAVAILABLE", "ROUTE_NOT_OBSERVED", "HASH_POLICY_UNKNOWN", "RULE_ENABLEMENT_UNKNOWN", "NOTRACK_REVIEW"]) assert.ok(result.findings.some(item => item.code === code), code)
|
|
assert.equal(result.sources.nat.observed, false)
|
|
const absentGateways = fixture()
|
|
absentGateways.routes.rows.forEach(row => { delete row.gateway })
|
|
assert.ok(!analyzeEcmpPreflight("ipv4", ["*1", "*2"], absentGateways).findings.some(item => item.code === "DUPLICATE_GATEWAY"), "Two unknown gateways do not prove duplication")
|
|
const malformedSettings = fixture()
|
|
malformedSettings.settings.rows = [{ "ipv4-multipath-hash-policy": { toString: null }, "rp-filter": 0 }]
|
|
assert.equal(analyzeEcmpPreflight("ipv4", ["*1", "*2"], malformedSettings).configurationObserved, false)
|
|
})
|
|
test("IPv6 uses IPv6 paths/hash policy and explicitly has no IPv4 RPF setting", () => {
|
|
assert.equal(preflightPaths("ipv6").nat, "/ipv6/firewall/nat")
|
|
assert.equal(preflightPaths("ipv6").connectionTracking, "/ip/firewall/connection/tracking")
|
|
const result = analyzeEcmpPreflight("ipv6", ["*1", "*2"], fixture())
|
|
assert.deepEqual(result.rpf, { supported: false, mode: null }); assert.equal(result.hashPolicy, "l4")
|
|
assert.ok(!result.findings.some(item => item.code.startsWith("RPF")))
|
|
})
|
|
test("ECMP identifies adjacency by canonical address and egress link", () => {
|
|
const duplicate = (first: string, second: string) => duplicateEcmpAdjacency([ecmpAdjacency(first), ecmpAdjacency(second)])
|
|
assert.ok(duplicate("fe80::1%gre1@main", "fe80:0:0:0:0:0:0:1%gre1@tenant"))
|
|
assert.ok(!duplicate("fe80::1%gre1", "fe80::1%gre2"))
|
|
assert.ok(!duplicate("192.0.2.1%gre1", "192.0.2.2%gre1"))
|
|
assert.ok(duplicate("192.0.2.1", "192.0.2.1%gre1"))
|
|
assert.equal(ecmpAdjacency("fe80::1"), null)
|
|
assert.equal(ecmpAdjacency("192.0.2.1%gre1,192.0.2.2%gre2"), null)
|
|
assert.equal(ecmpAdjacency("gre1"), null)
|
|
assert.deepEqual(ecmpAdjacency("gre1", new Set(["gre1"])), { address: null, interface: "gre1" })
|
|
const sources = fixture()
|
|
sources.routes.rows[1]["immediate-gw"] = sources.routes.rows[0]["immediate-gw"]
|
|
assert.ok(analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources).findings.some(item => item.code === "DUPLICATE_IMMEDIATE_GATEWAY"))
|
|
})
|
|
test("interface-only ECMP next-hop requires a unique observed name", () => {
|
|
const sources = fixture()
|
|
sources.routes.rows[0]["immediate-gw"] = "gre2"
|
|
const analyze = (rows: Record<string, unknown>[]) => analyzeEcmpPreflight("ipv4", ["*1", "*2"], sources, preflightObservation(rows))
|
|
const known = analyze([{ ".id": "*gre", name: "gre2" }])
|
|
assert.equal(known.configurationObserved, true)
|
|
assert.ok(known.findings.some(item => item.code === "SHARED_EGRESS_INTERFACE"))
|
|
const ambiguous = analyze([{ ".id": "*gre", name: "gre2" }, { ".id": "*other", name: "gre2" }])
|
|
assert.equal(ambiguous.configurationObserved, false)
|
|
assert.ok(ambiguous.findings.some(item => item.code === "EGRESS_INTERFACE_UNKNOWN"))
|
|
assert.equal(analyze([{ name: "gre2" }]).sources.interfaces.observed, false)
|
|
})
|