|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { parsePodCount, K8sPodCountSignalSource } from "./k8sPodCountSignalSource.js"; |
| 3 | + |
| 4 | +describe("parsePodCount", () => { |
| 5 | + it("reads the pods object count", () => { |
| 6 | + const text = [ |
| 7 | + "# HELP apiserver_storage_objects Number of stored objects", |
| 8 | + "# TYPE apiserver_storage_objects gauge", |
| 9 | + 'apiserver_storage_objects{resource="pods"} 8421', |
| 10 | + 'apiserver_storage_objects{resource="configmaps"} 17', |
| 11 | + ].join("\n"); |
| 12 | + expect(parsePodCount(text)).toBe(8421); |
| 13 | + }); |
| 14 | + |
| 15 | + it("is tolerant of extra labels in any order", () => { |
| 16 | + const text = 'apiserver_storage_objects{group="",resource="pods",extra="x"} 12'; |
| 17 | + expect(parsePodCount(text)).toBe(12); |
| 18 | + }); |
| 19 | + |
| 20 | + it("parses scientific notation", () => { |
| 21 | + const text = 'apiserver_storage_objects{resource="pods"} 1.2e+04'; |
| 22 | + expect(parsePodCount(text)).toBe(12000); |
| 23 | + }); |
| 24 | + |
| 25 | + it("throws when the pods metric is absent", () => { |
| 26 | + const text = 'apiserver_storage_objects{resource="configmaps"} 17'; |
| 27 | + expect(() => parsePodCount(text)).toThrow(/not found/); |
| 28 | + }); |
| 29 | + |
| 30 | + it("throws on a non-finite value (e.g. 1e999)", () => { |
| 31 | + const text = 'apiserver_storage_objects{resource="pods"} 1e999'; |
| 32 | + expect(() => parsePodCount(text)).toThrow(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("throws on a negative value", () => { |
| 36 | + const text = 'apiserver_storage_objects{resource="pods"} -5'; |
| 37 | + expect(() => parsePodCount(text)).toThrow(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +function metrics(count: number): string { |
| 42 | + return `apiserver_storage_objects{resource="pods"} ${count}`; |
| 43 | +} |
| 44 | + |
| 45 | +describe("K8sPodCountSignalSource", () => { |
| 46 | + it("engages at the engage threshold and reports the count", async () => { |
| 47 | + const counts: number[] = []; |
| 48 | + const source = new K8sPodCountSignalSource({ |
| 49 | + fetchMetrics: async () => metrics(10000), |
| 50 | + engageThreshold: 10000, |
| 51 | + releaseThreshold: 5000, |
| 52 | + reportPodCount: (c) => counts.push(c), |
| 53 | + }); |
| 54 | + const verdict = await source.read(); |
| 55 | + expect(verdict.engaged).toBe(true); |
| 56 | + expect(typeof verdict.ts).toBe("number"); |
| 57 | + expect(counts).toEqual([10000]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("does not engage below the engage threshold", async () => { |
| 61 | + const source = new K8sPodCountSignalSource({ |
| 62 | + fetchMetrics: async () => metrics(9999), |
| 63 | + engageThreshold: 10000, |
| 64 | + releaseThreshold: 5000, |
| 65 | + }); |
| 66 | + expect((await source.read()).engaged).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it("stays engaged in the hysteresis band, releases only below release threshold", async () => { |
| 70 | + let count = 10000; |
| 71 | + const source = new K8sPodCountSignalSource({ |
| 72 | + fetchMetrics: async () => metrics(count), |
| 73 | + engageThreshold: 10000, |
| 74 | + releaseThreshold: 5000, |
| 75 | + }); |
| 76 | + expect((await source.read()).engaged).toBe(true); // engage |
| 77 | + count = 7000; |
| 78 | + expect((await source.read()).engaged).toBe(true); // band -> still engaged |
| 79 | + count = 4999; |
| 80 | + expect((await source.read()).engaged).toBe(false); // below release -> off |
| 81 | + count = 7000; |
| 82 | + expect((await source.read()).engaged).toBe(false); // band again -> stays off |
| 83 | + }); |
| 84 | + |
| 85 | + it("propagates scrape failures (monitor fails open on throw)", async () => { |
| 86 | + const source = new K8sPodCountSignalSource({ |
| 87 | + fetchMetrics: async () => { |
| 88 | + throw new Error("connection refused"); |
| 89 | + }, |
| 90 | + engageThreshold: 10000, |
| 91 | + releaseThreshold: 5000, |
| 92 | + }); |
| 93 | + await expect(source.read()).rejects.toThrow("connection refused"); |
| 94 | + }); |
| 95 | +}); |
0 commit comments