projects/guix·blob master · refs

179 lines · 6.3 KiB

1#!/bin/sh
2
3# GNU Guix --- Functional package management for GNU
4# Copyright © 2012-2019, 2021, 2025 Ludovic Courtès <[email protected]>
5#
6# This file is part of GNU Guix.
7#
8# GNU Guix is free software; you can redistribute it and/or modify it
9# under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 3 of the License, or (at
11# your option) any later version.
12#
13# GNU Guix is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21# Usage: ./test-env COMMAND ARG...
22#
23# Run the daemon in the build directory, and run COMMAND within
24# `pre-inst-env'. This is used to run unit tests with the just-built
25# daemon, unless `--disable-daemon' was passed at configure time.
26
27
28# Make sure 'cd' behaves deterministically and doesn't write anything to
29# stdout.
30unset CDPATH
31
32case "$1" in
33 --quiet-stderr)
34 # Silence the daemon's output, which is often useless, as well as that
35 # of Bash (such as "Terminated" messages when 'guix-daemon' is
36 # killed.)
37 exec 2> /dev/null
38 shift
39 ;;
40esac
41
42if [ -x "@abs_top_builddir@/guix-daemon" ]
43then
44 NIX_STORE_DIR="@GUIX_TEST_ROOT@/store"
45
46 # Do that because store.scm calls `canonicalize-path' on it.
47 mkdir -p "$NIX_STORE_DIR"
48
49 # Canonicalize the store directory name in an attempt to avoid symlinks in
50 # it or its parent directories. See <http://bugs.gnu.org/17935>.
51 NIX_STORE_DIR="`cd "@GUIX_TEST_ROOT@/store"; pwd -P`"
52
53 GUIX_LOG_DIRECTORY="@GUIX_TEST_ROOT@/var/log/guix"
54 GUIX_DATABASE_DIRECTORY="@GUIX_TEST_ROOT@/db"
55
56 # Choose a PID-dependent name to allow for parallel builds. Note
57 # that the directory name must be chosen so that the socket's file
58 # name is less than 108-char long (the size of `sun_path' in glibc).
59 # Currently, in Nix builds, we're at ~106 chars...
60 GUIX_STATE_DIRECTORY="@GUIX_TEST_ROOT@/var/$$"
61
62 # We can't exit when we reach the limit, because perhaps the test doesn't
63 # actually rely on the daemon, but at least warn.
64 if test "`echo -n "$GUIX_STATE_DIRECTORY/daemon-socket/socket" | wc -c`" -ge 108
65 then
66 echo "warning: exceeding socket file name limit; test may fail!" >&2
67 fi
68
69 # The configuration directory, for import/export signing keys.
70 GUIX_CONFIGURATION_DIRECTORY="@GUIX_TEST_ROOT@/etc"
71 if [ ! -d "$GUIX_CONFIGURATION_DIRECTORY" ]
72 then
73 # Copy the keys so that the secret key has the right permissions (the
74 # daemon errors out when this is not the case.)
75 mkdir -p "$GUIX_CONFIGURATION_DIRECTORY"
76 cp "@abs_top_srcdir@/tests/keys/openpgp/signing-key.sec" \
77 "@abs_top_srcdir@/tests/keys/openpgp/signing-key.pub" \
78 "$GUIX_CONFIGURATION_DIRECTORY"
79 chmod 400 "$GUIX_CONFIGURATION_DIRECTORY/signing-key.sec"
80 fi
81
82 # A place to store data of the substituter.
83 GUIX_BINARY_SUBSTITUTE_URL="file://$GUIX_STATE_DIRECTORY/substituter-data"
84 rm -rf "$GUIX_STATE_DIRECTORY/substituter-data"
85 mkdir -p "$GUIX_STATE_DIRECTORY/substituter-data"
86
87 # For a number of tests, we want to allow unsigned narinfos, for
88 # simplicity.
89 GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES=yes
90
91 # Place for the substituter's cache.
92 XDG_CACHE_HOME="$GUIX_STATE_DIRECTORY/cache-$$"
93
94 export NIX_IGNORE_SYMLINK_STORE NIX_STORE_DIR \
95 GUIX_LOG_DIRECTORY GUIX_STATE_DIRECTORY GUIX_DATABASE_DIRECTORY \
96 GUIX_BINARY_SUBSTITUTE_URL \
97 GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES \
98 GUIX_CONFIGURATION_DIRECTORY XDG_CACHE_HOME
99
100 # Create a fresh directory with restrictive permissions so that our test
101 # daemon's weak isolation can't be exploited by other users
102 rm -rf "$GUIX_STATE_DIRECTORY/daemon-socket"
103 mkdir -m 0700 "$GUIX_STATE_DIRECTORY/daemon-socket"
104
105 # If unprivileged user namespaces are not supported, pass
106 # '--disable-chroot'.
107 if [ -f /proc/self/ns/user ] \
108 && { [ ! -f /proc/sys/kernel/unprivileged_userns_clone ] \
109 || [ "$(cat /proc/sys/kernel/unprivileged_userns_clone)" -eq 1 ]; }
110 then
111 extra_options=""
112 else
113 extra_options="--disable-chroot"
114 echo "unprivileged user namespaces not supported; \
115running 'guix-daemon $extra_options'" >&2
116 fi
117
118 # Launch the daemon without chroot support because is may be
119 # unavailable, for instance if we're not running as root.
120 "@abs_top_builddir@/pre-inst-env" \
121 "@abs_top_builddir@/guix-daemon" \
122 $extra_options \
123 --allow-aslr \
124 --substitute-urls="$GUIX_BINARY_SUBSTITUTE_URL" &
125
126 daemon_pid=$!
127 trap "kill $daemon_pid ; rm -rf $GUIX_STATE_DIRECTORY" EXIT
128
129 # The test suite expects the 'guile-bootstrap' package to be available.
130 # Normally the Guile bootstrap tarball is downloaded by a fixed-output
131 # derivation but when network access is missing we allow users to drop
132 # the tarball in 'gnu/packages/bootstrap/SYSTEM' and "intern" it here.
133 bootstrap_directory="@abs_top_builddir@/gnu/packages/bootstrap/@guix_system@"
134 if [ -d "$bootstrap_directory" ]
135 then
136 # Make sure 'guix-daemon' is listening before invoking 'guix
137 # download'.
138 "@abs_top_builddir@/pre-inst-env" "@GUILE@" -c \
139 '(use-modules (guix))
140(let loop ((i 10))
141 (catch #t
142 (lambda () (open-connection))
143 (lambda (key . args)
144 (if (zero? i)
145 (apply throw key args)
146 (begin (usleep 500000) (loop (- i 1)))))))'
147
148 for file in "$bootstrap_directory"/guile-*
149 do
150 [ -f "$file" ] && \
151 "@abs_top_builddir@/pre-inst-env" \
152 guix download "file://$file" > /dev/null
153 done
154 fi
155fi
156
157# Avoid issues that could stem from l10n, such as language/encoding
158# mismatches.
159unset LANGUAGE
160LC_MESSAGES=C
161export LC_MESSAGES
162
163# Disable grafts by default because they can cause things to be built
164# regardless of '--dry-run'.
165GUIX_BUILD_OPTIONS="--no-grafts"
166export GUIX_BUILD_OPTIONS
167
168# Ignore user settings.
169unset GUIX_PACKAGE_PATH
170
171storedir="@storedir@"
172prefix="@prefix@"
173datarootdir="@datarootdir@"
174datadir="@datadir@"
175localstatedir="@localstatedir@"
176export storedir prefix datarootdir datadir localstatedir
177
178"@abs_top_builddir@/pre-inst-env" "$@"
179exit $?