environment: Fix '--emulate-fhs' option overriding $PATH.
[jackhill/guix/guix.git] / tests / guix-environment-container.sh
CommitLineData
f535dcbe
DT
1# GNU Guix --- Functional package management for GNU
2# Copyright © 2015 David Thompson <davet@gnu.org>
3bfbfa29
JK
3# Copyright © 2022, 2023 John Kehayias <john.kehayias@protonmail.com>
4# Copyright © 2023 Ludovic Courtès <ludo@gnu.org>
f535dcbe
DT
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
90f496be 29if ! guile -c '((@ (guix scripts environment) assert-container-features))'
6493fd04
LC
30then
31 # User containers are not supported; skip this test.
32 exit 77
33fi
34
f535dcbe
DT
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
10208952
LC
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
0f53c801
LC
57# Make sure "localhost" resolves.
58guix environment --container --ad-hoc --bootstrap guile-bootstrap \
59 -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
60
b68d4106
LC
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
5a02f8e3
LC
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
1ccc0f80
LC
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
8a9922bd
LC
90if test "x$USER" = "x"; then USER="`id -un`"; fi
91
952afb6f 92# Check whether /etc/passwd and /etc/group are valid.
8a9922bd
LC
93guix environment -C --ad-hoc --bootstrap guile-bootstrap \
94 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
952afb6f
LC
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)))))'
8a9922bd 101
c06f6db7
LC
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
f535dcbe
DT
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)
1250034d
LC
123 ;; Ignore the root file system.
124 ((_ \"/\" _ _ _ _)
f535dcbe 125 #f)
1250034d
LC
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\") _ _ _)
7cdec6a9 130 (and (string-prefix? (getcwd) mount)
1250034d 131 mount))
f535dcbe
DT
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
855038b2 145cat "$tmpdir/mounts"
779aa003 146test `wc -l < $tmpdir/mounts` -eq 4
f535dcbe 147
c8855b99
LC
148current_dir="`cd $PWD; pwd -P`"
149grep -e "$current_dir$" $tmpdir/mounts # current directory
f535dcbe
DT
150grep $(guix build guile-bootstrap) $tmpdir/mounts
151grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
152
153rm $tmpdir/mounts
82e64fc1 154
9b65281d 155# Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
07ec3492
MG
156# within a container.
157(
9b65281d
LC
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")))))'
07ec3492
MG
164
165 cd "$tmpdir" \
166 && guix environment --bootstrap --container --link-profile \
167 --ad-hoc guile-bootstrap --pure \
168 -- guile -c "$linktest"
169)
170
e37944d8
MG
171# Test that user can be mocked.
172usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
1ccc0f80 173 (string=? (passwd:name (getpwuid 1000)) "foognu")
e37944d8
MG
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
75a6f668
LC
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)
e37944d8 188
07ec3492
MG
189# Check the exit code.
190
13bc8d5e
DT
191abnormal_exit_code="
192(use-modules (system foreign))
193;; Purposely make Guile crash with a segfault. :)
194(pointer->string (make-pointer 123) 123)"
195
82e64fc1 196if guix environment --bootstrap --container \
13bc8d5e 197 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
82e64fc1
LC
198then false;
199else
200 test $? -gt 127
201fi
c7ba5f38
JK
202
203# Test the Filesystem Hierarchy Standard (FHS) container option, --emulate-fhs (-F)
204
205# As this option requires a glibc package (glibc-for-fhs), try to run these
206# tests with the user's global store to make it easier to build or download a
207# substitute.
208storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
209localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
210NIX_STORE_DIR="$storedir"
211GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
212export NIX_STORE_DIR GUIX_DAEMON_SOCKET
213
214if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
215then
216 exit 77
217fi
218
219# Test that the container has FHS specific files/directories. Note that /bin
220# exists in a non-FHS container as it will contain sh, a symlink to the bash
221# package, so we don't test for it.
8b192c55 222guix shell -C --emulate-fhs --bootstrap guile-bootstrap \
c7ba5f38
JK
223 -- guile -c '(exit (and (file-exists? "/etc/ld.so.cache")
224 (file-exists? "/lib")
225 (file-exists? "/sbin")
226 (file-exists? "/usr/bin")
227 (file-exists? "/usr/include")
228 (file-exists? "/usr/lib")
229 (file-exists? "/usr/libexec")
230 (file-exists? "/usr/sbin")
231 (file-exists? "/usr/share")))'
232
233# Test that the ld cache was generated and can be successfully read.
8b192c55 234guix shell -CF --bootstrap guile-bootstrap \
c7ba5f38 235 -- guile -c '(execlp "ldconfig" "ldconfig" "-p")'
905443ab
JK
236
237# Test that the package glibc-for-fhs is in the container even if there is the
238# regular glibc package from another source. See
239# <https://issues.guix.gnu.org/58861>.
240guix shell -CF --bootstrap guile-bootstrap glibc \
241 -- guile -c '(exit (if (string-contains (readlink "/lib/libc.so")
242 "glibc-for-fhs")
243 0
244 1))'
b31ea797 245
3bfbfa29
JK
246# Test that $PATH inside the container includes the FHS directories.
247guix shell -CF coreutils -- env | grep ^PATH=/bin:/usr/bin:/sbin:/usr/sbin.*
248
249# Make sure '--preserve' is honored for $PATH, which the '--emulate-fhs'
250# option modifies. We can't (easily) check the whole $PATH as it will differ
251# inside and outside the container, so just check our test $PATH is still
252# present. See <https://issues.guix.gnu.org/60566>.
253PATH=/foo $(type -P guix) shell -CF -E ^PATH$ coreutils \
254 -- env | grep ^PATH=.*:/foo
255
b31ea797
MC
256# '--symlink' works.
257echo "TESTING SYMLINK IN CONTAINER"
258guix shell --bootstrap guile-bootstrap --container \
259 --symlink=/usr/bin/guile=bin/guile -- \
260 /usr/bin/guile --version
261
262# A dangling symlink causes the command to fail.
263! guix shell --bootstrap -CS /usr/bin/python=bin/python guile-bootstrap -- exit
788602b3
MC
264
265# An invalid symlink spec causes the command to fail.
266! guix shell --bootstrap -CS bin/guile=/usr/bin/guile guile-bootstrap -- exit