Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions hack/bats/extras/port-monitor.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-FileCopyrightText: Copyright The Lima Authors
# SPDX-License-Identifier: Apache-2.0

# This test 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 #4066.

# This test should not be run in CI as it is not totally reliable: there is always a chance that the server will
# take longer to actually respond to requests after opening the port. The test works around it by retrying once
# on curl exit code 52, but have been observed at least once to fail by refusing to connect.

load "../helpers/load"

: "${TEMPLATE:=default}" # Alternative: "docker"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be part of test-templates ?

Anyway we should establish the criteria to clarify what should be in test-templates and what else should be in bats.
Maybe we should also consider rewriting test-templates in bats.
https://lima-vm.io/docs/dev/testing/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also consider rewriting test-templates in bats.

I think we should, but it will take some time. Until then we can run them in parallel. New tests should be in BATS, I think, if possible with reasonable effort.

I don't have the time to rework the whole test-suite at once, but will add a little bit whenever I have some extra time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be part of test-templates ?

The reason this particular test has a TEMPLATES variable is so that you can use it to test both containerd and docker, as the 2 container engines may have different port binding mechanisms, and they should be tested both. There is no point in running this test with all the templates.

Potentially it could be extended to run with rootful containerd/docker, but I didn't think that was necessary.

When we have more tests we can figure out a set of tags to run different groups under different scenarios, but I think it is still too early to bother with that: https://bats-core.readthedocs.io/en/stable/writing-tests.html#tagging-tests


NAME=nginx

local_setup_file() {
limactl delete --force "$NAME" || :
limactl start --yes --name "$NAME" --mount "$BATS_TMPDIR" "template://${TEMPLATE}" 3>&- 4>&-
}

local_teardown_file() {
limactl delete --force "$NAME"
}

ctrctl() {
if [[ $(limactl ls "$NAME" --yq .config.containerd.user) == true ]]; then
limactl shell $NAME nerdctl "$@"
else
limactl shell $NAME docker "$@"
fi
}

nginx_start() {
echo "$COUNTER" >"${BATS_TEST_TMPDIR}/index.html"
ctrctl run -d --name nginx -p 8080:80 -v "${BATS_TEST_TMPDIR}:/usr/share/nginx/html:ro" nginx
}

nginx_stop() {
ctrctl stop nginx
ctrctl rm nginx
}

verify_port() {
run curl --silent http://127.0.0.1:8080
# If nginx is not quite ready and doesn't send any response at all, give it one extra chance
if [[ $status -eq 52 ]]; then
sleep 0.5
run curl --silent http://127.0.0.1:8080
fi
assert_success
assert_output "$COUNTER"
}

@test 'Verify that the container is working' {
COUNTER=0
ctrctl pull --quiet nginx
nginx_start
verify_port
}

@test 'Stop and restart the container multiple times' {
for COUNTER in {1..100}; do
nginx_stop
nginx_start
verify_port
done
}