You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
6.5 KiB
JavaScript
172 lines
6.5 KiB
JavaScript
|
2 weeks ago
|
import assert from 'node:assert/strict';
|
||
|
|
import {
|
||
|
|
buildAdminNetworkTargets,
|
||
|
|
normalizeAdminNetworkOptions,
|
||
|
|
} from '../public/cloud/admin-instance-configuration.js';
|
||
|
|
|
||
|
|
const key4a = 'a'.repeat(32);
|
||
|
|
const key6a = 'b'.repeat(32);
|
||
|
|
const key4b = 'c'.repeat(32);
|
||
|
|
const key6b = 'd'.repeat(32);
|
||
|
|
let fetchCalls = 0;
|
||
|
|
const originalFetch = globalThis.fetch;
|
||
|
|
globalThis.fetch = async () => {
|
||
|
|
fetchCalls += 1;
|
||
|
|
throw new Error('Target validation must not fetch.');
|
||
|
|
};
|
||
|
|
|
||
|
|
const options = normalizeAdminNetworkOptions({
|
||
|
|
current_public_network: {
|
||
|
|
contract: 1,
|
||
|
|
network_selector: 'br0',
|
||
|
|
ipv4: { pool_key: key4a, pool_cidr: '8.8.8.0/24', address_cidr: '8.8.8.10/24', gateway: '8.8.8.1', gateway_on_link: false },
|
||
|
|
ipv6: { pool_key: key6a, pool_cidr: '2404:c140:156:3a::/64', address_cidr: '2404:c140:156:3a::10/64', gateway: '2404:c140:156::1', gateway_on_link: true },
|
||
|
|
dns_servers: ['1.1.1.1', '2606:4700:4700::1111'],
|
||
|
|
},
|
||
|
|
current_attachment_id: '1',
|
||
|
|
attachments: [
|
||
|
|
{
|
||
|
|
attachment_id: '1', label: 'br0 · 当前', current: true,
|
||
|
|
ipv4: { pools: [{ pool_key: key4a, cidr: '8.8.8.0/24', guest_prefix: 24,
|
||
|
|
gateway: '8.8.8.1', available_options: [{ address_cidr: '8.8.8.22/24' }] }] },
|
||
|
|
ipv6: { pools: [{ pool_key: key6a, cidr: '2404:c140:156:3a::/64',
|
||
|
|
guest_prefix: 64, gateway: '2404:c140:156::1', sparse: true,
|
||
|
|
allocation_start: '2404:c140:156:3a::10',
|
||
|
|
allocation_end: '2404:c140:156:3a::ff',
|
||
|
|
excluded_addresses: ['2404:c140:156:3a::20'] }] },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
attachment_id: '2', label: 'br1 · 备用线路', current: false,
|
||
|
|
ipv4: { pools: [{ pool_key: key4b, cidr: '9.9.9.0/24', guest_prefix: 24,
|
||
|
|
gateway: '9.9.9.1', available_options: [{ address_cidr: '9.9.9.20/24' }] }] },
|
||
|
|
ipv6: { pools: [{ pool_key: key6b, cidr: '2404:c140:156:3b::/64',
|
||
|
|
guest_prefix: 64, gateway: '2404:c140:156::1', sparse: true,
|
||
|
|
allocation_start: '2404:c140:156:3b::10',
|
||
|
|
allocation_end: '2404:c140:156:3b::ff',
|
||
|
|
excluded_addresses: ['2404:c140:156:3b::20'] }] },
|
||
|
|
},
|
||
|
|
],
|
||
|
|
configuration_state: 'ready',
|
||
|
|
can_change: true,
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(options.attachments.length, 2);
|
||
|
|
assert.equal(options.current_public_network.ipv6.address_cidr, '2404:c140:156:3a::10/64');
|
||
|
|
|
||
|
|
const currentIpv4Only = buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '1', ipv4_target: `${key4a}|8.8.8.22/24`,
|
||
|
|
});
|
||
|
|
assert.deepEqual(currentIpv4Only.targets, {
|
||
|
|
ipv4: { pool_key: key4a, address_cidr: '8.8.8.22/24' },
|
||
|
|
});
|
||
|
|
|
||
|
|
const currentIpv6Only = buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '1', ipv6_pool_key: key6a,
|
||
|
|
ipv6_address_cidr: '2404:c140:156:3a::22/64',
|
||
|
|
});
|
||
|
|
assert.deepEqual(currentIpv6Only.targets, {
|
||
|
|
ipv6: { pool_key: key6a, address_cidr: '2404:c140:156:3a::22/64' },
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
}), /同时指定新的 IPv4 和 IPv6/);
|
||
|
|
|
||
|
|
const crossAttachment = buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
ipv6_pool_key: key6b, ipv6_address_cidr: '2404:c140:156:3b::21/64',
|
||
|
|
});
|
||
|
|
assert.deepEqual(crossAttachment.targets, {
|
||
|
|
ipv4: { pool_key: key4b, address_cidr: '9.9.9.20/24' },
|
||
|
|
ipv6: { pool_key: key6b, address_cidr: '2404:c140:156:3b::21/64' },
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const address of ['2404:c140:156:3b::10/64', '2404:c140:156:3b::ff/64']) {
|
||
|
|
const boundary = buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
ipv6_pool_key: key6b, ipv6_address_cidr: address,
|
||
|
|
});
|
||
|
|
assert.equal(boundary.targets.ipv6.address_cidr, address);
|
||
|
|
}
|
||
|
|
|
||
|
|
const optionsBeforeRejectedTargets = JSON.stringify(options, (_, value) =>
|
||
|
|
typeof value === 'bigint' ? value.toString() : value);
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
ipv6_pool_key: key6b, ipv6_address_cidr: '2404:c140:156:3b::100/64',
|
||
|
|
}), /可分配范围内/);
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
ipv6_pool_key: key6b, ipv6_address_cidr: '2404:c140:156:3b::20/64',
|
||
|
|
}), /网关、DNS、保留或管理地址/);
|
||
|
|
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(options, {
|
||
|
|
attachment_id: '1', ipv6_pool_key: key6a,
|
||
|
|
ipv6_address_cidr: '2404:c140:156:3b::20/64',
|
||
|
|
}), /必须属于所选地址池/);
|
||
|
|
|
||
|
|
const singleFamilyAlternative = normalizeAdminNetworkOptions({
|
||
|
|
current_public_network: options.current_public_network,
|
||
|
|
current_attachment_id: '1',
|
||
|
|
attachments: [
|
||
|
|
{
|
||
|
|
attachment_id: '1', current: true,
|
||
|
|
ipv4: { pools: [] }, ipv6: { pools: [] },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
attachment_id: '2', current: false,
|
||
|
|
ipv4: { pools: [{
|
||
|
|
pool_key: key4b, cidr: '9.9.9.0/24', guest_prefix: 24,
|
||
|
|
gateway: '9.9.9.1',
|
||
|
|
available_options: [{ address_cidr: '9.9.9.20/24' }],
|
||
|
|
}] },
|
||
|
|
ipv6: { pools: [] },
|
||
|
|
},
|
||
|
|
],
|
||
|
|
configuration_state: 'ready', can_change: true,
|
||
|
|
});
|
||
|
|
assert.equal(singleFamilyAlternative.can_change, false);
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(singleFamilyAlternative, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
}), /暂不能更换公网地址/);
|
||
|
|
|
||
|
|
const prefixMismatch = normalizeAdminNetworkOptions({
|
||
|
|
current_public_network: options.current_public_network,
|
||
|
|
current_attachment_id: '1',
|
||
|
|
attachments: [
|
||
|
|
{
|
||
|
|
attachment_id: '1', current: true,
|
||
|
|
ipv4: { pools: [] }, ipv6: { pools: [] },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
attachment_id: '2', current: false,
|
||
|
|
ipv4: { pools: [{
|
||
|
|
pool_key: key4b, cidr: '9.9.9.0/24', guest_prefix: 24,
|
||
|
|
gateway: '9.9.9.1',
|
||
|
|
available_options: [{ address_cidr: '9.9.9.20/24' }],
|
||
|
|
}] },
|
||
|
|
ipv6: { pools: [{
|
||
|
|
pool_key: key6b, cidr: '2404:c140:156:3b::/56',
|
||
|
|
guest_prefix: 64, gateway: '2404:c140:156::1', sparse: true,
|
||
|
|
allocation_start: '2404:c140:156:3b::10',
|
||
|
|
allocation_end: '2404:c140:156:3b::ff',
|
||
|
|
excluded_addresses: [],
|
||
|
|
}] },
|
||
|
|
},
|
||
|
|
],
|
||
|
|
configuration_state: 'ready', can_change: true,
|
||
|
|
});
|
||
|
|
assert.equal(prefixMismatch.attachments[1].ipv6.pools.length, 0);
|
||
|
|
assert.equal(prefixMismatch.can_change, false);
|
||
|
|
assert.throws(() => buildAdminNetworkTargets(prefixMismatch, {
|
||
|
|
attachment_id: '2', ipv4_target: `${key4b}|9.9.9.20/24`,
|
||
|
|
ipv6_pool_key: key6b, ipv6_address_cidr: '2404:c140:156:3b::21/64',
|
||
|
|
}), /暂不能更换公网地址/);
|
||
|
|
|
||
|
|
assert.equal(fetchCalls, 0);
|
||
|
|
assert.equal(JSON.stringify(options, (_, value) =>
|
||
|
|
typeof value === 'bigint' ? value.toString() : value), optionsBeforeRejectedTargets);
|
||
|
|
globalThis.fetch = originalFetch;
|
||
|
|
|
||
|
|
console.log('Administrator dual-stack network target behavior passed.');
|