gnu: linux-libre: Update to 5.3.
[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>
3#
4# This file is part of GNU Guix.
5#
6# GNU Guix is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or (at
9# your option) any later version.
10#
11# GNU Guix is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19#
20# Test 'guix environment'.
21#
22
23set -e
24
25guix environment --version
26
6493fd04
LC
27if ! guile -c '((@@ (guix scripts environment) assert-container-features))'
28then
29 # User containers are not supported; skip this test.
30 exit 77
31fi
32
f535dcbe
DT
33tmpdir="t-guix-environment-$$"
34trap 'rm -r "$tmpdir"' EXIT
35
36mkdir "$tmpdir"
37
38# Make sure the exit value is preserved.
39if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
40 -- guile -c '(exit 42)'
41then
42 false
43else
44 test $? = 42
45fi
46
1ccc0f80
LC
47# By default, the UID inside the container should be the same as outside.
48uid="`id -u`"
49inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
50 -- guile -c '(display (getuid))'`"
51test $inner_uid = $uid
52
53# When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
54# so that we don't run into problems when the test directory is under /home.)
55export tmpdir
56inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
57 --user=gnu-guix -- guile -c '(display (getuid))'`"
58test $inner_uid = 1000
59
8a9922bd
LC
60if test "x$USER" = "x"; then USER="`id -un`"; fi
61
952afb6f 62# Check whether /etc/passwd and /etc/group are valid.
8a9922bd
LC
63guix environment -C --ad-hoc --bootstrap guile-bootstrap \
64 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
952afb6f
LC
65guix environment -C --ad-hoc --bootstrap guile-bootstrap \
66 -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
67guix environment -C --ad-hoc --bootstrap guile-bootstrap \
68 -- guile -c '(use-modules (srfi srfi-1))
69 (exit (every group:name
70 (map getgrgid (vector->list (getgroups)))))'
8a9922bd 71
c06f6db7
LC
72# Make sure file-not-found errors in mounts are reported.
73if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
74 --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
75then
76 false
77else
78 grep "/does-not-exist" "$tmpdir/error"
79 grep "[Nn]o such file" "$tmpdir/error"
80fi
81
f535dcbe
DT
82# Make sure that the right directories are mapped.
83mount_test_code="
84(use-modules (ice-9 rdelim)
85 (ice-9 match)
86 (srfi srfi-1))
87
88(define mappings
89 (filter-map (lambda (line)
90 (match (string-split line #\space)
91 ;; Empty line.
92 ((\"\") #f)
1250034d
LC
93 ;; Ignore the root file system.
94 ((_ \"/\" _ _ _ _)
f535dcbe 95 #f)
1250034d
LC
96 ;; Ignore these types of file systems, except if they
97 ;; correspond to a parent file system.
98 ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
99 \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
7cdec6a9 100 (and (string-prefix? (getcwd) mount)
1250034d 101 mount))
f535dcbe
DT
102 ((_ mount _ _ _ _)
103 mount)))
104 (string-split (call-with-input-file \"/proc/mounts\" read-string)
105 #\newline)))
106
107(for-each (lambda (mount)
108 (display mount)
109 (newline))
110 mappings)"
111
112guix environment --container --ad-hoc --bootstrap guile-bootstrap \
113 -- guile -c "$mount_test_code" > $tmpdir/mounts
114
855038b2 115cat "$tmpdir/mounts"
779aa003 116test `wc -l < $tmpdir/mounts` -eq 4
f535dcbe 117
c8855b99
LC
118current_dir="`cd $PWD; pwd -P`"
119grep -e "$current_dir$" $tmpdir/mounts # current directory
f535dcbe
DT
120grep $(guix build guile-bootstrap) $tmpdir/mounts
121grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
122
123rm $tmpdir/mounts
82e64fc1 124
07ec3492
MG
125# Make sure 'GUIX_ENVIRONMENT' is linked to '~/.guix-profile' when requested
126# within a container.
127(
128 linktest='(exit (string=? (getenv "GUIX_ENVIRONMENT")
129(readlink (string-append (getenv "HOME") "/.guix-profile"))))'
130
131 cd "$tmpdir" \
132 && guix environment --bootstrap --container --link-profile \
133 --ad-hoc guile-bootstrap --pure \
134 -- guile -c "$linktest"
135)
136
e37944d8
MG
137# Test that user can be mocked.
138usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
1ccc0f80 139 (string=? (passwd:name (getpwuid 1000)) "foognu")
e37944d8
MG
140 (file-exists? "/home/foognu/umock")))'
141touch "$tmpdir/umock"
142HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
143 --ad-hoc guile-bootstrap --pure \
144 --share="$tmpdir/umock" \
145 -- guile -c "$usertest"
146
75a6f668
LC
147# if not sharing CWD, chdir home
148(
149 cd "$tmpdir" \
150 && guix environment --bootstrap --container --no-cwd --user=foo \
151 --ad-hoc guile-bootstrap --pure \
152 -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
153)
e37944d8 154
07ec3492
MG
155# Check the exit code.
156
13bc8d5e
DT
157abnormal_exit_code="
158(use-modules (system foreign))
159;; Purposely make Guile crash with a segfault. :)
160(pointer->string (make-pointer 123) 123)"
161
82e64fc1 162if guix environment --bootstrap --container \
13bc8d5e 163 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
82e64fc1
LC
164then false;
165else
166 test $? -gt 127
167fi