Merge pull request #764 from Cooldude2606/refactor/config-file-path

Refactor config file paths into the config class
This commit is contained in:
Daniel authored and GitHub committed 2025-05-19 20:52:14 +02:00
commit a39911d667
15 files changed
+311 -119

No files matched your search

+1 -1
View File
@@ -28,7 +28,7 @@ describe("controller/src/Controller", function() {
/** @type {Controller} */
let controller, mockInstanceConfig;
before(async function() {
controller = new Controller({}, [], "", new ControllerConfig("controller"));
controller = new Controller({}, [], new ControllerConfig("controller"));
mockInstanceConfig = new MockInstanceConfig(new Map([
["instance.id", 100], ["instance.name", "test"], ["factorio.settings", []],
]));
+1 -1
View File
@@ -10,7 +10,7 @@ describe("controller/src/WsServer", function() {
before(function() {
let config = new ControllerConfig("controller");
config.set("controller.trusted_proxies", "127.0.0.0/8, ::1");
let controller = new Controller({}, [], "", config);
let controller = new Controller({}, [], config);
wsServer = controller.wsServer;
});
describe(".remoteAddr()", function() {
+8 -6
View File
@@ -10,13 +10,15 @@ const { _discoverInstances } = require("@clusterio/host/dist/node/src/Host");
describe("Host testing", function() {
describe("discoverInstances()", function() {
it("should discover test instance", async function() {
let instancePath = path.join("test", "file", "instances");
let instanceInfos = await _discoverInstances(instancePath);
const instancePath = path.join("test", "file", "instances");
const instanceInfos = await _discoverInstances(instancePath);
let referenceConfig = new lib.InstanceConfig("host");
referenceConfig.set("instance.id", 1);
referenceConfig.set("instance.name", "test");
referenceConfig.dirty = false;
const configPath = path.join(instancePath, "test", "instance.json");
const referenceConfig = new lib.InstanceConfig("host", {
...Host.instanceConfigWarning,
"instance.id": 1,
"instance.name": "test",
}, configPath);
assert.deepEqual(instanceInfos, new Map([
[1, {
+37 -20
View File
@@ -175,6 +175,16 @@ async function exec(command, options = {}) {
return await util.promisify(child_process.exec)(command, options);
}
async function execController(...args) {
args[0] = `node --enable-source-maps ../../packages/controller ${args[0]}`;
return await exec(...args);
}
async function execHost(...args) {
args[0] = `node --enable-source-maps ../../packages/host ${args[0]}`;
return await exec(...args);
}
async function execCtl(...args) {
args[0] = `node --enable-source-maps ../../packages/ctl ${args[0]}`;
return await exec(...args);
@@ -191,8 +201,15 @@ function getControl() {
function spawn(name, cmd, waitFor) {
// eslint-disable-next-line node/no-process-env
const silent = process.env.SILENT_TEST;
const bootstrap = !controllerProcess || !hostProcess;
function log(...args) {
if (!silent || bootstrap) {
console.log(...args);
}
}
return new Promise((resolve, reject) => {
console.log(cmd);
log(cmd);
let parts = cmd.split(" ");
let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test") });
let stdout = new LineSplitter({ readableObjectMode: true });
@@ -206,10 +223,10 @@ function spawn(name, cmd, waitFor) {
}
resolve(process);
}
console.log(name, line);
log(name, line);
};
let onDataErr = line => {
console.log(name, line.toString("utf8"));
log(name, line.toString("utf8"));
};
stdout.on("data", onDataOut);
stderr.on("data", onDataErr);
@@ -265,33 +282,33 @@ before(async function() {
});
console.log("Setting Controller Config");
await exec("node ../../packages/controller config set controller.auth_secret TestSecretDoNotUse");
await exec("node ../../packages/controller config set controller.http_port 8880");
await exec("node ../../packages/controller config set controller.https_port 4443");
await exec("node ../../packages/controller config set controller.heartbeat_interval 0.25");
await exec("node ../../packages/controller config set controller.session_timeout 2");
await exec("node ../../packages/controller config set controller.tls_certificate ../../test/file/tls/cert.pem");
await exec("node ../../packages/controller config set controller.tls_private_key ../../test/file/tls/key.pem");
await execController("config set controller.auth_secret TestSecretDoNotUse");
await execController("config set controller.http_port 8880");
await execController("config set controller.https_port 4443");
await execController("config set controller.heartbeat_interval 0.25");
await execController("config set controller.session_timeout 2");
await execController("config set controller.tls_certificate ../../test/file/tls/cert.pem");
await execController("config set controller.tls_private_key ../../test/file/tls/key.pem");
console.log("Setting Controller Plugins");
await exec("node ../../packages/ctl plugin add ../../plugins/global_chat");
await exec("node ../../packages/ctl plugin add ../../plugins/research_sync");
await exec("node ../../packages/ctl plugin add ../../plugins/statistics_exporter");
await exec("node ../../packages/ctl plugin add ../../plugins/subspace_storage");
await exec("node ../../packages/ctl plugin add ../../plugins/player_auth");
await execCtl("plugin add ../../plugins/global_chat");
await execCtl("plugin add ../../plugins/research_sync");
await execCtl("plugin add ../../plugins/statistics_exporter");
await execCtl("plugin add ../../plugins/subspace_storage");
await execCtl("plugin add ../../plugins/player_auth");
console.log("Bootstrapping");
await exec("node ../../packages/controller bootstrap create-admin test");
await exec("node ../../packages/controller bootstrap create-ctl-config test");
await exec("node ../../packages/ctl control-config set control.tls_ca ../../test/file/tls/cert.pem");
await execController("bootstrap create-admin test");
await execController("bootstrap create-ctl-config test");
await execCtl("control-config set control.tls_ca ../../test/file/tls/cert.pem");
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
await execCtl("host create-config --id 4 --name host --generate-token");
const relativeFactorioDir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
await exec(`node ../../packages/host config set host.factorio_directory ${relativeFactorioDir}`);
await exec("node ../../packages/host config set host.tls_ca ../../test/file/tls/cert.pem");
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/);
+2 -7
View File
@@ -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, "invalid", hostConfig, undefined, []);
return new Host(hostSide, hostConfig, undefined, []);
}
/**
@@ -107,12 +107,7 @@ describe("Integration of link routing", function() {
let controlB;
beforeEach(function() {
controller = new Controller(
{},
[],
"invalid",
new lib.ControllerConfig("controller")
);
controller = new Controller({}, [], new lib.ControllerConfig("controller"));
hostA = connectHost(controller, 10, []);
hostB = connectHost(controller, 20, []);
instanceA1 = connectInstance(controller, hostA, 11);
+169 -4
View File
@@ -1,5 +1,7 @@
"use strict";
const lib = require("@clusterio/lib");
const fs = require("fs-extra");
const path = require("path");
const assert = require("assert").strict;
const CA = lib.ConfigAccess;
@@ -7,6 +9,8 @@ const CA = lib.ConfigAccess;
describe("lib/config/classes", function() {
describe("Config", function() {
class TestConfig extends lib.Config {
static defaultAccess = ["local", "remote"];
static migrations(config) {
if (config.hasOwnProperty("test.migration")) {
config["alpha.foo"] = config["test.migration"];
@@ -38,10 +42,6 @@ describe("lib/config/classes", function() {
"test.cred": { credential: ["local"] },
"test.rdo": { readonly: ["local"] },
};
constructor(location, fields) {
super(location, fields, ["local", "remote"]);
}
}
describe("constructor", function() {
@@ -76,6 +76,10 @@ describe("lib/config/classes", function() {
assert.equal(config.get("alpha.foo"), "foo");
assert.equal(config.fields["test.migration"], undefined);
});
it("should accept a filepath", function() {
const testInstance = new TestConfig("local", undefined, "filepath");
assert.equal(testInstance.filepath, "filepath");
});
});
describe(".toJSON()", function() {
@@ -187,6 +191,167 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.fields["test.priv"], undefined);
assert.equal(testInstance.fields["test.func"], 42);
});
it("should accept a filepath", function() {
const testInstance = TestConfig.fromJSON({}, "local", "filepath");
assert.equal(testInstance.filepath, "filepath");
});
});
describe("static .fromFile", function() {
const filepath = path.join("temp", "test", "config_test.json");
it("should throw for file and json errors", async function() {
await fs.writeFile(filepath, "abc");
assert.rejects(
TestConfig.fromFile("local", filepath),
new Error(
`${filepath}: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data`
)
);
assert.rejects(
TestConfig.fromFile("local", `${filepath}.notExist`),
);
});
it("should throw on incorrect input passed", async function() {
await fs.writeFile(filepath, "null");
await assert.rejects(
TestConfig.fromFile("local", filepath),
new Error("Invalid config")
);
await fs.writeFile(filepath, "undefined");
assert.rejects(
TestConfig.fromFile("local", filepath),
new Error("Invalid config")
);
await fs.writeFile(filepath, "[]");
assert.rejects(
TestConfig.fromFile("local", filepath),
new Error("Invalid config")
);
});
it("should load defaults for missing fields", async function() {
await fs.writeJSON(filepath, {
"alpha.foo": "a",
"test.enum": "a",
});
const testInstance = await TestConfig.fromFile("local", filepath);
assert.equal(testInstance.get("alpha.foo"), "a");
assert.deepEqual(testInstance.get("beta.bar"), {});
assert.equal(testInstance.get("test.enum"), "a");
assert.equal(testInstance.get("test.test"), null);
assert.equal(testInstance.get("test.func"), 42);
assert.equal(testInstance.filepath, filepath);
});
it("should load fields", async function() {
await fs.writeJSON(filepath, {
"test.enum": "c",
"test.test": "blah",
"test.func": 22,
"test.bool": null,
"test.json": { valid: true },
"test.priv": "bar",
});
const testInstance = await TestConfig.fromFile("local", filepath);
assert.equal(testInstance.get("test.enum"), "c");
assert.equal(testInstance.get("test.test"), "blah");
assert.equal(testInstance.get("test.func"), 22);
assert.equal(testInstance.get("test.bool"), null);
assert.equal(testInstance.get("test.priv"), "bar");
assert.deepEqual(testInstance.get("test.json"), { valid: true });
assert.equal(testInstance.filepath, filepath);
});
it("should preserve unknown fields", async function() {
const testFields = {
"extra.blah": true,
"extra.spam": "foobar",
"alpha.foo": "true",
"beta.bar": { value: 20 },
"test.enum": "a",
"test.test": "blah",
"test.func": 50,
"test.bool": false,
"test.json": {},
"test.rest": {},
"test.priv": null,
"test.cred": null,
"test.rdo": null,
"test.alpha": null,
"test.beta": "decay",
"test.gamma": 99,
};
await fs.writeJson(filepath, testFields);
const testInstance = await TestConfig.fromFile("local", filepath);
assert.deepEqual(testInstance.toJSON(), testFields);
assert.equal(testInstance.filepath, filepath);
});
it("should ignore inaccessible fields", async function() {
await fs.writeJSON(filepath, { "test.priv": "bad" });
const testInstance = await TestConfig.fromFile("remote", filepath);
assert.equal(testInstance.fields["test.priv"], undefined);
assert.equal(testInstance.fields["test.func"], 42);
assert.equal(testInstance.filepath, filepath);
});
});
describe(".save()", function() {
const filepath = path.join("temp", "test", "config_test.json");
beforeEach(async function() {
await fs.remove(filepath);
assert(!await fs.exists(filepath), "File was not removed");
});
it("should throw if there is no filepath", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" });
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await assert.rejects(
testInstance.save(),
new Error("Cannot save config which has no filepath")
);
});
it("should do nothing when not dirty", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
await testInstance.save();
assert(!await fs.exists(filepath), "File was created");
});
it("should save data to file", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await testInstance.save();
const json = await fs.readJSON(filepath);
assert.deepEqual(json, testInstance.toJSON());
});
it("should clear the dirty flag after saving", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await testInstance.save();
assert(await fs.exists(filepath), "File was not created");
await fs.remove(filepath);
assert(!await fs.exists(filepath), "File was not removed");
await testInstance.save();
assert(!await fs.exists(filepath), "File was created");
});
it("should be round trip savable", async function() {
const testInstance = new TestConfig("local", {
"extra.blah": true,
"extra.spam": "foobar",
"alpha.foo": "true",
"beta.bar": { value: 20 },
"test.enum": "a",
"test.test": "blah",
"test.func": 50,
"test.bool": false,
"test.json": {},
"test.rest": {},
"test.priv": null,
"test.cred": null,
"test.rdo": null,
"test.alpha": null,
"test.beta": "decay",
"test.gamma": 99,
}, filepath);
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await testInstance.save();
const loadedInstance = await TestConfig.fromFile("local", filepath);
assert.deepEqual(loadedInstance, testInstance);
});
});
describe(".update()", function() {