mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Merge branch 'master' into feature/config-validation
This commit is contained in:
75 files changed
+1215
-593
No files matched your search
@@ -252,6 +252,28 @@ describe("controller/src/Controller", function() {
|
||||
assert.equal(controller.config.restartRequired, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".factorioVersions", function() {
|
||||
const _fetch = global.fetch;
|
||||
const fetchCalls = [];
|
||||
before(function() {
|
||||
global.fetch = async (url) => {
|
||||
fetchCalls.push(url);
|
||||
return { ok: false, status: 503, statusText: "Service Unavailable" };
|
||||
};
|
||||
});
|
||||
it("should handle fetch failure", async function() {
|
||||
await assert.deepEqual(
|
||||
await controller.factorioVersions.get(0),
|
||||
[],
|
||||
);
|
||||
assert(fetchCalls.length === 1, "expected one fetch call");
|
||||
assert(fetchCalls[0].indexOf("/download") !== -1, "fetch call to be to a URL with /download in it");
|
||||
});
|
||||
after(function() {
|
||||
global.fetch = _fetch;
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("finalise", function() {
|
||||
describe(".finaliseHosts()", function() {
|
||||
|
||||
@@ -4,8 +4,7 @@ const fs = require("fs-extra");
|
||||
const http = require("http");
|
||||
const https = require("https");
|
||||
const util = require("util");
|
||||
|
||||
const phin = require("phin");
|
||||
const events = require("node:events");
|
||||
|
||||
const HttpCloser = require("@clusterio/controller/dist/node/src/HttpCloser").default;
|
||||
|
||||
@@ -38,8 +37,16 @@ function serverSuite(proto) {
|
||||
}
|
||||
|
||||
async function get(url, options = {}) {
|
||||
let core = { ...clientOptions, ...(options.core || {}) };
|
||||
return await phin({ ...options, url, core });
|
||||
const request = api.request(url, { ...clientOptions, ...options });
|
||||
request.end();
|
||||
const [response] = await events.once(request, "response");
|
||||
response.body = await new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
response.on("data", chunk => { chunks.push(chunk); });
|
||||
response.on("error", err => { reject(err); });
|
||||
response.on("end", () => { resolve(Buffer.concat(chunks)); });
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
before(async function() {
|
||||
@@ -101,7 +108,7 @@ function serverSuite(proto) {
|
||||
});
|
||||
|
||||
let agent = new api.Agent({ keepAlive: true });
|
||||
let request = catched(get(addr, { core: { agent }}));
|
||||
let request = catched(get(addr, { agent }));
|
||||
await waitForStage1;
|
||||
stage2();
|
||||
await closer.close();
|
||||
@@ -121,7 +128,7 @@ function serverSuite(proto) {
|
||||
});
|
||||
|
||||
let agent = new api.Agent({ keepAlive: true });
|
||||
let request = catched(get(addr, { core: { agent }}));
|
||||
let request = catched(get(addr, { agent }));
|
||||
await waitForStage1;
|
||||
await closer.close();
|
||||
|
||||
@@ -145,7 +152,7 @@ function serverSuite(proto) {
|
||||
let close = catched(closer.close());
|
||||
|
||||
let agent = new api.Agent({ keepAlive: true });
|
||||
await assert.rejects(get(addr, { core: { agent }}), { code: "ECONNREFUSED" });
|
||||
await assert.rejects(get(addr, { agent }), { code: "ECONNREFUSED" });
|
||||
|
||||
let response = await request;
|
||||
assert.equal(response.statusCode, 200, "non-200 response status code");
|
||||
@@ -165,7 +172,7 @@ function serverSuite(proto) {
|
||||
});
|
||||
|
||||
let agent = new api.Agent({ keepAlive: true });
|
||||
let request = catched(get(addr, { core: { agent }}));
|
||||
let request = catched(get(addr, { agent }));
|
||||
await waitForStage1;
|
||||
stage2();
|
||||
await closer.close();
|
||||
@@ -186,8 +193,8 @@ function serverSuite(proto) {
|
||||
});
|
||||
|
||||
let agent = new api.Agent({ keepAlive: true, maxSockets: 1 });
|
||||
let request = catched(get(addr, { core: { agent }}));
|
||||
let droppedRequest = catched(get(addr, { core: { agent }}));
|
||||
let request = catched(get(addr, { agent }));
|
||||
let droppedRequest = catched(get(addr, { agent }));
|
||||
await waitForStage1;
|
||||
stage2();
|
||||
await closer.close();
|
||||
@@ -212,10 +219,7 @@ function serverSuite(proto) {
|
||||
await closer.close(tick*2);
|
||||
stage2();
|
||||
|
||||
// Due to a bug in phin [https://github.com/ethanent/phin/issues/59]
|
||||
// the request Promise never settles.
|
||||
// TODO: uncomment when bug is fixed in phin.
|
||||
// await assert.rejects(request);
|
||||
await assert.rejects(request);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,12 @@ describe("controller/User", function () {
|
||||
user.saveRecord();
|
||||
assert(user.updatedAtMs > prev);
|
||||
});
|
||||
it("should always increment updatedAtMs even if called in quick succession", function () {
|
||||
user.saveRecord();
|
||||
let prev = user.updatedAtMs;
|
||||
user.saveRecord();
|
||||
assert(user.updatedAtMs > prev, "updatedAtMs did not increase");
|
||||
});
|
||||
});
|
||||
|
||||
describe(".set()", function () {
|
||||
|
||||
+2
-2
@@ -127,8 +127,8 @@ describe("Host testing", function() {
|
||||
version: hostVersion,
|
||||
}];
|
||||
const hostConfig = new lib.HostConfig("host", { "host.version": hostVersion });
|
||||
const hostConnector = new HostConnector(hostConfig, undefined, pluginInfos);
|
||||
host = new Host(hostConnector, hostConfig, undefined, pluginInfos);
|
||||
const hostConnector = new HostConnector(hostConfig, pluginInfos);
|
||||
host = new Host(hostConnector, hostConfig, pluginInfos);
|
||||
});
|
||||
it("returns false when no changes are present", async function() {
|
||||
host.pluginInfos[0].hostEntrypoint = true;
|
||||
|
||||
+116
-256
@@ -342,264 +342,124 @@ describe("host/server", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("full version", function() {
|
||||
it("should do nothing when there is no newer version", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1.1";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
for (const [suite, target] of [
|
||||
["full version", "0.1.1"],
|
||||
["partial version", "0.1"],
|
||||
["latest version", "latest"],
|
||||
]) {
|
||||
/* eslint-disable no-loop-func */
|
||||
describe(suite, function() {
|
||||
it("should do nothing when there are no versions", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target;
|
||||
await server.checkForUpdates([]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
if (target !== "latest") {
|
||||
it("should do nothing when no version matches", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target;
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.2.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.2.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
}
|
||||
it("should do nothing when there is no newer version", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target;
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing for direct installs", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio", "0.1.1");
|
||||
server._targetVersion = target === "0.1.1" ? "0.1.5" : target;
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing when on windows", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "win32" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target === "0.1.1" ? "0.1.5" : target;
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("but must be manually downloaded"));
|
||||
});
|
||||
it("should do attempt to download on linux", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "linux" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target === "0.1.1" ? "0.1.5" : target;
|
||||
await assert.rejects(server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]), new Error("Failed to fetch test1: -1 Fetch called"));
|
||||
|
||||
assert.equal(fetchCalledWith, "test1");
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("starting download..."));
|
||||
});
|
||||
});
|
||||
it("should do nothing for direct installs", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio", "0.1.1");
|
||||
server._targetVersion = "0.1.5";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing when on windows", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "win32" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1.5";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("but must be manually downloaded"));
|
||||
});
|
||||
it("should do attempt to download on linux", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "linux" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1.5";
|
||||
await assert.rejects(server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]), new Error("Failed to fetch test1: -1 Fetch called"));
|
||||
|
||||
assert.equal(fetchCalledWith, "test1");
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("starting download..."));
|
||||
});
|
||||
});
|
||||
describe("partial version", function() {
|
||||
it("should do nothing when there is no newer version", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing for direct installs", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio", "0.1.1");
|
||||
server._targetVersion = "0.1";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing when on windows", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "win32" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("but must be manually downloaded"));
|
||||
});
|
||||
it("should do attempt to download on linux", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "linux" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "0.1";
|
||||
await assert.rejects(server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]), new Error("Failed to fetch test1: -1 Fetch called"));
|
||||
|
||||
assert.equal(fetchCalledWith, "test1");
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("starting download..."));
|
||||
});
|
||||
});
|
||||
describe("latest version", function() {
|
||||
it("should do nothing when there is no newer version", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "latest";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing for direct installs", async function() {
|
||||
server._factorioDir = path.join("test", "file", "factorio", "0.1.1");
|
||||
server._targetVersion = "latest";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing when on windows", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "win32" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "latest";
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("but must be manually downloaded"));
|
||||
});
|
||||
it("should do attempt to download on linux", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
Object.defineProperty(process, "platform", { value: "linux" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = "latest";
|
||||
await assert.rejects(server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]), new Error("Failed to fetch test1: -1 Fetch called"));
|
||||
|
||||
assert.equal(fetchCalledWith, "test1");
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("starting download..."));
|
||||
});
|
||||
});
|
||||
/* eslint-enable no-loop-func */
|
||||
}
|
||||
it("should download a version correctly (live api)", async function() {
|
||||
slowTest(this);
|
||||
if (_platform !== "linux") {
|
||||
|
||||
@@ -4,7 +4,6 @@ const fs = require("fs-extra");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const path = require("path");
|
||||
const events = require("events");
|
||||
const phin = require("phin");
|
||||
|
||||
const lib = require("@clusterio/lib");
|
||||
const libBuildMod = require("@clusterio/lib/build_mod");
|
||||
@@ -41,7 +40,6 @@ async function startAltHost() {
|
||||
await fs.remove(path.join("temp", "test", "alt-instances"));
|
||||
await fs.remove(path.join("temp", "test", "alt-mods"));
|
||||
await execCtl(`host create-config --id 5 --name alt-host --generate-token --output ${config}`);
|
||||
await execHost(`--config ${config} config set host.tls_ca ../../test/file/tls/cert.pem`);
|
||||
await execHost(`--config ${config} config set host.instances_directory alt-instances`);
|
||||
const dir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
|
||||
await execHost(`--config ${config} config set host.factorio_directory ${dir}`);
|
||||
@@ -80,16 +78,15 @@ async function runWithAltHost(callback) {
|
||||
}
|
||||
|
||||
async function uploadSave(instanceId, name, content) {
|
||||
return await phin({
|
||||
url: `https://localhost:4443/api/upload-save?instance_id=${instanceId}&filename=${name}`,
|
||||
const uploadUrl = `https://localhost:4443/api/upload-save?instance_id=${instanceId}&filename=${name}`;
|
||||
return await fetch(uploadUrl, {
|
||||
method: "POST",
|
||||
core: { rejectUnauthorized: false },
|
||||
headers: {
|
||||
"X-Access-Token": controlToken,
|
||||
"Content-Type": "application/zip",
|
||||
"Content-Length": String(content.length),
|
||||
},
|
||||
data: content,
|
||||
body: content,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -168,8 +165,7 @@ describe("Integration of Clusterio", function() {
|
||||
describe("run", function() {
|
||||
it("should handle resume of an active connection", async function() {
|
||||
slowTest(this);
|
||||
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
let connectorA = new TestControlConnector(url, 2, tlsCa);
|
||||
let connectorA = new TestControlConnector(url, 2);
|
||||
connectorA.token = controlToken;
|
||||
let controlA = new TestControl(connectorA);
|
||||
await connectorA.connect();
|
||||
@@ -177,7 +173,7 @@ describe("Integration of Clusterio", function() {
|
||||
connectorA.stopHeartbeat();
|
||||
connectorA.on("error", () => {});
|
||||
|
||||
let connectorB = new TestControlConnector(url, 2, tlsCa);
|
||||
let connectorB = new TestControlConnector(url, 2);
|
||||
connectorB.token = controlToken;
|
||||
connectorB.src = connectorA.src;
|
||||
let controlB = new TestControl(connectorB);
|
||||
@@ -1679,8 +1675,7 @@ describe("Integration of Clusterio", function() {
|
||||
it("should kick existing sessions for the user", async function() {
|
||||
slowTest(this);
|
||||
await getControl().send(new lib.UserCreateRequest("revokee"));
|
||||
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
let connector = new TestControlConnector(url, 2, tlsCa);
|
||||
let connector = new TestControlConnector(url, 2);
|
||||
connector.token = jwt.sign(
|
||||
{ aud: "user", user: "revokee" }, Buffer.from("TestSecretDoNotUse", "base64")
|
||||
);
|
||||
@@ -1709,8 +1704,7 @@ describe("Integration of Clusterio", function() {
|
||||
await execCtl("user set-roles temp Player");
|
||||
let tempControl;
|
||||
try {
|
||||
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
let connector = new TestControlConnector(url, 2, tlsCa);
|
||||
let connector = new TestControlConnector(url, 2);
|
||||
connector.token = jwt.sign(
|
||||
{ aud: "user", user: "temp" }, Buffer.from("TestSecretDoNotUse", "base64")
|
||||
);
|
||||
|
||||
+17
-16
@@ -4,7 +4,6 @@ const path = require("path");
|
||||
const fs = require("fs-extra");
|
||||
const child_process = require("child_process");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const phin = require("phin");
|
||||
const util = require("util");
|
||||
const events = require("events");
|
||||
|
||||
@@ -148,13 +147,12 @@ function hasFactorio() {
|
||||
}
|
||||
|
||||
async function get(urlPath) {
|
||||
let res = await phin({
|
||||
method: "GET",
|
||||
url: `https://localhost:4443${urlPath}`,
|
||||
core: { rejectUnauthorized: false },
|
||||
});
|
||||
if (res.statusCode !== 200) {
|
||||
throw new Error(`Got response code ${res.statusCode}, content: ${res.body}`);
|
||||
const url = new URL("https://localhost:4443");
|
||||
url.pathname = urlPath;
|
||||
let res = await fetch(url);
|
||||
if (res.status !== 200) {
|
||||
const body = Buffer.from(await res.arrayBuffer());
|
||||
throw new Error(`Got response code ${res.status}, content: ${body.toString()}`);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -194,11 +192,18 @@ let controllerConfigPath = path.join("temp", "test", "config-controller.json");
|
||||
let hostConfigPath = path.join("temp", "test", "config-host.json");
|
||||
let controlConfigPath = path.join("temp", "test", "config-control.json");
|
||||
|
||||
function childOptions(options) {
|
||||
return {
|
||||
cwd: path.join("temp", "test"),
|
||||
env: { ...process.env },
|
||||
...options,
|
||||
};
|
||||
}
|
||||
|
||||
function exec(command, options = {}) {
|
||||
// Uncomment to show commands run in tests
|
||||
// console.log(command);
|
||||
options = { cwd: path.join("temp", "test"), ...options };
|
||||
return util.promisify(child_process.exec)(command, options);
|
||||
return util.promisify(child_process.exec)(command, childOptions(options));
|
||||
}
|
||||
|
||||
function execController(...args) {
|
||||
@@ -264,7 +269,7 @@ function spawn(name, cmd, waitFor, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
log(cmd);
|
||||
let parts = cmd.split(" ");
|
||||
let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test"), ...options });
|
||||
let process = child_process.spawn(parts[0], parts.slice(1), childOptions(options));
|
||||
let stdout = new LineSplitter({ readableObjectMode: true });
|
||||
let stderr = new LineSplitter({ readableObjectMode: true });
|
||||
let onDataOut = line => {
|
||||
@@ -354,7 +359,6 @@ before(async function() {
|
||||
console.log("Bootstrapping");
|
||||
await execController("bootstrap create-admin test");
|
||||
await execController("bootstrap create-ctl-config test");
|
||||
await execCtlProcess("control-config set control.tls_ca ../../test/file/tls/cert.pem");
|
||||
|
||||
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
|
||||
|
||||
@@ -362,12 +366,10 @@ before(async function() {
|
||||
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
|
||||
await execCtlProcess("host create-config --id 4 --name host --generate-token");
|
||||
await execHost(`config set host.factorio_directory ${relativeFactorioDir}`);
|
||||
await execHost("config set host.tls_ca ../../test/file/tls/cert.pem");
|
||||
|
||||
hostProcess = await spawnNode("host:", "../../packages/host run", /Started host/);
|
||||
|
||||
const tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
const controlConnector = new TestControlConnector(url, 2, tlsCa);
|
||||
const controlConnector = new TestControlConnector(url, 2);
|
||||
controlConnector.token = controlToken;
|
||||
control = new TestControl(controlConnector);
|
||||
await controlConnector.connect();
|
||||
@@ -375,7 +377,6 @@ before(async function() {
|
||||
const initArgs = await initializeCtl(`--plugin-list ${pluginListPath} control-config list`, undefined, true);
|
||||
control.config = initArgs.controlConfig;
|
||||
control.plugins = initArgs.ctlPlugins;
|
||||
control.tlsCa = tlsCa;
|
||||
|
||||
const testPack = lib.ModPack.fromJSON({});
|
||||
testPack.id = 12;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"use strict";
|
||||
const path = require("path");
|
||||
const assert = require("assert").strict;
|
||||
const lib = require("@clusterio/lib");
|
||||
const fs = require("fs-extra");
|
||||
|
||||
const { testMatrix } = require("../common");
|
||||
const {
|
||||
slowTest, exec, execCtl, execCtlProcess, sendRcon, getControl,
|
||||
requiresFactorio, hasFactorio,
|
||||
requiresFactorio, hasFactorio, instancesDir,
|
||||
} = require("./index");
|
||||
|
||||
const instId = 48;
|
||||
@@ -67,6 +69,32 @@ describe("Clusterio Instance", function() {
|
||||
await execCtl(`instance stop ${instAltName}`);
|
||||
await execCtl(`instance delete ${instAltName}`);
|
||||
});
|
||||
describe("whitelist creation", function() {
|
||||
const whitelistPath = path.join(instancesDir, instName, "server-whitelist.json");
|
||||
before(async function() {
|
||||
this.timeout(20000);
|
||||
await execCtl(`${instSetConfig} factorio.sync_whitelist disabled`);
|
||||
await execCtl(`${instSetConfig} factorio.enable_whitelist false`);
|
||||
});
|
||||
afterEach(async function() {
|
||||
this.timeout(20000);
|
||||
await execCtl(`instance stop ${instName}`);
|
||||
});
|
||||
it("should always create a whitelist json even if unused", async function() {
|
||||
slowTest(this);
|
||||
await fs.remove(whitelistPath);
|
||||
await execCtl(`instance start ${instName}`);
|
||||
const whitelist = await fs.readJSON(whitelistPath);
|
||||
assert.deepEqual(whitelist, []);
|
||||
});
|
||||
it("should not overwrite the existing whitelist if sync is disabled", async function() {
|
||||
slowTest(this);
|
||||
await fs.writeJSON(whitelistPath, ["test_user"]);
|
||||
await execCtl(`instance start ${instName}`);
|
||||
const whitelist = await fs.readJSON(whitelistPath);
|
||||
assert.deepEqual(whitelist, ["test_user"]);
|
||||
});
|
||||
});
|
||||
for (const [savePatchingEnabled, scriptCommandsEnabled] of testMatrix([true, false], [true, false])) {
|
||||
const strSavePatching = `save patching: ${savePatchingEnabled ? "Enabled" : "Disabled"}`;
|
||||
const strScriptCommands = `script commands: ${scriptCommandsEnabled ? "Enabled" : "Disabled"}`;
|
||||
@@ -81,6 +109,7 @@ describe("Clusterio Instance", function() {
|
||||
before(async function() {
|
||||
this.timeout(20000);
|
||||
// Create and start a patched save
|
||||
await execCtl(`${instSetConfig} factorio.enable_whitelist true`);
|
||||
await execCtl(`${instSetConfig} factorio.enable_save_patching ${savePatchingEnabled}`);
|
||||
await execCtl(`${instSetConfig} factorio.enable_script_commands ${scriptCommandsEnabled}`);
|
||||
await execCtl(`instance save create ${instName}`);
|
||||
|
||||
@@ -11,18 +11,14 @@ let token = jwt.sign({ aud: "user", user: "test" }, Buffer.from("TestSecretDoNot
|
||||
let tokenHost = jwt.sign({ aud: "host", host: 0 }, Buffer.from("TestSecretDoNotUse", "base64"));
|
||||
|
||||
describe("Integration of lib/link", function() {
|
||||
let tlsCa;
|
||||
let control;
|
||||
let controlConnector;
|
||||
let hostConnector;
|
||||
before(async function() {
|
||||
tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
});
|
||||
beforeEach(async function() {
|
||||
controlConnector = new TestControlConnector(url, 0.2, tlsCa);
|
||||
controlConnector = new TestControlConnector(url, 0.2);
|
||||
controlConnector.token = token;
|
||||
control = new TestControl(controlConnector, false);
|
||||
hostConnector = new TestHostConnector(url, 0.2, tlsCa);
|
||||
hostConnector = new TestHostConnector(url, 0.2);
|
||||
hostConnector.token = tokenHost;
|
||||
hostConnector.hostId = 0;
|
||||
});
|
||||
@@ -123,7 +119,7 @@ describe("Integration of lib/link", function() {
|
||||
|
||||
it("should close old connections from the same address", async function() {
|
||||
slowTest(this);
|
||||
const hostConnectorFirst = new TestHostConnector(url, 0.2, tlsCa);
|
||||
const hostConnectorFirst = new TestHostConnector(url, 0.2);
|
||||
hostConnectorFirst.token = hostConnector.token;
|
||||
hostConnectorFirst.hostId = hostConnector.hostId;
|
||||
const onceClose = events.once(hostConnectorFirst, "close");
|
||||
|
||||
@@ -23,7 +23,7 @@ function connectHost(controller, hostId, plugins) {
|
||||
controller.wsServer.hostConnections.set(hostId, hostConnection);
|
||||
const hostConfig = new lib.HostConfig("host");
|
||||
hostConfig.set("host.id", hostId);
|
||||
return new Host(hostSide, hostConfig, undefined, []);
|
||||
return new Host(hostSide, hostConfig, []);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
const assert = require("assert").strict;
|
||||
const { checkSingletonImport } = require("@clusterio/lib/dist/node/src/check_singleton_import");
|
||||
// Import lib so it sets the global indicating lib has been imported.
|
||||
require("@clusterio/lib");
|
||||
|
||||
describe("lib/check_singleton_import", function() {
|
||||
it("Should throw an installation error if called twice", function() {
|
||||
assert.throws(
|
||||
() => checkSingletonImport(__filename),
|
||||
{
|
||||
message: /Attempt to import duplicate copy of @clusterio\/lib/,
|
||||
code: "InstallationError",
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
+76
-15
@@ -5,13 +5,6 @@ const lib = require("@clusterio/lib");
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
|
||||
async function incrementDateMs() {
|
||||
const prev = Date.now();
|
||||
do {
|
||||
await lib.wait(1);
|
||||
} while (prev >= Date.now());
|
||||
}
|
||||
|
||||
class MockDatastoreProvider extends lib.DatastoreProvider {
|
||||
constructor() {
|
||||
super();
|
||||
@@ -571,12 +564,31 @@ describe("lib/datastore", function() {
|
||||
eventsRaised = [];
|
||||
datastoreProvider = new MockDatastoreProvider();
|
||||
datastoreProvider.value = new Map([
|
||||
["foo", {id: "foo"}], ["bar", {id: "bar"}], ["baz", {id: "baz"}],
|
||||
["foo", {id: "foo", updatedAtMs: 10}], ["bar", {id: "bar"}], ["baz", {id: "baz"}],
|
||||
]);
|
||||
datastore = new lib.SubscribableDatastore(datastoreProvider, datastoreProvider.value);
|
||||
datastore.on("update", updates => eventsRaised.push(updates));
|
||||
});
|
||||
|
||||
function forceFuture() {
|
||||
const future = {id: "future"};
|
||||
for (let i = 1; i < 100; i++) {
|
||||
datastore.set(future);
|
||||
}
|
||||
return future.updatedAtMs;
|
||||
}
|
||||
|
||||
describe("lastUpdateAtMs", function() {
|
||||
it("is set to the highest value after constructing a new instance", function() {
|
||||
assert(datastore.lastUpdatedAtMs === 10, `${datastore.lastUpdatedAtMs} !== 10`);
|
||||
});
|
||||
it("is set to the highest value after loading from a provider", async function() {
|
||||
const future = Date.now() + 2000;
|
||||
datastore.provider.value = ["future", {id: "future", updatedAtMs: future}];
|
||||
await datastore.load();
|
||||
assert(datastore.lastUpdatedAtMs === future, `${datastore.lastUpdatedAtMs} !== ${future}`);
|
||||
});
|
||||
});
|
||||
describe("set", function() {
|
||||
it("can set a value", function() {
|
||||
const value = { id: "test" };
|
||||
@@ -598,12 +610,21 @@ describe("lib/datastore", function() {
|
||||
const value = { id: "test" };
|
||||
datastore.set(value);
|
||||
assert.notEqual(value.updatedAtMs, undefined);
|
||||
assert(value.updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
assert(value.updatedAtMs > 0, "updatedAtMs not greater than zero");
|
||||
});
|
||||
it("increments the updatedAtMs property", async function() {
|
||||
const value = { id: "test" };
|
||||
datastore.set(value);
|
||||
const prev = value.updatedAtMs;
|
||||
await incrementDateMs();
|
||||
datastore.set(value);
|
||||
assert(value.updatedAtMs > prev, "updatedAtMs must increase");
|
||||
});
|
||||
it("increments the updatedAtMs property past existing values", async function() {
|
||||
const future = forceFuture();
|
||||
const value = { id: "test" };
|
||||
datastore.set(value);
|
||||
assert(value.updatedAtMs > future, "updatedAtMs must increase");
|
||||
});
|
||||
});
|
||||
|
||||
describe("setMany", function() {
|
||||
@@ -632,11 +653,22 @@ describe("lib/datastore", function() {
|
||||
assert.notEqual(updates[1].updatedAtMs, undefined);
|
||||
assert(updates[0].updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
assert(updates[1].updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
const prev = [updates[0].updatedAtMs, updates[1].updatedAtMs];
|
||||
await incrementDateMs();
|
||||
});
|
||||
it("increments the updatedAtMs property", async function() {
|
||||
const value = { id: "test1" };
|
||||
datastore.set(value);
|
||||
const prev = value.updatedAtMs;
|
||||
const updates = [{ id: "test1" }, { id: "test2" }];
|
||||
datastore.setMany(updates);
|
||||
assert(updates[0].updatedAtMs > prev[0], "updatedAtMs must increase");
|
||||
assert(updates[1].updatedAtMs > prev[1], "updatedAtMs must increase");
|
||||
assert(updates[0].updatedAtMs > prev, "updatedAtMs must increase");
|
||||
assert(updates[1].updatedAtMs > prev, "updatedAtMs must increase");
|
||||
});
|
||||
it("increments the updatedAtMs property past existing values", async function() {
|
||||
const future = forceFuture();
|
||||
const updates = [{ id: "test1" }, { id: "test2" }];
|
||||
datastore.setMany(updates);
|
||||
assert(updates[0].updatedAtMs > future, "updatedAtMs must increase");
|
||||
assert(updates[1].updatedAtMs > future, "updatedAtMs must increase");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -667,7 +699,20 @@ describe("lib/datastore", function() {
|
||||
const value = { id: "test" };
|
||||
datastore.delete(value);
|
||||
assert.notEqual(value.updatedAtMs, undefined);
|
||||
assert(value.updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
assert(value.updatedAtMs > 0, "updatedAtMs not greater than zero");
|
||||
});
|
||||
it("increments the updatedAtMs property", function() {
|
||||
const value = { id: "test" };
|
||||
datastore.set(value);
|
||||
const prev = value.updatedAtMs;
|
||||
datastore.delete(value);
|
||||
assert(value.updatedAtMs > prev, "updatedAtMs must increase");
|
||||
});
|
||||
it("increments the updatedAtMs property past existing values", function() {
|
||||
const future = forceFuture();
|
||||
const value = { id: "test" };
|
||||
datastore.delete(value);
|
||||
assert(value.updatedAtMs > future, "updatedAtMs must increase");
|
||||
});
|
||||
it("sets the isDeleted property", function() {
|
||||
const value = { id: "test" };
|
||||
@@ -704,6 +749,22 @@ describe("lib/datastore", function() {
|
||||
assert(values[0].updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
assert(values[1].updatedAtMs > 0, "updatedAtMs greater than zero");
|
||||
});
|
||||
it("increments the updatedAtMs property", function() {
|
||||
const value = { id: "foo" };
|
||||
datastore.set(value);
|
||||
const prev = value.updatedAtMs;
|
||||
const values = [{id: "foo", updatedAtMs: prev}, {id: "bar", updatedAtMs: prev}];
|
||||
datastore.deleteMany(values);
|
||||
assert(values[0].updatedAtMs > prev, "updatedAtMs must increase");
|
||||
assert(values[1].updatedAtMs > prev, "updatedAtMs must increase");
|
||||
});
|
||||
it("increments the updatedAtMs property past existing values", function() {
|
||||
const future = forceFuture();
|
||||
const values = [{id: "foo"}, {id: "bar"}];
|
||||
datastore.deleteMany(values);
|
||||
assert(values[0].updatedAtMs > future, "updatedAtMs must increase");
|
||||
assert(values[1].updatedAtMs > future, "updatedAtMs must increase");
|
||||
});
|
||||
it("sets the isDeleted property", function() {
|
||||
const values = [{id: "foo"}, {id: "bar"}];
|
||||
datastore.deleteMany(values);
|
||||
|
||||
+6
@@ -121,6 +121,12 @@ describe("FactorioVersions", function() {
|
||||
assert.equal(versions.filter(v => v.stable).length, 48);
|
||||
assert.equal(versions.filter(v => !v.stable).length, 57);
|
||||
});
|
||||
it("parses versions from empty html file", async function() {
|
||||
mockFetch("");
|
||||
|
||||
const versions = await fetchFactorioVersions();
|
||||
assert.equal(versions.length, 0);
|
||||
});
|
||||
it("parses versions from live api", async function() {
|
||||
slowTest(this);
|
||||
global.fetch = originalFetch;
|
||||
|
||||
+14
-2
@@ -651,8 +651,20 @@ describe("lib/prometheus", function() {
|
||||
let gauge = new prometheus.Gauge(
|
||||
"test", "Help", { register: false, labels: ["a", "b"] }
|
||||
);
|
||||
gauge.labels('x\\\\\n\"\"\n\\', "2");
|
||||
gauge.removeAll({ a: 'x\\\\\n\"\"\n\\' });
|
||||
|
||||
let labelValue = "";
|
||||
// Include every ASCII codepoint
|
||||
for (let i = 0; i < 128; i++) {
|
||||
labelValue += String.fromCodePoint(i);
|
||||
}
|
||||
// Include Unicode codepoints from the basic and supplementary planes.
|
||||
labelValue += "©💣";
|
||||
|
||||
gauge.labels(labelValue, "2");
|
||||
gauge.removeAll({ a: labelValue });
|
||||
assert(gauge._values.size === 0, "Matching labels remained");
|
||||
gauge.labels("1", labelValue);
|
||||
gauge.removeAll({ a: "1" });
|
||||
assert(gauge._values.size === 0, "Matching labels remained");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,7 +14,7 @@ describe("messages/host", function() {
|
||||
lib.Address.fromShorthand({ hostId: 1 }),
|
||||
lib.Address.fromShorthand("controller"),
|
||||
);
|
||||
host = new Host(hostConnector, hostConfig, undefined, []);
|
||||
host = new Host(hostConnector, hostConfig, []);
|
||||
});
|
||||
|
||||
describe("HostUpdateRequest", function() {
|
||||
|
||||
@@ -28,7 +28,7 @@ describe("messages/plugin", function() {
|
||||
lib.Address.fromShorthand({ hostId: 1 }),
|
||||
lib.Address.fromShorthand("controller"),
|
||||
);
|
||||
host = new Host(hostConnector, hostConfig, undefined, []);
|
||||
host = new Host(hostConnector, hostConfig, []);
|
||||
});
|
||||
|
||||
describe("PluginDetails", function() {
|
||||
|
||||
Reference in new issue
Block a user