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 Aug 12, 2024
1 parent cfc3867 commit 4c485d0
Show file tree
Hide file tree
Showing 30 changed files with 672 additions and 115 deletions.
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
"${nixpkgsPrefix}${pkgName}-${testName}" = test;
})
)
// lib.optionalAttrs (nixpkgs.stdenv.hostPlatform == nixpkgs.stdenv.buildPlatform) {
"${nixpkgsPrefix}nix-functional-tests" = nixpkgs.nixComponents.nix-functional-tests;
}
)
// devFlake.checks.${system} or {}
);
Expand Down Expand Up @@ -324,6 +327,7 @@
++ lib.optionals havePerl pkgs.nixComponents.nix-perl-bindings.nativeBuildInputs
++ pkgs.nixComponents.nix-internal-api-docs.nativeBuildInputs
++ pkgs.nixComponents.nix-external-api-docs.nativeBuildInputs
++ pkgs.nixComponents.nix-functional-tests.baseNativeBuildInputs
++ lib.optional
(!stdenv.buildPlatform.canExecute stdenv.hostPlatform
# Hack around https://github.com/nixos/nixpkgs/commit/bf7ad8cfbfa102a90463433e2c5027573b462479
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ subproject('nix-fetchers-tests')
subproject('nix-expr-test-support')
subproject('nix-expr-tests')
subproject('nix-flake-tests')
subproject('nix-functional-tests')
2 changes: 2 additions & 0 deletions packaging/components.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
7 changes: 5 additions & 2 deletions packaging/hydra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let
"nix-main-c"
"nix-cmd"
"nix-ng"
"nix-functional-tests"
];
in
{
Expand All @@ -68,8 +69,10 @@ in
lib.genAttrs linux64BitSystems (system: nixpkgsFor.${system}.static.nixComponents.${pkgName}));

buildCross = forAllPackages (pkgName:
forAllCrossSystems (crossSystem:
lib.genAttrs [ "x86_64-linux" ] (system: nixpkgsFor.${system}.cross.${crossSystem}.nixComponents.${pkgName})));
# Hack to avoid non-evaling package
(if pkgName == "nix-functional-tests" then lib.flip builtins.removeAttrs ["x86_64-w64-mingw32"] else lib.id)
(forAllCrossSystems (crossSystem:
lib.genAttrs [ "x86_64-linux" ] (system: nixpkgsFor.${system}.cross.${crossSystem}.nixComponents.${pkgName}))));

buildNoGc = forAllSystems (system:
self.packages.${system}.nix.override { enableGC = false; }
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
1 change: 1 addition & 0 deletions tests/functional/.version
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,10 @@
# 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

isTestOnNixOS() {
[[ "${isTestOnNixOS:-}" == 1 ]]
Expand All @@ -15,64 +15,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 +55,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 +72,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 +101,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,14 +127,11 @@ 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 ]]
}

Expand Down Expand Up @@ -237,7 +184,7 @@ expect() {
shift
"$@" && res=0 || res="$?"
# also match "negative" codes, which wrap around to >127
if [[ $res -ne $expected && $res -ne $[256 + expected] ]]; then
if [[ $res -ne $expected && $res -ne $((256 + expected)) ]]; then
echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1
fi
Expand All @@ -252,7 +199,7 @@ expectStderr() {
shift
"$@" 2>&1 && res=0 || res="$?"
# also match "negative" codes, which wrap around to >127
if [[ $res -ne $expected && $res -ne $[256 + expected] ]]; then
if [[ $res -ne $expected && $res -ne $((256 + expected)) ]]; then
echo "Expected exit code '$expected' but got '$res' from command ${*@Q}" >&2
return 1
fi
Expand All @@ -267,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 @@ -283,11 +230,9 @@ 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

onError() {
set +x
echo "$0: test failed at:" >&2
Expand All @@ -311,15 +256,15 @@ callerPrefix() {
local i file line fn savedFn
# Use `caller`
for i in $(seq 0 100); do
caller $i > /dev/null || {
caller "$i" > /dev/null || {
if [[ -n "${file:-}" ]]; then
echo "$file:$line: ${savedFn+in call to $savedFn: }"
fi
break
}
line="$(caller $i | cut -d' ' -f1)"
fn="$(caller $i | cut -d' ' -f2)"
file="$(caller $i | cut -d' ' -f3)"
line="$(caller "$i" | cut -d' ' -f1)"
fn="$(caller "$i" | cut -d' ' -f2)"
file="$(caller "$i" | cut -d' ' -f3)"
if [[ $file != "${BASH_SOURCE[0]}" ]]; then
echo "$file:$line: ${savedFn+in call to $savedFn: }"
return
Expand All @@ -342,7 +287,7 @@ checkGrepArgs() {
for arg in "$@"; do
if [[ "$arg" != "${arg//$'\n'/_}" ]]; then
echo "$(callerPrefix)newline not allowed in arguments; grep would try each line individually as if connected by an OR operator" >&2
return -101
return 155 # = -101 mod 256
fi
done
}
Expand Down Expand Up @@ -400,4 +345,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,
)
19 changes: 16 additions & 3 deletions tests/functional/common/paths.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# 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"
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 +29,5 @@ DAEMON_PATH="$PATH"
if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
DAEMON_PATH="${NIX_DAEMON_PACKAGE}/bin:$DAEMON_PATH"
fi

fi # COMMON_PATHS_SH_SOURCED
Loading

0 comments on commit 4c485d0

Please sign in to comment.