mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Rewrite Factorio server spawning
Break out the server process management to a new FactorioServer class in lib/factorio. This class takes responsibility for managing a factorio server, communicating with it and handling the stopping of it. It also replaces the previous effort to do this in lib/factorioServer. This change also updates the version of rcon-client, and since the author of it has updated it to use UTF-8 the fork is no longer necessary. It also removes the messageInterface implementation in favor of just calling the rcon-client library directly since this has rate limiting logic built in. The lib/factorio library is intended to abstract all the interfacing that happens with Factorio.
This commit is contained in:
1 parent
6b4ab6660c
commit
a01b971ba2
13 files changed
+1217
-585
No files matched your search
@@ -72,41 +72,6 @@ describe("Client testing", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("randomDynamicPort()", function() {
|
||||
it("should return a port number", function() {
|
||||
let port = client._randomDynamicPort()
|
||||
assert.equal(typeof port, "number");
|
||||
assert(Number.isInteger(port));
|
||||
assert(0 <= port && port < 2**16);
|
||||
});
|
||||
|
||||
it("should return a port number in the dynamic range", function() {
|
||||
function validate(port) {
|
||||
return 49152 <= port && port <= 65535;
|
||||
}
|
||||
for (let i=0; i < 20; i++) {
|
||||
assert(validate(client._randomDynamicPort()));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("generatePassword()", function() {
|
||||
it("should return a string", async function() {
|
||||
let password = await client._generatePassword(1);
|
||||
assert.equal(typeof password, "string");
|
||||
});
|
||||
|
||||
it("should return a string of the given length", async function() {
|
||||
let password = await client._generatePassword(10);
|
||||
assert.equal(password.length, 10);
|
||||
});
|
||||
|
||||
it("should contain only a-z, A-Z, 0-9", async function() {
|
||||
let password = await client._generatePassword(10);
|
||||
assert(/^[a-zA-Z0-9]+$/.test(password), `${password} failed test`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("symlinkMods()", function() {
|
||||
// Remove previous test output
|
||||
let testDir = path.join("test", "temp", "symlink");
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Date: 04. 09. 2019
|
||||
Changes:
|
||||
- Bad changelog with no version
|
||||
@@ -0,0 +1,13 @@
|
||||
---------------------------------------------------------------------------------------------------
|
||||
NotVersion: Wrong
|
||||
Version: 0.1.1
|
||||
Date: 04. 09. 2019
|
||||
Changes:
|
||||
- Test for changelog
|
||||
Bugfixes:
|
||||
- Has the same format as factorio's changelog
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.1.0
|
||||
Date: 03. 09. 2019
|
||||
Changes:
|
||||
- Nothing
|
||||
@@ -0,0 +1,3 @@
|
||||
---------------------------------------------------------------------------------------------------
|
||||
Version: 0.1.1
|
||||
About: Dummy changelog.txt for FactorioServer tests
|
||||
@@ -0,0 +1,169 @@
|
||||
const assert = require("assert").strict;
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
|
||||
const factorio = require("lib/factorio");
|
||||
|
||||
|
||||
describe("Integration of lib/factorio/server", function() {
|
||||
describe("_getVersion()", function() {
|
||||
it("should get a version from factorio's changelog.txt", async function() {
|
||||
let version = await factorio._getVersion(path.join("factorio", "data", "changelog.txt"));
|
||||
if (!/^\d+\.\d+\.\d+$/.test(version)) {
|
||||
assert.fail(`Detected version '${version}' does not followed the format x.y.z`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("class FactorioServer", function() {
|
||||
let writePath = path.join("test", "temp", "integration");
|
||||
let server = new factorio.FactorioServer(path.join("factorio", "data"), writePath, {});
|
||||
let logFile;
|
||||
|
||||
before(async function() {
|
||||
// Delete result from previous run of these tests
|
||||
if (fs.existsSync(writePath)) {
|
||||
await fs.remove(writePath);
|
||||
}
|
||||
|
||||
await fs.ensureDir(writePath);
|
||||
logFile = fs.createWriteStream(path.join(writePath, "log.txt"), "utf8");
|
||||
server.on('output', function(output) {
|
||||
logFile.write(JSON.stringify(output) + "\n");
|
||||
});
|
||||
|
||||
// Initialize sever.
|
||||
await server.init();
|
||||
});
|
||||
|
||||
after(function() {
|
||||
logFile.end();
|
||||
});
|
||||
|
||||
describe(".exampleSettings()", function() {
|
||||
let settings;
|
||||
it("returns an object", async function() {
|
||||
settings = await server.exampleSettings();
|
||||
assert(typeof settings === "object");
|
||||
});
|
||||
|
||||
it("contains the settings used by Clusterio", async function() {
|
||||
let keysUsed = new Set([
|
||||
"name", "description", "tags", "max_players", "visibility", "username", "token",
|
||||
"game_password", "require_user_verification", "allow_commands", "autosave_interval",
|
||||
"autosave_slots", "afk_autokick_interval", "auto_pause",
|
||||
]);
|
||||
|
||||
for (let key of Object.keys(settings)) {
|
||||
keysUsed.delete(key);
|
||||
}
|
||||
|
||||
assert(
|
||||
keysUsed.size === 0,
|
||||
`Factorio's server-settings.example.json does not contain the key(s) ${[...keysUsed]}`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// Mark that this test takes a lot of time, or depeneds on a test
|
||||
// that takes a lot of time.
|
||||
function slowTest(test) {
|
||||
if (process.env.FAST_TEST) {
|
||||
test.skip();
|
||||
}
|
||||
|
||||
test.timeout(20000);
|
||||
}
|
||||
|
||||
function log(message) {
|
||||
logFile.write("=================== " + message + "\n");
|
||||
}
|
||||
|
||||
describe(".create()", function() {
|
||||
it("creates a map file at writeDir/saves/name", async function() {
|
||||
slowTest(this);
|
||||
log(".create() with new save");
|
||||
|
||||
// Make sure the test is not fooled by previous data
|
||||
let mapPath = server.writePath("saves", "test.zip");
|
||||
assert(!await fs.exists(mapPath), "save exist before test");
|
||||
|
||||
await server.create("test.zip");
|
||||
assert(await fs.exists(mapPath), "test did not create save");
|
||||
});
|
||||
});
|
||||
|
||||
describe(".start()", function() {
|
||||
it("starts the server", async function() {
|
||||
slowTest(this);
|
||||
log(".start()");
|
||||
|
||||
// Make sure the test does not fail due to create() failing.
|
||||
let mapPath = server.writePath("saves", "test.zip");
|
||||
assert(await fs.exists(mapPath), "save is missing");
|
||||
|
||||
await server.start("test.zip");
|
||||
});
|
||||
});
|
||||
|
||||
describe(".disableAchievments()", function() {
|
||||
it("disables acheivements", async function() {
|
||||
slowTest(this);
|
||||
log(".disableAchievements()");
|
||||
assert.equal(await server.disableAchievements(), true);
|
||||
});
|
||||
|
||||
it("can tell when acheivements were disabled", async function() {
|
||||
slowTest(this);
|
||||
assert.equal(await server.disableAchievements(), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".sendRcon()", function() {
|
||||
it("returns the result of a command", async function() {
|
||||
slowTest(this);
|
||||
log(".sendRcon()");
|
||||
|
||||
let result = await server.sendRcon("/sc rcon.print('success')");
|
||||
assert.equal(result, 'success\n');
|
||||
});
|
||||
});
|
||||
|
||||
describe(".stop()", function() {
|
||||
it("stops the server", async function() {
|
||||
slowTest(this);
|
||||
log(".stop()");
|
||||
|
||||
await server.stop();
|
||||
});
|
||||
});
|
||||
|
||||
describe(".startScenario()", function() {
|
||||
before("Write test_scenario", async function() {
|
||||
let content = "script.on_init(function() print('test_scenario init') end)\n";
|
||||
await fs.outputFile(server.writePath("scenarios", "test_scenario", "control.lua"), content);
|
||||
});
|
||||
|
||||
it("runs the given scenario", async function() {
|
||||
slowTest(this);
|
||||
log(".startScenario()");
|
||||
|
||||
let pass = false
|
||||
function filter(output) {
|
||||
if (output.message === "test_scenario init") {
|
||||
pass = true;
|
||||
}
|
||||
}
|
||||
server.on('output', filter);
|
||||
|
||||
await server.startScenario("test_scenario");
|
||||
|
||||
log(".stop()");
|
||||
await server.stop();
|
||||
|
||||
server.off('output', filter);
|
||||
assert(pass, "server did not output line from test scenario");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,208 @@
|
||||
const assert = require("assert").strict;
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
|
||||
const factorio = require("lib/factorio");
|
||||
|
||||
|
||||
describe("lib/factorio/server", function() {
|
||||
describe("_getVersion()", function() {
|
||||
it("should get the version from a changelog", async function() {
|
||||
let version = await factorio._getVersion(path.join("test", "file", "changelog-test.txt"));
|
||||
assert.equal(version, "0.1.1");
|
||||
});
|
||||
it("should throw if unable to find the version", async function() {
|
||||
await assert.rejects(
|
||||
factorio._getVersion(path.join("test", "file", "changelog-bad.txt")),
|
||||
new Error("Unable to determine the version of Factorio")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("randomDynamicPort()", function() {
|
||||
it("should return a port number", function() {
|
||||
let port = factorio._randomDynamicPort()
|
||||
assert.equal(typeof port, "number");
|
||||
assert(Number.isInteger(port));
|
||||
assert(0 <= port && port < 2**16);
|
||||
});
|
||||
|
||||
it("should return a port number in the dynamic range", function() {
|
||||
function validate(port) {
|
||||
return 49152 <= port && port <= 65535;
|
||||
}
|
||||
for (let i=0; i < 20; i++) {
|
||||
assert(validate(factorio._randomDynamicPort()));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("generatePassword()", function() {
|
||||
it("should return a string", async function() {
|
||||
let password = await factorio._generatePassword(1);
|
||||
assert.equal(typeof password, "string");
|
||||
});
|
||||
|
||||
it("should return a string of the given length", async function() {
|
||||
let password = await factorio._generatePassword(10);
|
||||
assert.equal(password.length, 10);
|
||||
});
|
||||
|
||||
it("should contain only a-z, A-Z, 0-9", async function() {
|
||||
let password = await factorio._generatePassword(10);
|
||||
assert(/^[a-zA-Z0-9]+$/.test(password), `${password} failed test`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("class LineSplitter", function() {
|
||||
function createSplitter(lines) {
|
||||
return new factorio._LineSplitter(line => lines.push(line.toString("utf-8")));
|
||||
}
|
||||
|
||||
it("should split three lines", function() {
|
||||
let lines = [];
|
||||
let ls = createSplitter(lines);
|
||||
ls.data(Buffer.from("line 1\nline 2\nline 3\n"));
|
||||
ls.end();
|
||||
assert.deepEqual(lines, ["line 1", "line 2", "line 3"]);
|
||||
});
|
||||
it("should split three Windows line endings lines", function() {
|
||||
let lines = [];
|
||||
let ls = createSplitter(lines);
|
||||
ls.data(Buffer.from("line 1\r\nline 2\r\nline 3\r\n"));
|
||||
assert.deepEqual(lines, ["line 1", "line 2", "line 3"]);
|
||||
});
|
||||
it("should give the last non-terminated line on .end()", function() {
|
||||
let lines = [];
|
||||
let ls = createSplitter(lines);
|
||||
ls.data(Buffer.from("line a\nline b"));
|
||||
assert.deepEqual(lines, ["line a"]);
|
||||
ls.end();
|
||||
assert.deepEqual(lines, ["line a", "line b"]);
|
||||
});
|
||||
it("should handled partial lines", function() {
|
||||
let lines = [];
|
||||
let ls = createSplitter(lines);
|
||||
ls.data(Buffer.from("part 1"));
|
||||
ls.data(Buffer.from(" part 2 "));
|
||||
ls.data(Buffer.from("part 3\n"));
|
||||
ls.end();
|
||||
assert.deepEqual(lines, ["part 1 part 2 part 3"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseOutput()", function() {
|
||||
let testLines = new Map([
|
||||
[
|
||||
" 1.306 Info RemoteCommandProcessor.cpp:131: Starting RCON interface at port 4000",
|
||||
{
|
||||
format: 'seconds',
|
||||
time: '1.306',
|
||||
type: 'log',
|
||||
level: 'Info',
|
||||
file: "RemoteCommandProcessor.cpp:131",
|
||||
message: "Starting RCON interface at port 4000",
|
||||
}
|
||||
],
|
||||
[
|
||||
" 640.910 Quitting: remote-quit.",
|
||||
{
|
||||
format: 'seconds',
|
||||
time: '640.910',
|
||||
type: 'generic',
|
||||
message: "Quitting: remote-quit.",
|
||||
}
|
||||
],
|
||||
[
|
||||
"1641.421 Goodbye",
|
||||
{
|
||||
format: 'seconds',
|
||||
time: '1641.421',
|
||||
type: 'generic',
|
||||
message: "Goodbye",
|
||||
}
|
||||
],
|
||||
[
|
||||
"2019-01-20 10:30:04 [JOIN] User joined the game",
|
||||
{
|
||||
format: 'date',
|
||||
time: '2019-01-20 10:30:04',
|
||||
type: 'action',
|
||||
action: "JOIN",
|
||||
message: "User joined the game",
|
||||
}
|
||||
],
|
||||
[
|
||||
"2019-01-20 10:30:07 [CHAT] User: chat message",
|
||||
{
|
||||
format: 'date',
|
||||
time: '2019-01-20 10:30:07',
|
||||
type: 'action',
|
||||
action: "CHAT",
|
||||
message: "User: chat message",
|
||||
}
|
||||
],
|
||||
[
|
||||
"2019-01-20 10:30:14 [COMMAND] User (command): blah",
|
||||
{
|
||||
format: 'date',
|
||||
time: '2019-01-20 10:30:14',
|
||||
type: 'action',
|
||||
action: "COMMAND",
|
||||
message: "User (command): blah",
|
||||
}
|
||||
],
|
||||
[
|
||||
"2019-01-20 10:30:14 Cannot execute command. Error: [string \"blah\"]:1: syntax error near <eof>",
|
||||
{
|
||||
format: 'date',
|
||||
time: '2019-01-20 10:30:14',
|
||||
type: 'generic',
|
||||
message: "Cannot execute command. Error: [string \"blah\"]:1: syntax error near <eof>",
|
||||
}
|
||||
],
|
||||
[
|
||||
"2019-01-20 10:30:21 [LEAVE] User left the game",
|
||||
{
|
||||
format: 'date',
|
||||
time: '2019-01-20 10:30:21',
|
||||
type: 'action',
|
||||
action: "LEAVE",
|
||||
message: "User left the game",
|
||||
}
|
||||
],
|
||||
[
|
||||
"Error while running event level::on_nth_tick(1)",
|
||||
{
|
||||
format: 'none',
|
||||
type: 'generic',
|
||||
message: "Error while running event level::on_nth_tick(1)",
|
||||
}
|
||||
],
|
||||
])
|
||||
|
||||
it("should parse the test lines", function() {
|
||||
for (let [line, reference] of testLines) {
|
||||
reference.source = 'test';
|
||||
let output = factorio._parseOutput(line, 'test');
|
||||
delete output.received;
|
||||
assert.deepEqual(output, reference);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("class FactorioServer", function() {
|
||||
let writePath = path.join("test", "temp", "should_not_exist");
|
||||
let server = new factorio.FactorioServer(path.join("test", "file", "factorio", "data"), writePath, {});
|
||||
|
||||
describe(".init()", function() {
|
||||
it("should not throw on first call", async function() {
|
||||
await server.init();
|
||||
});
|
||||
|
||||
it("should throw if called twice", async function() {
|
||||
await assert.rejects(server.init(), new Error("Expected state new but state is init"));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,115 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const parallel = require("mocha.parallel");
|
||||
const mkdirp = require("mkdirp");
|
||||
|
||||
const factorioServer = require("lib/factorioServer");
|
||||
|
||||
describe("class factorioServer/index.js", ()=>{
|
||||
it("is a class (or at least a function)", ()=>{
|
||||
assert.equal(typeof factorioServer, "function");
|
||||
});
|
||||
describe(".deleteInstance(instanceName)", ()=>{
|
||||
it("deletes an instance, defaulting to the classes instanceDirectory", async () => {
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.resolve(__dirname, "_testData_deleteInstance"),
|
||||
instanceName: "Unit testing instance",
|
||||
});
|
||||
await server.initialize();
|
||||
await server.deleteInstance();
|
||||
assert.equal(await server.isValidInstance(), false);
|
||||
return true;
|
||||
});
|
||||
});
|
||||
describe(".isValidInstance(instanceName)", ()=>{
|
||||
it("tells us whether an instance exists or not", async () => {
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.join(__dirname, "_testData_isValidInstance"),
|
||||
instanceName: "Unit testing instance",
|
||||
});
|
||||
await server.deleteInstance();
|
||||
let status = await server.isValidInstance();
|
||||
assert.equal(status, false);
|
||||
let x = await server.initialize(); // creates our instance
|
||||
let status2 = await server.isValidInstance();
|
||||
assert.equal(status2, true);
|
||||
|
||||
await server.deleteInstance(); // clean up folder
|
||||
return true;
|
||||
});
|
||||
it("does not get tricked by an empty folder", async () => {
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.join(__dirname, "_testData_isValidInstance_2"),
|
||||
instanceName: "Unit testing instance",
|
||||
});
|
||||
assert.equal(await server.isValidInstance(), false);
|
||||
|
||||
mkdirp.sync(path.join(__dirname, "_testData_isValidInstance_2")); // create empty folder simulating instance
|
||||
assert.equal(await server.isValidInstance(), false);
|
||||
|
||||
await server.initialize(); // create a proper instance
|
||||
assert.equal(await server.isValidInstance(), true);
|
||||
|
||||
await server.deleteInstance(); // delete the instance and ensure we see that it is in fact deleted
|
||||
assert.equal(await server.isValidInstance(), false);
|
||||
|
||||
return true;
|
||||
}).timeout(5000);
|
||||
});
|
||||
describe(".initialize()", ()=>{
|
||||
it("creates an instance", done => {
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.resolve(__dirname, "_testData_initialize"),
|
||||
instanceName: "Unit testing instance",
|
||||
});
|
||||
server.initialize().then(()=>{
|
||||
return server.deleteInstance();
|
||||
}).then(() => {
|
||||
done();
|
||||
}).catch(e => console.log(e));
|
||||
});
|
||||
});
|
||||
describe(".createMap(mapName.zip)", ()=>{
|
||||
it("creates a map file as instanceDirectory/saves/map.zip", async function(){
|
||||
/* istanbul ignore if */ if (process.env.FAST_TEST) this.skip();
|
||||
this.timeout(20000); // these test often takes a lot of time in the map creation step (4500 ms on i3 dev system)
|
||||
let relativePath = path.relative('.', __dirname);
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.join(relativePath, "_testData_createMap"),
|
||||
instanceName: "Unit testing instance",
|
||||
onProgress: false,
|
||||
});
|
||||
await server.initialize();
|
||||
let maps = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
||||
assert.equal(maps.length, 0);
|
||||
await server.createMap();
|
||||
let maps2 = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
||||
assert(maps2.includes("map.zip"));
|
||||
|
||||
await server.deleteInstance();
|
||||
return true;
|
||||
});
|
||||
it("supports custom filenames", async function(){
|
||||
/* istanbul igoner if */ if (process.env.FAST_TEST) this.skip();
|
||||
this.timeout(20000);
|
||||
let relativePath = path.relative('.', __dirname);
|
||||
let server = new factorioServer({
|
||||
instanceDirectory: path.join(relativePath, "_testData_createMap_2"),
|
||||
instanceName: "Unit testing instance",
|
||||
onProgress: false,
|
||||
});
|
||||
await server.initialize();
|
||||
|
||||
let maps = fs.readdirSync(path.join(relativePath, "_testData_createMap_2", "saves"));
|
||||
assert.equal(maps.length, 0);
|
||||
await server.createMap("mapWithCustomName.zip");
|
||||
let maps2 = fs.readdirSync(path.join(relativePath, "_testData_createMap_2", "saves"));
|
||||
assert(maps2.includes("mapWithCustomName.zip"));
|
||||
|
||||
await server.deleteInstance();
|
||||
return true;
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user