v0.41.0
This commit is contained in:
1 parent
27b640a0df
commit
aa27866531
46 files changed
+3368
-189
No files matched your search
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
// Checks that the panel's consistent-hash simulator (src/admin/web/vhosts/balancing.js)
|
||||
// ranks backends exactly like the proxy (src/balance/model.rs). The reference
|
||||
// vectors come from the Rust test `rendezvous_reference_vectors`.
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
const crypto = require("node:crypto");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const root = path.resolve(__dirname, "../..");
|
||||
const source = fs.readFileSync(path.join(root, "src/admin/web/vhosts/balancing.js"), "utf8");
|
||||
const sandbox = { window: {}, TextEncoder, BigInt, Math, Number, Uint8Array, Uint32Array, DataView };
|
||||
vm.runInNewContext(source, sandbox, { filename: "balancing.js" });
|
||||
const core = sandbox.window.rustProxyBalancingCore;
|
||||
assert.equal(typeof core?.score, "function");
|
||||
|
||||
for (const input of ["", "abc", "a".repeat(55), "a".repeat(56), "a".repeat(64), "zażółć|/music/ü.mp3|s1", "x".repeat(1000)]) {
|
||||
const expected = crypto.createHash("sha256").update(input, "utf8").digest("hex");
|
||||
const actual = Buffer.from(core.sha256(new TextEncoder().encode(input))).toString("hex");
|
||||
assert.equal(actual, expected, `sha256 mismatch for length ${input.length}`);
|
||||
}
|
||||
|
||||
const members = [{ id: "s1", weight: 1 }, { id: "s2", weight: 1 }, { id: "s3", weight: 2 }];
|
||||
const rendered = ["/a.mp3", "/music/b.mp3", "/c", "tenant-42", "192.0.2.10"]
|
||||
.map(key => `${key}=${core.rank("files", key, members).map(m => m.id).join(",")}`).join(";");
|
||||
const expected = fs.readFileSync(path.join(root, "tests/fixtures/balance_rendezvous_vectors.txt"), "utf8").trim();
|
||||
assert.equal(rendered, expected, "simulator ranking differs from the proxy");
|
||||
|
||||
const key = (settings, line) => core.hashKey(settings, line);
|
||||
assert.equal(key({ source: "path" }, "/music/Album/track.MP3?user=42&x"), "/music/Album/track.MP3");
|
||||
assert.equal(key({ source: "path", ignore_case: true }, "/music/Album/track.MP3"), "/music/album/track.mp3");
|
||||
assert.equal(key({ source: "path", path_depth: 1 }, "/music/Album/track.MP3"), "/music");
|
||||
assert.equal(key({ source: "path", path_depth: 2 }, "https://cdn.example.com/music/Album/track.MP3"), "/music/Album");
|
||||
assert.equal(key({ source: "uri" }, "/music/a.mp3?user=42&x"), "/music/a.mp3?user=42&x");
|
||||
assert.equal(key({ source: "host" }, "https://Files.Example.com:8443/x"), "files.example.com");
|
||||
assert.equal(key({ source: "host" }, "https://[2001:db8::1]:443/x"), "2001:db8::1");
|
||||
assert.equal(key({ source: "query", name: "user" }, "/x?user=42&x"), "42");
|
||||
assert.equal(key({ source: "query", name: "x" }, "/x?user=42&x"), null);
|
||||
assert.equal(key({ source: "query", name: "user" }, "/x"), null);
|
||||
assert.equal(key({ source: "cookie", name: "sid" }, "ABC"), "ABC");
|
||||
assert.equal(key({ source: "ip" }, " 203.0.113.9 "), "203.0.113.9");
|
||||
|
||||
// Removing a backend moves only its own keys.
|
||||
const pool = [
|
||||
{ id: "a", weight: 1, enabled: true, drain: false, backup: false, pool_eligible: false },
|
||||
{ id: "b", weight: 1, enabled: true, drain: false, backup: false, pool_eligible: false },
|
||||
{ id: "c", weight: 1, enabled: true, drain: false, backup: false, pool_eligible: false },
|
||||
];
|
||||
const samples = Array.from({ length: 300 }, (_, n) => `/f/${n}`);
|
||||
assert.equal(core.simulate({ poolId: "v", settings: { source: "path" }, members: pool, samples }).filter(r => r.member).length, 0,
|
||||
"location-only members are not used by the vhost pool");
|
||||
const rows = core.simulate({ poolId: "v", settings: { source: "path" }, members: pool, explicit: true, samples, down: new Set(["b"]) });
|
||||
for (const row of rows) {
|
||||
if (row.previous === "b") assert.notEqual(row.member, "b");
|
||||
else assert.equal(row.member, row.previous);
|
||||
}
|
||||
assert.ok(rows.some(row => row.previous === "b"));
|
||||
console.log("balance simulator: ok");
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
// Checks that the panel routing tester (src/admin/web/vhosts/routing.js) selects the
|
||||
// same location as the proxy for tests/fixtures/routing_conditions_cases.json, which
|
||||
// the Rust test `fixture_cases_select_the_expected_location` also uses.
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const root = path.resolve(__dirname, "../..");
|
||||
const sandbox = { window: {}, BigInt, RegExp, Math, Number, String, Array, Boolean };
|
||||
vm.runInNewContext(fs.readFileSync(path.join(root, "src/admin/web/vhosts/routing.js"), "utf8"), sandbox, { filename: "routing.js" });
|
||||
const core = sandbox.window.rustProxyRoutingCore;
|
||||
assert.equal(typeof core?.evaluate, "function");
|
||||
|
||||
const fixture = JSON.parse(fs.readFileSync(path.join(root, "tests/fixtures/routing_conditions_cases.json"), "utf8"));
|
||||
for (const item of fixture.cases) {
|
||||
const request = {
|
||||
method: item.method || "GET", path: item.path, query: item.query ?? null, host: item.host || "site.test",
|
||||
client_ip: item.client_ip || "203.0.113.5", country: item.country || null,
|
||||
headers: Object.entries(item.headers || {}),
|
||||
};
|
||||
const { winner } = core.evaluate(fixture.vhost.routes, request);
|
||||
assert.equal(winner ? winner.route.id : "main", item.expect, item.name);
|
||||
}
|
||||
|
||||
const cidr = core.cidrContains;
|
||||
assert.ok(cidr("10.0.0.0/8", "10.200.1.1"));
|
||||
assert.ok(!cidr("10.0.0.0/8", "11.0.0.1"));
|
||||
assert.ok(cidr("2001:db8::/32", "2001:db8:1::5"));
|
||||
assert.ok(!cidr("2001:db8::/32", "2001:db9::1"));
|
||||
assert.ok(cidr("192.0.2.7", "::ffff:192.0.2.7"), "IPv4-mapped addresses are compared as IPv4");
|
||||
assert.ok(cidr("::1", "::1"));
|
||||
console.log(`routing tester: ok (${fixture.cases.length} cases)`);
|
||||
Reference in new issue
Block a user