mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Cover ctl output code that only ran in child processes
CI coverage does not measure the ctl processes the integration tests start, so cover the Ctl log and debug event handlers, start-all, stop-all and pickFields with unit tests, and run mod show, user list flags, parse-exchange-string and log follow in process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f49c633e77
commit
f52821d1cd
4 files changed
+170
-1
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { CommandOutput, errorToJson, toPlain } from "@clusterio/ctl/dist/node/src/command_ops.js";
|
||||
import { CommandOutput, errorToJson, pickFields, toPlain } from "@clusterio/ctl/dist/node/src/command_ops.js";
|
||||
|
||||
/** Run fn and return the lines it printed with console.log */
|
||||
function captureLog(fn) {
|
||||
@@ -29,6 +29,13 @@ describe("ctl/command_ops", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("pickFields()", function() {
|
||||
it("keeps only the given fields unless all is given", function() {
|
||||
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["a"]), { a: 1 });
|
||||
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["all"]), { a: 1, b: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("errorToJson()", function() {
|
||||
it("uses the class name of the error", function() {
|
||||
assert.deepEqual(errorToJson(new lib.NotFoundError("gone")), { error: "NotFoundError", message: "gone" });
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { instanceCommands } from "@clusterio/ctl/dist/node/src/commands_instance.js";
|
||||
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
|
||||
|
||||
/** Run fn and return the lines it printed with console.log */
|
||||
async function captureLog(fn) {
|
||||
const lines = [];
|
||||
const originalLog = console.log; // eslint-disable-line no-console
|
||||
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
console.log = originalLog; // eslint-disable-line no-console
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
describe("ctl/commands_instance", function() {
|
||||
let instances;
|
||||
let sentTo;
|
||||
const ctl = {
|
||||
output: new CommandOutput(),
|
||||
async send(message) {
|
||||
assert(message instanceof lib.InstanceDetailsListRequest);
|
||||
return instances;
|
||||
},
|
||||
async sendTo(destination, message) {
|
||||
sentTo.push([destination.instanceId, message.constructor.name]);
|
||||
if (destination.instanceId === 2) {
|
||||
throw new lib.CommandError("broken");
|
||||
}
|
||||
},
|
||||
};
|
||||
beforeEach(function() {
|
||||
sentTo = [];
|
||||
instances = [
|
||||
{ id: 1, name: "one", status: "stopped", excludeFromStartAll: false },
|
||||
{ id: 2, name: "two", status: "stopped", excludeFromStartAll: false },
|
||||
{ id: 3, name: "three", status: "stopped", excludeFromStartAll: true },
|
||||
{ id: 4, name: "four", status: "running", excludeFromStartAll: false },
|
||||
];
|
||||
});
|
||||
|
||||
describe("start-all", function() {
|
||||
const startAll = instanceCommands.get("start-all");
|
||||
it("starts stopped instances and reports each one", async function() {
|
||||
ctl.output = new CommandOutput("human");
|
||||
const lines = await captureLog(() => startAll.run({ force: false }, ctl));
|
||||
assert.deepEqual(sentTo, [[1, "InstanceStartRequest"], [2, "InstanceStartRequest"]]);
|
||||
assert(lines.includes("✓ Started one"), lines.join("\n"));
|
||||
assert(lines.some(line => line.startsWith("✗ Failed to start two")), lines.join("\n"));
|
||||
});
|
||||
it("prints an entry per instance in json mode", async function() {
|
||||
ctl.output = new CommandOutput("json");
|
||||
const lines = await captureLog(() => startAll.run({ force: true }, ctl));
|
||||
assert.deepEqual(JSON.parse(lines[0]), [
|
||||
{ id: 1, name: "one" },
|
||||
{ id: 2, name: "two", error: { error: "CommandError", message: "broken" } },
|
||||
{ id: 3, name: "three" },
|
||||
]);
|
||||
});
|
||||
it("reports when there is nothing to start", async function() {
|
||||
instances = [];
|
||||
ctl.output = new CommandOutput("human");
|
||||
assert.deepEqual(await captureLog(() => startAll.run({ force: false }, ctl)), ["No instances to start."]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("stop-all", function() {
|
||||
const stopAll = instanceCommands.get("stop-all");
|
||||
it("stops running instances and reports each one", async function() {
|
||||
instances[1].status = "running";
|
||||
ctl.output = new CommandOutput("human");
|
||||
const lines = await captureLog(() => stopAll.run({}, ctl));
|
||||
assert.deepEqual(sentTo, [[2, "InstanceStopRequest"], [4, "InstanceStopRequest"]]);
|
||||
assert(lines.includes("✓ Stopped four"), lines.join("\n"));
|
||||
assert(lines.some(line => line.startsWith("✗ Failed to stop two")), lines.join("\n"));
|
||||
});
|
||||
it("prints an entry per instance in json mode", async function() {
|
||||
ctl.output = new CommandOutput("json");
|
||||
const lines = await captureLog(() => stopAll.run({}, ctl));
|
||||
assert.deepEqual(JSON.parse(lines[0]), [{ id: 4, name: "four" }]);
|
||||
});
|
||||
it("reports when there is nothing to stop", async function() {
|
||||
instances = [];
|
||||
ctl.output = new CommandOutput("human");
|
||||
assert.deepEqual(await captureLog(() => stopAll.run({}, ctl)), ["No instances to stop."]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Ctl } from "@clusterio/ctl/dist/node/ctl.js";
|
||||
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
|
||||
|
||||
/** Run fn and return the lines it printed with console.log */
|
||||
async function captureLog(fn) {
|
||||
const lines = [];
|
||||
const originalLog = console.log; // eslint-disable-line no-console
|
||||
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
console.log = originalLog; // eslint-disable-line no-console
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
describe("ctl/ctl", function() {
|
||||
describe("class Ctl", function() {
|
||||
let ctl;
|
||||
beforeEach(function() {
|
||||
const connector = new lib.VirtualConnector(
|
||||
lib.Address.fromShorthand({ controlId: 1 }),
|
||||
lib.Address.fromShorthand("controller"),
|
||||
);
|
||||
ctl = new Ctl(connector, new lib.CtlConfig("control"));
|
||||
});
|
||||
|
||||
it("prints log events as JSON lines only when streaming the log", async function() {
|
||||
const info = { level: "info", message: "hello" };
|
||||
ctl.output = new CommandOutput("json");
|
||||
const logged = await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info })));
|
||||
assert(logged.every(line => !line.startsWith("{")), "log event printed as a result");
|
||||
ctl.output.streamingLog = true;
|
||||
assert.deepEqual(
|
||||
await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info, timestamp: 1 }))),
|
||||
['{"level":"info","message":"hello","timestamp":1}'],
|
||||
);
|
||||
});
|
||||
it("logs log events in human mode", async function() {
|
||||
ctl.output.streamingLog = true;
|
||||
const info = { level: "verbose", message: "hello" };
|
||||
assert.deepEqual(await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent(info))), []);
|
||||
});
|
||||
it("prints debug WebSocket messages", async function() {
|
||||
const event = new lib.DebugWsMessageEvent("in", "content");
|
||||
assert.deepEqual(await captureLog(() => ctl.handleDebugWsMessageEvent(event)), ["WS in content"]);
|
||||
ctl.output = new CommandOutput("json");
|
||||
assert.deepEqual(
|
||||
await captureLog(() => ctl.handleDebugWsMessageEvent(event)),
|
||||
['{"direction":"in","content":"content"}'],
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2122,6 +2122,7 @@ describe("Integration of Clusterio", function() {
|
||||
|
||||
describe("mod show", function() {
|
||||
it("gives details of a mod", async function() {
|
||||
assert.match(await execCtlOutput("mod show empty_mod 1.0.0"), /name: empty_mod/);
|
||||
let result = await execCtlProcess("mod show empty_mod 1.0.0");
|
||||
let hash = await lib.hashFile(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
|
||||
let stat = await fs.stat(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
|
||||
@@ -2396,6 +2397,20 @@ describe("Integration of Clusterio", function() {
|
||||
const output = await execCtlOutput("user show test --instance-stats");
|
||||
assert.match(output, /name +\| test/);
|
||||
});
|
||||
it("prints the result of human mode commands with more options", async function() {
|
||||
assert.match(await execCtlOutput("user list --stats --attributes"), /isAdmin/);
|
||||
const exchange = JSON.parse(await execCtlOutput(
|
||||
`--output json debug parse-exchange-string ${testStrings.default.replace(/\s/g, "")}`
|
||||
));
|
||||
assert.equal(typeof exchange, "object");
|
||||
});
|
||||
it("follows the log without a final result", async function() {
|
||||
try {
|
||||
assert.equal(await execCtlOutput("--output json log follow --controller"), "");
|
||||
} finally {
|
||||
getControl().keepOpen = false;
|
||||
}
|
||||
});
|
||||
it("prints log queries in human mode", async function() {
|
||||
assert.notEqual(await execCtlOutput("log query --controller --limit 1"), "");
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user