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