From 0f8bbc7c1e2c2f6fec0b17753ac0d9c4a3ad4317 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Fri, 22 May 2026 20:12:14 -0500 Subject: fix(test): fail clearly when the VM forward port is taken A test run launched qemu without first checking the SSH forward port, so a collision with another VM already holding it surfaced only as an opaque "Failed to start VM," with qemu unable to bind and no hint why. I added a port_in_use check in run_test before the launch: it errors with the port number and the SSH_PORT override to set, records the failure, and moves on. The check lives in run_test, not start_vm, because start_vm runs in a command substitution (vm_pid=$(start_vm ...)) where this harness's non-exiting error() would be captured as the PID instead of failing the run. The pure half, port_listening_in, takes an `ss -tln` snapshot as a string so it's unit-testable. --- tests/unit/test_test_install.bats | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/unit/test_test_install.bats b/tests/unit/test_test_install.bats index 6b911af..bc4a63e 100644 --- a/tests/unit/test_test_install.bats +++ b/tests/unit/test_test_install.bats @@ -235,3 +235,34 @@ EOF source "${BATS_TEST_DIRNAME}/../../scripts/test-install.sh" [ "$SSH_PORT" = "2222" ] } + +############################# +# port_listening_in (pure half of the port-in-use guard) +############################# +# The live ss query lives in port_in_use; this pure predicate takes an +# `ss -tln` snapshot as a string so it's testable with fixtures. + +@test "port_listening_in detects a port present in ss output" { + run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:*" + [ "$status" -eq 0 ] +} + +@test "port_listening_in returns 1 when the port is absent" { + run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*" + [ "$status" -eq 1 ] +} + +@test "port_listening_in does not match a port that is only a substring" { + run port_listening_in 2222 "LISTEN 0 4096 0.0.0.0:12222 0.0.0.0:*" + [ "$status" -eq 1 ] +} + +@test "port_listening_in matches an IPv6 listener" { + run port_listening_in 2222 "LISTEN 0 4096 [::]:2222 [::]:*" + [ "$status" -eq 0 ] +} + +@test "port_listening_in returns 1 on empty ss output" { + run port_listening_in 2222 "" + [ "$status" -eq 1 ] +} -- cgit v1.2.3