1- // Docker Stats Resource Ceiling tests cover docker stats resource ceiling script behavior.
21import { spawnSync } from "node:child_process" ;
32import { mkdtempSync , readFileSync , rmSync , writeFileSync } from "node:fs" ;
43import { tmpdir } from "node:os" ;
@@ -93,17 +92,30 @@ describe("scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs", () => {
9392 expect ( looseCpu . stderr ) . toContain ( "had invalid CPUPerc" ) ;
9493 } ) ;
9594
96- it ( "reports and enforces parsed Docker resource peaks" , ( ) => {
95+ it ( "parses mixed stats lines, enforces peaks, and ignores terminal samples" , ( ) => {
96+ const cappedLine = validStatsLineWithBytes ( MAX_STATS_SAMPLE_LINE_BYTES ) ;
97+ const paddedLine = validStatsLineWithBytes ( 1024 ) ;
9798 const result = runAssert (
98- writeStats ( '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\n' ) ,
99+ writeStats (
100+ [
101+ '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\n' ,
102+ `${ paddedLine } \n` ,
103+ `${ cappedLine } \r\n` ,
104+ '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\r' ,
105+ '{"MemUsage":"64MiB / 2GiB","CPUPerc":"15.0%"}\r' ,
106+ '{"MemUsage":"512B / 2GiB","CPUPerc":"0.5%"}\n' ,
107+ '{"MemUsage":"0B / 0B","CPUPerc":"0.0%"}\n' ,
108+ ] . join ( "" ) ,
109+ ) ,
99110 "256.5" ,
100111 "50.5" ,
101112 ) ;
102113
103- expect ( result . status ) . toBe ( 0 ) ;
114+ expect ( Buffer . byteLength ( cappedLine , "utf8" ) ) . toBe ( MAX_STATS_SAMPLE_LINE_BYTES ) ;
115+ expect ( result . status , result . stderr ) . toBe ( 0 ) ;
104116 expect ( result . stdout ) . toContain ( "memory=128.0MiB" ) ;
105117 expect ( result . stdout ) . toContain ( "cpu=25.0%" ) ;
106- expect ( result . stdout ) . toContain ( "samples=1 " ) ;
118+ expect ( result . stdout ) . toContain ( "samples=6 " ) ;
107119 } ) ;
108120
109121 it ( "streams stats logs instead of slurping them into memory" , ( ) => {
@@ -124,56 +136,6 @@ describe("scripts/e2e/lib/docker-stats/assert-resource-ceiling.mjs", () => {
124136 expect ( result . stderr ) . not . toContain ( "was not valid JSON" ) ;
125137 } ) ;
126138
127- it ( "accepts large stats sample lines within the line cap" , ( ) => {
128- const padding = "x" . repeat ( 1024 ) ;
129- const result = runAssert (
130- writeStats ( `{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%","padding":"${ padding } "}\n` ) ,
131- ) ;
132-
133- expect ( result . status ) . toBe ( 0 ) ;
134- expect ( result . stdout ) . toContain ( "samples=1" ) ;
135- } ) ;
136-
137- it ( "accepts CRLF stats sample lines whose content exactly matches the line cap" , ( ) => {
138- const line = validStatsLineWithBytes ( MAX_STATS_SAMPLE_LINE_BYTES ) ;
139- const result = runAssert ( writeStats ( `${ line } \r\n` ) ) ;
140-
141- expect ( Buffer . byteLength ( line , "utf8" ) ) . toBe ( MAX_STATS_SAMPLE_LINE_BYTES ) ;
142- expect ( result . status ) . toBe ( 0 ) ;
143- expect ( result . stdout ) . toContain ( "samples=1" ) ;
144- } ) ;
145-
146- it ( "accepts stats sample lines separated by standalone carriage returns" , ( ) => {
147- const result = runAssert (
148- writeStats (
149- '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\r{"MemUsage":"64MiB / 2GiB","CPUPerc":"15.0%"}\r' ,
150- ) ,
151- ) ;
152-
153- expect ( result . status ) . toBe ( 0 ) ;
154- expect ( result . stdout ) . toContain ( "memory=128.0MiB" ) ;
155- expect ( result . stdout ) . toContain ( "samples=2" ) ;
156- } ) ;
157-
158- it ( "accepts byte-unit Docker memory samples" , ( ) => {
159- const result = runAssert ( writeStats ( '{"MemUsage":"512B / 2GiB","CPUPerc":"0.5%"}\n' ) ) ;
160-
161- expect ( result . status ) . toBe ( 0 ) ;
162- expect ( result . stdout ) . toContain ( "samples=1" ) ;
163- } ) ;
164-
165- it ( "ignores terminal zero-capacity Docker stats samples" , ( ) => {
166- const result = runAssert (
167- writeStats (
168- '{"MemUsage":"128MiB / 2GiB","CPUPerc":"25.0%"}\n{"MemUsage":"0B / 0B","CPUPerc":"0.0%"}\n' ,
169- ) ,
170- ) ;
171-
172- expect ( result . status ) . toBe ( 0 ) ;
173- expect ( result . stdout ) . toContain ( "memory=128.0MiB" ) ;
174- expect ( result . stdout ) . toContain ( "samples=1" ) ;
175- } ) ;
176-
177139 it ( "still fails when only terminal zero-capacity samples were captured" , ( ) => {
178140 const result = runAssert ( writeStats ( '{"MemUsage":"0B / 0B","CPUPerc":"0.0%"}\n' ) ) ;
179141
0 commit comments