Skip to content

Commit 814c0dc

Browse files
committed
Add port monitoring test script
It verifies that when a container is destroyed, its ports can be reused immediately and are not subject to being freed by a polling loop. See lima-vm#4066. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent 7e08d68 commit 814c0dc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

hack/bats/extras/port-monitor.bats

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This test verifies that when a container is destroyed, its ports can be reused
5+
# immediately and are not subject to being freed by a polling loop. See #4066.
6+
7+
load "../helpers/load"
8+
9+
: "${TEMPLATE:=default}" # Alternative: "docker"
10+
11+
NAME=nginx
12+
13+
local_setup_file() {
14+
limactl delete --force "$NAME" || :
15+
limactl start --yes --name "$NAME" --mount "$BATS_TMPDIR" "template://${TEMPLATE}" 3>&- 4>&-
16+
}
17+
18+
local_teardown_file() {
19+
limactl delete --force "$NAME"
20+
}
21+
22+
ctrctl() {
23+
if [[ $(limactl ls "$NAME" --yq .config.containerd.user) == true ]]; then
24+
limactl shell $NAME nerdctl "$@"
25+
else
26+
limactl shell $NAME docker "$@"
27+
fi
28+
}
29+
30+
nginx_start() {
31+
echo "$COUNTER" >"${BATS_TEST_TMPDIR}/index.html"
32+
ctrctl run -d --name nginx -p 8080:80 -v "${BATS_TEST_TMPDIR}:/usr/share/nginx/html:ro" nginx
33+
}
34+
35+
nginx_stop() {
36+
ctrctl stop nginx
37+
ctrctl rm nginx
38+
}
39+
40+
verify_port() {
41+
run curl --silent http://127.0.0.1:8080
42+
# If nginx is not quite ready and doesn't send any response at all, give it one extra chance
43+
if [[ $status -eq 52 ]]; then
44+
sleep 0.5
45+
run curl --silent http://127.0.0.1:8080
46+
fi
47+
assert_success
48+
assert_output "$COUNTER"
49+
}
50+
51+
@test 'Verify that the container is working' {
52+
COUNTER=0
53+
ctrctl pull --quiet nginx
54+
nginx_start
55+
verify_port
56+
}
57+
58+
@test 'Stop and restart the container multiple times' {
59+
for COUNTER in {1..100}; do
60+
nginx_stop
61+
nginx_start
62+
verify_port
63+
done
64+
}

0 commit comments

Comments
 (0)