mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 07:24:52 +09:00
Merge pull request #815 from Cooldude2606/feature/database-lock
Add extra startup warnings to CLI: User count and Lock file
This commit is contained in:
10 files changed
+506
-27
No files matched your search
@@ -13,8 +13,8 @@ const { wait } = lib;
|
||||
const testStrings = require("../lib/factorio/test_strings");
|
||||
const {
|
||||
TestControl, TestControlConnector, url, controlToken, slowTest,
|
||||
exec, execCtl, execCtlProcess, execHost, sendRcon, getControl,
|
||||
spawnNode, instancesDir, factorioDir,
|
||||
execCtl, execCtlProcess, execController, execHost, sendRcon, getControl,
|
||||
spawnNode, instancesDir, factorioDir, databaseDir, controllerConfigPath,
|
||||
} = require("./index");
|
||||
|
||||
|
||||
@@ -109,16 +109,59 @@ describe("Integration of Clusterio", function() {
|
||||
describe("clusteriocontroller", function() {
|
||||
describe("bootstrap generate-user-token", function() {
|
||||
it("work for existing user", async function() {
|
||||
await exec("node ../../packages/controller bootstrap generate-user-token test");
|
||||
await execController("bootstrap generate-user-token test");
|
||||
});
|
||||
|
||||
it("fails if user does not exist", async function() {
|
||||
await assert.rejects(
|
||||
exec("node ../../packages/controller bootstrap generate-user-token invalid")
|
||||
execController("bootstrap generate-user-token invalid")
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("bootstrap create-admin", function() {
|
||||
it("should refuse to modify the database files when locked", async function() {
|
||||
await assert.rejects(
|
||||
execController("bootstrap create-admin BootstrapAdminTest")
|
||||
);
|
||||
});
|
||||
it("should modify the database files when locked if bypass option is given", async function() {
|
||||
const lockFilePath = `${controllerConfigPath}.lock`;
|
||||
await fs.copyFile(lockFilePath, "temp.lock");
|
||||
try {
|
||||
await execController("bootstrap create-admin BootstrapAdminTest2 --bypass-lock-file");
|
||||
} finally {
|
||||
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
|
||||
}
|
||||
|
||||
const json = await fs.readJSON(path.join(databaseDir, "users.json"));
|
||||
assert.equal(Object.values(json).some(user => user.name === "BootstrapAdminTest2"), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("config", function() {
|
||||
it("can read the config file", async function() {
|
||||
await execController("config list");
|
||||
});
|
||||
it("should refuse to modify the config file while locked", async function() {
|
||||
await assert.rejects(
|
||||
execController("config set controller.name ConfigEditTest")
|
||||
);
|
||||
});
|
||||
it("should modify the config file while locked if bypass option is given", async function() {
|
||||
const lockFilePath = `${controllerConfigPath}.lock`;
|
||||
await fs.copyFile(lockFilePath, "temp.lock");
|
||||
try {
|
||||
await execController("config set controller.name ConfigEditTest2 --bypass-lock-file");
|
||||
} finally {
|
||||
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
|
||||
}
|
||||
|
||||
const json = await fs.readJSON(path.join(controllerConfigPath));
|
||||
assert.equal(json["controller.name"], "ConfigEditTest2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("run", function() {
|
||||
it("should handle resume of an active connection", async function() {
|
||||
slowTest(this);
|
||||
@@ -143,6 +186,38 @@ describe("Integration of Clusterio", function() {
|
||||
await events.once(connectorB, "resume");
|
||||
await connectorB.close(1000, "");
|
||||
});
|
||||
it("should refuse to start a second process while locked", async function() {
|
||||
await assert.rejects(execController("run"));
|
||||
});
|
||||
it("should start a second process while locked if bypass option is given", async function() {
|
||||
slowTest(this);
|
||||
const lockFilePath = `${controllerConfigPath}.lock`;
|
||||
await fs.copyFile(lockFilePath, "temp.lock");
|
||||
try {
|
||||
await execController("run --bypass-lock-file");
|
||||
} catch (err) {
|
||||
assert.equal(/Error: Server listening failed/.test(err.stderr), true);
|
||||
}
|
||||
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
|
||||
});
|
||||
it("should refuse to start when no users are loaded", async function() {
|
||||
const dir = path.join("temp", "test", "empty_controller");
|
||||
await fs.emptyDir(dir);
|
||||
await assert.rejects(execController("run", { cwd: dir }));
|
||||
});
|
||||
it("should start when no users are loaded if bypass option given", async function() {
|
||||
slowTest(this);
|
||||
const dir = path.join("temp", "test", "empty_controller");
|
||||
await fs.emptyDir(dir);
|
||||
const child = await spawnNode(
|
||||
"altController",
|
||||
"../../../packages/controller run --no-check-user-count",
|
||||
/Started controller/,
|
||||
{ cwd: dir },
|
||||
);
|
||||
assert.ok(child.kill());
|
||||
assert.equal(child.killed, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("queryLogRequestHandler", function() {
|
||||
@@ -245,6 +320,67 @@ describe("Integration of Clusterio", function() {
|
||||
await stopAltHost(hostProcess);
|
||||
}
|
||||
});
|
||||
describe("config", function() {
|
||||
it("can read the config file", async function() {
|
||||
await execHost("config list");
|
||||
});
|
||||
it("should refuse to modify the config file while locked", async function() {
|
||||
slowTest(this);
|
||||
let hostProcess; // Alt host used since the process could get killed
|
||||
const config = "alt-host-config.json";
|
||||
try {
|
||||
hostProcess = await startAltHost();
|
||||
await assert.rejects(
|
||||
execHost(`--config ${config} config set host.name ConfigEditTest`)
|
||||
);
|
||||
} finally {
|
||||
await stopAltHost(hostProcess);
|
||||
}
|
||||
});
|
||||
it("should modify the config file while locked if bypass option is given", async function() {
|
||||
slowTest(this);
|
||||
let hostProcess; // Alt host used since the process could get killed
|
||||
const config = "alt-host-config.json";
|
||||
try {
|
||||
hostProcess = await startAltHost();
|
||||
await execHost(`--config ${config} config set host.name ConfigEditTest2 --bypass-lock-file`);
|
||||
} finally {
|
||||
await stopAltHost(hostProcess);
|
||||
}
|
||||
|
||||
const json = await fs.readJSON(path.join("temp", "test", config));
|
||||
assert.equal(json["host.name"], "ConfigEditTest2");
|
||||
});
|
||||
});
|
||||
describe("run", function() {
|
||||
it("should refuse to start a second process while locked", async function() {
|
||||
slowTest(this);
|
||||
let hostProcess; // Alt host used since the process could get killed
|
||||
const config = "alt-host-config.json";
|
||||
try {
|
||||
hostProcess = await startAltHost();
|
||||
await assert.rejects(
|
||||
execHost(`--config ${config} run`)
|
||||
);
|
||||
} finally {
|
||||
await stopAltHost(hostProcess);
|
||||
}
|
||||
});
|
||||
it("should start a second process while locked if bypass option is given", async function() {
|
||||
slowTest(this);
|
||||
let hostProcessA, hostProcessB; // Alt host used since the process could get killed
|
||||
const config = "alt-host-config.json";
|
||||
try {
|
||||
hostProcessA = await startAltHost();
|
||||
hostProcessB = await spawnNode(
|
||||
"alt-host-b:", `../../packages/host run --config ${config} --bypass-lock-file`, /Started host/
|
||||
);
|
||||
} finally {
|
||||
await stopAltHost(hostProcessA);
|
||||
await stopAltHost(hostProcessB);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("clusterioctl", function() {
|
||||
|
||||
+14
-12
@@ -173,26 +173,26 @@ 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");
|
||||
|
||||
async function exec(command, options = {}) {
|
||||
function exec(command, options = {}) {
|
||||
// Uncomment to show commands run in tests
|
||||
// console.log(command);
|
||||
options = { cwd: path.join("temp", "test"), ...options };
|
||||
return await util.promisify(child_process.exec)(command, options);
|
||||
return util.promisify(child_process.exec)(command, options);
|
||||
}
|
||||
|
||||
async function execController(...args) {
|
||||
function execController(...args) {
|
||||
args[0] = `node --enable-source-maps ../../packages/controller ${args[0]}`;
|
||||
return await exec(...args);
|
||||
return exec(...args);
|
||||
}
|
||||
|
||||
async function execHost(...args) {
|
||||
function execHost(...args) {
|
||||
args[0] = `node --enable-source-maps ../../packages/host ${args[0]}`;
|
||||
return await exec(...args);
|
||||
return exec(...args);
|
||||
}
|
||||
|
||||
async function execCtlProcess(...args) {
|
||||
function execCtlProcess(...args) {
|
||||
args[0] = `node --enable-source-maps ../../packages/ctl ${args[0]}`;
|
||||
return await exec(...args);
|
||||
return exec(...args);
|
||||
}
|
||||
|
||||
let inExecCtl = false;
|
||||
@@ -229,7 +229,8 @@ function getControl() {
|
||||
return control;
|
||||
}
|
||||
|
||||
function spawn(name, cmd, waitFor) {
|
||||
/** @returns {Promise<child_process.ChildProcess>} */
|
||||
function spawn(name, cmd, waitFor, options = {}) {
|
||||
|
||||
const silent = process.env.SILENT_TEST;
|
||||
const bootstrap = !controllerProcess || !hostProcess;
|
||||
@@ -242,7 +243,7 @@ function spawn(name, cmd, waitFor) {
|
||||
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") });
|
||||
let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test"), ...options });
|
||||
let stdout = new LineSplitter({ readableObjectMode: true });
|
||||
let stderr = new LineSplitter({ readableObjectMode: true });
|
||||
let onDataOut = line => {
|
||||
@@ -266,8 +267,8 @@ function spawn(name, cmd, waitFor) {
|
||||
});
|
||||
}
|
||||
|
||||
async function spawnNode(name, cmd, waitFor) {
|
||||
return await spawn(name, `node --enable-source-maps ${cmd}`, waitFor);
|
||||
function spawnNode(name, cmd, waitFor, options) {
|
||||
return spawn(name, `node --enable-source-maps ${cmd}`, waitFor, options);
|
||||
}
|
||||
|
||||
before(async function() {
|
||||
@@ -394,6 +395,7 @@ module.exports = {
|
||||
TestControl,
|
||||
TestControlConnector,
|
||||
TestHostConnector,
|
||||
execController,
|
||||
execCtlProcess,
|
||||
slowTest,
|
||||
get,
|
||||
|
||||
Reference in new issue
Block a user