projects/guix·blob python-team · refs

314 lines · 11.6 KiB

1# GNU Guix --- Functional package management for GNU
2# Copyright © 2015 David Thompson <[email protected]>
3# Copyright © 2022, 2023 John Kehayias <[email protected]>
4# Copyright © 2023, 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#
22# Test 'guix environment'.
23#
24
25set -e
26
27guix environment --version
28
29if ! guile -c '((@ (guix scripts environment) assert-container-features))'
30then
31 # User containers are not supported; skip this test.
32 exit 77
33fi
34
35tmpdir="t-guix-environment-$$"
36trap 'rm -r "$tmpdir"' EXIT
37
38mkdir "$tmpdir"
39
40# Make sure the exit value is preserved.
41if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
42 -- guile -c '(exit 42)'
43then
44 false
45else
46 test $? = 42
47fi
48
49# Try '--root' and '--profile'.
50root="$tmpdir/root"
51guix environment -C --ad-hoc --bootstrap guile-bootstrap -r "$root" -- guile --version
52guix environment -C -p "$root" --bootstrap -- guile --version
53path1=$(guix environment -C -p "$root" --bootstrap -- guile -c '(display (getenv "PATH"))')
54path2=$(guix environment -C --ad-hoc --bootstrap guile-bootstrap -- guile -c '(display (getenv "PATH"))')
55test "$path1" = "$path2"
56
57# Make sure "localhost" resolves.
58guix environment --container --ad-hoc --bootstrap guile-bootstrap \
59 -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
60
61# We should get ECONNREFUSED, not ENETUNREACH, which would indicate that "lo"
62# is down.
63guix environment --container --ad-hoc --bootstrap guile-bootstrap \
64 -- guile -c "(exit (= ECONNREFUSED
65 (catch 'system-error
66 (lambda ()
67 (let ((sock (socket AF_INET SOCK_STREAM 0)))
68 (connect sock AF_INET INADDR_LOOPBACK 12345)))
69 (lambda args
70 (pk 'errno (system-error-errno args))))))"
71
72# Make sure '--preserve' is honored.
73result="`FOOBAR=42; export FOOBAR; guix environment -C --ad-hoc --bootstrap \
74 guile-bootstrap -E ^FOO -- guile -c '(display (getenv \"FOOBAR\"))'`"
75test "$result" = "42"
76
77# By default, the UID inside the container should be the same as outside.
78uid="`id -u`"
79inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
80 -- guile -c '(display (getuid))'`"
81test $inner_uid = $uid
82
83# When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
84# so that we don't run into problems when the test directory is under /home.)
85export tmpdir
86inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
87 --user=gnu-guix -- guile -c '(display (getuid))'`"
88test $inner_uid = 1000
89
90if test "x$USER" = "x"; then USER="`id -un`"; fi
91
92# Check whether /etc/passwd and /etc/group are valid.
93guix environment -C --ad-hoc --bootstrap guile-bootstrap \
94 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
95guix environment -C --ad-hoc --bootstrap guile-bootstrap \
96 -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
97guix environment -C --ad-hoc --bootstrap guile-bootstrap \
98 -- guile -c '(use-modules (srfi srfi-1))
99 (exit (every group:name
100 (map getgrgid (vector->list (getgroups)))))'
101
102# Make sure file-not-found errors in mounts are reported.
103if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
104 --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
105then
106 false
107else
108 grep "/does-not-exist" "$tmpdir/error"
109 grep "[Nn]o such file" "$tmpdir/error"
110fi
111
112# Make sure that the right directories are mapped.
113mount_test_code="
114(use-modules (ice-9 rdelim)
115 (ice-9 match)
116 (srfi srfi-1))
117
118(define mappings
119 (filter-map (lambda (line)
120 (match (string-split line #\space)
121 ;; Empty line.
122 ((\"\") #f)
123 ;; Ignore the root file system.
124 ((_ \"/\" _ _ _ _)
125 #f)
126 ;; Ignore these types of file systems, except if they
127 ;; correspond to a parent file system.
128 ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
129 \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
130 (and (string-prefix? (getcwd) mount)
131 mount))
132 ((_ mount _ _ _ _)
133 mount)))
134 (string-split (call-with-input-file \"/proc/mounts\" read-string)
135 #\newline)))
136
137(for-each (lambda (mount)
138 (display mount)
139 (newline))
140 mappings)"
141
142guix environment --container --ad-hoc --bootstrap guile-bootstrap \
143 -- guile -c "$mount_test_code" > $tmpdir/mounts
144
145cat "$tmpdir/mounts"
146test `wc -l < $tmpdir/mounts` -eq 4
147
148current_dir="`cd $PWD; pwd -P`"
149grep -e "$current_dir$" $tmpdir/mounts # current directory
150grep $(guix build guile-bootstrap) $tmpdir/mounts
151grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
152
153rm $tmpdir/mounts
154
155# Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
156# within a container.
157(
158 linktest='
159(exit (and (string=? (getenv "GUIX_ENVIRONMENT")
160 (string-append (getenv "HOME") "/.guix-profile"))
161 (string-prefix? "'"$NIX_STORE_DIR"'"
162 (readlink (string-append (getenv "HOME")
163 "/.guix-profile")))))'
164
165 cd "$tmpdir" \
166 && guix environment --bootstrap --container --link-profile \
167 --ad-hoc guile-bootstrap --pure \
168 -- guile -c "$linktest"
169)
170
171# Test that user can be mocked.
172usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
173 (string=? (passwd:name (getpwuid 1000)) "foognu")
174 (file-exists? "/home/foognu/umock")))'
175touch "$tmpdir/umock"
176HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
177 --ad-hoc guile-bootstrap --pure \
178 --share="$tmpdir/umock" \
179 -- guile -c "$usertest"
180
181# if not sharing CWD, chdir home
182(
183 cd "$tmpdir" \
184 && guix environment --bootstrap --container --no-cwd --user=foo \
185 --ad-hoc guile-bootstrap --pure \
186 -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
187)
188
189# '--cwd' is independent from sharing the host current working directory.
190(
191 cd "$tmpdir" \
192 && guix environment --bootstrap --container --no-cwd --cwd=/tmp \
193 --ad-hoc guile-bootstrap --pure \
194 -- /bin/sh -c 'test $(pwd) == "/tmp" -a ! -d '"$tmpdir"
195)
196
197# Relative '--cwd' is resolved against the host CWD and still honors '--user'.
198mkdir -p "$tmpdir/home/wd"
199home_dir="$(cd "$tmpdir/home"; pwd -P)"
200(
201 cd "$tmpdir/home/wd" \
202 && HOME="$home_dir" guix environment --bootstrap --container --user=foo \
203 --cwd=. --ad-hoc guile-bootstrap --pure \
204 -- /bin/sh -c 'test "$(pwd)" = "/home/foo/wd"'
205)
206
207# Check that the root file system is read-only by default...
208guix environment --bootstrap --container --ad-hoc guile-bootstrap \
209 -- guile -c '(mkdir "/whatever")' && false
210
211# ... and can be made writable.
212guix environment --bootstrap --container --ad-hoc guile-bootstrap \
213 --writable-root \
214 -- guile -c '(mkdir "/whatever")'
215
216# /tmp itself is always writable.
217guix environment --bootstrap --container --ad-hoc guile-bootstrap \
218 -- guile -c '(mkdir "/tmp/foo")'
219
220# And so is ~.
221guix environment --bootstrap --container --ad-hoc guile-bootstrap \
222 -- guile -c '(mkdir (string-append (getenv "HOME") "/foo"))'
223
224# And /run too!
225guix environment --bootstrap --container --ad-hoc guile-bootstrap \
226 -- guile -c '(mkdir "/run/user/1000/shepherd")'
227
228# Check the exit code.
229
230abnormal_exit_code="
231(use-modules (system foreign))
232;; Purposely make Guile crash with a segfault. :)
233(pointer->string (make-pointer 123) 123)"
234
235if guix environment --bootstrap --container \
236 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
237then false;
238else
239 test $? -gt 127
240fi
241
242# Test the Filesystem Hierarchy Standard (FHS) container option, --emulate-fhs (-F)
243
244# As this option requires a glibc package (glibc-for-fhs), try to run these
245# tests with the user's global store to make it easier to build or download a
246# substitute.
247storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
248localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
249NIX_STORE_DIR="$storedir"
250GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
251GUIX_BUILD_OPTIONS="--timeout=`guile -c '(use-modules (guix tests))(display %tests-build-timeout)'`"
252export NIX_STORE_DIR GUIX_DAEMON_SOCKET GUIX_BUILD_OPTIONS
253
254if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
255then
256 exit 77
257fi
258
259# Test that the container has FHS specific files/directories. Note that /bin
260# exists in a non-FHS container as it will contain sh, a symlink to the bash
261# package, so we don't test for it.
262guix shell -C --emulate-fhs --bootstrap guile-bootstrap \
263 -- guile -c '(exit (and (file-exists? "/etc/ld.so.cache")
264 (file-exists? "/lib")
265 (file-exists? "/sbin")
266 (file-exists? "/usr/bin")
267 (file-exists? "/usr/include")
268 (file-exists? "/usr/lib")
269 (file-exists? "/usr/libexec")
270 (file-exists? "/usr/sbin")
271 (file-exists? "/usr/share")))'
272
273# Test that the ld cache was generated and can be successfully read.
274guix shell -CF --bootstrap guile-bootstrap \
275 -- guile -c '(execlp "ldconfig" "ldconfig" "-p")'
276
277# Test that the package glibc-for-fhs is in the container even if there is the
278# regular glibc package from another source. See
279# <https://issues.guix.gnu.org/58861>.
280guix shell -CF --bootstrap guile-bootstrap glibc \
281 -- guile -c '(exit (if (string-contains (readlink "/lib/libc.so")
282 "glibc-for-fhs")
283 0
284 1))'
285
286# Test that $PATH inside the container includes the FHS directories.
287guix shell -CF coreutils -- env | grep ^PATH=/bin:/usr/bin:/sbin:/usr/sbin.*
288
289# Make sure '--preserve' is honored for $PATH, which the '--emulate-fhs'
290# option modifies. We can't (easily) check the whole $PATH as it will differ
291# inside and outside the container, so just check our test $PATH is still
292# present. See <https://issues.guix.gnu.org/60566>.
293PATH=/foo $(type -P guix) shell -CF -E ^PATH$ coreutils \
294 -- env | grep ^PATH=.*:/foo
295
296# '--symlink' works.
297echo "TESTING SYMLINK IN CONTAINER"
298guix shell --bootstrap guile-bootstrap --container \
299 --symlink=/usr/bin/guile=bin/guile -- \
300 /usr/bin/guile --version
301
302# A dangling symlink causes the command to fail.
303guix shell --bootstrap -CS /usr/bin/python=bin/python guile-bootstrap -- exit && false
304
305# An invalid symlink spec causes the command to fail.
306guix shell --bootstrap -CS bin/guile=/usr/bin/guile guile-bootstrap -- exit && false
307
308# Check whether '--nesting' works.
309guix build hello -d
310env="$(type -P pre-inst-env)"
311guix shell -C -D guix -- "$env" guix build hello -d && false # cannot work
312hello_drv="$(guix build hello -d)"
313hello_drv_nested="$(cd "$(dirname env)" && guix shell --bootstrap -E GUIX_BUILD_OPTIONS -CW -D guix -- "$env" guix build hello -d)"
314test "$hello_drv" = "$hello_drv_nested"