Skip to content

Commit a75ac9e

Browse files
vincentkocmiorbnli
authored andcommitted
test(systemd): cover reset failure and stop controls
1 parent d384443 commit a75ac9e

2 files changed

Lines changed: 38 additions & 47 deletions

File tree

‎src/daemon/systemd.test.ts‎

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,14 +2752,17 @@ describe("systemd service control", () => {
27522752
});
27532753

27542754
it("starts the resolved user unit and ignores audit observer failures", async () => {
2755+
const sequence: string[] = [];
27552756
execFileMock
27562757
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))
27572758
.mockImplementationOnce((_cmd, args, _opts, cb) => {
27582759
assertUserSystemctlArgs(args, "reset-failed", GATEWAY_SERVICE);
2760+
sequence.push(args[1] ?? "");
27592761
cb(null, "", "");
27602762
})
27612763
.mockImplementationOnce((_cmd, args, _opts, cb) => {
27622764
assertUserSystemctlArgs(args, "start", GATEWAY_SERVICE);
2765+
sequence.push(args[1] ?? "");
27632766
cb(null, "", "");
27642767
});
27652768
const write = vi.fn();
@@ -2775,18 +2778,49 @@ describe("systemd service control", () => {
27752778
}),
27762779
).resolves.toBeUndefined();
27772780

2781+
expect(sequence).toEqual(["reset-failed", "start"]);
27782782
expect(onMutation).toHaveBeenCalledWith({ mode: "systemctl-start" });
27792783
expect(
27802784
expectDefined(onMutation.mock.invocationCallOrder[0], "start audit call order"),
27812785
).toBeLessThan(expectDefined(write.mock.invocationCallOrder[0], "start output call order"));
27822786
expect(requireFirstWrite(write)).toContain("Started systemd service");
27832787
});
27842788

2789+
it("still starts when reset-failed cannot resolve an unloaded user unit", async () => {
2790+
const sequence: string[] = [];
2791+
execFileMock
2792+
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))
2793+
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2794+
assertUserSystemctlArgs(args, "reset-failed", GATEWAY_SERVICE);
2795+
sequence.push(args[1] ?? "");
2796+
cb(
2797+
createExecFileError("unit not loaded", {
2798+
stderr: `Unit ${GATEWAY_SERVICE} not loaded.`,
2799+
}),
2800+
"",
2801+
`Unit ${GATEWAY_SERVICE} not loaded.`,
2802+
);
2803+
})
2804+
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2805+
assertUserSystemctlArgs(args, "start", GATEWAY_SERVICE);
2806+
sequence.push(args[1] ?? "");
2807+
cb(null, "", "");
2808+
});
2809+
const { stdout, write } = createWritableStreamMock();
2810+
2811+
await startSystemdService({ stdout, env: {} });
2812+
2813+
expect(sequence).toEqual(["reset-failed", "start"]);
2814+
expect(requireFirstWrite(write)).toContain("Started systemd service");
2815+
});
2816+
27852817
it("stops the resolved user unit", async () => {
2818+
const sequence: string[] = [];
27862819
execFileMock
27872820
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))
27882821
.mockImplementationOnce((_cmd, args, _opts, cb) => {
27892822
assertUserSystemctlArgs(args, "stop", GATEWAY_SERVICE);
2823+
sequence.push(args[1] ?? "");
27902824
cb(null, "", "");
27912825
});
27922826
const write = vi.fn();
@@ -2795,6 +2829,7 @@ describe("systemd service control", () => {
27952829

27962830
await stopSystemdService({ stdout, env: {}, onMutation });
27972831

2832+
expect(sequence).toEqual(["stop"]);
27982833
expect(write).toHaveBeenCalledTimes(1);
27992834
expect(requireFirstWrite(write)).toContain("Stopped systemd service");
28002835
expect(onMutation).toHaveBeenCalledWith({ mode: "systemctl-stop" });
@@ -2868,48 +2903,6 @@ describe("systemd service control", () => {
28682903
expect(restartSequence).toEqual(["reset-failed", "restart"]);
28692904
});
28702905

2871-
it("runs reset-failed before starting a crash-looped user unit", async () => {
2872-
const startSequence: string[] = [];
2873-
execFileMock
2874-
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))
2875-
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2876-
assertUserSystemctlArgs(args, "reset-failed", GATEWAY_SERVICE);
2877-
startSequence.push(args[1] ?? "");
2878-
cb(null, "", "");
2879-
})
2880-
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2881-
assertUserSystemctlArgs(args, "start", GATEWAY_SERVICE);
2882-
startSequence.push(args[1] ?? "");
2883-
cb(null, "", "");
2884-
});
2885-
const { write, stdout } = createWritableStreamMock();
2886-
await startSystemdService({ stdout, env: {} });
2887-
// start must clear the latch too, so `openclaw gateway start` recovers a
2888-
// crash-looped unit just like restart (mirrors launchd start's enable).
2889-
expect(startSequence).toEqual(["reset-failed", "start"]);
2890-
expect(requireFirstWrite(write)).toContain("Started systemd service");
2891-
});
2892-
2893-
it("tolerates reset-failed as a no-op on a healthy start", async () => {
2894-
// reset-failed is idempotent: on a non-failed unit systemd returns success
2895-
// and leaves the unit untouched, so prepending it to start cannot regress a
2896-
// healthy gateway. Assert the start still proceeds and succeeds when
2897-
// reset-failed exits 0 with no failed state to clear.
2898-
execFileMock
2899-
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))
2900-
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2901-
assertUserSystemctlArgs(args, "reset-failed", GATEWAY_SERVICE);
2902-
cb(null, "", ""); // healthy unit: no failed state, exit 0
2903-
})
2904-
.mockImplementationOnce((_cmd, args, _opts, cb) => {
2905-
assertUserSystemctlArgs(args, "start", GATEWAY_SERVICE);
2906-
cb(null, "", "");
2907-
});
2908-
const { write, stdout } = createWritableStreamMock();
2909-
await startSystemdService({ stdout, env: {} });
2910-
expect(requireFirstWrite(write)).toContain("Started systemd service");
2911-
});
2912-
29132906
it("surfaces stop failures with systemctl detail", async () => {
29142907
execFileMock
29152908
.mockImplementationOnce((_cmd, _args, _opts, cb) => cb(null, "", ""))

‎src/daemon/systemd.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,7 @@ async function runSystemdServiceAction(params: {
15971597
// systemd latches a unit into failed/start-limit-hit after it crashes faster
15981598
// than StartLimitBurst allows and then stops auto-restarting it. Clear the
15991599
// latch before start/restart so an operator can recover a crash-looped
1600-
// gateway with the natural `openclaw gateway start` command; reset-failed is
1601-
// idempotent and a no-op on a healthy unit.
1600+
// gateway with the natural start command. Idempotent on healthy units.
16021601
await execSystemctl(["reset-failed", unitName], env);
16031602
}
16041603
const res = await execSystemctl([params.action, unitName], env);
@@ -1612,9 +1611,8 @@ async function runSystemdServiceAction(params: {
16121611
await assertSystemdAvailable(env);
16131612
if (params.action !== "stop") {
16141613
await assertNoSystemGatewayOwnership(env);
1615-
// Clear any failed/start-limit-hit latch before start/restart so a
1616-
// crash-looped gateway recovers (see system-scope branch above). Idempotent on
1617-
// healthy units.
1614+
// Clear the same latch for user-scope start/restart after the ownership
1615+
// guard, so a conflicting system unit is never mutated.
16181616
await execSystemctlUser(env, ["reset-failed", unitName]);
16191617
}
16201618
const res = await execSystemctlUser(env, [params.action, unitName]);

0 commit comments

Comments
 (0)