Support Capture-backed Docker monitors
This commit is contained in:
1 parent
da8b230cec
commit
862ff15eef
14 files changed
+399
-33
No files matched your search
@@ -76,6 +76,33 @@ const makeStats = (overrides?: Record<string, any>) => ({
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const makeCaptureContainer = (overrides?: Record<string, any>) => ({
|
||||
container_id: "capture-container-id",
|
||||
container_name: "capture-container",
|
||||
status: "running",
|
||||
health: { healthy: true, source: "container_health_check" },
|
||||
running: true,
|
||||
base_image: "nginx:latest",
|
||||
exposed_ports: [{ port: "80", protocol: "tcp" }],
|
||||
started_at: 1_788_000_000,
|
||||
stats: {
|
||||
cpu_percent: 80,
|
||||
memory_usage: 256,
|
||||
memory_limit: 1024,
|
||||
memory_percent: 25,
|
||||
},
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const makeCaptureResponse = (overrides?: Record<string, any>) => ({
|
||||
status: true,
|
||||
code: 200,
|
||||
message: "OK",
|
||||
responseTime: 42,
|
||||
payload: { data: [makeCaptureContainer()], errors: [] },
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const makeLogFrame = (streamType: number, text: string): Buffer => {
|
||||
const payload = Buffer.from(text);
|
||||
const header = Buffer.alloc(8);
|
||||
@@ -92,6 +119,7 @@ const setup = (
|
||||
inspect: any;
|
||||
stats: any;
|
||||
logs: any;
|
||||
httpHandle: any;
|
||||
encryptionService: IEncryptionService;
|
||||
}> = {}
|
||||
) => {
|
||||
@@ -107,12 +135,14 @@ const setup = (
|
||||
const DockerLib = jest.fn().mockReturnValue(instance) as any;
|
||||
const logger = createMockLogger();
|
||||
const encryptionService = overrides.encryptionService ?? makeEncryptionService();
|
||||
const provider = new DockerProvider(logger as any, DockerLib, encryptionService);
|
||||
const httpHandle = overrides.httpHandle ?? jest.fn();
|
||||
const provider = new DockerProvider(logger as any, DockerLib, encryptionService, { handle: httpHandle } as any);
|
||||
return {
|
||||
provider,
|
||||
logger,
|
||||
DockerLib,
|
||||
encryptionService,
|
||||
httpHandle,
|
||||
ping: instance.ping,
|
||||
listContainers: instance.listContainers,
|
||||
getContainer,
|
||||
@@ -162,6 +192,105 @@ describe("DockerProvider", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── Capture urls ─────────────────────────────────────────────────────
|
||||
|
||||
describe("Capture urls", () => {
|
||||
it("requests all containers and normalizes Capture metrics", async () => {
|
||||
const httpHandle = jest.fn().mockResolvedValue(makeCaptureResponse());
|
||||
const { provider, DockerLib } = setup({ httpHandle });
|
||||
const monitor = makeMonitor({
|
||||
url: "https://capture.example.com/api/v1/metrics/docker?site=west&all=false",
|
||||
secret: "capture-secret",
|
||||
});
|
||||
|
||||
const result = await provider.handle(monitor);
|
||||
|
||||
expect(httpHandle).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
url: "https://capture.example.com/api/v1/metrics/docker?site=west&all=true",
|
||||
secret: "capture-secret",
|
||||
}),
|
||||
undefined
|
||||
);
|
||||
expect(DockerLib).not.toHaveBeenCalled();
|
||||
expect(result).toMatchObject({
|
||||
status: true,
|
||||
payload: { summary: { total: 1, running: 1, stopped: 0, unhealthy: 0 } },
|
||||
});
|
||||
expect(result.payload?.containers[0]).toMatchObject({
|
||||
id: "capture-container-id",
|
||||
state: "running",
|
||||
health: "healthy",
|
||||
cpuPct: 0.8,
|
||||
memoryUsedBytes: 256,
|
||||
memoryPct: 0.25,
|
||||
startedAt: new Date(1_788_000_000 * 1000).toISOString(),
|
||||
ports: [{ privatePort: 80, protocol: "tcp" }],
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps state-derived Capture health separate from Docker healthchecks", async () => {
|
||||
const response = makeCaptureResponse({
|
||||
payload: {
|
||||
data: [makeCaptureContainer({ health: { healthy: false, source: "state_based_health_check" } })],
|
||||
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" }));
|
||||
|
||||
expect(result.payload?.containers[0]?.health).toBe("none");
|
||||
expect(result.payload?.summary.unhealthy).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps a partial Capture response up", async () => {
|
||||
const response = makeCaptureResponse({
|
||||
code: 207,
|
||||
payload: {
|
||||
data: [makeCaptureContainer()],
|
||||
errors: [{ metric: ["docker.container.stats"], err: "stats unavailable" }],
|
||||
},
|
||||
});
|
||||
const { provider, logger } = setup({ httpHandle: jest.fn().mockResolvedValue(response) });
|
||||
|
||||
const result = await provider.handle(makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" }));
|
||||
|
||||
expect(result.status).toBe(true);
|
||||
expect(result.code).toBe(207);
|
||||
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Capture returned 1 partial Docker collection error(s)" }));
|
||||
});
|
||||
|
||||
it("marks fatal Capture Docker errors down", async () => {
|
||||
const response = makeCaptureResponse({
|
||||
code: 207,
|
||||
payload: { data: null, errors: [{ metric: ["docker.client"], err: "Docker daemon unavailable" }] },
|
||||
});
|
||||
const { provider } = setup({ httpHandle: jest.fn().mockResolvedValue(response) });
|
||||
|
||||
const result = await provider.handle(makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" }));
|
||||
|
||||
expect(result).toMatchObject({ status: false, code: NETWORK_ERROR, message: "Docker daemon unavailable", payload: null });
|
||||
});
|
||||
|
||||
it("accepts an empty Capture host and rejects malformed payloads", async () => {
|
||||
const emptyHandle = jest.fn().mockResolvedValue(makeCaptureResponse({ payload: { data: [], errors: null } }));
|
||||
const { provider } = setup({ httpHandle: emptyHandle });
|
||||
const monitor = makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" });
|
||||
|
||||
const emptyResult = await provider.handle(monitor);
|
||||
expect(emptyResult).toMatchObject({ status: true, payload: { containers: [], summary: { total: 0 } } });
|
||||
|
||||
emptyHandle.mockResolvedValueOnce(makeCaptureResponse({ payload: { data: "not-an-array" } }));
|
||||
const malformedResult = await provider.handle(monitor);
|
||||
expect(malformedResult).toMatchObject({
|
||||
status: false,
|
||||
code: NETWORK_ERROR,
|
||||
message: "Capture returned an invalid Docker metrics payload",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ── TLS host urls ────────────────────────────────────────────────────
|
||||
|
||||
describe("tls host urls", () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "@jest/globals";
|
||||
import { DOCKER_TLS_URL, isDockerSocketUrl, isDockerTlsUrl } from "../../../src/utils/dockerHost.ts";
|
||||
import { DOCKER_TLS_URL, isCaptureDockerUrl, isDockerSocketUrl, isDockerTlsUrl } from "../../../src/utils/dockerHost.ts";
|
||||
|
||||
describe("isDockerTlsUrl", () => {
|
||||
it.each(["tcp://host", "tcp://host:2376", "https://host", "https://host:2377", "tcp://host/", "tcp://10.0.0.1:2376", " tcp://host "])(
|
||||
@@ -31,6 +31,22 @@ describe("isDockerTlsUrl", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("isCaptureDockerUrl", () => {
|
||||
it.each(["http://host:59232/api/v1/metrics/docker", "https://example.com/prefix/metrics/docker/?all=false"])("accepts %s", (url) => {
|
||||
expect(isCaptureDockerUrl(url)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"https://host:2376",
|
||||
"tcp://host:2376/api/v1/metrics/docker",
|
||||
"https://example.com/api/v1/metrics/docker/containers",
|
||||
"https://example.com/?path=/metrics/docker",
|
||||
undefined,
|
||||
])("rejects %s", (url) => {
|
||||
expect(isCaptureDockerUrl(url)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isDockerSocketUrl", () => {
|
||||
it.each(["unix:///var/run/docker.sock", "/var/run/docker.sock", " /run/docker.sock "])("accepts %s", (url) => {
|
||||
expect(isDockerSocketUrl(url)).toBe(true);
|
||||
|
||||
@@ -629,6 +629,18 @@ describe("monitorValidation — Docker host url", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("accepts a Capture Docker metric endpoint with a secret", () => {
|
||||
const url = "http://capture:59232/api/v1/metrics/docker";
|
||||
const parsed = createMonitorBodyValidation.parse({ ...baseDockerBody, url, secret: "capture-secret" });
|
||||
expect(parsed.url).toBe(url);
|
||||
});
|
||||
|
||||
it("requires a secret for a Capture Docker endpoint", () => {
|
||||
expect(() => createMonitorBodyValidation.parse({ ...baseDockerBody, url: "https://capture.example.com/api/v1/metrics/docker" })).toThrow(
|
||||
"Capture API secret is required"
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects container names and unsupported engine urls", () => {
|
||||
for (const badUrl of [
|
||||
"my-container",
|
||||
@@ -671,6 +683,13 @@ describe("monitorValidation — Docker host url", () => {
|
||||
expect(parsed.monitors[0].url).toBe("unix:///var/run/docker.sock");
|
||||
});
|
||||
|
||||
it("accepts an imported Capture Docker monitor with a secret", () => {
|
||||
const parsed = importMonitorsBodyValidation.parse({
|
||||
monitors: [{ ...baseDockerBody, url: "http://capture:59232/api/v1/metrics/docker", secret: "capture-secret" }],
|
||||
});
|
||||
expect(parsed.monitors[0].secret).toBe("capture-secret");
|
||||
});
|
||||
|
||||
it("rejects an imported docker monitor with a container-name url", () => {
|
||||
expect(() => importMonitorsBodyValidation.parse({ monitors: [{ ...baseDockerBody, url: "my-container" }] })).toThrow();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user