guix-environment-container.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2015 David Thompson <davet@gnu.org>
  3. # Copyright © 2022, 2023 John Kehayias <john.kehayias@protonmail.com>
  4. # Copyright © 2023 Ludovic Courtès <ludo@gnu.org>
  5. #
  6. # This file is part of GNU Guix.
  7. #
  8. # GNU Guix is free software; you can redistribute it and/or modify it
  9. # under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License, or (at
  11. # your option) any later version.
  12. #
  13. # GNU Guix is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # Test 'guix environment'.
  22. #
  23. set -e
  24. guix environment --version
  25. if ! guile -c '((@ (guix scripts environment) assert-container-features))'
  26. then
  27. # User containers are not supported; skip this test.
  28. exit 77
  29. fi
  30. tmpdir="t-guix-environment-$$"
  31. trap 'rm -r "$tmpdir"' EXIT
  32. mkdir "$tmpdir"
  33. # Make sure the exit value is preserved.
  34. if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  35. -- guile -c '(exit 42)'
  36. then
  37. false
  38. else
  39. test $? = 42
  40. fi
  41. # Try '--root' and '--profile'.
  42. root="$tmpdir/root"
  43. guix environment -C --ad-hoc --bootstrap guile-bootstrap -r "$root" -- guile --version
  44. guix environment -C -p "$root" --bootstrap -- guile --version
  45. path1=$(guix environment -C -p "$root" --bootstrap -- guile -c '(display (getenv "PATH"))')
  46. path2=$(guix environment -C --ad-hoc --bootstrap guile-bootstrap -- guile -c '(display (getenv "PATH"))')
  47. test "$path1" = "$path2"
  48. # Make sure "localhost" resolves.
  49. guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  50. -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
  51. # We should get ECONNREFUSED, not ENETUNREACH, which would indicate that "lo"
  52. # is down.
  53. guix 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. # Make sure '--preserve' is honored.
  62. result="`FOOBAR=42; export FOOBAR; guix environment -C --ad-hoc --bootstrap \
  63. guile-bootstrap -E ^FOO -- guile -c '(display (getenv \"FOOBAR\"))'`"
  64. test "$result" = "42"
  65. # By default, the UID inside the container should be the same as outside.
  66. uid="`id -u`"
  67. inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  68. -- guile -c '(display (getuid))'`"
  69. test $inner_uid = $uid
  70. # When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
  71. # so that we don't run into problems when the test directory is under /home.)
  72. export tmpdir
  73. inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  74. --user=gnu-guix -- guile -c '(display (getuid))'`"
  75. test $inner_uid = 1000
  76. if test "x$USER" = "x"; then USER="`id -un`"; fi
  77. # Check whether /etc/passwd and /etc/group are valid.
  78. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  79. -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
  80. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  81. -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
  82. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  83. -- guile -c '(use-modules (srfi srfi-1))
  84. (exit (every group:name
  85. (map getgrgid (vector->list (getgroups)))))'
  86. # Make sure file-not-found errors in mounts are reported.
  87. if guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  88. --expose=/does-not-exist -- guile -c 1 2> "$tmpdir/error"
  89. then
  90. false
  91. else
  92. grep "/does-not-exist" "$tmpdir/error"
  93. grep "[Nn]o such file" "$tmpdir/error"
  94. fi
  95. # Make sure that the right directories are mapped.
  96. mount_test_code="
  97. (use-modules (ice-9 rdelim)
  98. (ice-9 match)
  99. (srfi srfi-1))
  100. (define mappings
  101. (filter-map (lambda (line)
  102. (match (string-split line #\space)
  103. ;; Empty line.
  104. ((\"\") #f)
  105. ;; Ignore the root file system.
  106. ((_ \"/\" _ _ _ _)
  107. #f)
  108. ;; Ignore these types of file systems, except if they
  109. ;; correspond to a parent file system.
  110. ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
  111. \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
  112. (and (string-prefix? (getcwd) mount)
  113. mount))
  114. ((_ mount _ _ _ _)
  115. mount)))
  116. (string-split (call-with-input-file \"/proc/mounts\" read-string)
  117. #\newline)))
  118. (for-each (lambda (mount)
  119. (display mount)
  120. (newline))
  121. mappings)"
  122. guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  123. -- guile -c "$mount_test_code" > $tmpdir/mounts
  124. cat "$tmpdir/mounts"
  125. test `wc -l < $tmpdir/mounts` -eq 4
  126. current_dir="`cd $PWD; pwd -P`"
  127. grep -e "$current_dir$" $tmpdir/mounts # current directory
  128. grep $(guix build guile-bootstrap) $tmpdir/mounts
  129. grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
  130. rm $tmpdir/mounts
  131. # Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
  132. # within a container.
  133. (
  134. linktest='
  135. (exit (and (string=? (getenv "GUIX_ENVIRONMENT")
  136. (string-append (getenv "HOME") "/.guix-profile"))
  137. (string-prefix? "'"$NIX_STORE_DIR"'"
  138. (readlink (string-append (getenv "HOME")
  139. "/.guix-profile")))))'
  140. cd "$tmpdir" \
  141. && guix environment --bootstrap --container --link-profile \
  142. --ad-hoc guile-bootstrap --pure \
  143. -- guile -c "$linktest"
  144. )
  145. # Test that user can be mocked.
  146. usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
  147. (string=? (passwd:name (getpwuid 1000)) "foognu")
  148. (file-exists? "/home/foognu/umock")))'
  149. touch "$tmpdir/umock"
  150. HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
  151. --ad-hoc guile-bootstrap --pure \
  152. --share="$tmpdir/umock" \
  153. -- guile -c "$usertest"
  154. # if not sharing CWD, chdir home
  155. (
  156. cd "$tmpdir" \
  157. && guix environment --bootstrap --container --no-cwd --user=foo \
  158. --ad-hoc guile-bootstrap --pure \
  159. -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
  160. )
  161. # Check the exit code.
  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. if guix environment --bootstrap --container \
  167. --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
  168. then false;
  169. else
  170. test $? -gt 127
  171. fi
  172. # Test the Filesystem Hierarchy Standard (FHS) container option, --emulate-fhs (-F)
  173. # As this option requires a glibc package (glibc-for-fhs), try to run these
  174. # tests with the user's global store to make it easier to build or download a
  175. # substitute.
  176. storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
  177. localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
  178. NIX_STORE_DIR="$storedir"
  179. GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
  180. export NIX_STORE_DIR GUIX_DAEMON_SOCKET
  181. if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
  182. then
  183. exit 77
  184. fi
  185. # Test that the container has FHS specific files/directories. Note that /bin
  186. # exists in a non-FHS container as it will contain sh, a symlink to the bash
  187. # package, so we don't test for it.
  188. guix shell -C --emulate-fhs --bootstrap guile-bootstrap \
  189. -- guile -c '(exit (and (file-exists? "/etc/ld.so.cache")
  190. (file-exists? "/lib")
  191. (file-exists? "/sbin")
  192. (file-exists? "/usr/bin")
  193. (file-exists? "/usr/include")
  194. (file-exists? "/usr/lib")
  195. (file-exists? "/usr/libexec")
  196. (file-exists? "/usr/sbin")
  197. (file-exists? "/usr/share")))'
  198. # Test that the ld cache was generated and can be successfully read.
  199. guix shell -CF --bootstrap guile-bootstrap \
  200. -- guile -c '(execlp "ldconfig" "ldconfig" "-p")'
  201. # Test that the package glibc-for-fhs is in the container even if there is the
  202. # regular glibc package from another source. See
  203. # <https://issues.guix.gnu.org/58861>.
  204. guix shell -CF --bootstrap guile-bootstrap glibc \
  205. -- guile -c '(exit (if (string-contains (readlink "/lib/libc.so")
  206. "glibc-for-fhs")
  207. 0
  208. 1))'
  209. # Test that $PATH inside the container includes the FHS directories.
  210. guix shell -CF coreutils -- env | grep ^PATH=/bin:/usr/bin:/sbin:/usr/sbin.*
  211. # Make sure '--preserve' is honored for $PATH, which the '--emulate-fhs'
  212. # option modifies. We can't (easily) check the whole $PATH as it will differ
  213. # inside and outside the container, so just check our test $PATH is still
  214. # present. See <https://issues.guix.gnu.org/60566>.
  215. PATH=/foo $(type -P guix) shell -CF -E ^PATH$ coreutils \
  216. -- env | grep ^PATH=.*:/foo
  217. # '--symlink' works.
  218. echo "TESTING SYMLINK IN CONTAINER"
  219. guix shell --bootstrap guile-bootstrap --container \
  220. --symlink=/usr/bin/guile=bin/guile -- \
  221. /usr/bin/guile --version
  222. # A dangling symlink causes the command to fail.
  223. guix shell --bootstrap -CS /usr/bin/python=bin/python guile-bootstrap -- exit && false
  224. # An invalid symlink spec causes the command to fail.
  225. guix shell --bootstrap -CS bin/guile=/usr/bin/guile guile-bootstrap -- exit && false
  226. # Check whether '--nesting' works.
  227. guix build hello -d
  228. env="$(type -P pre-inst-env)"
  229. guix shell -C -D guix -- "$env" guix build hello -d && false # cannot work
  230. hello_drv="$(guix build hello -d)"
  231. hello_drv_nested="$(cd "$(dirname env)" && guix shell --bootstrap -CW -D guix -- "$env" guix build hello -d)"
  232. test "$hello_drv" = "$hello_drv_nested"