tests: Check the effect of '--without-tests' on implicit inputs.
[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
90f496be 27if ! guile -c '((@ (guix scripts environment) assert-container-features))'
6493fd04
LC
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
0f53c801
LC
47# Make sure "localhost" resolves.
48guix environment --container --ad-hoc --bootstrap guile-bootstrap \
49 -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
50
b68d4106
LC
51# We should get ECONNREFUSED, not ENETUNREACH, which would indicate that "lo"
52# is down.
53guix environment --container --ad-hoc --bootstrap guile-bootstrap \
54 -- guile -c "(exit (= ECONNREFUSED
55 (catch 'system-error
56 (lambda ()
57 (let ((sock (socket AF_INET SOCK_STREAM 0)))
58 (connect sock AF_INET INADDR_LOOPBACK 12345)))
59 (lambda args
60 (pk 'errno (system-error-errno args))))))"
61
5a02f8e3
LC
62# Make sure '--preserve' is honored.
63result="`FOOBAR=42; export FOOBAR; guix environment -C --ad-hoc --bootstrap \
64 guile-bootstrap -E ^FOO -- guile -c '(display (getenv \"FOOBAR\"))'`"
65test "$result" = "42"
66
1ccc0f80
LC
67# By default, the UID inside the container should be the same as outside.
68uid="`id -u`"
69inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
70 -- guile -c '(display (getuid))'`"
71test $inner_uid = $uid
72
73# When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
74# so that we don't run into problems when the test directory is under /home.)
75export tmpdir
76inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
77 --user=gnu-guix -- guile -c '(display (getuid))'`"
78test $inner_uid = 1000
79
8a9922bd
LC
80if test "x$USER" = "x"; then USER="`id -un`"; fi
81
952afb6f 82# Check whether /etc/passwd and /etc/group are valid.
8a9922bd
LC
83guix environment -C --ad-hoc --bootstrap guile-bootstrap \
84 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
952afb6f
LC
85guix environment -C --ad-hoc --bootstrap guile-bootstrap \
86 -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
87guix environment -C --ad-hoc --bootstrap guile-bootstrap \
88 -- guile -c '(use-modules (srfi srfi-1))
89 (exit (every group:name
90 (map getgrgid (vector->list (getgroups)))))'
8a9922bd 91
c06f6db7
LC
92# Make sure file-not-found errors in mounts are reported.
93if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
94 --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
95then
96 false
97else
98 grep "/does-not-exist" "$tmpdir/error"
99 grep "[Nn]o such file" "$tmpdir/error"
100fi
101
f535dcbe
DT
102# Make sure that the right directories are mapped.
103mount_test_code="
104(use-modules (ice-9 rdelim)
105 (ice-9 match)
106 (srfi srfi-1))
107
108(define mappings
109 (filter-map (lambda (line)
110 (match (string-split line #\space)
111 ;; Empty line.
112 ((\"\") #f)
1250034d
LC
113 ;; Ignore the root file system.
114 ((_ \"/\" _ _ _ _)
f535dcbe 115 #f)
1250034d
LC
116 ;; Ignore these types of file systems, except if they
117 ;; correspond to a parent file system.
118 ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
119 \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
7cdec6a9 120 (and (string-prefix? (getcwd) mount)
1250034d 121 mount))
f535dcbe
DT
122 ((_ mount _ _ _ _)
123 mount)))
124 (string-split (call-with-input-file \"/proc/mounts\" read-string)
125 #\newline)))
126
127(for-each (lambda (mount)
128 (display mount)
129 (newline))
130 mappings)"
131
132guix environment --container --ad-hoc --bootstrap guile-bootstrap \
133 -- guile -c "$mount_test_code" > $tmpdir/mounts
134
855038b2 135cat "$tmpdir/mounts"
779aa003 136test `wc -l < $tmpdir/mounts` -eq 4
f535dcbe 137
c8855b99
LC
138current_dir="`cd $PWD; pwd -P`"
139grep -e "$current_dir$" $tmpdir/mounts # current directory
f535dcbe
DT
140grep $(guix build guile-bootstrap) $tmpdir/mounts
141grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
142
143rm $tmpdir/mounts
82e64fc1 144
9b65281d 145# Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
07ec3492
MG
146# within a container.
147(
9b65281d
LC
148 linktest='
149(exit (and (string=? (getenv "GUIX_ENVIRONMENT")
150 (string-append (getenv "HOME") "/.guix-profile"))
151 (string-prefix? "'"$NIX_STORE_DIR"'"
152 (readlink (string-append (getenv "HOME")
153 "/.guix-profile")))))'
07ec3492
MG
154
155 cd "$tmpdir" \
156 && guix environment --bootstrap --container --link-profile \
157 --ad-hoc guile-bootstrap --pure \
158 -- guile -c "$linktest"
159)
160
e37944d8
MG
161# Test that user can be mocked.
162usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
1ccc0f80 163 (string=? (passwd:name (getpwuid 1000)) "foognu")
e37944d8
MG
164 (file-exists? "/home/foognu/umock")))'
165touch "$tmpdir/umock"
166HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
167 --ad-hoc guile-bootstrap --pure \
168 --share="$tmpdir/umock" \
169 -- guile -c "$usertest"
170
75a6f668
LC
171# if not sharing CWD, chdir home
172(
173 cd "$tmpdir" \
174 && guix environment --bootstrap --container --no-cwd --user=foo \
175 --ad-hoc guile-bootstrap --pure \
176 -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
177)
e37944d8 178
07ec3492
MG
179# Check the exit code.
180
13bc8d5e
DT
181abnormal_exit_code="
182(use-modules (system foreign))
183;; Purposely make Guile crash with a segfault. :)
184(pointer->string (make-pointer 123) 123)"
185
82e64fc1 186if guix environment --bootstrap --container \
13bc8d5e 187 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
82e64fc1
LC
188then false;
189else
190 test $? -gt 127
191fi