Skip to content

Commit 82653ee

Browse files
committed
test/system: use a random port for the local Docker registry
Hard-coding the port to 50000 can cause intermittent failures when the port happens to already be in use, and also prevents running the test suite more than once at the same time. Let Podman pick a random available port and persist it to a file so test processes in the same run can find it. Signed-off-by: Rolv Apneseth <rolv.apneseth@gmail.com>
1 parent 8b85b9a commit 82653ee

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

test/system/libs/helpers.bash

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ readonly ROOTLESS_PODMAN_RUNROOT_DIR="$BATS_SUITE_TMPDIR/runroot"
3131
readonly DOCKER_REG_ROOT="$BATS_SUITE_TMPDIR/docker-registry-root"
3232
readonly DOCKER_REG_CERTS_DIR="$BATS_SUITE_TMPDIR/certs"
3333
readonly DOCKER_REG_AUTH_DIR="$BATS_SUITE_TMPDIR/auth"
34-
readonly DOCKER_REG_URI="localhost:50000"
3534
readonly DOCKER_REG_NAME="docker-registry"
35+
# Store the registry URI in a file so that loading this script from different
36+
# test processes will still use the same URI for the same test suite run. The
37+
# contents of the file are set when setting up the local Docker registry.
38+
readonly DOCKER_REG_URI_FILE="$BATS_SUITE_TMPDIR/docker-reg-uri"
39+
DOCKER_REG_URI="$(cat "$DOCKER_REG_URI_FILE" 2>/dev/null || echo "")"
3640

3741
# Podman and Toolbx commands to run
3842
readonly TOOLBX="${TOOLBX:-$(command -v toolbox)}"
@@ -166,12 +170,6 @@ function _setup_docker_registry() {
166170
-out "${DOCKER_REG_CERTS_DIR}"/domain.crt
167171
assert_success
168172

169-
# Add certificate to Podman's trusted certificates (rootless)
170-
run mkdir -p "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"
171-
assert_success
172-
run cp "${DOCKER_REG_CERTS_DIR}"/domain.crt "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"/domain.crt
173-
assert_success
174-
175173
# Create a registry user
176174
# username: user; password: user
177175
run mkdir -p "${DOCKER_REG_AUTH_DIR}"
@@ -197,13 +195,25 @@ function _setup_docker_registry() {
197195
--env REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
198196
--name "${DOCKER_REG_NAME}" \
199197
--privileged \
200-
--publish 50000:5000 \
198+
--publish 5000 \
201199
--rm \
202200
--volume "${DOCKER_REG_AUTH_DIR}":/auth \
203201
--volume "${DOCKER_REG_CERTS_DIR}":/certs \
204202
"${IMAGES[docker-reg]}"
205203
assert_success
206204

205+
# Determine the randomly assigned host port and persist the DOCKER_REG_URI to a file
206+
local docker_reg_port
207+
docker_reg_port="$(podman --root "${DOCKER_REG_ROOT}" port "${DOCKER_REG_NAME}" 5000)"
208+
DOCKER_REG_URI="localhost:${docker_reg_port##*:}"
209+
echo "$DOCKER_REG_URI" > "$DOCKER_REG_URI_FILE"
210+
211+
# Add certificate to Podman's trusted certificates (rootless)
212+
run mkdir -p "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"
213+
assert_success
214+
run cp "${DOCKER_REG_CERTS_DIR}"/domain.crt "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"/domain.crt
215+
assert_success
216+
207217
_wait_for_docker_registry
208218

209219
run podman login \

0 commit comments

Comments
 (0)