guix-daemon.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2012, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@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 the daemon and its interaction with 'guix substitute'.
  20. #
  21. set -e
  22. guix-daemon --version
  23. guix build --version
  24. drv="`guix build emacs -d`"
  25. out="`guile -c ' \
  26. (use-modules (guix) (gnu packages emacs)) \
  27. (define store (open-connection)) \
  28. (%graft? #f)
  29. (display (derivation->output-path (package-derivation store emacs)))'`"
  30. hash_part="`basename $out | cut -c 1-32`"
  31. narinfo="$hash_part.narinfo"
  32. substitute_dir="`echo $GUIX_BINARY_SUBSTITUTE_URL | sed -'es,file://,,g'`"
  33. cat > "$substitute_dir/nix-cache-info"<<EOF
  34. StoreDir: `dirname $drv`
  35. WantMassQuery: 0
  36. EOF
  37. cat > "$substitute_dir/$narinfo"<<EOF
  38. StorePath: $out
  39. URL: /nowhere/example.nar
  40. Compression: none
  41. NarSize: 1234
  42. References:
  43. System: `guile -c '(use-modules (guix)) (display (%current-system))'`
  44. Deriver: $drv
  45. EOF
  46. # Remove the cached narinfo.
  47. rm -f "$XDG_CACHE_HOME/guix/substitute/$hash_part"
  48. # Make sure we see the substitute.
  49. guile -c "
  50. (use-modules (guix))
  51. (define store (open-connection))
  52. (set-build-options store #:use-substitutes? #t
  53. #:substitute-urls (list \"$GUIX_BINARY_SUBSTITUTE_URL\"))
  54. (exit (has-substitutes? store \"$out\"))"
  55. # Now, run guix-daemon --no-substitutes.
  56. socket="$GUIX_STATE_DIRECTORY/alternate-socket"
  57. guix-daemon --no-substitutes --listen="$socket" --disable-chroot &
  58. daemon_pid=$!
  59. trap 'kill $daemon_pid' EXIT
  60. # Make sure we DON'T see the substitute.
  61. guile -c "
  62. (use-modules (guix))
  63. (define store (open-connection \"$socket\"))
  64. ;; This setting MUST NOT override the daemon's --no-substitutes.
  65. (set-build-options store #:use-substitutes? #t
  66. #:substitute-urls (list \"$GUIX_BINARY_SUBSTITUTE_URL\"))
  67. (exit (not (has-substitutes? store \"$out\")))"
  68. kill "$daemon_pid"
  69. # Pass several '--listen' options, and make sure they are all honored.
  70. guix-daemon --disable-chroot --listen="$socket" --listen="$socket-second" \
  71. --listen="localhost" --listen="localhost:9876" &
  72. daemon_pid=$!
  73. for uri in "$socket" "$socket-second" \
  74. "guix://localhost" "guix://localhost:9876"
  75. do
  76. GUIX_DAEMON_SOCKET="$uri" guix build guile-bootstrap
  77. done
  78. kill "$daemon_pid"
  79. # Make sure 'profiles/per-user' is created when connecting over TCP.
  80. orig_GUIX_STATE_DIRECTORY="$GUIX_STATE_DIRECTORY"
  81. GUIX_STATE_DIRECTORY="$GUIX_STATE_DIRECTORY-2"
  82. guix-daemon --disable-chroot --listen="localhost:9877" &
  83. daemon_pid=$!
  84. GUIX_DAEMON_SOCKET="guix://localhost:9877"
  85. export GUIX_DAEMON_SOCKET
  86. test ! -d "$GUIX_STATE_DIRECTORY/profiles/per-user"
  87. guix build guile-bootstrap -d
  88. test -d "$GUIX_STATE_DIRECTORY/profiles/per-user/$USER"
  89. kill "$daemon_pid"
  90. unset GUIX_DAEMON_SOCKET
  91. GUIX_STATE_DIRECTORY="$orig_GUIX_STATE_DIRECTORY"
  92. # Check the failed build cache.
  93. guix-daemon --no-substitutes --listen="$socket" --disable-chroot \
  94. --cache-failures &
  95. daemon_pid=$!
  96. guile -c "
  97. (use-modules (guix) (guix tests) (srfi srfi-34))
  98. (define store (open-connection-for-tests \"$socket\"))
  99. ;; Disable grafts to avoid building more than needed.
  100. (%graft? #f)
  101. (define (build-without-failing drv)
  102. (lambda (store)
  103. (guard (c ((store-protocol-error? c) (values #t store)))
  104. (build-derivations store (list drv))
  105. (values #f store))))
  106. ;; Make sure failed builds are cached and can be removed from
  107. ;; the cache.
  108. (run-with-store store
  109. (mlet* %store-monad ((drv (gexp->derivation \"failure\"
  110. #~(begin
  111. (ungexp output)
  112. #f)))
  113. (out -> (derivation->output-path drv))
  114. (ok? (build-without-failing drv)))
  115. ;; Note the mixture of monadic and direct style. Don't try
  116. ;; this at home!
  117. (return (exit (and ok?
  118. (equal? (query-failed-paths store) (list out))
  119. (begin
  120. (clear-failed-paths store (list out))
  121. (null? (query-failed-paths store)))))))
  122. #:guile-for-build (%guile-for-build)) "
  123. kill "$daemon_pid"
  124. # Make sure the daemon's default 'build-cores' setting is honored.
  125. guix-daemon --listen="$socket" --disable-chroot --cores=42 &
  126. daemon_pid=$!
  127. GUIX_DAEMON_SOCKET="$socket" \
  128. guile -c '
  129. (use-modules (guix) (guix tests))
  130. (with-store store
  131. (let* ((build (add-text-to-store store "build.sh"
  132. "echo $NIX_BUILD_CORES > $out"))
  133. (bash (add-to-store store "bash" #t "sha256"
  134. (search-bootstrap-binary "bash"
  135. (%current-system))))
  136. (drv (derivation store "the-thing" bash
  137. `("-e" ,build)
  138. #:inputs `((,bash) (,build))
  139. #:env-vars `(("x" . ,(random-text))))))
  140. (and (build-derivations store (list drv))
  141. (exit
  142. (= 42 (pk (call-with-input-file (derivation->output-path drv)
  143. read)))))))'
  144. kill "$daemon_pid"
  145. # Make sure the daemon's default 'timeout' and 'max-silent-time' settings are
  146. # honored.
  147. client_code='
  148. (use-modules (guix) (guix tests) (srfi srfi-34))
  149. (with-store store
  150. (let* ((build (add-text-to-store store "build.sh"
  151. "while true ; do : ; done"))
  152. (bash (add-to-store store "bash" #t "sha256"
  153. (search-bootstrap-binary "bash"
  154. (%current-system))))
  155. (drv (derivation store "the-thing" bash
  156. `("-e" ,build)
  157. #:inputs `((,bash) (,build))
  158. #:env-vars `(("x" . ,(random-text))))))
  159. (exit (guard (c ((store-protocol-error? c)
  160. (->bool
  161. (string-contains (pk (store-protocol-error-message c))
  162. "failed"))))
  163. (build-derivations store (list drv))
  164. #f))))'
  165. for option in --max-silent-time=1 --timeout=1
  166. do
  167. guix-daemon --listen="$socket" --disable-chroot "$option" &
  168. daemon_pid=$!
  169. GUIX_DAEMON_SOCKET="$socket" guile -c "$client_code"
  170. kill "$daemon_pid"
  171. done
  172. # Make sure garbage collection from a TCP connection does not work.
  173. tcp_socket="127.0.0.1:9998"
  174. guix-daemon --listen="$tcp_socket" &
  175. daemon_pid=$!
  176. GUIX_DAEMON_SOCKET="guix://$tcp_socket"
  177. export GUIX_DAEMON_SOCKET
  178. guix gc && false
  179. unset GUIX_DAEMON_SOCKET
  180. kill "$daemon_pid"
  181. # Log compression.
  182. guix-daemon --listen="$socket" --disable-chroot --debug --log-compression=gzip &
  183. daemon_pid=$!
  184. stamp="compressed-build-log-test-$$-`date +%H%M%S`"
  185. client_code="
  186. (use-modules (guix) (gnu packages bootstrap))
  187. (with-store store
  188. (run-with-store store
  189. (mlet %store-monad ((drv (lower-object
  190. (computed-file \"compressed-log-test\"
  191. #~(begin
  192. (display \"$stamp\")
  193. (newline)
  194. (mkdir #\$output))
  195. #:guile %bootstrap-guile))))
  196. (display (derivation-file-name drv))
  197. (newline)
  198. (return #t))))
  199. "
  200. GUIX_DAEMON_SOCKET="$socket"
  201. export GUIX_DAEMON_SOCKET
  202. drv=`guile -c "$client_code"`
  203. guix build "$drv"
  204. log=`guix build "$drv" --log-file`
  205. test -f "$log"
  206. case "$log" in
  207. *.gz) test "`gunzip -c < "$log"`" = "$stamp" ;;
  208. *) false ;;
  209. esac