| 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 | # This test should not be run in CI as it is not totally reliable: there is always a chance that the server will |
| 8 | # take longer to actually respond to requests after opening the port. The test works around it by retrying once |
| 9 | # on curl exit code 52, but have been observed at least once to fail by refusing to connect. |
| 10 | |
| 11 | load "../helpers/load" |
| 12 | |
| 13 | : "${TEMPLATE:=default}" # Alternative: "docker" |
| 14 | |
| 15 | NAME=nginx |
| 16 | |
| 17 | local_setup_file() { |
| 18 | limactl delete --force "$NAME" || : |
| 19 | limactl start --yes --name "$NAME" --mount "$BATS_TMPDIR" "template:${TEMPLATE}" 3>&- 4>&- |
| 20 | } |
| 21 | |
| 22 | local_teardown_file() { |
| 23 | limactl delete --force "$NAME" |
| 24 | } |
| 25 | |
| 26 | ctrctl() { |
| 27 | if [[ $(limactl ls "$NAME" --yq .config.containerd.user) == true ]]; then |
| 28 | limactl shell $NAME nerdctl "$@" |
| 29 | else |
| 30 | limactl shell $NAME docker "$@" |
| 31 | fi |
| 32 | } |
| 33 | |
| 34 | nginx_start() { |
| 35 | echo "$COUNTER" >"${BATS_TEST_TMPDIR}/index.html" |
| 36 | ctrctl run -d --name nginx -p 8080:80 -v "${BATS_TEST_TMPDIR}:/usr/share/nginx/html:ro" nginx |
| 37 | } |
| 38 | |
| 39 | nginx_stop() { |
| 40 | ctrctl stop nginx |
| 41 | ctrctl rm nginx |
| 42 | } |
| 43 | |
| 44 | verify_port() { |
| 45 | run curl --silent http://127.0.0.1:8080 |
| 46 | # If nginx is not quite ready and doesn't send any response at all, give it one extra chance |
| 47 | if [[ $status -eq 52 ]]; then |
| 48 | sleep 0.5 |
| 49 | run curl --silent http://127.0.0.1:8080 |
| 50 | fi |
| 51 | assert_success |
| 52 | assert_output "$COUNTER" |
| 53 | } |
| 54 | |
| 55 | @test 'Verify that the container is working' { |
| 56 | COUNTER=0 |
| 57 | ctrctl pull --quiet nginx |
| 58 | nginx_start |
| 59 | verify_port |
| 60 | } |
| 61 | |
| 62 | @test 'Stop and restart the container multiple times' { |
| 63 | for COUNTER in {1..100}; do |
| 64 | nginx_stop |
| 65 | nginx_start |
| 66 | verify_port |
| 67 | done |
| 68 | } |