252 lines
9.4 KiB
Bash
252 lines
9.4 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
runner_dir=$(cd "$(dirname "$0")" && pwd)
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
mkdir -p "$tmp/bin" "$tmp/store" "$tmp/partial-store"
|
|
log=$tmp/commands.log
|
|
export RUNNER_TEST_LOG=$log
|
|
|
|
cat >"$tmp/bin/mkfs.ext4" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf 'mkfs\n' >>"$RUNNER_TEST_LOG"
|
|
if [[ -n ${RUNNER_TEST_REPLACE_IMAGE:-} ]]; then
|
|
rm -f "$RUNNER_TEST_IMAGE"
|
|
ln -s "$RUNNER_TEST_REPLACE_IMAGE" "$RUNNER_TEST_IMAGE"
|
|
fi
|
|
EOF
|
|
cat >"$tmp/bin/blkid" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf '%s\n' "${RUNNER_TEST_FS:-ext4}"
|
|
EOF
|
|
cat >"$tmp/bin/systemctl" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf '%s\n' "$*" >>"$RUNNER_TEST_LOG"
|
|
if [[ $1 == start ]]; then
|
|
[[ ${RUNNER_TEST_FAIL_START:-0} == 0 ]] || exit 1
|
|
: >"$RUNNER_TEST_MOUNTED"
|
|
fi
|
|
EOF
|
|
cat >"$tmp/bin/mountpoint" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
[[ -e $RUNNER_TEST_MOUNTED ]]
|
|
EOF
|
|
cat >"$tmp/bin/findmnt" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf '%s ext4 /dev/loop0 %s\n' "$RUNNER_TEST_STORE" "${RUNNER_TEST_OPTIONS:-rw,nosuid,nodev,relatime}"
|
|
EOF
|
|
cat >"$tmp/bin/losetup" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf '%s\n' "$RUNNER_TEST_BACKING"
|
|
EOF
|
|
chmod +x "$tmp/bin/"*
|
|
PATH="$tmp/bin:$PATH"
|
|
export PATH
|
|
source "$runner_dir/install-runner-lib.sh"
|
|
runuser() {
|
|
shift 2
|
|
[[ $1 == -- ]] || return 2
|
|
shift
|
|
"$@"
|
|
}
|
|
getent() {
|
|
[[ $1 == passwd ]] || return 2
|
|
if [[ -n ${RUNNER_TEST_PASSWD:-} ]]; then
|
|
printf '%s\n' "$RUNNER_TEST_PASSWD"
|
|
elif [[ -r ${RUNNER_TEST_ACCOUNT_STATE:-} ]]; then
|
|
cat "$RUNNER_TEST_ACCOUNT_STATE"
|
|
else
|
|
return 2
|
|
fi
|
|
}
|
|
id() {
|
|
case $1 in
|
|
-gn) printf '%s\n' "${RUNNER_TEST_PRIMARY_GROUP:-akyldash-runner}" ;;
|
|
-nG) [[ ${TEST_EXTRA_GROUP:-0} == 1 ]] && printf 'akyldash-runner docker\n' || printf 'akyldash-runner\n' ;;
|
|
*) command id "$@" ;;
|
|
esac
|
|
}
|
|
stat() {
|
|
if [[ $1 == -c && $2 == %U && $3 == ${RUNNER_TEST_HOME:-} ]]; then
|
|
printf '%s\n' "${RUNNER_TEST_HOME_OWNER:-akyldash-runner}"
|
|
elif [[ $1 == -c && $2 == %G:%a && $3 == ${RUNNER_TEST_HOME:-} ]]; then
|
|
printf '%s:%s\n' "${RUNNER_TEST_HOME_GROUP:-root}" "${RUNNER_TEST_HOME_MODE:-711}"
|
|
else
|
|
command stat "$@"
|
|
fi
|
|
}
|
|
useradd() {
|
|
printf 'useradd\n' >>"$RUNNER_TEST_LOG"
|
|
local user=${@: -1} home=
|
|
while (($#)); do
|
|
if [[ $1 == --home-dir ]]; then home=$2; shift 2; else shift; fi
|
|
done
|
|
mkdir -p "$home"
|
|
printf '%s:x:990:990::%s:/usr/sbin/nologin\n' "$user" "$home" >"$RUNNER_TEST_ACCOUNT_STATE"
|
|
}
|
|
|
|
export RUNNER_TEST_HOME="$tmp/account-home"
|
|
export RUNNER_TEST_ACCOUNT_STATE="$tmp/account.passwd"
|
|
mkdir -p "$RUNNER_TEST_HOME"
|
|
export RUNNER_TEST_PASSWD="akyldash-runner:x:990:990::${RUNNER_TEST_HOME}:/usr/sbin/nologin"
|
|
runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME"
|
|
RUNNER_TEST_HOME_OWNER=root runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME"
|
|
if RUNNER_TEST_HOME_OWNER=root RUNNER_TEST_HOME_MODE=755 runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Writable root-owned runner home was accepted' >&2
|
|
exit 1
|
|
fi
|
|
TEST_EXTRA_GROUP=1
|
|
if runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Runner account with extra group membership was accepted' >&2
|
|
exit 1
|
|
fi
|
|
unset TEST_EXTRA_GROUP
|
|
if RUNNER_TEST_PASSWD="akyldash-runner:x:990:990::${tmp}/wrong-home:/usr/sbin/nologin" runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Runner account with a different home was accepted' >&2
|
|
exit 1
|
|
fi
|
|
if RUNNER_TEST_PASSWD="akyldash-runner:x:0:990::${RUNNER_TEST_HOME}:/usr/sbin/nologin" runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Runner account with UID 0 was accepted' >&2
|
|
exit 1
|
|
fi
|
|
if RUNNER_TEST_PASSWD="akyldash-runner:x:1000:990::${RUNNER_TEST_HOME}:/usr/sbin/nologin" runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Runner account with a regular-user UID was accepted' >&2
|
|
exit 1
|
|
fi
|
|
RUNNER_TEST_PRIMARY_GROUP=unexpected
|
|
if runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME" 2>/dev/null; then
|
|
echo 'Runner account with an unexpected primary group was accepted' >&2
|
|
exit 1
|
|
fi
|
|
unset RUNNER_TEST_PRIMARY_GROUP
|
|
unset RUNNER_TEST_PASSWD
|
|
export RUNNER_TEST_HOME="$tmp/new-account-home"
|
|
runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME"
|
|
runner_ensure_account akyldash-runner "$RUNNER_TEST_HOME"
|
|
[[ $(grep -c '^useradd$' "$log") == 1 ]]
|
|
|
|
private_root=$tmp/private
|
|
mkdir -m 0700 "$private_root"
|
|
runner_owner=$(command id -un)
|
|
runner_group=$(command id -gn)
|
|
runner_ensure_private_directory "$private_root/config" "$runner_owner" "$runner_group"
|
|
runner_ensure_private_directory "$private_root/config" "$runner_owner" "$runner_group"
|
|
mkdir -m 0700 "$tmp/outside"
|
|
ln -s "$tmp/outside" "$private_root/linked"
|
|
if runner_ensure_private_directory "$private_root/linked/docker" "$runner_owner" "$runner_group" 2>/dev/null; then
|
|
echo 'Runner config symlink was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ ! -e $tmp/outside/docker ]]
|
|
|
|
legacy_root=$tmp/legacy
|
|
mkdir -p "$legacy_root/.config/systemd/user/docker.service.d"
|
|
chmod 0755 "$legacy_root" "$legacy_root/.config" "$legacy_root/.config/systemd" "$legacy_root/.config/systemd/user" "$legacy_root/.config/systemd/user/docker.service.d"
|
|
for legacy_dir in "$legacy_root" "$legacy_root/.config" "$legacy_root/.config/systemd" "$legacy_root/.config/systemd/user" "$legacy_root/.config/systemd/user/docker.service.d"; do
|
|
runner_ensure_private_directory "$legacy_dir" "$runner_owner" "$runner_group"
|
|
[[ $(stat -c %a "$legacy_dir") == 700 ]]
|
|
done
|
|
user_config=$private_root/daemon.json
|
|
runner_install_user_content "$user_config" "$runner_owner" "$runner_group" 0600 <<'EOF'
|
|
{"data-root":"/private/runner"}
|
|
EOF
|
|
runner_install_user_content "$user_config" "$runner_owner" "$runner_group" 0600 <<'EOF'
|
|
{"data-root":"/private/runner"}
|
|
EOF
|
|
if runner_install_user_content "$user_config" "$runner_owner" "$runner_group" 0600 <<'EOF' 2>/dev/null
|
|
{"data-root":"/unexpected"}
|
|
EOF
|
|
then
|
|
echo 'Conflicting runner user config was accepted' >&2
|
|
exit 1
|
|
fi
|
|
|
|
image=$tmp/docker-store.img
|
|
runner_ensure_image "$image" 1048576 "$(command id -u)" 700
|
|
runner_ensure_image "$image" 1048576 "$(command id -u)" 700
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
|
|
truncate -s 1048575 "$image"
|
|
if runner_ensure_image "$image" 1048576 "$(command id -u)" 700 2>/dev/null; then
|
|
echo 'Mismatched image size was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
truncate -s 1048576 "$image"
|
|
if RUNNER_TEST_FS=ext3 runner_ensure_image "$image" 1048576 "$(command id -u)" 700 2>/dev/null; then
|
|
echo 'Existing non-ext4 image was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
external=$tmp/external-file
|
|
printf 'preserve this file\n' >"$external"
|
|
race_image=$tmp/race.img
|
|
ln -s "$external" "$race_image"
|
|
if runner_ensure_image "$race_image" 1048576 "$(command id -u)" 700 2>/dev/null; then
|
|
echo 'Symlink runner image was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(cat "$external") == 'preserve this file' ]]
|
|
rm -f "$race_image"
|
|
race_image=$tmp/race-during-format.img
|
|
export RUNNER_TEST_IMAGE="$race_image" RUNNER_TEST_REPLACE_IMAGE="$external"
|
|
if runner_ensure_image "$race_image" 1048576 "$(command id -u)" 700 2>/dev/null; then
|
|
echo 'Image path replacement during formatting was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(cat "$external") == 'preserve this file' ]]
|
|
rm -f "$race_image"
|
|
unset RUNNER_TEST_IMAGE RUNNER_TEST_REPLACE_IMAGE
|
|
[[ $(grep -c '^mkfs$' "$log") == 2 ]]
|
|
|
|
unit_name='var-lib-akyldash\x2drunner-docker\x2ddata.mount'
|
|
unit_file=$tmp/systemd/$unit_name
|
|
mkdir -p "$(dirname "$unit_file")"
|
|
export RUNNER_TEST_STORE="$tmp/store" RUNNER_TEST_BACKING="$image"
|
|
export RUNNER_TEST_MOUNTED="$tmp/mounted"
|
|
runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$unit_file"
|
|
[[ $(grep -c '^start ' "$log") == 1 ]]
|
|
grep -Fq "enable $unit_name" "$log"
|
|
grep -Fq "start $unit_name" "$log"
|
|
|
|
# A second run with the image mounted must reuse it and avoid another start.
|
|
runner_ensure_image "$image" 1048576 "$(command id -u)" 700
|
|
runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$unit_file"
|
|
[[ $(grep -c '^mkfs$' "$log") == 2 ]]
|
|
[[ $(grep -c '^start ' "$log") == 1 ]]
|
|
chmod 0664 "$unit_file"
|
|
if runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$unit_file" 2>/dev/null; then
|
|
echo 'Writable systemd mount unit was accepted' >&2
|
|
exit 1
|
|
fi
|
|
chmod 0644 "$unit_file"
|
|
export RUNNER_TEST_BACKING="$tmp/unrelated.img"
|
|
touch "$RUNNER_TEST_BACKING"
|
|
if runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$unit_file" 2>/dev/null; then
|
|
echo 'Active mount backed by a different image was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(grep -c '^start ' "$log") == 1 ]]
|
|
export RUNNER_TEST_BACKING="$image"
|
|
|
|
# Simulate interruption after unit installation/enable, before mount activation.
|
|
export RUNNER_TEST_STORE="$tmp/partial-store" RUNNER_TEST_MOUNTED="$tmp/partial-mounted"
|
|
export RUNNER_TEST_FAIL_START=1
|
|
if runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$tmp/systemd/partial.mount" 2>/dev/null; then
|
|
echo 'Injected mount interruption was not observed' >&2
|
|
exit 1
|
|
fi
|
|
export RUNNER_TEST_FAIL_START=0
|
|
runner_ensure_image "$image" 1048576 "$(command id -u)" 700
|
|
runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$tmp/systemd/partial.mount"
|
|
[[ $(grep -c '^mkfs$' "$log") == 2 ]]
|
|
[[ $(grep -c '^start ' "$log") == 3 ]]
|
|
export RUNNER_TEST_STORE="$tmp/store" RUNNER_TEST_MOUNTED="$tmp/mounted" RUNNER_TEST_OPTIONS=rw,relatime
|
|
if runner_validate_mount "$image" "$RUNNER_TEST_STORE" 2>/dev/null; then
|
|
echo 'Runner mount missing nosuid/nodev was accepted' >&2
|
|
exit 1
|
|
fi
|
|
unset RUNNER_TEST_OPTIONS
|
|
echo 'runner installer idempotency checks passed'
|