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