Skip to content

Commit

Permalink
Build Functional tests with Meson
Browse files Browse the repository at this point in the history
Co-Authored-By: Qyriad <qyriad@qyriad.me>
  • Loading branch information
Ericson2314 and Qyriad committed Jul 9, 2024
1 parent 360c381 commit 4fbc4b2
Show file tree
Hide file tree
Showing 33 changed files with 654 additions and 126 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ifeq ($(ENABLE_FUNCTIONAL_TESTS), yes)
ifdef HOST_UNIX
makefiles += \
tests/functional/local.mk \
tests/functional/flakes/local.mk \
tests/functional/ca/local.mk \
tests/functional/git-hashing/local.mk \
tests/functional/dyn-drv/local.mk \
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ subproject('nix-fetchers-tests')
subproject('nix-expr-test-support')
subproject('nix-expr-tests')
subproject('nix-flake-tests')
subproject('nix-functional-tests')
2 changes: 1 addition & 1 deletion mk/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ run_test

if [[ "$status" = 0 ]]; then
echo "$post_run_msg [${green}PASS$normal]"
elif [[ "$status" = 99 ]]; then
elif [[ "$status" = 77 ]]; then
echo "$post_run_msg [${yellow}SKIP$normal]"
else
echo "$post_run_msg [${red}FAIL$normal]"
Expand Down
2 changes: 2 additions & 0 deletions packaging/components.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ in
# Will replace `nix` once the old build system is gone.
nix-ng = callPackage ../src/nix/package.nix { };

nix-functional-tests = callPackage ../src/nix-functional-tests/package.nix { };

nix-internal-api-docs = callPackage ../src/internal-api-docs/package.nix { };
nix-external-api-docs = callPackage ../src/external-api-docs/package.nix { };

Expand Down
1 change: 1 addition & 0 deletions packaging/hydra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ let
"nix-main"
"nix-cmd"
"nix-ng"
"nix-functional-tests"
];
in
{
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ configdata = configuration_data()
# TODO rename, because it will conflict with downstream projects
configdata.set_quoted('PACKAGE_VERSION', meson.project_version())

configdata.set_quoted('SYSTEM', host_machine.system())
configdata.set_quoted('SYSTEM', host_machine.cpu_family() + '-' + host_machine.system())

deps_private_maybe_subproject = [
]
Expand Down
1 change: 1 addition & 0 deletions src/nix-functional-tests
2 changes: 2 additions & 0 deletions src/nix/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ this_exe = executable(
install : true,
)

meson.override_find_program('nix', this_exe)

nix_symlinks = [
'nix-build',
'nix-channel',
Expand Down
1 change: 1 addition & 0 deletions tests/functional/.version
2 changes: 1 addition & 1 deletion tests/functional/bash-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source common.sh

sed -e "s|@localstatedir@|$TEST_ROOT/profile-var|g" -e "s|@coreutils@|$coreutils|g" < ../../scripts/nix-profile.sh.in > "$TEST_ROOT"/nix-profile.sh
sed -e "s|@localstatedir@|$TEST_ROOT/profile-var|g" -e "s|@coreutils@|$coreutils|g" < nix-profile.sh.in > "$TEST_ROOT"/nix-profile.sh

user=$(whoami)
rm -rf "$TEST_HOME" "$TEST_ROOT/profile-var"
Expand Down
33 changes: 33 additions & 0 deletions tests/functional/ca/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
configure_file(
input : 'config.nix.in',
output : 'config.nix',
configuration : test_confdata,
)

suites += {
'name': 'ca',
'deps': [],
'tests': [
'build-with-garbage-path.sh',
'build.sh',
'build-cache.sh',
'concurrent-builds.sh',
'derivation-json.sh',
'duplicate-realisation-in-closure.sh',
'eval-store.sh',
'gc.sh',
'import-derivation.sh',
'new-build-cmd.sh',
'nix-copy.sh',
'nix-run.sh',
'nix-shell.sh',
'post-hook.sh',
'recursive.sh',
'repl.sh',
'selfref-gc.sh',
'signatures.sh',
'substitute.sh',
'why-depends.sh',
],
'workdir': meson.current_build_dir(),
}
3 changes: 2 additions & 1 deletion tests/functional/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ COMMON_SH_SOURCED=1

functionalTestsDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

source "$functionalTestsDir/common/vars-and-functions.sh"
source "$functionalTestsDir/common/vars.sh"
source "$functionalTestsDir/common/functions.sh"
source "$functionalTestsDir/common/init.sh"

if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# NOTE: instances of @variable@ are substituted as defined in /mk/templates.mk
# shellcheck shell=bash

set -eu -o pipefail

if [[ -z "${COMMON_VARS_AND_FUNCTIONS_SH_SOURCED-}" ]]; then
if [[ -z "${COMMON_FUNCTIONS_SH_SOURCED-}" ]]; then

COMMON_VARS_AND_FUNCTIONS_SH_SOURCED=1
COMMON_FUNCTIONS_SH_SOURCED=1

set +x

isTestOnNixOS() {
[[ "${isTestOnNixOS:-}" == 1 ]]
Expand All @@ -15,64 +17,14 @@ die() {
exit 1
}

set +x

commonDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

source "$commonDir/subst-vars.sh"
# Make sure shellcheck knows all these will be defined by the above generated snippet
: "${bindir?} ${coreutils?} ${dot?} ${SHELL?} ${PAGER?} ${busybox?} ${version?} ${system?} ${BUILD_SHARED_LIBS?}"

source "$commonDir/paths.sh"
source "$commonDir/test-root.sh"

test_nix_conf_dir=$TEST_ROOT/etc
test_nix_conf=$test_nix_conf_dir/nix.conf

export TEST_HOME=$TEST_ROOT/test-home

if ! isTestOnNixOS; then
export NIX_STORE_DIR
if ! NIX_STORE_DIR=$(readlink -f $TEST_ROOT/store 2> /dev/null); then
# Maybe the build directory is symlinked.
export NIX_IGNORE_SYMLINK_STORE=1
NIX_STORE_DIR=$TEST_ROOT/store
fi
export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
export NIX_STATE_DIR=$TEST_ROOT/var/nix
export NIX_CONF_DIR=$test_nix_conf_dir
export NIX_DAEMON_SOCKET_PATH=$TEST_ROOT/dSocket
unset NIX_USER_CONF_FILES
export _NIX_TEST_SHARED=$TEST_ROOT/shared
if [[ -n $NIX_STORE ]]; then
export _NIX_TEST_NO_SANDBOX=1
fi
export _NIX_IN_TEST=$TEST_ROOT/shared
export _NIX_TEST_NO_LSOF=1
export NIX_REMOTE=${NIX_REMOTE_-}

fi # ! isTestOnNixOS

unset NIX_PATH
export HOME=$TEST_HOME
unset XDG_STATE_HOME
unset XDG_DATA_HOME
unset XDG_CONFIG_HOME
unset XDG_CONFIG_DIRS
unset XDG_CACHE_HOME

export IMPURE_VAR1=foo
export IMPURE_VAR2=bar

cacheDir=$TEST_ROOT/binary-cache

readLink() {
# TODO fix this
# shellcheck disable=SC2012
ls -l "$1" | sed 's/.*->\ //'
}

clearProfiles() {
profiles="$HOME"/.local/state/nix/profiles
profiles="$HOME/.local/state/nix/profiles"
rm -rf "$profiles"
}

Expand Down Expand Up @@ -105,11 +57,11 @@ doClearStore() {
}

clearCache() {
rm -rf "$cacheDir"
rm -rf "${cacheDir?}"
}

clearCacheCache() {
rm -f $TEST_HOME/.cache/nix/binary-cache*
rm -f "$TEST_HOME/.cache/nix/binary-cache"*
}

startDaemon() {
Expand All @@ -122,7 +74,7 @@ startDaemon() {
return
fi
# Start the daemon, wait for the socket to appear.
rm -f $NIX_DAEMON_SOCKET_PATH
rm -f "$NIX_DAEMON_SOCKET_PATH"
PATH=$DAEMON_PATH nix --extra-experimental-features 'nix-command' daemon &
_NIX_TEST_DAEMON_PID=$!
export _NIX_TEST_DAEMON_PID
Expand Down Expand Up @@ -151,14 +103,14 @@ killDaemon() {
if [[ "${_NIX_TEST_DAEMON_PID-}" == '' ]]; then
return
fi
kill $_NIX_TEST_DAEMON_PID
kill "$_NIX_TEST_DAEMON_PID"
for i in {0..100}; do
kill -0 $_NIX_TEST_DAEMON_PID 2> /dev/null || break
kill -0 "$_NIX_TEST_DAEMON_PID" 2> /dev/null || break
sleep 0.1
done
kill -9 $_NIX_TEST_DAEMON_PID 2> /dev/null || true
wait $_NIX_TEST_DAEMON_PID || true
rm -f $NIX_DAEMON_SOCKET_PATH
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
Expand All @@ -177,20 +129,17 @@ restartDaemon() {
startDaemon
}

if [[ $(uname) == Linux ]] && [[ -L /proc/self/ns/user ]] && unshare --user true; then
_canUseSandbox=1
fi

isDaemonNewer () {
[[ -n "${NIX_DAEMON_PACKAGE:-}" ]] || return 0
local requiredVersion="$1"
local daemonVersion=$($NIX_DAEMON_PACKAGE/bin/nix daemon --version | cut -d' ' -f3)
local daemonVersion
daemonVersion=$("$NIX_DAEMON_PACKAGE/bin/nix" daemon --version | cut -d' ' -f3)
[[ $(nix eval --expr "builtins.compareVersions ''$daemonVersion'' ''$requiredVersion''") -ge 0 ]]
}

skipTest () {
echo "$1, skipping this test..." >&2
exit 99
exit 77
}

TODO_NixOS() {
Expand Down Expand Up @@ -265,7 +214,7 @@ expectStderr() {
# error: This error is expected
# EOF
assertStderr() {
diff -u /dev/stdin <($@ 2>/dev/null 2>&1)
diff -u /dev/stdin <("$@" 2>/dev/null 2>&1)
}

needLocalStore() {
Expand All @@ -281,7 +230,7 @@ buggyNeedLocalStore() {

enableFeatures() {
local features="$1"
sed -i 's/experimental-features .*/& '"$features"'/' "$test_nix_conf_dir"/nix.conf
sed -i 's/experimental-features .*/& '"$features"'/' "${test_nix_conf?}"
}

set -x
Expand Down Expand Up @@ -331,4 +280,4 @@ count() {

trap onError ERR

fi # COMMON_VARS_AND_FUNCTIONS_SH_SOURCED
fi # COMMON_FUNCTIONS_SH_SOURCED
4 changes: 2 additions & 2 deletions tests/functional/common/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ if isTestOnNixOS; then

mkdir -p "$test_nix_conf_dir" "$TEST_HOME"

export NIX_USER_CONF_FILES="$test_nix_conf_dir/nix.conf"
export NIX_USER_CONF_FILES="$test_nix_conf"
mkdir -p "$test_nix_conf_dir" "$TEST_HOME"
! test -e "$test_nix_conf"
cat > "$test_nix_conf_dir/nix.conf" <<EOF
cat > "$test_nix_conf" <<EOF
# TODO: this is not needed for all tests and prevents stable commands from be tested in isolation
experimental-features = nix-command flakes
flake-registry = $TEST_ROOT/registry.json
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/common/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
configure_file(
input : 'subst-vars.sh.in',
output : 'subst-vars.sh',
configuration : test_confdata,
)
23 changes: 20 additions & 3 deletions tests/functional/common/paths.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
# shellcheck shell=bash

set -eu -o pipefail

if [[ -z "${COMMON_PATHS_SH_SOURCED-}" ]]; then

COMMON_PATHS_SH_SOURCED=1

commonDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

# Since this is a generated file
# Since these are generated files
# shellcheck disable=SC1091
source "$commonDir/functions.sh"
# shellcheck disable=SC1091
source "$commonDir/subst-vars.sh"
# Make sure shellcheck knows this will be defined by the above generated snippet
: "${bindir?}"
: "${bash?}" "${bindir?}"

export PATH="$bindir:$PATH"
set +x

if ! isTestOnNixOS; then
export SHELL="$bash"
export PATH="$bindir:$PATH"
fi

if [[ -n "${NIX_CLIENT_PACKAGE:-}" ]]; then
export PATH="$NIX_CLIENT_PACKAGE/bin":$PATH
Expand All @@ -18,3 +31,7 @@ DAEMON_PATH="$PATH"
if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
DAEMON_PATH="${NIX_DAEMON_PACKAGE}/bin:$DAEMON_PATH"
fi

set -x

fi # COMMON_PATHS_SH_SOURCED
20 changes: 6 additions & 14 deletions tests/functional/common/subst-vars.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ if [[ -z "${COMMON_SUBST_VARS_SH_SOURCED-}" ]]; then

COMMON_SUBST_VARS_SH_SOURCED=1

bash=@bash@
bindir=@bindir@
export coreutils=@coreutils@
#lsof=@lsof@
coreutils=@coreutils@

export dot=@dot@
export PAGER=cat
export busybox="@sandbox_shell@"
dot=@dot@
busybox="@sandbox_shell@"

export version=@PACKAGE_VERSION@
export system=@system@

export BUILD_SHARED_LIBS=@BUILD_SHARED_LIBS@

if ! isTestOnNixOS; then
export SHELL="@bash@"
export PATH=@bindir@:$PATH
fi
version=@PACKAGE_VERSION@
system=@system@

fi
Loading

0 comments on commit 4fbc4b2

Please sign in to comment.