Skip to content

Commit 0689fff

Browse files
Rolv-Apnesethdebarshiray
authored andcommitted
test/system: Use a random port for the local temporary Docker registry
Hard coding the port to 50000 can cause intermittent failures when the port happens to already be in use, and 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. #1822 Signed-off-by: Rolv Apneseth <rolv.apneseth@gmail.com>
1 parent 773768a commit 0689fff

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

test/system/libs/helpers.bash

Lines changed: 16 additions & 6 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 || true)"
3640

3741
# Podman and Toolbx commands to run
3842
readonly TOOLBX="${TOOLBX:-$(command -v toolbox)}"
@@ -163,10 +167,6 @@ function _setup_docker_registry() {
163167
-subj '/' \
164168
-out "${DOCKER_REG_CERTS_DIR}"/domain.crt
165169

166-
# Add certificate to Podman's trusted certificates (rootless)
167-
mkdir --parents "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"
168-
cp "${DOCKER_REG_CERTS_DIR}"/domain.crt "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"/domain.crt
169-
170170
# Create a registry user
171171
# username: user; password: user
172172
mkdir --parents "${DOCKER_REG_AUTH_DIR}"
@@ -188,12 +188,22 @@ function _setup_docker_registry() {
188188
--env REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
189189
--name "${DOCKER_REG_NAME}" \
190190
--privileged \
191-
--publish 50000:5000 \
191+
--publish 5000 \
192192
--rm \
193193
--volume "${DOCKER_REG_AUTH_DIR}":/auth \
194194
--volume "${DOCKER_REG_CERTS_DIR}":/certs \
195195
"${IMAGES[docker-reg]}"
196196

197+
# Determine the randomly assigned host port and persist the DOCKER_REG_URI to a file
198+
local docker_reg_port
199+
docker_reg_port="$(podman --root "${DOCKER_REG_ROOT}" port "${DOCKER_REG_NAME}" 5000)"
200+
DOCKER_REG_URI="localhost:${docker_reg_port##*:}"
201+
echo "$DOCKER_REG_URI" > "$DOCKER_REG_URI_FILE"
202+
203+
# Add certificate to Podman's trusted certificates (rootless)
204+
mkdir --parents "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"
205+
cp "${DOCKER_REG_CERTS_DIR}"/domain.crt "$HOME"/.config/containers/certs.d/"${DOCKER_REG_URI}"/domain.crt
206+
197207
_wait_for_docker_registry
198208

199209
podman login --authfile "${BATS_SUITE_TMPDIR}/authfile.json" --username user --password user "${DOCKER_REG_URI}"

0 commit comments

Comments
 (0)