44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createDataplaneClient } from '../../src/server/dataplaneClient.js';
|
|
|
|
test('control uses the dataplane socket protocol', async () => {
|
|
const requests = [];
|
|
const send = async (socketPath, pathname, method, body) => {
|
|
requests.push({ method, pathname, socketPath, body });
|
|
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);
|
|
await client.observeDevicePolicy();
|
|
await client.applyDevicePolicies([{ id: 'dev_0011223344556677' }]);
|
|
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 /device-policy /run/dataplane.sock',
|
|
'PUT /device-policy /run/dataplane.sock',
|
|
'POST /restart /run/dataplane.sock',
|
|
'POST /stop /run/dataplane.sock',
|
|
]);
|
|
assert.deepEqual(requests[5].body, { devices: [{ id: 'dev_0011223344556677' }] });
|
|
});
|