docker capture fixes

This commit is contained in:
Alex Holliday committed 2026-09-21 10:22:46 -07:00
1 parent d7f8bcc9e1
commit 4a2429d316
13 files changed
+180 -51

No files matched your search

@@ -289,6 +289,46 @@ describe("DockerProvider", () => {
message: "Capture returned an invalid Docker metrics payload",
});
});
it("rejects a malformed errors field as an invalid payload instead of throwing", async () => {
const httpHandle = jest.fn();
const { provider } = setup({ httpHandle });
const monitor = makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" });
for (const errors of [{}, [{ err: "no metric" }], [{ metric: "docker.client", err: "x" }]]) {
httpHandle.mockResolvedValueOnce(makeCaptureResponse({ payload: { data: [], errors } }));
const result = await provider.handle(monitor);
expect(result).toMatchObject({
status: false,
code: NETWORK_ERROR,
message: "Capture returned an invalid Docker metrics payload",
});
}
});
it("records no stats for containers that are not running", async () => {
const response = makeCaptureResponse({
payload: {
data: [
makeCaptureContainer({
status: "exited",
running: false,
stats: { cpu_percent: 0, memory_usage: 0, memory_limit: 0, memory_percent: 0 },
}),
],
errors: [],
},
});
const { provider } = setup({ httpHandle: jest.fn().mockResolvedValue(response) });
const result = await provider.handle(makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" }));
const [container] = result.payload!.containers;
expect(container.state).toBe("exited");
expect(container).not.toHaveProperty("cpuPct");
expect(container).not.toHaveProperty("memoryPct");
expect(container).not.toHaveProperty("memoryUsedBytes");
});
});
// ── TLS host urls ────────────────────────────────────────────────────
@@ -2160,4 +2160,63 @@ describe("MonitorService — Docker TLS credentials", () => {
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
});
describe("editMonitor for Capture hosts", () => {
const captureUrl = "http://capture:59232/api/v1/metrics/docker";
it("rejects a url change to Capture with a 422 when no secret is stored or provided", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", url: "unix:///var/run/docker.sock" }));
const { service } = createService({ monitorsRepository });
await expectAppError(
() => service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", url: captureUrl } }),
422,
/Capture API secret is required/
);
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
it("keeps the stored secret and disables logs when only the url changes to Capture", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", secret: "stored-secret", dockerLogsEnabled: true }));
(monitorsRepository.updateById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker" }));
const { service } = createService({ monitorsRepository });
await service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", url: captureUrl } });
const [, , patch] = (monitorsRepository.updateById as jest.Mock).mock.calls[0] as [string, string, Record<string, unknown>];
expect(patch).not.toHaveProperty("secret");
expect(patch.dockerLogsEnabled).toBe(false);
});
it("rejects a blank secret against a stored Capture url when the body omits the url", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", url: captureUrl, secret: "stored-secret" }));
const { service } = createService({ monitorsRepository });
await expectAppError(
() => service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", secret: " " } }),
422,
/Capture API secret is required/
);
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
it("clears the secret without reading the stored monitor when the url changes to a socket", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.updateById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker" }));
const { service } = createService({ monitorsRepository });
await service.editMonitor({
teamId: TEAM_ID,
monitorId: MONITOR_ID,
body: { type: "docker", url: "unix:///var/run/docker.sock", secret: "stale-capture-secret" },
});
expect(monitorsRepository.findById).not.toHaveBeenCalled();
const [, , patch] = (monitorsRepository.updateById as jest.Mock).mock.calls[0] as [string, string, Record<string, unknown>];
expect(patch.secret).toBe("");
});
});
});