guix-environment-container.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # Make sure "localhost" resolves.
  40. guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  41. -- guile -c '(exit (pair? (getaddrinfo "localhost" "80")))'
  42. # We should get ECONNREFUSED, not ENETUNREACH, which would indicate that "lo"
  43. # is down.
  44. guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  45. -- guile -c "(exit (= ECONNREFUSED
  46. (catch 'system-error
  47. (lambda ()
  48. (let ((sock (socket AF_INET SOCK_STREAM 0)))
  49. (connect sock AF_INET INADDR_LOOPBACK 12345)))
  50. (lambda args
  51. (pk 'errno (system-error-errno args))))))"
  52. # Make sure '--preserve' is honored.
  53. result="`FOOBAR=42; export FOOBAR; guix environment -C --ad-hoc --bootstrap \
  54. guile-bootstrap -E ^FOO -- guile -c '(display (getenv \"FOOBAR\"))'`"
  55. test "$result" = "42"
  56. # By default, the UID inside the container should be the same as outside.
  57. uid="`id -u`"
  58. inner_uid="`guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  59. -- guile -c '(display (getuid))'`"
  60. test $inner_uid = $uid
  61. # When '--user' is passed, the UID should be 1000. (Note: Use a separate HOME
  62. # so that we don't run into problems when the test directory is under /home.)
  63. export tmpdir
  64. inner_uid="`HOME=$tmpdir guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  65. --user=gnu-guix -- guile -c '(display (getuid))'`"
  66. test $inner_uid = 1000
  67. if test "x$USER" = "x"; then USER="`id -un`"; fi
  68. # Check whether /etc/passwd and /etc/group are valid.
  69. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  70. -- guile -c "(exit (string=? \"$USER\" (passwd:name (getpwuid (getuid)))))"
  71. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  72. -- guile -c '(exit (string? (group:name (getgrgid (getgid)))))'
  73. guix environment -C --ad-hoc --bootstrap guile-bootstrap \
  74. -- guile -c '(use-modules (srfi srfi-1))
  75. (exit (every group:name
  76. (map getgrgid (vector->list (getgroups)))))'
  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. # Make sure that the right directories are mapped.
  87. mount_test_code="
  88. (use-modules (ice-9 rdelim)
  89. (ice-9 match)
  90. (srfi srfi-1))
  91. (define mappings
  92. (filter-map (lambda (line)
  93. (match (string-split line #\space)
  94. ;; Empty line.
  95. ((\"\") #f)
  96. ;; Ignore the root file system.
  97. ((_ \"/\" _ _ _ _)
  98. #f)
  99. ;; Ignore these types of file systems, except if they
  100. ;; correspond to a parent file system.
  101. ((_ mount (or \"tmpfs\" \"proc\" \"sysfs\" \"devtmpfs\"
  102. \"devpts\" \"cgroup\" \"mqueue\") _ _ _)
  103. (and (string-prefix? (getcwd) mount)
  104. mount))
  105. ((_ mount _ _ _ _)
  106. mount)))
  107. (string-split (call-with-input-file \"/proc/mounts\" read-string)
  108. #\newline)))
  109. (for-each (lambda (mount)
  110. (display mount)
  111. (newline))
  112. mappings)"
  113. guix environment --container --ad-hoc --bootstrap guile-bootstrap \
  114. -- guile -c "$mount_test_code" > $tmpdir/mounts
  115. cat "$tmpdir/mounts"
  116. test `wc -l < $tmpdir/mounts` -eq 4
  117. current_dir="`cd $PWD; pwd -P`"
  118. grep -e "$current_dir$" $tmpdir/mounts # current directory
  119. grep $(guix build guile-bootstrap) $tmpdir/mounts
  120. grep -e "$NIX_STORE_DIR/.*-bash" $tmpdir/mounts # bootstrap bash
  121. rm $tmpdir/mounts
  122. # Make sure 'GUIX_ENVIRONMENT' is set to '~/.guix-profile' when requested
  123. # within a container.
  124. (
  125. linktest='
  126. (exit (and (string=? (getenv "GUIX_ENVIRONMENT")
  127. (string-append (getenv "HOME") "/.guix-profile"))
  128. (string-prefix? "'"$NIX_STORE_DIR"'"
  129. (readlink (string-append (getenv "HOME")
  130. "/.guix-profile")))))'
  131. cd "$tmpdir" \
  132. && guix environment --bootstrap --container --link-profile \
  133. --ad-hoc guile-bootstrap --pure \
  134. -- guile -c "$linktest"
  135. )
  136. # Test that user can be mocked.
  137. usertest='(exit (and (string=? (getenv "HOME") "/home/foognu")
  138. (string=? (passwd:name (getpwuid 1000)) "foognu")
  139. (file-exists? "/home/foognu/umock")))'
  140. touch "$tmpdir/umock"
  141. HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \
  142. --ad-hoc guile-bootstrap --pure \
  143. --share="$tmpdir/umock" \
  144. -- guile -c "$usertest"
  145. # if not sharing CWD, chdir home
  146. (
  147. cd "$tmpdir" \
  148. && guix environment --bootstrap --container --no-cwd --user=foo \
  149. --ad-hoc guile-bootstrap --pure \
  150. -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir"
  151. )
  152. # Check the exit code.
  153. abnormal_exit_code="
  154. (use-modules (system foreign))
  155. ;; Purposely make Guile crash with a segfault. :)
  156. (pointer->string (make-pointer 123) 123)"
  157. if guix environment --bootstrap --container \
  158. --ad-hoc guile-bootstrap -- guile -c "$abnormal_exit_code"
  159. then false;
  160. else
  161. test $? -gt 127
  162. fi