guix-daemon.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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) (guix grafts) (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. # Check the failed build cache.
  80. guix-daemon --no-substitutes --listen="$socket" --disable-chroot \
  81. --cache-failures &
  82. daemon_pid=$!
  83. guile -c "
  84. (use-modules (guix) (guix grafts) (guix tests) (srfi srfi-34))
  85. (define store (open-connection-for-tests \"$socket\"))
  86. ;; Disable grafts to avoid building more than needed.
  87. (%graft? #f)
  88. (define (build-without-failing drv)
  89. (lambda (store)
  90. (guard (c ((store-protocol-error? c) (values #t store)))
  91. (build-derivations store (list drv))
  92. (values #f store))))
  93. ;; Make sure failed builds are cached and can be removed from
  94. ;; the cache.
  95. (run-with-store store
  96. (mlet* %store-monad ((drv (gexp->derivation \"failure\"
  97. #~(begin
  98. (ungexp output)
  99. #f)))
  100. (out -> (derivation->output-path drv))
  101. (ok? (build-without-failing drv)))
  102. ;; Note the mixture of monadic and direct style. Don't try
  103. ;; this at home!
  104. (return (exit (and ok?
  105. (equal? (query-failed-paths store) (list out))
  106. (begin
  107. (clear-failed-paths store (list out))
  108. (null? (query-failed-paths store)))))))
  109. #:guile-for-build (%guile-for-build)) "
  110. kill "$daemon_pid"
  111. # Make sure the daemon's default 'build-cores' setting is honored.
  112. guix-daemon --listen="$socket" --disable-chroot --cores=42 &
  113. daemon_pid=$!
  114. GUIX_DAEMON_SOCKET="$socket" \
  115. guile -c '
  116. (use-modules (guix) (gnu packages) (guix tests))
  117. (with-store store
  118. (let* ((build (add-text-to-store store "build.sh"
  119. "echo $NIX_BUILD_CORES > $out"))
  120. (bash (add-to-store store "bash" #t "sha256"
  121. (search-bootstrap-binary "bash"
  122. (%current-system))))
  123. (drv (derivation store "the-thing" bash
  124. `("-e" ,build)
  125. #:inputs `((,bash) (,build))
  126. #:env-vars `(("x" . ,(random-text))))))
  127. (and (build-derivations store (list drv))
  128. (exit
  129. (= 42 (pk (call-with-input-file (derivation->output-path drv)
  130. read)))))))'
  131. kill "$daemon_pid"
  132. # Make sure the daemon's default 'timeout' and 'max-silent-time' settings are
  133. # honored.
  134. client_code='
  135. (use-modules (guix) (gnu packages) (guix tests) (srfi srfi-34))
  136. (with-store store
  137. (let* ((build (add-text-to-store store "build.sh"
  138. "while true ; do : ; done"))
  139. (bash (add-to-store store "bash" #t "sha256"
  140. (search-bootstrap-binary "bash"
  141. (%current-system))))
  142. (drv (derivation store "the-thing" bash
  143. `("-e" ,build)
  144. #:inputs `((,bash) (,build))
  145. #:env-vars `(("x" . ,(random-text))))))
  146. (exit (guard (c ((store-protocol-error? c)
  147. (->bool
  148. (string-contains (pk (store-protocol-error-message c))
  149. "failed"))))
  150. (build-derivations store (list drv))
  151. #f))))'
  152. for option in --max-silent-time=1 --timeout=1
  153. do
  154. guix-daemon --listen="$socket" --disable-chroot "$option" &
  155. daemon_pid=$!
  156. GUIX_DAEMON_SOCKET="$socket" guile -c "$client_code"
  157. kill "$daemon_pid"
  158. done
  159. # Make sure garbage collection from a TCP connection does not work.
  160. tcp_socket="127.0.0.1:9999"
  161. guix-daemon --listen="$tcp_socket" &
  162. daemon_pid=$!
  163. GUIX_DAEMON_SOCKET="guix://$tcp_socket"
  164. export GUIX_DAEMON_SOCKET
  165. if guix gc; then false; else true; fi
  166. unset GUIX_DAEMON_SOCKET
  167. kill "$daemon_pid"
  168. # Log compression.
  169. guix-daemon --listen="$socket" --disable-chroot --debug --log-compression=gzip &
  170. daemon_pid=$!
  171. stamp="compressed-build-log-test-$$-`date +%H%M%S`"
  172. client_code="
  173. (use-modules (guix) (gnu packages bootstrap))
  174. (with-store store
  175. (run-with-store store
  176. (mlet %store-monad ((drv (lower-object
  177. (computed-file \"compressed-log-test\"
  178. #~(begin
  179. (display \"$stamp\")
  180. (newline)
  181. (mkdir #\$output))
  182. #:guile %bootstrap-guile))))
  183. (display (derivation-file-name drv))
  184. (newline)
  185. (return #t))))
  186. "
  187. GUIX_DAEMON_SOCKET="$socket"
  188. export GUIX_DAEMON_SOCKET
  189. drv=`guile -c "$client_code"`
  190. guix build "$drv"
  191. log=`guix build "$drv" --log-file`
  192. test -f "$log"
  193. case "$log" in
  194. *.gz) test "`gunzip -c < "$log"`" = "$stamp" ;;
  195. *) false ;;
  196. esac