Skip to content

Commit d3fc040

Browse files
kalyazinShadowCurse
authored andcommitted
test(serial): add integration test for rate limiting
Add test_serial_rate_limiting to test_serial_io.py. The test configures a rate limiter via the PUT /serial API before boot. Update fcmetrics.py schema with the new uart metric field: rate_limiter_dropped_bytes. Signed-off-by: Nikita Kalyazin <kalyazin@amazon.com>
1 parent 0515853 commit d3fc040

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

tests/host_tools/fcmetrics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def validate_fc_metrics(metrics):
264264
"missed_write_count",
265265
"read_count",
266266
"write_count",
267+
"rate_limiter_dropped_bytes",
267268
],
268269
"signals": [
269270
"sigbus",

tests/integration_tests/functional/test_serial_io.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import signal
99
import termios
1010
import time
11+
from pathlib import Path
1112

1213
from framework import utils
1314
from framework.microvm import Serial
@@ -253,3 +254,41 @@ def test_serial_file_output(uvm_any):
253254
uvm_any.ssh.check_output("echo 'hello' > /dev/ttyS0")
254255

255256
assert b"hello" in uvm_any.serial_out_path.read_bytes()
257+
258+
259+
def test_serial_rate_limiting(uvm_plain):
260+
"""Test that serial output is rate-limited when a rate limiter is configured."""
261+
microvm = uvm_plain
262+
microvm.spawn()
263+
microvm.add_net_iface()
264+
microvm.basic_config(vcpu_count=1, mem_size_mib=256)
265+
266+
# Configure serial output to a file with a rate limiter:
267+
# 1 KiB/sec sustained, 64 KiB one-time burst.
268+
serial_path = Path(microvm.path) / "serial.log"
269+
serial_path.touch()
270+
microvm.create_jailed_resource(serial_path)
271+
microvm.api.serial.put(
272+
serial_out_path="serial.log",
273+
rate_limiter={"size": 1024, "one_time_burst": 65536, "refill_time": 1000},
274+
)
275+
microvm.start()
276+
277+
size_before = serial_path.stat().st_size
278+
279+
# Write a large payload (~1MB) from the guest to the serial port.
280+
microvm.ssh.check_output("base64 /dev/urandom | head -c 1000000 > /dev/ttyS0")
281+
282+
# Wait for any in-flight writes to settle.
283+
time.sleep(2)
284+
285+
# With 64 KiB burst + ~2s at 1 KiB/sec, output should be well under 80 KB.
286+
new_bytes = serial_path.stat().st_size - size_before
287+
assert new_bytes < 80000, (
288+
f"Serial output is {new_bytes} bytes, "
289+
"expected under 80000 due to rate limiting"
290+
)
291+
292+
# Verify the rate_limiter_dropped_bytes metric was incremented.
293+
fc_metrics = microvm.flush_metrics()
294+
assert fc_metrics["uart"]["rate_limiter_dropped_bytes"] > 0

0 commit comments

Comments
 (0)