Files
factorio-clusterio/test/integration/server.js
HornwitserandHornwitser 94692dc418 Fix tests failing due broken Factorio detection (#839)
* Fix tests failing due to missmatched Factorio detection

When the configured factorio directory is full of junk then the tests
use the first directory it finds for one of its tests, while the server
code uses a more elaborate logic to figure this out.

Turns out this is complicating things more than needed. Simply trying
each entry in the directory and seeing if it works when treated as a
directory always work, regardsless of symlinks, junctions,
reparse-points or whatever else filesystem magic there is.

* Skip tests requiring Factorio if it's not present

Prevent confusing test failures by skipping all the tests that require a
working Factorio installation to sucteed, and print a warning at the end
of the test suite if tests may have been skipped due to this.

* Handle ENOTDIR when determining Factorio version

If the path to the factorio install contains a component that is not a
file then ENOTDIR may be returned on POSIX like systems.  Treat this the
same as ENOENT so that detection logic works when used on a file.

---------

Co-authored-by: Hornwitser <code@hornwitser.no>
2026-03-01 23:06:03 +00:00

327 lines
9.5 KiB
JavaScript

"use strict";
const assert = require("assert").strict;
const events = require("events");
const fs = require("fs-extra");
const path = require("path");
const lib = require("@clusterio/lib");
const { InstanceManager } = require("@clusterio/controller");
const { FactorioServer, _getFactorioVersion } = require("@clusterio/host/dist/node/src/server");
const { logger } = lib;
const { slowTest, factorioDir, requiresFactorio } = require("./index");
describe("Integration of host/src/server", function() {
requiresFactorio(this);
describe("getFactorioVersion()", function() {
it("should get a version from factorio's changelog.txt", async function() {
function checkVersion(version) {
if (!/^\d+\.\d+\.\d+$/.test(version)) {
assert.fail(`Detected version '${version}' does not followed the format x.y.z`);
}
}
let version = await _getFactorioVersion(factorioDir);
if (version !== null) {
checkVersion(version);
} else {
for (let name of await fs.readdir(factorioDir)) {
// Avoid any fancy directory detection in order to follow symlinks
version = await _getFactorioVersion(path.join(factorioDir, name));
if (version !== null) {
checkVersion(version);
}
}
}
});
});
describe("class FactorioServer", function() {
let writePath = path.join("temp", "test", "integration");
let server = new FactorioServer(factorioDir, writePath, {});
let logFile;
before(async function() {
// Delete result from previous run of these tests
if (await fs.pathExists(writePath)) {
await fs.remove(writePath);
}
await fs.ensureDir(writePath);
logFile = fs.createWriteStream(path.join(writePath, "log.txt"), "utf8");
server.on("output", function(output) {
if (output.source === "stderr") {
// Factorio doesn't print on stderr but sometimes on
// Travis a BrokenPipeError is printed on stderr after
// the server has closed for mysterious reasons.
// I have no idea what causes it, but it causes a write
// after end error if not handled.
logger.error(`Factorio stderr: ${JSON.stringify(output)}`);
return;
}
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("aligns with the default settings in Clusterio", async function() {
const unusedDefaultKeys = new Set(Object.keys(InstanceManager.DefaultFactorioSettings));
for (const key of Object.keys(settings)) {
unusedDefaultKeys.delete(key);
}
assert(
unusedDefaultKeys.size === 0,
`Factorio's server-settings.example.json does not contain the key(s) ${[...unusedDefaultKeys]}`
);
});
});
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", undefined, { width: 50, height: 50 });
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 not having internet
let settings = await server.exampleSettings();
settings.visibility = { public: false, lan: true };
settings.require_user_verification = false;
await fs.outputFile(server.writePath("server-settings.json"), JSON.stringify(settings, null, "\t"));
// 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");
});
it("throws on non-empty response when enabled", async function() {
slowTest(this);
await assert.rejects(
server.sendRcon("/sc rcon.print('fail')", true),
new Error('Expected empty response but got "fail\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");
});
});
describe(".start() error handling", function() {
it("should handle factorio erroring out", async function() {
slowTest(this);
log(".start() for error handling");
await server.start("test.zip");
if (!server._rconReady) {
await events.once(server, "rcon-ready");
}
server.sendRcon("/c script.on_nth_tick(1, function() o.o = 1 end)").catch(() => {});
function discard() { }
server.on("error", discard);
await new Promise(resolve => server.once("exit", resolve));
server.off("error", discard);
});
});
describe(".stop() hang detection", function() {
it("should detect factorio hanging on shutdown", async function() {
slowTest(this);
log(".start() for hang detection");
await server.start("test.zip");
if (!server._rconReady) {
await events.once(server, "rcon-ready");
}
server.sendRcon("/c while true do end").catch(() => {});
await new Promise((resolve) => setTimeout(resolve, 300));
log(".stop() for hang detection");
server.shutdownTimeoutMs = 2000;
await server.stop();
server.shutdownTimeoutMs = 0;
});
it("should not hang if RCON fails to connect while stopping", async function() {
slowTest(this);
const origWaitForReady = server._waitForReady;
server._waitForReady = () => {
server._waitForReady = origWaitForReady;
server.rconPort -= 1; // Mangle port so it fails to connect.
return server._waitForReady();
};
try {
await server.start("test.zip");
if (server._rconReady) {
assert.fail("Test failed, RCON managed to connect");
}
await server.stop();
} finally {
if (server._state !== "init") {
await server.kill();
assert.fail("Server did not stop");
}
}
});
it("should not hang if RCON fails to connect before stopping", async function() {
slowTest(this);
const origWaitForReady = server._waitForReady;
server._waitForReady = () => {
server._waitForReady = origWaitForReady;
server.rconPassword = "wrong"; // Mangle password so it fails to authenticate.
return server._waitForReady();
};
try {
await server.start("test.zip");
if (server._rconReady) {
assert.fail("Test failed, RCON managed to connect");
}
await events.once(server, "rcon-ready");
await server.stop();
} finally {
if (server._state !== "init") {
await server.kill();
assert.fail("Server did not stop");
}
}
});
it("should work if RCON has dropped out", async function() {
slowTest(this);
try {
await server.start("test.zip");
if (!server._rconReady) {
await events.once(server, "rcon-ready");
}
await server._rconClient.end();
await server.stop();
} finally {
if (server._state !== "init") {
await server.kill();
assert.fail("Server did not stop");
}
}
});
});
describe(".start() termination detection", function() {
it("should tell if Factorio got killed", async function() {
slowTest(this);
log(".start() for kill detection");
let startPromise = server.start("test.zip");
server.once("output", () => server._server.kill("SIGKILL"));
await assert.rejects(
startPromise,
new lib.EnvironmentError(
"Factorio server was unexpectedly killed, is the system low on memory?"
)
);
});
it("should tell if Factorio unexpectedly closed with a code", async function() {
slowTest(this);
log(".start() for unexpected close detection");
await assert.rejects(
server.start("does-not-exist.zip"),
new lib.EnvironmentError("Factorio server unexpectedly shut down with code 1")
);
});
});
});
});