Merge pull request #5753 from obsidiansystems/init-twice-works

Clean up daemon handling in the tests
This commit is contained in:
Robert Hensing 2023-02-23 18:13:14 +01:00 committed by GitHub
commit b5bbf14b87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 23 deletions

2
.gitignore vendored
View file

@ -75,7 +75,7 @@ perl/Makefile.config
# /tests/
/tests/test-tmp
/tests/common.sh
/tests/common/vars-and-functions.sh
/tests/result*
/tests/restricted-innocent
/tests/shell

12
tests/common.sh Normal file
View file

@ -0,0 +1,12 @@
set -e
if [[ -z "${COMMON_SH_SOURCED-}" ]]; then
COMMON_SH_SOURCED=1
source "$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")/common/vars-and-functions.sh"
if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
startDaemon
fi
fi # COMMON_SH_SOURCED

View file

@ -1,8 +1,8 @@
set -e
if [[ -z "$COMMON_SH_SOURCED" ]]; then
if [[ -z "${COMMON_VARS_AND_FUNCTIONS_SH_SOURCED-}" ]]; then
COMMON_SH_SOURCED=1
COMMON_VARS_AND_FUNCTIONS_SH_SOURCED=1
export PS4='+(${BASH_SOURCE[0]}:$LINENO) '
@ -25,7 +25,7 @@ if [[ -n $NIX_STORE ]]; then
fi
export _NIX_IN_TEST=$TEST_ROOT/shared
export _NIX_TEST_NO_LSOF=1
export NIX_REMOTE=$NIX_REMOTE_
export NIX_REMOTE=${NIX_REMOTE_-}
unset NIX_PATH
export TEST_HOME=$TEST_ROOT/test-home
export HOME=$TEST_HOME
@ -90,13 +90,14 @@ clearCacheCache() {
startDaemon() {
# Dont start the daemon twice, as this would just make it loop indefinitely
if [[ "$NIX_REMOTE" == daemon ]]; then
if [[ "${_NIX_TEST_DAEMON_PID-}" != '' ]]; then
return
fi
# Start the daemon, wait for the socket to appear.
rm -f $NIX_DAEMON_SOCKET_PATH
PATH=$DAEMON_PATH nix-daemon &
pidDaemon=$!
_NIX_TEST_DAEMON_PID=$!
export _NIX_TEST_DAEMON_PID
for ((i = 0; i < 300; i++)); do
if [[ -S $NIX_DAEMON_SOCKET_PATH ]]; then
DAEMON_STARTED=1
@ -108,25 +109,35 @@ startDaemon() {
fail "Didnt manage to start the daemon"
fi
trap "killDaemon" EXIT
# Save for if daemon is killed
NIX_REMOTE_OLD=$NIX_REMOTE
export NIX_REMOTE=daemon
}
killDaemon() {
kill $pidDaemon
# Dont fail trying to stop a non-existant daemon twice
if [[ "${_NIX_TEST_DAEMON_PID-}" == '' ]]; then
return
fi
kill $_NIX_TEST_DAEMON_PID
for i in {0..100}; do
kill -0 $pidDaemon 2> /dev/null || break
kill -0 $_NIX_TEST_DAEMON_PID 2> /dev/null || break
sleep 0.1
done
kill -9 $pidDaemon 2> /dev/null || true
wait $pidDaemon || true
kill -9 $_NIX_TEST_DAEMON_PID 2> /dev/null || true
wait $_NIX_TEST_DAEMON_PID || true
rm -f $NIX_DAEMON_SOCKET_PATH
# Indicate daemon is stopped
unset _NIX_TEST_DAEMON_PID
# Restore old nix remote
NIX_REMOTE=$NIX_REMOTE_OLD
trap "" EXIT
}
restartDaemon() {
[[ -z "${pidDaemon:-}" ]] && return 0
[[ -z "${_NIX_TEST_DAEMON_PID:-}" ]] && return 0
killDaemon
unset NIX_REMOTE
startDaemon
}
@ -190,10 +201,6 @@ enableFeatures() {
set -x
if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
startDaemon
fi
onError() {
set +x
echo "$0: test failed at:" >&2
@ -205,4 +212,4 @@ onError() {
trap onError ERR
fi # COMMON_SH_SOURCED
fi # COMMON_VARS_AND_FUNCTIONS_SH_SOURCED

View file

@ -9,7 +9,6 @@ fi
source common.sh
killDaemon
unset NIX_REMOTE
# Fill the db using the older Nix
PATH_WITH_NEW_NIX="$PATH"

7
tests/init.sh Normal file → Executable file
View file

@ -1,8 +1,13 @@
source common.sh
set -eu -o pipefail
# Don't start the daemon
source common/vars-and-functions.sh
test -n "$TEST_ROOT"
if test -d "$TEST_ROOT"; then
chmod -R u+w "$TEST_ROOT"
# We would delete any daemon socket, so let's stop the daemon first.
killDaemon
rm -rf "$TEST_ROOT"
fi
mkdir "$TEST_ROOT"

View file

@ -1,4 +1,5 @@
nix_tests = \
init.sh \
flakes/flakes.sh \
flakes/run.sh \
flakes/mercurial.sh \
@ -128,9 +129,9 @@ endif
install-tests += $(foreach x, $(nix_tests), tests/$(x))
clean-files += $(d)/common.sh $(d)/config.nix $(d)/ca/config.nix
clean-files += $(d)/tests/common/vars-and-functions.sh $(d)/config.nix $(d)/ca/config.nix
test-deps += tests/common.sh tests/config.nix tests/ca/config.nix
test-deps += tests/common/vars-and-functions.sh tests/config.nix tests/ca/config.nix tests/plugins/libplugintest.$(SO_EXT)
ifeq ($(BUILD_SHARED_LIBS), 1)
test-deps += tests/plugins/libplugintest.$(SO_EXT)