11import { describe , it , expect } from "vitest" ;
2- import { parsePodCount , K8sPodCountSignalSource } from "./k8sPodCountSignalSource.js" ;
2+ import { K8sPodCountSignalSource } from "./k8sPodCountSignalSource.js" ;
3+ import { createPodCountFetcher } from "../clients/kubernetes.js" ;
4+ import type { K8sApi } from "../clients/kubernetes.js" ;
35
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 ) ;
6+ function apiReturning ( list : unknown ) : K8sApi {
7+ return { core : { listNamespacedPod : async ( ) => list } } as unknown as K8sApi ;
8+ }
9+
10+ describe ( "createPodCountFetcher" , ( ) => {
11+ it ( "returns items.length when the list is not truncated" , async ( ) => {
12+ const fetch = createPodCountFetcher ( apiReturning ( { items : [ { } ] , metadata : { } } ) , "v4-runs" , 1000 ) ;
13+ expect ( await fetch ( ) ) . toBe ( 1 ) ;
1314 } ) ;
1415
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 ) ;
16+ it ( "returns zero for an empty namespace" , async ( ) => {
17+ const fetch = createPodCountFetcher ( apiReturning ( { items : [ ] , metadata : { } } ) , "v4-runs" , 1000 ) ;
18+ expect ( await fetch ( ) ) . toBe ( 0 ) ;
1819 } ) ;
1920
20- it ( "parses scientific notation" , ( ) => {
21- const text = 'apiserver_storage_objects{resource="pods"} 1.2e+04' ;
22- expect ( parsePodCount ( text ) ) . toBe ( 12000 ) ;
21+ it ( "adds remainingItemCount when the list is truncated" , async ( ) => {
22+ const list = { items : [ { } ] , metadata : { _continue : "tok" , remainingItemCount : 24492 } } ;
23+ const fetch = createPodCountFetcher ( apiReturning ( list ) , "v4-runs" , 1000 ) ;
24+ expect ( await fetch ( ) ) . toBe ( 24493 ) ;
2325 } ) ;
2426
25- it ( "throws when the pods metric is absent" , ( ) => {
26- const text = 'apiserver_storage_objects{resource="configmaps"} 17' ;
27- expect ( ( ) => parsePodCount ( text ) ) . toThrow ( / n o t f o u n d / ) ;
27+ it ( "throws when truncated but remainingItemCount is absent" , async ( ) => {
28+ const list = { items : [ { } ] , metadata : { _continue : "tok" } } ;
29+ const fetch = createPodCountFetcher ( apiReturning ( list ) , "v4-runs" , 1000 ) ;
30+ await expect ( fetch ( ) ) . rejects . toThrow ( / r e m a i n i n g I t e m C o u n t / ) ;
2831 } ) ;
2932
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+ it ( "throws when truncated but remainingItemCount is negative" , async ( ) => {
34+ const list = { items : [ { } ] , metadata : { _continue : "tok" , remainingItemCount : - 1 } } ;
35+ const fetch = createPodCountFetcher ( apiReturning ( list ) , "v4-runs" , 1000 ) ;
36+ await expect ( fetch ( ) ) . rejects . toThrow ( / r e m a i n i n g I t e m C o u n t / ) ;
3337 } ) ;
3438
35- it ( "throws on a negative value" , ( ) => {
36- const text = 'apiserver_storage_objects{resource="pods"} -5' ;
37- expect ( ( ) => parsePodCount ( text ) ) . toThrow ( ) ;
39+ it ( "rejects when the list hangs past the timeout" , async ( ) => {
40+ const api = { core : { listNamespacedPod : ( ) => new Promise ( ( ) => { } ) } } as unknown as K8sApi ;
41+ await expect ( createPodCountFetcher ( api , "v4-runs" , 10 ) ( ) ) . rejects . toThrow ( / t i m e d o u t / ) ;
3842 } ) ;
3943} ) ;
4044
41- function metrics ( count : number ) : string {
42- return `apiserver_storage_objects{resource="pods"} ${ count } ` ;
43- }
44-
4545describe ( "K8sPodCountSignalSource" , ( ) => {
4646 it ( "engages at the engage threshold and reports the count" , async ( ) => {
4747 const counts : number [ ] = [ ] ;
4848 const source = new K8sPodCountSignalSource ( {
49- fetchMetrics : async ( ) => metrics ( 10000 ) ,
49+ fetchPodCount : async ( ) => 10000 ,
5050 engageThreshold : 10000 ,
5151 releaseThreshold : 5000 ,
5252 reportPodCount : ( c ) => counts . push ( c ) ,
@@ -59,7 +59,7 @@ describe("K8sPodCountSignalSource", () => {
5959
6060 it ( "does not engage below the engage threshold" , async ( ) => {
6161 const source = new K8sPodCountSignalSource ( {
62- fetchMetrics : async ( ) => metrics ( 9999 ) ,
62+ fetchPodCount : async ( ) => 9999 ,
6363 engageThreshold : 10000 ,
6464 releaseThreshold : 5000 ,
6565 } ) ;
@@ -69,7 +69,7 @@ describe("K8sPodCountSignalSource", () => {
6969 it ( "stays engaged in the hysteresis band, releases only below release threshold" , async ( ) => {
7070 let count = 10000 ;
7171 const source = new K8sPodCountSignalSource ( {
72- fetchMetrics : async ( ) => metrics ( count ) ,
72+ fetchPodCount : async ( ) => count ,
7373 engageThreshold : 10000 ,
7474 releaseThreshold : 5000 ,
7575 } ) ;
@@ -82,9 +82,9 @@ describe("K8sPodCountSignalSource", () => {
8282 expect ( ( await source . read ( ) ) . engaged ) . toBe ( false ) ; // band again -> stays off
8383 } ) ;
8484
85- it ( "propagates scrape failures (monitor fails open on throw)" , async ( ) => {
85+ it ( "propagates fetch failures (monitor fails open on throw)" , async ( ) => {
8686 const source = new K8sPodCountSignalSource ( {
87- fetchMetrics : async ( ) => {
87+ fetchPodCount : async ( ) => {
8888 throw new Error ( "connection refused" ) ;
8989 } ,
9090 engageThreshold : 10000 ,
0 commit comments