test: Move test support files to separate dir and keep tests in /test

Signed-off-by: Jacques de Laval <jacques@de-laval.se>
This commit is contained in:
Jacques de Laval committed 2021-02-27 14:50:49 +01:00
1 parent 4aa17a84ab
commit 7fe1189f7b
15 files changed
+62 -65

No files matched your search

+1
View File
@@ -0,0 +1 @@
ea7e2353af0067a926d3e021e504290e busybox-x86_64
+23
View File
@@ -0,0 +1,23 @@
#!/bin/sh
set -eu
# It would have been nice to mount /dev as devtmpfs, but unfortunately
# that's not possible if you're not privileged. For now tmpfs will
# have to do.
mount -n -t tmpfs none /dev
mount -t proc none /proc
mount -t sysfs none /sys
mkdir -p /tmp.shadow
mount --bind /tmp /tmp.shadow
mount -t tmpfs none /tmp
cp -a /tmp.shadow/* /tmp/
mkdir -p "$FINIT_RCSD"
tty=/dev/$(cat /sys/class/tty/console/active)
mkfifo "$tty"
exec "$@"
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
set -eu
cmd=""
if [ "$#" -lt 1 ]; then
cmd="/bin/sh"
else
cmd=$1
shift
fi
TEST_DIR=$(dirname "$0")/..
TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}"
PID_FILE="$TENV_ROOT/running_test.pid"
if [ -f "$PID_FILE" ] ; then
target=$(cat "$PID_FILE")
target=$(pgrep -P "$target")
else
echo "No test running!"
exit 1
fi
source "$TEST_DIR/test.env"
"$(dirname "$0")"/exec.sh "$target" "$cmd" "$@"
+36
View File
@@ -0,0 +1,36 @@
#!/bin/sh
set -eu
if [ "$#" -lt 2 ]; then
echo "Usage:"
echo " $0 [target-pid] [command [arguments]]"
exit 1
fi
target="$1"
shift
TEST_DIR=$(dirname "$0"../)
TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}"
nsenter=$(command -v nsenter)
chroot=$(command -v chroot)
export PS1='\w \$ '
export PS2='> '
export PS3='#? '
export PS4='+ '
PATH="$TESTENV_PATH"
export PATH
"$nsenter" \
--preserve-credentials \
--user \
--mount \
--uts \
--ipc \
--net \
--pid \
-w -t "$target" \
"$chroot" "$TENV_ROOT" "$@"
+123
View File
@@ -0,0 +1,123 @@
#!/bin/sh
assert_num_children() {
assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1"
}
texec() {
# shellcheck disable=SC2154
"$TEST_DIR/tenv/exec.sh" "$finit_pid" "$@"
}
pause() {
echo "Press any key to continue... "
read -r REPLY
}
toggle_finit_debug() {
say 'Toggle finit debug'
texec initctl debug
sleep 0.5
}
color_reset='\e[0m'
fg_red='\e[1;31m'
fg_green='\e[1;32m'
fg_yellow='\e[1;33m'
log() {
printf "< %s > %b%b%b %s\n" "$0" "$1" "$2" "$color_reset" "$3"
}
assert() {
__assert_msg=$1
shift
if [ ! "$@" ]; then
log "$fg_red" ✘ "$__assert_msg ($*)"
return 1
fi
log "$fg_green" ✔ "$__assert_msg"
}
retry() {
__retry_cmd=$1
shift
case "$#" in
2)
__retry_n=$1
__retry_sleep=$2
;;
1)
__retry_n=$1
__retry_sleep=0.1
;;
*)
__retry_n=50
__retry_sleep=0.1
;;
esac
for _ in $(seq 1 "$__retry_n"); do
sleep "$__retry_sleep"
__retry_cmd_out=$(eval "$__retry_cmd") && \
echo "$__retry_cmd_out" && \
return 0
done
__retry_cmd_exit="$?"
echo "$__retry_cmd_out"
return "$__retry_cmd_exit"
}
say() {
log "$fg_yellow" • "$@"
}
teardown() {
test_status="$?"
if type test_teardown > /dev/null 2>&1 ; then
test_teardown
fi
log "$color_reset" '--' ''
if [ "$test_status" -eq 0 ]; then
log "$fg_green" 'TEST PASS' ''
else
log "$fg_red" 'TEST FAIL' ''
fi
if [ -n "${finit_pid+x}" ]; then
texec kill -SIGUSR2 1
fi
wait
if [ -d "$TENV_ROOT/var/lock" ]; then
chmod +r "$TENV_ROOT/var/lock"
fi
rm -f "$TENV_ROOT"/running_test.pid
}
trap teardown EXIT
TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}"
export TENV_ROOT
# shellcheck source=/dev/null
. "$TENV_ROOT/../test.env"
# Setup test environment
if [ -n "${DEBUG:-}" ]; then
FINIT_ARGS="${FINIT_ARGS:-} finit.debug=on"
fi
# shellcheck disable=2086
"$TEST_DIR/tenv/start.sh" finit ${FINIT_ARGS:-} &
finit_ppid=$!
echo "$finit_ppid" > "$TENV_ROOT"/running_test.pid
>&2 echo "Hint: Execute 'TENV_ROOT=$TENV_ROOT $TEST_DIR/tenv/enter.sh' to enter the test namespace"
log "$color_reset" 'Setup of test environment done' ''
finit_pid=$(retry "pgrep -P $finit_ppid")
tty=/dev/$(texec cat /sys/class/tty/console/active)
texec cat "$tty" &
sleep 1
+71
View File
@@ -0,0 +1,71 @@
.PHONY: clean check
DEST ?= ../tenv-root
srcdir ?= ../
ARCH ?= x86_64
FINITBIN ?= ./sbin/finit
BBVER ?= 1.31.0
BBBIN = busybox-$(ARCH)
BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN)
binaries = $(DEST)/bin/cat \
$(DEST)/bin/cp \
$(DEST)/bin/date \
$(DEST)/bin/echo \
$(DEST)/bin/env \
$(DEST)/bin/find \
$(DEST)/bin/kill \
$(DEST)/bin/ls \
$(DEST)/bin/mkdir \
$(DEST)/bin/mkfifo \
$(DEST)/bin/mknod \
$(DEST)/bin/mount \
$(DEST)/bin/printf \
$(DEST)/bin/pgrep \
$(DEST)/bin/ps \
$(DEST)/bin/rm \
$(DEST)/bin/sh \
$(DEST)/bin/sleep \
$(DEST)/bin/top \
$(DEST)/bin/touch
dirs = $(DEST)/bin \
$(DEST)/dev \
$(DEST)/etc \
$(DEST)/proc \
$(DEST)/sbin \
$(DEST)/sys \
$(DEST)/test_assets \
$(DEST)/tmp
_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+')
libs = $(foreach path,$(_libs_src),$(abspath $(DEST))$(path))
all: $(dirs) $(binaries) $(libs) $(DEST)/bin/chrootsetup.sh
$(dirs):
mkdir -p $@
$(DEST)/bin/$(BBBIN).md5:
cp $(srcdir)/tenv/$(notdir $@) $@
$(DEST)/bin/$(BBBIN): $(DEST)/bin/$(BBBIN).md5
wget -O $@ $(BBURL)
chmod +x $@
cd $(dir $@); \
md5sum -c $(BBBIN).md5
$(DEST)/bin/chrootsetup.sh:
cp $(srcdir)/tenv/$(notdir $@) $@
$(binaries): $(DEST)/bin/$(BBBIN)
cd $(DEST)/bin; \
rm -f $(notdir $@); \
ln -s $(BBBIN) $(notdir $@)
$(libs):
mkdir -p $(dir $@)
cp $(patsubst $(abspath $(DEST))%,%,$@) $@
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
set -eu
TEST_DIR=$(dirname "$0"../)
TENV_ROOT="${TENV_ROOT:-$(pwd)/${TEST_DIR}/tenv-root}"
unshare=$(command -v unshare)
chroot=$(command -v chroot)
export PS1='\w \$ '
export PS2='> '
export PS3='#? '
export PS4='+ '
PATH="$TESTENV_PATH"
export PATH
# Not supported by Busybox unshare:
# --cgroup --time
exec "$unshare" \
--user --map-root-user \
--fork --pid --mount-proc \
--mount \
--mount-proc \
--uts --ipc --net \
"$chroot" "$TENV_ROOT" /bin/chrootsetup.sh "$@"