guix-environment-container.sh 6.8 KB

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