Files
MikrotikManager/backend/scripts/acceptance/chr-netflow-endpoint-discovery.test.mjs
T

103 lines
5.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { discoverPinnedInterfaceCounterShape, discoverPinnedNetFlowEndpoints, discoverPinnedNetFlowPolicy, loadPinnedNetFlowTransport } from './chr-netflow-endpoint-discovery.mjs';
const hosts = ['194.226.121.150', '104.165.244.7'];
const routes = ['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target'];
const target = { disabled: 'false', version: 'ipfix', 'dst-address': '192.0.2.10', port: '2055', 'src-address': '0.0.0.0' };
function fixture(targets, enabled = ['true', 'true']) {
const calls = [];
const read = async (host, path) => {
calls.push([host, path]);
if (path === routes[0]) return [{ version: '7.24.5 (stable)' }];
if (path === routes[1]) return [{ enabled: enabled[hosts.indexOf(host)] }];
assert.equal(path, routes[2]);
return targets[hosts.indexOf(host)];
};
return { read, calls };
}
test('loads the reviewed source with the exact read-only path and relay fence', async () => {
const transport = await loadPinnedNetFlowTransport();
assert.deepEqual(transport.PATHS, routes);
assert.throws(() => transport.readResponse('http://127.0.0.1:19082', '/ip/address', '', {}), /request-budget/);
assert.throws(() => transport.readResponse('http://127.0.0.1:19083', routes[0], '', {}), /non-whitelisted-endpoint/);
});
test('reports only a common configured endpoint, with no ingress or capture claim', async () => {
const { read, calls } = fixture([[target], [{ ...target, 'src-address': '192.0.2.20' }]]);
const report = await discoverPinnedNetFlowEndpoints(read);
assert.deepEqual(calls, hosts.flatMap(host => routes.map(path => [host, path])));
assert.equal(report.status, 'configured-endpoint');
assert.deepEqual(report.configuredEndpoint, { destination: '192.0.2.10', port: 2055 });
assert.deepEqual(report.exporters.map(item => item.configuredTargets[0].configuredSource), [null, '192.0.2.20']);
assert.equal(report.ingressVerified, false);
assert.equal(report.capturePerformed, false);
assert.equal(report.mutationsPerformed, false);
});
test('returns successful absence for missing or disabled baseline', async () => {
for (const input of [
fixture([[], [target]]),
fixture([[target], [target]], [true, false]),
fixture([[target], [{ ...target, disabled: true }]]),
fixture([[target], [{ ...target, version: '9' }]]),
]) {
const result = await discoverPinnedNetFlowEndpoints(input.read);
assert.equal(result.status, 'no-common-active-ipfix-target');
assert.equal(result.configuredEndpoint, null);
}
});
test('refuses malformed relevant rows and withholds ambiguous common endpoints', async () => {
const result = await discoverPinnedNetFlowEndpoints(fixture([[target], [target, target]]).read);
assert.equal(result.status, 'ambiguous-endpoint');
assert.equal(result.configuredEndpoint, null);
await assert.rejects(discoverPinnedNetFlowEndpoints(fixture([[target], [{ ...target, port: '65536' }]]).read), /^Error: NetFlow discovery refused$/);
});
test('reports preliminary global vetoes and counts with no claim on tuple policy or routes', async () => {
const transport = await loadPinnedNetFlowTransport(true);
const calls = [];
const report = await discoverPinnedNetFlowPolicy(async (host, path) => {
calls.push([host, path]);
return ({
'/system/resource': [{ version: '7.24.5 (stable)' }],
'/ip/settings': [{ 'ip-forward': 'yes', 'allow-fast-path': host === hosts[0] ? 'yes' : 'no' }],
'/ip/firewall/connection/tracking': [{ enabled: host === hosts[0] ? 'auto' : 'yes' }],
'/ip/firewall/filter': [{ action: 'fasttrack-connection' }, { action: 'drop', disabled: 'true' }],
'/ip/firewall/raw': [], '/ip/firewall/mangle': [], '/ip/firewall/nat': [],
'/routing/rule': host === hosts[0] ? [{}] : [],
'/ip/vrf': [{ name: 'main', interfaces: 'all' }],
})[path];
});
assert.deepEqual(calls, hosts.flatMap(host => transport.PATHS.map(path => [host, path])));
assert.equal(report.status, 'pre-topology-veto');
assert.deepEqual(report.exporters[0].vetoes, ['fast-path-not-confirmed-disabled', 'conntrack-not-explicitly-enabled', 'routing-policy-present']);
assert.deepEqual(report.exporters[1].vetoes, []);
assert.deepEqual(report.exporters[0].firewall.filter, { total: 2, notExplicitlyDisabled: 1, fastTrack: 1 });
assert.equal(report.routeSelectionVerified, false);
assert.equal(report.tuplePolicyVerified, false);
assert.equal(report.ingressVerified, false);
});
test('counter shape reads exact selected native IDs and emits no identities or counter values', async () => {
const ether = { '.id': '*A', name: 'private-ether', type: 'ether', running: 'true', disabled: 'false' };
const wg = { ...ether, '.id': '*B', name: 'private-wg', type: 'wg' };
const calls = [];
const report = await discoverPinnedInterfaceCounterShape(async (host, route) => {
calls.push([host, route]);
if (route === '/interface') return host === hosts[0] ? [ether, wg] : [ether];
return [{ ...(host === hosts[0] ? wg : ether), ifindex: '61455', 'rx-packet': 71922, 'fp-rx-byte': null }];
});
assert.deepEqual(calls, [[hosts[0], '/interface'], [hosts[0], '/interface/*B'], [hosts[1], '/interface'], [hosts[1], '/interface/*A']]);
assert.deepEqual(report.devices[0].fields.ifindex, { present: true, type: 'string' });
assert.deepEqual(report.devices[0].fields['rx-packet'], { present: true, type: 'number' });
assert.deepEqual(report.devices[0].fields['fp-rx-byte'], { present: true, type: 'null' });
assert.deepEqual(report.devices[0].fields['tx-packet'], { present: false, type: 'undefined' });
assert.doesNotMatch(JSON.stringify(report), /private|61455|71922|104\.165|194\.226|\*[AB]/);
await assert.rejects(discoverPinnedInterfaceCounterShape(async (_host, route) =>
route === '/interface' ? [ether] : [{ ...ether, '.id': '*C' }]), /discovery refused/);
});