1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-30 06:01:00 +01:00

Allow running all the tests with the daemon

When `NIX_DAEMON_PACKAGE` is set, make all the tests use the Nix daemon.
That way we can test every piece of Nix functionality both with and
without the daemon.

Tests for which using the daemon isn’t possible or doesn’t make sens can
selectively be disabled with `needLocalStore`

(cherry picked from commit addacfce4a)
This commit is contained in:
regnat 2021-07-26 06:54:55 +02:00 committed by John Ericson
parent 62a42ab43e
commit c434a11a04
15 changed files with 85 additions and 7 deletions

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "“--no-require-sigs” cant be used with the daemon"
clearStore clearStore
clearCache clearCache

View file

@ -1,5 +1,8 @@
source common.sh source common.sh
# XXX: This shouldnt be, but #4813 cause this test to fail
buggyNeedLocalStore "see #4813"
clearStore clearStore
nix-build dependencies.nix --no-out-link nix-build dependencies.nix --no-out-link

View file

@ -1,5 +1,9 @@
set -e set -e
if [[ -z "$COMMON_SH_SOURCED" ]]; then
COMMON_SH_SOURCED=1
export TEST_ROOT=$(realpath ${TMPDIR:-/tmp}/nix-test)/${TEST_NAME:-default} export TEST_ROOT=$(realpath ${TMPDIR:-/tmp}/nix-test)/${TEST_NAME:-default}
export NIX_STORE_DIR export NIX_STORE_DIR
if ! NIX_STORE_DIR=$(readlink -f $TEST_ROOT/store 2> /dev/null); then if ! NIX_STORE_DIR=$(readlink -f $TEST_ROOT/store 2> /dev/null); then
@ -43,6 +47,9 @@ export HAVE_SODIUM="@HAVE_SODIUM@"
export version=@PACKAGE_VERSION@ export version=@PACKAGE_VERSION@
export system=@system@ export system=@system@
export IMPURE_VAR1=foo
export IMPURE_VAR2=bar
cacheDir=$TEST_ROOT/binary-cache cacheDir=$TEST_ROOT/binary-cache
readLink() { readLink() {
@ -73,6 +80,10 @@ clearCacheCache() {
} }
startDaemon() { startDaemon() {
# Dont start the daemon twice, as this would just make it loop indefinitely
if [[ "$NIX_REMOTE" == daemon ]]; then
return
fi
# Start the daemon, wait for the socket to appear. !!! # Start the daemon, wait for the socket to appear. !!!
# nix-daemon should have an option to fork into the background. # nix-daemon should have an option to fork into the background.
rm -f $NIX_STATE_DIR/daemon-socket/socket rm -f $NIX_STATE_DIR/daemon-socket/socket
@ -82,20 +93,44 @@ startDaemon() {
sleep 1 sleep 1
done done
pidDaemon=$! pidDaemon=$!
trap "kill -9 $pidDaemon" EXIT trap "killDaemon" EXIT
export NIX_REMOTE=daemon export NIX_REMOTE=daemon
} }
killDaemon() { killDaemon() {
kill -9 $pidDaemon kill $pidDaemon
for i in {0.10}; do
kill -0 $pidDaemon || break
sleep 1
done
kill -9 $pidDaemon || true
wait $pidDaemon || true wait $pidDaemon || true
trap "" EXIT trap "" EXIT
} }
restartDaemon() {
[[ -z "${pidDaemon:-}" ]] && return 0
killDaemon
unset NIX_REMOTE
startDaemon
}
if [[ $(uname) == Linux ]] && [[ -L /proc/self/ns/user ]] && unshare --user true; then if [[ $(uname) == Linux ]] && [[ -L /proc/self/ns/user ]] && unshare --user true; then
_canUseSandbox=1 _canUseSandbox=1
fi fi
isDaemonNewer () {
[[ -n "${NIX_DAEMON_PACKAGE:-}" ]] || return 0
local requiredVersion="$1"
local daemonVersion=$($NIX_DAEMON_PACKAGE/bin/nix-daemon --version | cut -d' ' -f3)
return [[ $(nix eval --expr "builtins.compareVersions ''$daemonVersion'' ''2.4''") -ge 0 ]]
}
requireDaemonNewerThan () {
isDaemonNewer "$1" || exit 99
}
canUseSandbox() { canUseSandbox() {
if [[ ! $_canUseSandbox ]]; then if [[ ! $_canUseSandbox ]]; then
echo "Sandboxing not supported, skipping this test..." echo "Sandboxing not supported, skipping this test..."
@ -121,4 +156,22 @@ expect() {
[[ $res -eq $expected ]] [[ $res -eq $expected ]]
} }
needLocalStore() {
if [[ "$NIX_REMOTE" == "daemon" ]]; then
echo "Cant run through the daemon ($1), skipping this test..."
return 99
fi
}
# Just to make it easy to find which tests should be fixed
buggyNeedLocalStore () {
needLocalStore
}
set -x set -x
if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
startDaemon
fi
fi # COMMON_SH_SOURCED

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "--dump-db requires a local store"
clearStore clearStore
path=$(nix-build dependencies.nix -o $TEST_ROOT/result) path=$(nix-build dependencies.nix -o $TEST_ROOT/result)

View file

@ -2,9 +2,6 @@ source common.sh
clearStore clearStore
export IMPURE_VAR1=foo
export IMPURE_VAR2=bar
path=$(nix-store -q $(nix-instantiate fixed.nix -A good.0)) path=$(nix-store -q $(nix-instantiate fixed.nix -A good.0))
echo 'testing bad...' echo 'testing bad...'

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "“min-free” and “max-free” are daemon options"
clearStore clearStore
garbage1=$(nix add-to-store --name garbage1 ./nar-access.sh) garbage1=$(nix add-to-store --name garbage1 ./nar-access.sh)

View file

@ -18,6 +18,7 @@ build-users-group =
keep-derivations = false keep-derivations = false
sandbox = false sandbox = false
include nix.conf.extra include nix.conf.extra
trusted-users = $(whoami)
EOF EOF
cat > "$NIX_CONF_DIR"/nix.conf.extra <<EOF cat > "$NIX_CONF_DIR"/nix.conf.extra <<EOF

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "the sandbox only runs on the builder side, so it makes no sense to test it with the daemon"
clearStore clearStore
if ! canUseSandbox; then exit; fi if ! canUseSandbox; then exit; fi

View file

@ -52,7 +52,7 @@ outPath2=$(nix-build $(nix-instantiate multiple-outputs.nix -A a.second) --no-ou
# Delete one of the outputs and rebuild it. This will cause a hash # Delete one of the outputs and rebuild it. This will cause a hash
# rewrite. # rewrite.
nix-store --delete $TEST_ROOT/result-second --ignore-liveness env -u NIX_REMOTE nix-store --delete $TEST_ROOT/result-second --ignore-liveness
nix-build multiple-outputs.nix -A a.all -o $TEST_ROOT/result nix-build multiple-outputs.nix -A a.all -o $TEST_ROOT/result
[ "$(cat $TEST_ROOT/result-second/file)" = "second" ] [ "$(cat $TEST_ROOT/result-second/file)" = "second" ]
[ "$(cat $TEST_ROOT/result-second/link/file)" = "first" ] [ "$(cat $TEST_ROOT/result-second/link/file)" = "first" ]

View file

@ -26,7 +26,8 @@ if [ "$inode1" = "$inode3" ]; then
exit 1 exit 1
fi fi
nix-store --optimise # XXX: This should work through the daemon too
NIX_REMOTE="" nix-store --optimise
inode1="$(stat --format=%i $outPath1/foo)" inode1="$(stat --format=%i $outPath1/foo)"
inode3="$(stat --format=%i $outPath3/foo)" inode3="$(stat --format=%i $outPath3/foo)"

View file

@ -2,7 +2,12 @@ source common.sh
clearStore clearStore
rm -f $TEST_ROOT/result
export REMOTE_STORE=$TEST_ROOT/remote_store export REMOTE_STORE=$TEST_ROOT/remote_store
echo 'require-sigs = false' >> $NIX_CONF_DIR/nix.conf
restartDaemon
# Build the dependencies and push them to the remote store # Build the dependencies and push them to the remote store
nix-build -o $TEST_ROOT/result dependencies.nix --post-build-hook $PWD/push-to-store.sh nix-build -o $TEST_ROOT/result dependencies.nix --post-build-hook $PWD/push-to-store.sh

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "uses some low-level store manipulations that arent available through the daemon"
clearStore clearStore
max=500 max=500

View file

@ -1,5 +1,7 @@
source common.sh source common.sh
needLocalStore "--repair needs a local store"
clearStore clearStore
path=$(nix-build dependencies.nix -o $TEST_ROOT/result) path=$(nix-build dependencies.nix -o $TEST_ROOT/result)

View file

@ -1,5 +1,9 @@
source common.sh source common.sh
# 27ce722638 required some incompatible changes to the nix file, so skip this
# tests for the older versions
requireDaemonNewerThan "2.4pre20210622"
clearStore clearStore
outPath=$(nix-build structured-attrs.nix --no-out-link) outPath=$(nix-build structured-attrs.nix --no-out-link)

View file

@ -2,6 +2,8 @@
source common.sh source common.sh
# XXX: This shouldnt be, but #4813 cause this test to fail
needLocalStore "see #4813"
set +e set +e
messages=$(nix-build -Q timeout.nix -A infiniteLoop --timeout 2 2>&1) messages=$(nix-build -Q timeout.nix -A infiniteLoop --timeout 2 2>&1)