linux-container: Make the guest UID and GID a parameter.
[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
8a9922bd
LC
47if test "x$USER" = "x"; then USER="`id -un`"; fi
48
952afb6f 49# Check whether /etc/passwd and /etc/group are valid.
8a9922bd
LC
50guix environment -C --ad-hoc --bootstrap guile-bootstrap \
51 -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
952afb6f
LC
52guix environment -C --ad-hoc --bootstrap guile-bootstrap \
53 -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
54guix environment -C --ad-hoc --bootstrap guile-bootstrap \
55 -- guile -c '(use-modules (srfi srfi-1))
56 (exit (every group:name
57 (map getgrgid (vector->list (getgroups)))))'
8a9922bd 58
c06f6db7
LC
59# Make sure file-not-found errors in mounts are reported.
60if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
61 --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
62then
63 false
64else
65 grep "/does-not-exist" "$tmpdir/error"
66 grep "[Nn]o such file" "$tmpdir/error"
67fi
68
f535dcbe
DT
69# Make sure that the right directories are mapped.
70mount_test_code="
71(use-modules (ice-9 rdelim)
72 (ice-9 match)
73 (srfi srfi-1))
74
75(define mappings
76 (filter-map (lambda (line)
77 (match (string-split line #\space)
78 ;; Empty line.
79 ((\"\") #f)
1250034d
LC
80 ;; Ignore the root file system.
81 ((_ \"/\" _ _ _ _)
f535dcbe 82 #f)
1250034d
LC
83 ;; Ignore these types of file systems, except if they
84 ;; correspond to a parent file system.
85 ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
86 \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
7cdec6a9 87 (and (string-prefix? (getcwd) mount)
1250034d 88 mount))
f535dcbe
DT
89 ((_ mount _ _ _ _)
90 mount)))
91 (string-split (call-with-input-file \"/proc/mounts\" read-string)
92 #\newline)))
93
94(for-each (lambda (mount)
95 (display mount)
96 (newline))
97 mappings)"
98
99guix environment --container --ad-hoc --bootstrap guile-bootstrap \
100 -- guile -c "$mount_test_code" > $tmpdir/mounts
101
855038b2 102cat "$tmpdir/mounts"
779aa003 103test `wc -l < $tmpdir/mounts` -eq 4
f535dcbe 104
c8855b99
LC
105current_dir="`cd $PWD; pwd -P`"
106grep -e "$current_dir$" $tmpdir/mounts # current directory
f535dcbe
DT
107grep $(guix build guile-bootstrap) $tmpdir/mounts
108grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
109
110rm $tmpdir/mounts
82e64fc1 111
07ec3492
MG
112# Make sure 'GUIX_ENVIRONMENT' is linked to '~/.guix-profile' when requested
113# within a container.
114(
115 linktest='(exit (string=? (getenv "GUIX_ENVIRONMENT")
116(readlink (string-append (getenv "HOME") "/.guix-profile"))))'
117
118 cd "$tmpdir" \
119 && guix environment --bootstrap --container --link-profile \
120 --ad-hoc guile-bootstrap --pure \
121 -- guile -c "$linktest"
122)
123
e37944d8
MG
124# Test that user can be mocked.
125usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
126 (string=? (passwd:name (getpwuid 0)) "foognu")
127 (file-exists? "/home/foognu/umock")))'
128touch "$tmpdir/umock"
129HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
130 --ad-hoc guile-bootstrap --pure \
131 --share="$tmpdir/umock" \
132 -- guile -c "$usertest"
133
134
07ec3492
MG
135# Check the exit code.
136
13bc8d5e
DT
137abnormal_exit_code="
138(use-modules (system foreign))
139;; Purposely make Guile crash with a segfault. :)
140(pointer->string (make-pointer 123) 123)"
141
82e64fc1 142if guix environment --bootstrap --container \
13bc8d5e 143 --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
82e64fc1
LC
144then false;
145else
146 test $? -gt 127
147fi