mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Merge pull request #1010 from bbassie/feature/plugin-browser
Plugin management through web interface
This commit is contained in:
1 parent
e36a76acc5
commit
ffe82f81d8
48 files changed
+4275
-536
No files matched your search
+163
-10
@@ -1,17 +1,25 @@
|
||||
/* eslint-disable node/no-sync */
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import fsSync from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import { LockFile } from "@clusterio/lib";
|
||||
|
||||
const baseDir = path.join("temp", "test", "lock_file");
|
||||
const ownContent = `${process.pid} ${performance.timeOrigin}\n`;
|
||||
// The parent process (mocha) is alive but isn't this process
|
||||
const otherContent = `${process.ppid}\n`;
|
||||
const exitedContent = "999999999\n";
|
||||
|
||||
describe("class LockFile", function () {
|
||||
/** @type {string} */
|
||||
let filePath;
|
||||
/** @type {string} */
|
||||
let recoveryPath;
|
||||
/** @type {LockFile} */
|
||||
let lock;
|
||||
const writeFile = fs.writeFile;
|
||||
|
||||
before(async function () {
|
||||
await fs.mkdir(baseDir, { recursive: true });
|
||||
@@ -19,17 +27,30 @@ describe("class LockFile", function () {
|
||||
|
||||
beforeEach(function() {
|
||||
filePath = path.join(baseDir, `${Date.now()}-${Math.random()}.lock`);
|
||||
recoveryPath = `${filePath}.recovery`;
|
||||
lock = new LockFile(filePath);
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
fs.writeFile = writeFile;
|
||||
if (await lock.isHeld()) {
|
||||
await lock.release();
|
||||
}
|
||||
await fs.unlink(filePath).catch(() => {});
|
||||
await fs.unlink(recoveryPath).catch(() => {});
|
||||
});
|
||||
|
||||
async function writeOld(content) {
|
||||
await fs.writeFile(filePath, content);
|
||||
const old = new Date(Date.now() - 60e3);
|
||||
await fs.utimes(filePath, old, old);
|
||||
return old;
|
||||
}
|
||||
|
||||
describe("acquire()", function () {
|
||||
it("should create the lock file when acquiring", async function () {
|
||||
await lock.acquire();
|
||||
await fs.access(filePath);
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), ownContent);
|
||||
});
|
||||
|
||||
it("should throw LockFileBusyError if called when not idle", async function () {
|
||||
@@ -38,15 +59,89 @@ describe("class LockFile", function () {
|
||||
});
|
||||
|
||||
it("should remove stale lock file and acquire it", async function () {
|
||||
await fs.writeFile(filePath, "999999\n"); // stale lock
|
||||
await fs.writeFile(filePath, exitedContent);
|
||||
await lock.acquire();
|
||||
await fs.access(filePath);
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), ownContent);
|
||||
await assert.rejects(fs.access(recoveryPath));
|
||||
});
|
||||
|
||||
it("should throw if existing lock file is not stale", async function () {
|
||||
await fs.writeFile(filePath, `${process.pid}\n`);
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
});
|
||||
|
||||
it("should throw if another lock in this process holds the file", async function () {
|
||||
const other = new LockFile(filePath);
|
||||
await other.acquire();
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
await other.release();
|
||||
});
|
||||
|
||||
it("should acquire a lock left by an earlier process with the same pid", async function () {
|
||||
for (const content of [`${process.pid}\n`, `${process.pid} 1234.5\n`]) {
|
||||
await fs.writeFile(filePath, content);
|
||||
await lock.acquire();
|
||||
await lock.release();
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw while the lock file may still be being written", async function () {
|
||||
await fs.writeFile(filePath, "");
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
});
|
||||
|
||||
it("should acquire an unreadable lock that is old", async function () {
|
||||
await writeOld("");
|
||||
await lock.acquire();
|
||||
});
|
||||
|
||||
// Runs takeOver right before this process starts clearing the stale lock, as another process could
|
||||
function beforeRecovery(takeOver) {
|
||||
fs.writeFile = async function(...args) {
|
||||
if (String(args[0]).endsWith(".recovery")) {
|
||||
fs.writeFile = writeFile;
|
||||
await takeOver();
|
||||
}
|
||||
return writeFile(...args);
|
||||
};
|
||||
}
|
||||
|
||||
it("should leave a lock taken after the stale check in place", async function () {
|
||||
await fs.writeFile(filePath, exitedContent);
|
||||
beforeRecovery(async () => {
|
||||
await fs.rm(filePath);
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
});
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), otherContent);
|
||||
await assert.rejects(fs.access(recoveryPath));
|
||||
});
|
||||
|
||||
it("should leave a new empty lock in place of an old empty lock", async function () {
|
||||
const old = await writeOld("");
|
||||
// Another process clears it and creates a new lock that it hasn't written its pid to yet
|
||||
beforeRecovery(async () => {
|
||||
await fs.rm(filePath);
|
||||
await fs.writeFile(filePath, "");
|
||||
});
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), "");
|
||||
assert((await fs.stat(filePath)).mtimeMs > old.getTime() + 30e3);
|
||||
});
|
||||
|
||||
it("should leave a stale lock alone while another process clears it", async function () {
|
||||
await fs.writeFile(filePath, exitedContent);
|
||||
await fs.writeFile(recoveryPath, otherContent);
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileExistsError" });
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), exitedContent);
|
||||
assert.equal(await fs.readFile(recoveryPath, "utf8"), otherContent);
|
||||
});
|
||||
|
||||
it("should throw LockFileRecoveryError if clearing a stale lock was interrupted", async function () {
|
||||
await fs.writeFile(filePath, exitedContent);
|
||||
await fs.writeFile(recoveryPath, exitedContent);
|
||||
await assert.rejects(lock.acquire(), { name: "LockFileRecoveryError" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("release()", function () {
|
||||
@@ -59,6 +154,13 @@ describe("class LockFile", function () {
|
||||
it("should throw LockFileBusyError if called when not acquired", async function () {
|
||||
await assert.rejects(lock.release(), { name: "LockFileBusyError" });
|
||||
});
|
||||
|
||||
it("should leave a lock file it does not own in place", async function () {
|
||||
await lock.acquire();
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
await lock.release();
|
||||
assert.equal(await fs.readFile(filePath, "utf8"), otherContent);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isHeld()", function () {
|
||||
@@ -74,7 +176,7 @@ describe("class LockFile", function () {
|
||||
});
|
||||
|
||||
it("should return false if lock file contains different PID", async function () {
|
||||
await fs.writeFile(filePath, "999999\n"); // simulate another process
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
const held = await lock.isHeld();
|
||||
assert.equal(held, false);
|
||||
});
|
||||
@@ -93,23 +195,54 @@ describe("class LockFile", function () {
|
||||
assert.equal(stale, true);
|
||||
});
|
||||
|
||||
it("should return true for invalid PID in lock file", async function () {
|
||||
await fs.writeFile(filePath, "abc\n"); // non-numeric PID
|
||||
it("should return true for invalid PID in an old lock file", async function () {
|
||||
await writeOld("abc\n");
|
||||
const stale = await lock.isStale();
|
||||
assert.equal(stale, true);
|
||||
});
|
||||
|
||||
it("should return false for invalid PID in a new lock file", async function () {
|
||||
await fs.writeFile(filePath, "abc\n");
|
||||
const stale = await lock.isStale();
|
||||
assert.equal(stale, false);
|
||||
});
|
||||
|
||||
it("should return true for non-existent process PID", async function () {
|
||||
await fs.writeFile(filePath, "999999\n"); // assume PID does not exist
|
||||
await fs.writeFile(filePath, exitedContent);
|
||||
const stale = await lock.isStale();
|
||||
assert.equal(stale, true);
|
||||
});
|
||||
|
||||
it("should return false for current process PID", async function () {
|
||||
await fs.writeFile(filePath, `${process.pid}\n`);
|
||||
it("should return false for another running process", async function () {
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
const stale = await lock.isStale();
|
||||
assert.equal(stale, false);
|
||||
});
|
||||
|
||||
it("should return false for a lock taken by this process", async function () {
|
||||
await lock.acquire();
|
||||
const stale = await new LockFile(filePath).isStale();
|
||||
assert.equal(stale, false);
|
||||
});
|
||||
|
||||
it("should return true for an earlier process with the same pid", async function () {
|
||||
await fs.writeFile(filePath, `${process.pid}\n`);
|
||||
const stale = await lock.isStale();
|
||||
assert.equal(stale, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("holderPid()", function () {
|
||||
it("should return the pid in the lock file", async function () {
|
||||
await fs.writeFile(filePath, `${process.ppid} 1234.5\n`);
|
||||
assert.equal(await lock.holderPid(), process.ppid);
|
||||
});
|
||||
|
||||
it("should return undefined without a valid pid", async function () {
|
||||
assert.equal(await lock.holderPid(), undefined);
|
||||
await fs.writeFile(filePath, "");
|
||||
assert.equal(await lock.holderPid(), undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe("attach/detach exit handlers", function () {
|
||||
@@ -117,10 +250,21 @@ describe("class LockFile", function () {
|
||||
await lock.acquire();
|
||||
const listenersBefore = process.listeners("exit").slice();
|
||||
assert.ok(listenersBefore.includes(lock.handleExitSync));
|
||||
assert.ok(process.listeners("SIGTERM").includes(lock.handleExit));
|
||||
|
||||
await lock.release();
|
||||
const listenersAfter = process.listeners("exit").slice();
|
||||
assert.ok(!listenersAfter.includes(lock.handleExitSync));
|
||||
assert.ok(!process.listeners("SIGTERM").includes(lock.handleExit));
|
||||
});
|
||||
|
||||
it("should only attach the exit handler without handleSignals", async function () {
|
||||
lock = new LockFile(filePath, false);
|
||||
await lock.acquire();
|
||||
assert.ok(process.listeners("exit").includes(lock.handleExitSync));
|
||||
for (const event of ["SIGINT", "SIGTERM", "SIGHUP", "uncaughtException"]) {
|
||||
assert.ok(!process.listeners(event).includes(lock.handleExit), event);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -128,5 +272,14 @@ describe("class LockFile", function () {
|
||||
it("should not throw if file already removed", function () {
|
||||
assert.doesNotThrow(() => lock.handleExitSync()); // simulate non-existent file
|
||||
});
|
||||
|
||||
it("should only remove a lock file it owns", async function () {
|
||||
await fs.writeFile(filePath, otherContent);
|
||||
lock.handleExitSync();
|
||||
assert.equal(fsSync.readFileSync(filePath, "utf8"), otherContent);
|
||||
await fs.writeFile(filePath, ownContent);
|
||||
lock.handleExitSync();
|
||||
assert.equal(fsSync.existsSync(filePath), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user