environment: Provide /etc/hosts in containers without '--network'.
[jackhill/guix/guix.git] / tests / guix-environment-container.sh
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
23 set -e
24
25 guix environment --version
26
27 if ! guile -c '((@ (guix scripts environment) assert-container-features))'
28 then
29 # User containers are not supported; skip this test.
30 exit 77
31 fi
32
33 tmpdir="t-guix-environment-$$"
34 trap 'rm -r "$tmpdir"' EXIT
35
36 mkdir "$tmpdir"
37
38 # Make sure the exit value is preserved.
39 if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
40 -- guile -c '(exit 42)'
41 then
42 false
43 else
44 test $? = 42
45 fi
46
47 # Make sure "localhost" resolves.
48 guix environment --container --ad-hoc --bootstrap guile-bootstrap \
49 -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
50
51 # Make sure '--preserve' is honored.
52 result="`FOOBAR=42; export FOOBAR; guix environment -C --ad-hoc --bootstrap \
53 guile-bootstrap -E ^FOO -- guile -c '(display (getenv \"FOOBAR\"))'`"
54 test "$result" = "42"
55
56 # By default, the UID inside the container should be the same as outside.
57 uid="`id -u`"
58 inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
59 -- guile -c '(display (getuid))'`"
60 test $inner_uid = $uid
61
62 # When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
63 # so that we don't run into problems when the test directory is under /home.)
64 export tmpdir
65 inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
66 --user=gnu-guix -- guile -c '(display (getuid))'`"
67 test $inner_uid = 1000
68
69 if test "x$USER" = "x"; then USER="`id -un`"; fi
70
71 # Check whether /etc/passwd and /etc/group are valid.
72 guix environment -C --ad-hoc --bootstrap guile-bootstrap \
73 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
74 guix environment -C --ad-hoc --bootstrap guile-bootstrap \
75 -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
76 guix environment -C --ad-hoc --bootstrap guile-bootstrap \
77 -- guile -c '(use-modules (srfi srfi-1))
78 (exit (every group:name
79 (map getgrgid (vector->list (getgroups)))))'
80
81 # Make sure file-not-found errors in mounts are reported.
82 if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
83 --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
84 then
85 false
86 else
87 grep "/does-not-exist" "$tmpdir/error"
88 grep "[Nn]o such file" "$tmpdir/error"
89 fi
90
91 # Make sure that the right directories are mapped.
92 mount_test_code="
93 (use-modules (ice-9 rdelim)
94 (ice-9 match)
95 (srfi srfi-1))
96
97 (define mappings
98 (filter-map (lambda (line)
99 (match (string-split line #\space)
100 ;; Empty line.
101 ((\"\") #f)
102 ;; Ignore the root file system.
103 ((_ \"/\" _ _ _ _)
104 #f)
105 ;; Ignore these types of file systems, except if they
106 ;; correspond to a parent file system.
107 ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
108 \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
109 (and (string-prefix? (getcwd) mount)
110 mount))
111 ((_ mount _ _ _ _)
112 mount)))
113 (string-split (call-with-input-file \"/proc/mounts\" read-string)
114 #\newline)))
115
116 (for-each (lambda (mount)
117 (display mount)
118 (newline))
119 mappings)"
120
121 guix environment --container --ad-hoc --bootstrap guile-bootstrap \
122 -- guile -c "$mount_test_code" > $tmpdir/mounts
123
124 cat "$tmpdir/mounts"
125 test `wc -l < $tmpdir/mounts` -eq 4
126
127 current_dir="`cd $PWD; pwd -P`"
128 grep -e "$current_dir$" $tmpdir/mounts # current directory
129 grep $(guix build guile-bootstrap) $tmpdir/mounts
130 grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
131
132 rm $tmpdir/mounts
133
134 # Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
135 # within a container.
136 (
137 linktest='
138 (exit (and (string=? (getenv "GUIX_ENVIRONMENT")
139 (string-append (getenv "HOME") "/.guix-profile"))
140 (string-prefix? "'"$NIX_STORE_DIR"'"
141 (readlink (string-append (getenv "HOME")
142 "/.guix-profile")))))'
143
144 cd "$tmpdir" \
145 && guix environment --bootstrap --container --link-profile \
146 --ad-hoc guile-bootstrap --pure \
147 -- guile -c "$linktest"
148 )
149
150 # Test that user can be mocked.
151 usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
152 (string=? (passwd:name (getpwuid 1000)) "foognu")
153 (file-exists? "/home/foognu/umock")))'
154 touch "$tmpdir/umock"
155 HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
156 --ad-hoc guile-bootstrap --pure \
157 --share="$tmpdir/umock" \
158 -- guile -c "$usertest"
159
160 # if not sharing CWD, chdir home
161 (
162 cd "$tmpdir" \
163 && guix environment --bootstrap --container --no-cwd --user=foo \
164 --ad-hoc guile-bootstrap --pure \
165 -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
166 )
167
168 # Check the exit code.
169
170 abnormal_exit_code="
171 (use-modules (system foreign))
172 ;; Purposely make Guile crash with a segfault. :)
173 (pointer->string (make-pointer 123) 123)"
174
175 if guix environment --bootstrap --container \
176 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
177 then false;
178 else
179 test $? -gt 127
180 fi