73 lines
5.0 KiB
TypeScript
73 lines
5.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { canonicalGreSnapshot, greAddressPath, GreRestoreConflict, planGreCreate, planGreDelete, planGreRestore, parseGreSnapshot } from "./entity-snapshots.js"
|
|
import { fetchGreState, mapGreLive } from "./gre-live.js"
|
|
import { MikrotikClient } from "./mikrotik.js"
|
|
import type { servers } from "../db/schema.js"
|
|
|
|
const server = { id: 1, name: "CHR", enabled: true } as typeof servers.$inferSelect
|
|
const raw = [{ ".id": "*g", name: "own-gre", "remote-address": "192.0.2.1", running: "true", disabled: "false" }]
|
|
const rows = [
|
|
{ ".id": "*1", interface: "own-gre", address: "10.1.0.1/30" },
|
|
{ ".id": "*1", interface: "own-gre", address: "fd00:151::1/64", advertise: "yes" },
|
|
{ ".id": "*2", interface: "own-gre", address: "fd00:151::2/64", disabled: "true", advertise: "no" },
|
|
{ ".id": "*3", interface: "own-gre", address: "fe80::1/64", dynamic: "true" },
|
|
{ ".id": "*4", interface: "public", address: "192.0.2.2/24" },
|
|
]
|
|
const state = mapGreLive(server, raw, rows)
|
|
assert.equal(state.addrs.length, 3)
|
|
assert.equal(state.tunnels[0]?.localInnerIp, "10.1.0.1/30")
|
|
assert.equal(state.snapshot.tunnels[0]?.innerAddresses?.length, 3)
|
|
assert.deepEqual(mapGreLive(server, raw, [...rows].reverse()).snapshot, state.snapshot)
|
|
assert.equal(mapGreLive(server, raw, rows.slice(1)).tunnels[0]?.localInnerIp, "fd00:151::1/64")
|
|
const del = planGreDelete("own-gre", state)
|
|
assert.deepEqual(del.map(op => op.path), ["/ip/address/*1", "/ipv6/address/*1", "/ipv6/address/*2", "/interface/gre/*g"])
|
|
assert(!del.some(op => op.path.includes("*3") || op.path.includes("*4")))
|
|
const noChanges = planGreRestore(state.snapshot, state)
|
|
assert.equal(noChanges.filter(op => op.path.includes("/address")).length, 0)
|
|
const expanded = structuredClone(state.snapshot)
|
|
expanded.tunnels[0]!.innerAddresses![1]!.address = "fd00:0151:0000:0000:0000:0000:0000:0001/64"
|
|
assert.equal(planGreRestore(expanded, state).filter(op => op.path.includes("/address")).length, 0)
|
|
const legacy = canonicalGreSnapshot({ tunnels: [{ name: "own-gre", localInnerIp: "fd00:151::1/64" }] })
|
|
assert.equal(parseGreSnapshot(legacy).tunnels[0]?.innerAddresses, undefined)
|
|
assert.throws(() => planGreRestore(legacy, state), GreRestoreConflict)
|
|
for (const address of ["fd00::1/64", "10.0.0.1/30"]) {
|
|
const t = canonicalGreSnapshot({ tunnels: [{ name: "own-gre", localInnerIp: address }] }).tunnels[0]!
|
|
const create = planGreCreate(t)
|
|
assert.equal(create[1]?.path, greAddressPath(address))
|
|
if (create[1]?.op === "put" && address.includes(":")) assert.equal(create[1].body.advertise, "no")
|
|
const only = { gre: state.gre, addrs: [{ rosId: "*x", interfaceName: "own-gre", address: address.includes(":") ? "10.0.0.1/30" : "fd00::1/64" }] }
|
|
const restore = planGreRestore({ tunnels: [t] }, only)
|
|
assert(restore.some(op => op.op === "delete" && op.path === `${greAddressPath(only.addrs[0]!.address)}/*x`))
|
|
assert(restore.some(op => op.op === "put" && op.path === greAddressPath(address)))
|
|
}
|
|
const flags = structuredClone(state.snapshot)
|
|
flags.tunnels[0]!.innerAddresses![1]!.advertise = false
|
|
assert(planGreRestore(flags, state).some(op => op.op === "patch" && op.path === "/ipv6/address/*1" && op.body.advertise === "no"))
|
|
for (const address of ["fd00::1", "fd00::1/129", "fd00::1/-1", "bad/64", "10.0.0.1/33", "10.0.0.1/", "10.0.0.1/24/x"]) assert.throws(() => greAddressPath(address), GreRestoreConflict)
|
|
const duplicate = structuredClone(state.snapshot)
|
|
duplicate.tunnels[0]!.innerAddresses!.push({ address: "fd00:0151:0:0:0:0:0:1/64", disabled: false })
|
|
assert.throws(() => planGreRestore(duplicate, state), GreRestoreConflict)
|
|
assert.throws(() => planGreRestore(state.snapshot, { ...state, addrs: state.addrs.map(a => ({ ...a, fromPool: "pool6" })) }), GreRestoreConflict)
|
|
assert.throws(() => planGreCreate(canonicalGreSnapshot({ tunnels: [{ name: "pooled", innerAddresses: [{ address: "fd00::1/64", disabled: false, eui64: true }] }] }).tunnels[0]!), GreRestoreConflict)
|
|
const original = MikrotikClient.fromServer
|
|
try {
|
|
const calls: string[] = []
|
|
MikrotikClient.fromServer = () => ({ get: async (path: string) => {
|
|
calls.push(path)
|
|
if (path === "/interface/gre") return raw
|
|
if (path === "/ip/address") return [rows[0]]
|
|
if (path === "/ipv6/address") return rows.slice(1)
|
|
throw new Error("Unexpected path")
|
|
} }) as unknown as MikrotikClient
|
|
assert.equal((await fetchGreState(server)).addrs.length, 3)
|
|
assert.deepEqual(calls.sort(), ["/interface/gre", "/ip/address", "/ipv6/address"])
|
|
MikrotikClient.fromServer = () => ({ get: async (path: string) => {
|
|
if (path === "/ipv6/address") throw new Error("Unauthorized")
|
|
return []
|
|
} }) as unknown as MikrotikClient
|
|
await assert.rejects(fetchGreState(server), /Unauthorized/)
|
|
MikrotikClient.fromServer = () => ({ get: async () => ({}) }) as unknown as MikrotikClient
|
|
await assert.rejects(fetchGreState(server), /неполный каталог/)
|
|
} finally { MikrotikClient.fromServer = original }
|
|
console.log("GRE IPv6: static catalog, disabled/advertise, family IDs, legacy conflict, normalization, create/restore/delete and failed reads passed")
|