Files
harbor-net/test/server/dataplane-client.test.js
T
dokril 76a99f098a
Build and Deploy Gateway / build-and-push (push) Successful in 31s
Build and Deploy Gateway / deploy (push) Successful in 13s
Add DNS diagnostics across dataplane and client
2026-09-01 04:16:15 +03:00

99 lines
4.2 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createDataplaneClient } from '../../dist/server/dataplaneClient.js';
test('control uses the dataplane socket protocol', async () => {
const requests = [];
const send = async (socketPath, pathname, method, body, timeoutMs) => {
requests.push({ method, pathname, socketPath, body, timeoutMs });
return {
running: pathname !== '/stop',
startedAt: 'now',
gatewayBackendVersion: '0.1.0',
singBoxVersion: '1.12.13',
};
};
const client = createDataplaneClient('/run/dataplane.sock', send);
const status = await client.refresh();
assert.equal(status.running, true);
assert.equal(status.gatewayBackendVersion, '0.1.0');
assert.equal(status.singBoxVersion, '1.12.13');
await client.apply();
const devices = await client.observeDevices();
assert.equal(devices.running, true);
const traffic = await client.observeTraffic();
assert.equal(traffic.running, true);
const domainTraffic = await client.observeDomainTraffic();
assert.equal(domainTraffic.running, true);
const liveTraffic = await client.observeLiveTraffic();
assert.equal(liveTraffic.running, true);
await client.observeDevicePolicy();
await client.applyDevicePolicies([{ id: 'dev_0011223344556677' }]);
await client.runConnectivityDiagnostics(
[{ id: 'custom-test', url: 'https://example.com' }],
'site:custom-test',
);
await client.getDnsDiagnosticsCatalog([{ id: 'dns' }], [{ id: 'domain' }]);
await client.runDnsDiagnostics([{ id: 'dns' }], [{ id: 'domain' }], 'youtube', 'google-dns');
await client.checkConfig({ outbounds: [] });
await client.runFailoverProbe('primary', [], 'site:youtube', 9_000);
await client.readFailoverSelector();
await client.selectFailoverRole('reserve');
await client.setFailoverActivityEnabled(true);
await client.readFailoverActivity(1024);
assert.equal(client.running, true);
await client.restart();
assert.equal((await client.stop()).running, false);
assert.deepEqual(requests.map(({ method, pathname, socketPath }) => `${method} ${pathname} ${socketPath}`), [
'GET /status /run/dataplane.sock',
'POST /apply /run/dataplane.sock',
'GET /devices /run/dataplane.sock',
'GET /device-traffic /run/dataplane.sock',
'GET /domain-traffic /run/dataplane.sock',
'GET /traffic/live /run/dataplane.sock',
'GET /device-policy /run/dataplane.sock',
'PUT /device-policy /run/dataplane.sock',
'POST /diagnostics/connectivity /run/dataplane.sock',
'POST /diagnostics/dns/catalog /run/dataplane.sock',
'POST /diagnostics/dns /run/dataplane.sock',
'POST /config/check /run/dataplane.sock',
'POST /failover/probe /run/dataplane.sock',
'GET /failover/selector /run/dataplane.sock',
'PUT /failover/selector /run/dataplane.sock',
'PUT /failover/activity /run/dataplane.sock',
'POST /failover/activity/read /run/dataplane.sock',
'POST /restart /run/dataplane.sock',
'POST /stop /run/dataplane.sock',
]);
assert.deepEqual(requests[7].body, { devices: [{ id: 'dev_0011223344556677' }] });
assert.deepEqual(requests[8].body, {
services: [{ id: 'custom-test', url: 'https://example.com' }],
target: 'site:custom-test',
});
assert.equal(requests[8].timeoutMs, 25_000);
assert.deepEqual(requests[9].body, {
customResolvers: [{ id: 'dns' }], customDomains: [{ id: 'domain' }],
});
assert.deepEqual(requests[10].body, {
customResolvers: [{ id: 'dns' }], customDomains: [{ id: 'domain' }],
domainId: 'youtube', resolverId: 'google-dns',
});
assert.equal(requests[10].timeoutMs, 40_000);
assert.deepEqual(requests[11].body, { config: { outbounds: [] } });
assert.deepEqual(requests[12].body, { role: 'primary', services: [], target: 'site:youtube', timeoutMs: 9_000 });
assert.equal(requests[12].timeoutMs, 19_000);
assert.deepEqual(requests[14].body, { role: 'reserve' });
});
test('connectivity diagnostics expose a retryable domain error', async () => {
const client = createDataplaneClient('/run/dataplane.sock', async () => {
throw new Error('Dataplane не ответил');
});
await assert.rejects(client.runConnectivityDiagnostics(), {
code: 'DIAGNOSTICS_FAILED',
retryable: true,
});
});