mirror of
https://github.com/troglobit/finit.git
synced 2026-10-18 10:59:34 +00:00
test: namespace cleanup, common -> src, tenv -> lib, etc.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
49cd3a441d
commit
9c727ed08d
38 files changed
+61
-66
No files matched your search
@@ -0,0 +1,2 @@
|
||||
/Makefile
|
||||
/Makefile.in
|
||||
@@ -0,0 +1 @@
|
||||
EXTRA_DIST = exec.sh setup.sh start.sh sysroot.mk
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
cmd=""
|
||||
if [ "$#" -lt 1 ]; then
|
||||
cmd="/bin/sh"
|
||||
else
|
||||
cmd=$1
|
||||
shift
|
||||
fi
|
||||
|
||||
TEST_DIR=$(dirname "$0")/..
|
||||
SYSROOT="${SYSROOT:-$(pwd)/${TEST_DIR}/sysroot}"
|
||||
PID_FILE="$SYSROOT/running_test.pid"
|
||||
|
||||
if [ -f "$PID_FILE" ] ; then
|
||||
target=$(cat "$PID_FILE")
|
||||
target=$(pgrep -P "$target")
|
||||
else
|
||||
echo "No test running!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC1091
|
||||
. "$TEST_DIR/test.env"
|
||||
|
||||
"$(dirname "$0")"/exec.sh "$target" "$cmd" "$@"
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
# Execute a given command from within the test environment.
|
||||
#
|
||||
# Copyright (c) 2021 Jacques de Laval <jacques@de-laval.se>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
set -eu
|
||||
|
||||
if [ "$#" -lt 2 ]; then
|
||||
echo "Usage:"
|
||||
echo " $0 [target-pid] [command [arguments]]"
|
||||
exit 1
|
||||
fi
|
||||
target="$1"
|
||||
shift
|
||||
|
||||
TEST_DIR=$(dirname "$0"../)
|
||||
SYSROOT="${SYSROOT:-$(pwd)/${TEST_DIR}/sysroot}"
|
||||
|
||||
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" "$SYSROOT" "$@"
|
||||
Executable
+292
@@ -0,0 +1,292 @@
|
||||
#!/bin/sh
|
||||
# Small framework for executing tests in a test environment.
|
||||
#
|
||||
# Copyright (c) 2021 Jacques de Laval <jacques@de-laval.se>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
assert_file_contains()
|
||||
{
|
||||
assert "File $1 contains the string $2" "$(texec grep "$2" "$1")"
|
||||
}
|
||||
|
||||
assert_num_children()
|
||||
{
|
||||
assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1"
|
||||
}
|
||||
|
||||
assert_num_services()
|
||||
{
|
||||
assert "$1 services are loaded" "$(texec initctl -t status "$2" | wc -l)" -eq "$1"
|
||||
}
|
||||
|
||||
assert_forking()
|
||||
{
|
||||
assert "service $1 forking:$2" "$(texec initctl -j status "$1" | jq -M .forking)" = "$2"
|
||||
}
|
||||
|
||||
assert_nopid()
|
||||
{
|
||||
assert "service $1 pid:0" "$(texec initctl -j status "$1" | jq -M .pid)" -eq 0
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
assert_new_pid()
|
||||
{
|
||||
assert "Finit has registered new PID" "$(texec initctl |grep $1 | awk '{print $1;}')" -eq "$(texec cat $2)"
|
||||
}
|
||||
|
||||
assert_restarts()
|
||||
{
|
||||
assert "Finit has registered restarts" "$(texec initctl status "$2" | awk '/Restarts/{print $3;}')" -ge "$1"
|
||||
}
|
||||
|
||||
assert_norestart()
|
||||
{
|
||||
assert "Service $1 is stable, no restarts" "$(texec initctl status "$1" | awk '/Restarts/{print $3;}')" -eq "0"
|
||||
}
|
||||
|
||||
assert_desc()
|
||||
{
|
||||
assert "Service description == $1" "$(texec initctl status "$2" | grep 'Description' | sed 's/Description : //')" = "$1"
|
||||
}
|
||||
|
||||
assert_status()
|
||||
{
|
||||
service=$1
|
||||
status=$2
|
||||
assert "Service $service $status" "$(texec initctl -p status "$service" | awk '/Status/{print $3}')" = "$status"
|
||||
}
|
||||
|
||||
assert_cond()
|
||||
{
|
||||
cond=$1
|
||||
assert "Condition $cond asserted" "$(texec initctl -v cond get "$cond")" = "on"
|
||||
}
|
||||
|
||||
assert_nocond()
|
||||
{
|
||||
cond=$1
|
||||
assert "Condition $cond cleared" "$(texec initctl -v cond get "$cond")" = "off"
|
||||
}
|
||||
|
||||
assert_is_pidfile()
|
||||
{
|
||||
assert "Process has PID file: $2" "$(texec initctl -p status "$1" | awk '/PID file/{print $4}')" = "$2"
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
assert_has_pidfile()
|
||||
{
|
||||
assert "Process has PID file" "$(texec find $1 2>/dev/null)"
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2154
|
||||
texec()
|
||||
{
|
||||
"$TEST_DIR/lib/exec.sh" "$finit_pid" "$@"
|
||||
}
|
||||
|
||||
run()
|
||||
{
|
||||
texec sh -c "$@"
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
test=$(basename "$0" ".sh")
|
||||
printf "\e[2m[%s]\e[0m %b%b%b %s\n" "$test" "$1" "$2" "$color_reset" "$3"
|
||||
}
|
||||
|
||||
sep()
|
||||
{
|
||||
printf "\e[2m――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\e[0m\n"
|
||||
}
|
||||
|
||||
say()
|
||||
{
|
||||
log "$fg_yellow" • "$@"
|
||||
}
|
||||
|
||||
skip()
|
||||
{
|
||||
log "$fg_yellow" − "$*"
|
||||
exit 77
|
||||
}
|
||||
|
||||
fail()
|
||||
{
|
||||
log "$fg_red" ✘ "$*"
|
||||
exit 99
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2068
|
||||
check_dep()
|
||||
{
|
||||
if ! command -v "$1"; then
|
||||
skip "Cannot find $1, skipping test."
|
||||
fi
|
||||
}
|
||||
|
||||
assert()
|
||||
{
|
||||
__assert_msg=$1
|
||||
shift
|
||||
|
||||
if [ ! "$@" ]; then
|
||||
log "$fg_red" ✘ "$__assert_msg ($*)"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "$fg_green" ✔ "$__assert_msg"
|
||||
return 0
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
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' ''
|
||||
elif [ "$test_status" -eq 77 ]; then
|
||||
log "$fg_yellow" 'TEST SKIP' ''
|
||||
else
|
||||
log "$fg_red" 'TEST FAIL' ''
|
||||
fi
|
||||
|
||||
say "Telling Finit to shut down (set -$-) ..."
|
||||
set +e
|
||||
if [ -n "${finit_pid+x}" ]; then
|
||||
texec kill -SIGUSR2 1
|
||||
fi
|
||||
|
||||
say "Waiting for Finit to shut down ..."
|
||||
wait
|
||||
|
||||
say "Final cleanup ..."
|
||||
rm -f "$SYSROOT"/running_test.pid
|
||||
|
||||
|
||||
say "EXIT: $test_status"
|
||||
exit $test_status
|
||||
}
|
||||
|
||||
trap teardown EXIT
|
||||
|
||||
SYSROOT="${SYSROOT:-$(pwd)/${TEST_DIR}/sysroot}"
|
||||
export SYSROOT
|
||||
|
||||
# shellcheck source=/dev/null
|
||||
. "$SYSROOT/../test.env"
|
||||
|
||||
# Setup test environment
|
||||
if [ -n "${DEBUG:-}" ]; then
|
||||
FINIT_ARGS="${FINIT_ARGS:-} finit.debug=on"
|
||||
fi
|
||||
|
||||
# Tests may want to override runlevel
|
||||
if [ -n "${FINIT_RUNLEVEL:-}" ]; then
|
||||
FINIT_ARGS="${FINIT_ARGS:-} $FINIT_RUNLEVEL"
|
||||
else
|
||||
FINIT_RUNLEVEL=2
|
||||
fi
|
||||
|
||||
# shellcheck disable=2086
|
||||
"$TEST_DIR/lib/start.sh" finit ${FINIT_ARGS:-} &
|
||||
finit_ppid=$!
|
||||
echo "$finit_ppid" > "$SYSROOT"/running_test.pid
|
||||
|
||||
#>&2 echo "Hint: Execute 'SYSROOT=$SYSROOT $TEST_DIR/lib/enter.sh' to enter the test namespace"
|
||||
log "$color_reset" 'Setup of test environment done, waiting for Finit ...' ''
|
||||
|
||||
finit_pid=$(retry "pgrep -P $finit_ppid")
|
||||
|
||||
#tty=/dev/$(texec cat /sys/class/tty/console/active)
|
||||
#texec cat "$tty" &
|
||||
|
||||
# Allow Finit to start up properly before launching the test
|
||||
i=0
|
||||
while [ $i -lt 10 ]; do
|
||||
lvl=$(texec sh -c "initctl runlevel | awk '{print \$2;}'")
|
||||
if [ "$lvl" -eq "$FINIT_RUNLEVEL" ]; then
|
||||
# say "Reached runlevel $FINIT_RUNLEVEL, releasing test."
|
||||
break;
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if type test_setup > /dev/null 2>&1 ; then
|
||||
test_setup
|
||||
fi
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
# Start a new test environment.
|
||||
#
|
||||
# Copyright (c) 2021 Jacques de Laval <jacques@de-laval.se>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
set -eu
|
||||
|
||||
TEST_DIR=$(dirname "$0"../)
|
||||
SYSROOT="${SYSROOT:-$(pwd)/${TEST_DIR}/sysroot}"
|
||||
|
||||
unshare=$(command -v unshare)
|
||||
chroot=$(command -v chroot)
|
||||
|
||||
export container="unshare"
|
||||
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" "$SYSROOT" /sbin/chrootsetup.sh "$@"
|
||||
@@ -0,0 +1,77 @@
|
||||
# Root file system for test environment.
|
||||
#
|
||||
# Copyright (c) 2021 Jacques de Laval <jacques@de-laval.se>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
srcdir ?= ../
|
||||
SKEL ?= $(srcdir)/skel
|
||||
DEST ?= ../sysroot
|
||||
|
||||
CACHE ?= ~/.cache
|
||||
ARCH ?= x86_64
|
||||
|
||||
FINITBIN ?= ./sbin/finit
|
||||
|
||||
BBVER ?= 1_35_0
|
||||
BBBIN = busybox-$(ARCH)
|
||||
BBHOME ?= https://github.com/troglobit/busybox-builder/releases/download
|
||||
BBURL ?= $(BBHOME)/$(BBVER)/$(BBBIN)
|
||||
|
||||
_libs_src = $(shell ldd $(FINITBIN) | grep -Eo '/[^ ]+')
|
||||
libs = $(foreach path,$(_libs_src),$(abspath $(DEST))$(path))
|
||||
|
||||
all: $(libs) $(DEST)/bin/$(BBBIN)
|
||||
@(cd $(DEST); \
|
||||
for prg in `./bin/$(BBBIN) --list-full`; do \
|
||||
ln -sf /bin/$(BBBIN) $$prg; \
|
||||
done)
|
||||
|
||||
$(DEST)/bin/$(BBBIN).sha256:
|
||||
@mkdir -p $(DEST)
|
||||
@cp -a $(SKEL)/* $(DEST)/
|
||||
@chmod -R u+w $(DEST)/
|
||||
@find $(DEST) -name .empty -delete
|
||||
|
||||
$(DEST)/bin/$(BBBIN): $(DEST)/bin/$(BBBIN).sha256
|
||||
@(cd $(dir $@); \
|
||||
if ! sha256sum --status -c $(BBBIN).sha256 2>/dev/null; then \
|
||||
if [ -d $(CACHE) ]; then \
|
||||
echo "Cannot find $(BBBIN), checking $(CACHE) ..."; \
|
||||
cd $(CACHE); \
|
||||
cp -v $(DEST)/bin/$(BBBIN).sha256 .; \
|
||||
if ! sha256sum --status -c $(BBBIN).sha256; then \
|
||||
echo "No $(BBBIN), downloading $(BBURL) ..."; \
|
||||
wget -O $(BBBIN) $(BBURL); \
|
||||
else \
|
||||
echo "Found valid $(BBBIN) in cache!"; \
|
||||
fi; \
|
||||
cp $(BBBIN) $@; \
|
||||
cd $(dir $@); \
|
||||
else \
|
||||
echo "Cannot find $(BBBIN), downloading ..."; \
|
||||
wget -O $@ $(BBURL); \
|
||||
fi; \
|
||||
sha256sum -c $(BBBIN).sha256 || (rm $@; false); \
|
||||
fi)
|
||||
@chmod +x $@
|
||||
|
||||
$(libs): $(DEST)/bin/$(BBBIN).sha256
|
||||
mkdir -p $(dir $@)
|
||||
cp $(patsubst $(abspath $(DEST))%,%,$@) $@
|
||||
Reference in new issue
Block a user