183 lines
6.2 KiB
Bash
183 lines
6.2 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/fallocate" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf 'fallocate\n' >>"$RUNNER_TEST_LOG"
|
|
truncate -s 1048576 "$3"
|
|
EOF
|
|
cat >"$tmp/bin/mkfs.ext4" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
printf 'mkfs\n' >>"$RUNNER_TEST_LOG"
|
|
printf 'ext4\n' >"$3.fs"
|
|
EOF
|
|
cat >"$tmp/bin/blkid" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
cat "${@: -1}.fs"
|
|
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\n' "$RUNNER_TEST_STORE"
|
|
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"
|
|
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}"
|
|
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"
|
|
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 ]]
|
|
|
|
image=$tmp/docker-store.img
|
|
runner_ensure_image "$image" 1M 1048576
|
|
runner_ensure_image "$image" 1M 1048576
|
|
[[ $(grep -c '^fallocate$' "$log") == 1 ]]
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
|
|
printf 'ext4\n' >"$image.fs"
|
|
truncate -s 1048575 "$image"
|
|
if runner_ensure_image "$image" 1M 1048576 2>/dev/null; then
|
|
echo 'Mismatched image size was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
truncate -s 1048576 "$image"
|
|
printf 'ext3\n' >"$image.fs"
|
|
if runner_ensure_image "$image" 1M 1048576 2>/dev/null; then
|
|
echo 'Existing non-ext4 image was accepted' >&2
|
|
exit 1
|
|
fi
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
printf 'ext4\n' >"$image.fs"
|
|
|
|
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" 1M 1048576
|
|
runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$unit_file"
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
[[ $(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" 1M 1048576
|
|
runner_ensure_mount "$image" "$RUNNER_TEST_STORE" "$unit_name" "$tmp/systemd/partial.mount"
|
|
[[ $(grep -c '^mkfs$' "$log") == 1 ]]
|
|
[[ $(grep -c '^start ' "$log") == 3 ]]
|
|
echo 'runner installer idempotency checks passed'
|