You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

392 lines
18 KiB

  1. ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
  2. ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of Guix.
  5. ;;;
  6. ;;; 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. ;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-derivations)
  19. #:use-module (guix derivations)
  20. #:use-module (guix store)
  21. #:use-module (guix utils)
  22. #:use-module ((guix packages) #:select (package-derivation))
  23. #:use-module ((distro packages base) #:select (%bootstrap-guile))
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-64)
  29. #:use-module (rnrs io ports)
  30. #:use-module (rnrs bytevectors)
  31. #:use-module (ice-9 rdelim)
  32. #:use-module (ice-9 regex)
  33. #:use-module (ice-9 ftw)
  34. #:use-module (ice-9 match))
  35. (define %store
  36. (false-if-exception (open-connection)))
  37. (when %store
  38. ;; Make sure we build everything by ourselves.
  39. (set-build-options %store #:use-substitutes? #f)
  40. ;; By default, use %BOOTSTRAP-GUILE for the current system.
  41. (let ((drv (package-derivation %store %bootstrap-guile)))
  42. (%guile-for-build drv)))
  43. (define (directory-contents dir)
  44. "Return an alist representing the contents of DIR."
  45. (define prefix-len (string-length dir))
  46. (sort (file-system-fold (const #t) ; enter?
  47. (lambda (path stat result) ; leaf
  48. (alist-cons (string-drop path prefix-len)
  49. (call-with-input-file path
  50. get-bytevector-all)
  51. result))
  52. (lambda (path stat result) result) ; down
  53. (lambda (path stat result) result) ; up
  54. (lambda (path stat result) result) ; skip
  55. (lambda (path stat errno result) result) ; error
  56. '()
  57. dir)
  58. (lambda (e1 e2)
  59. (string<? (car e1) (car e2)))))
  60. (test-begin "derivations")
  61. (test-assert "parse & export"
  62. (let* ((f (search-path %load-path "tests/test.drv"))
  63. (b1 (call-with-input-file f get-bytevector-all))
  64. (d1 (read-derivation (open-bytevector-input-port b1)))
  65. (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
  66. (d2 (read-derivation (open-bytevector-input-port b2))))
  67. (and (equal? b1 b2)
  68. (equal? d1 d2))))
  69. (test-skip (if %store 0 4))
  70. (test-assert "add-to-store, flat"
  71. (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
  72. (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
  73. (and (eq? 'regular (stat:type (stat drv)))
  74. (valid-path? %store drv)
  75. (equal? (call-with-input-file file get-bytevector-all)
  76. (call-with-input-file drv get-bytevector-all)))))
  77. (test-assert "add-to-store, recursive"
  78. (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
  79. (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
  80. (and (eq? 'directory (stat:type (stat drv)))
  81. (valid-path? %store drv)
  82. (equal? (directory-contents dir)
  83. (directory-contents drv)))))
  84. (test-assert "derivation with no inputs"
  85. (let* ((builder (add-text-to-store %store "my-builder.sh"
  86. "#!/bin/sh\necho hello, world\n"
  87. '()))
  88. (drv-path (derivation %store "foo" (%current-system) builder
  89. '() '(("HOME" . "/homeless")) '())))
  90. (and (store-path? drv-path)
  91. (valid-path? %store drv-path))))
  92. (test-assert "build derivation with 1 source"
  93. (let*-values (((builder)
  94. (add-text-to-store %store "my-builder.sh"
  95. "echo hello, world > \"$out\"\n"
  96. '()))
  97. ((drv-path drv)
  98. (derivation %store "foo" (%current-system)
  99. "/bin/sh" `(,builder)
  100. '(("HOME" . "/homeless")
  101. ("zzz" . "Z!")
  102. ("AAA" . "A!"))
  103. `((,builder))))
  104. ((succeeded?)
  105. (build-derivations %store (list drv-path))))
  106. (and succeeded?
  107. (let ((path (derivation-output-path
  108. (assoc-ref (derivation-outputs drv) "out"))))
  109. (and (valid-path? %store path)
  110. (string=? (call-with-input-file path read-line)
  111. "hello, world"))))))
  112. (test-assert "fixed-output derivation"
  113. (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
  114. "echo -n hello > $out" '()))
  115. (hash (sha256 (string->utf8 "hello")))
  116. (drv-path (derivation %store "fixed" (%current-system)
  117. "/bin/sh" `(,builder)
  118. '() `((,builder))
  119. #:hash hash #:hash-algo 'sha256))
  120. (succeeded? (build-derivations %store (list drv-path))))
  121. (and succeeded?
  122. (let ((p (derivation-path->output-path drv-path)))
  123. (and (equal? (string->utf8 "hello")
  124. (call-with-input-file p get-bytevector-all))
  125. (bytevector? (query-path-hash %store p)))))))
  126. (test-assert "multiple-output derivation"
  127. (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
  128. "echo one > $out ; echo two > $second"
  129. '()))
  130. (drv-path (derivation %store "fixed" (%current-system)
  131. "/bin/sh" `(,builder)
  132. '(("HOME" . "/homeless")
  133. ("zzz" . "Z!")
  134. ("AAA" . "A!"))
  135. `((,builder))
  136. #:outputs '("out" "second")))
  137. (succeeded? (build-derivations %store (list drv-path))))
  138. (and succeeded?
  139. (let ((one (derivation-path->output-path drv-path "out"))
  140. (two (derivation-path->output-path drv-path "second")))
  141. (and (eq? 'one (call-with-input-file one read))
  142. (eq? 'two (call-with-input-file two read)))))))
  143. (test-assert "multiple-output derivation, non-alphabetic order"
  144. ;; Here, the outputs are not listed in alphabetic order. Yet, the store
  145. ;; path computation must reorder them first.
  146. (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
  147. "echo one > $out ; echo two > $AAA"
  148. '()))
  149. (drv-path (derivation %store "fixed" (%current-system)
  150. "/bin/sh" `(,builder)
  151. '()
  152. `((,builder))
  153. #:outputs '("out" "AAA")))
  154. (succeeded? (build-derivations %store (list drv-path))))
  155. (and succeeded?
  156. (let ((one (derivation-path->output-path drv-path "out"))
  157. (two (derivation-path->output-path drv-path "AAA")))
  158. (and (eq? 'one (call-with-input-file one read))
  159. (eq? 'two (call-with-input-file two read)))))))
  160. (test-assert "user of multiple-output derivation"
  161. ;; Check whether specifying several inputs coming from the same
  162. ;; multiple-output derivation works.
  163. (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
  164. "echo one > $out ; echo two > $two"
  165. '()))
  166. (mdrv (derivation %store "multiple-output" (%current-system)
  167. "/bin/sh" `(,builder1)
  168. '()
  169. `((,builder1))
  170. #:outputs '("out" "two")))
  171. (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
  172. "read x < $one;
  173. read y < $two;
  174. echo \"($x $y)\" > $out"
  175. '()))
  176. (udrv (derivation %store "multiple-output-user"
  177. (%current-system)
  178. "/bin/sh" `(,builder2)
  179. `(("one" . ,(derivation-path->output-path
  180. mdrv "out"))
  181. ("two" . ,(derivation-path->output-path
  182. mdrv "two")))
  183. `((,builder2)
  184. ;; two occurrences of MDRV:
  185. (,mdrv)
  186. (,mdrv "two")))))
  187. (and (build-derivations %store (list (pk 'udrv udrv)))
  188. (let ((p (derivation-path->output-path udrv)))
  189. (and (valid-path? %store p)
  190. (equal? '(one two) (call-with-input-file p read)))))))
  191. (define %coreutils
  192. (false-if-exception
  193. (or (package-derivation %store
  194. (@@ (distro packages base)
  195. %bootstrap-coreutils&co))
  196. (nixpkgs-derivation "coreutils"))))
  197. (test-skip (if %coreutils 0 1))
  198. (test-assert "build derivation with coreutils"
  199. (let* ((builder
  200. (add-text-to-store %store "build-with-coreutils.sh"
  201. "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
  202. '()))
  203. (drv-path
  204. (derivation %store "foo" (%current-system)
  205. "/bin/sh" `(,builder)
  206. `(("PATH" .
  207. ,(string-append
  208. (derivation-path->output-path %coreutils)
  209. "/bin")))
  210. `((,builder)
  211. (,%coreutils))))
  212. (succeeded?
  213. (build-derivations %store (list drv-path))))
  214. (and succeeded?
  215. (let ((p (derivation-path->output-path drv-path)))
  216. (and (valid-path? %store p)
  217. (file-exists? (string-append p "/good")))))))
  218. (test-skip (if (%guile-for-build) 0 6))
  219. (test-assert "build-expression->derivation and derivation-prerequisites"
  220. (let-values (((drv-path drv)
  221. (build-expression->derivation %store "fail" (%current-system)
  222. #f '())))
  223. (any (match-lambda
  224. (($ <derivation-input> path)
  225. (string=? path (%guile-for-build))))
  226. (derivation-prerequisites drv))))
  227. (test-assert "build-expression->derivation without inputs"
  228. (let* ((builder '(begin
  229. (mkdir %output)
  230. (call-with-output-file (string-append %output "/test")
  231. (lambda (p)
  232. (display '(hello guix) p)))))
  233. (drv-path (build-expression->derivation %store "goo" (%current-system)
  234. builder '()))
  235. (succeeded? (build-derivations %store (list drv-path))))
  236. (and succeeded?
  237. (let ((p (derivation-path->output-path drv-path)))
  238. (equal? '(hello guix)
  239. (call-with-input-file (string-append p "/test") read))))))
  240. (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
  241. (let-values (((drv-path drv)
  242. (build-expression->derivation %store "fail" (%current-system)
  243. #f '())))
  244. ;; The only direct dependency is (%guile-for-build) and it's already
  245. ;; built.
  246. (null? (derivation-prerequisites-to-build %store drv))))
  247. (test-assert "build-expression->derivation with expression returning #f"
  248. (let* ((builder '(begin
  249. (mkdir %output)
  250. #f)) ; fail!
  251. (drv-path (build-expression->derivation %store "fail" (%current-system)
  252. builder '()))
  253. (out-path (derivation-path->output-path drv-path)))
  254. (guard (c ((nix-protocol-error? c)
  255. ;; Note that the output path may exist at this point, but it
  256. ;; is invalid.
  257. (and (string-match "build .* failed"
  258. (nix-protocol-error-message c))
  259. (not (valid-path? %store out-path)))))
  260. (build-derivations %store (list drv-path))
  261. #f)))
  262. (test-assert "build-expression->derivation with two outputs"
  263. (let* ((builder '(begin
  264. (call-with-output-file (assoc-ref %outputs "out")
  265. (lambda (p)
  266. (display '(hello) p)))
  267. (call-with-output-file (assoc-ref %outputs "second")
  268. (lambda (p)
  269. (display '(world) p)))))
  270. (drv-path (build-expression->derivation %store "double"
  271. (%current-system)
  272. builder '()
  273. #:outputs '("out"
  274. "second")))
  275. (succeeded? (build-derivations %store (list drv-path))))
  276. (and succeeded?
  277. (let ((one (derivation-path->output-path drv-path))
  278. (two (derivation-path->output-path drv-path "second")))
  279. (and (equal? '(hello) (call-with-input-file one read))
  280. (equal? '(world) (call-with-input-file two read)))))))
  281. (test-assert "build-expression->derivation with one input"
  282. (let* ((builder '(call-with-output-file %output
  283. (lambda (p)
  284. (let ((cu (assoc-ref %build-inputs "cu")))
  285. (close 1)
  286. (dup2 (port->fdes p) 1)
  287. (execl (string-append cu "/bin/uname")
  288. "uname" "-a")))))
  289. (drv-path (build-expression->derivation %store "uname" (%current-system)
  290. builder
  291. `(("cu" ,%coreutils))))
  292. (succeeded? (build-derivations %store (list drv-path))))
  293. (and succeeded?
  294. (let ((p (derivation-path->output-path drv-path)))
  295. (string-contains (call-with-input-file p read-line) "GNU")))))
  296. (test-assert "imported-files"
  297. (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
  298. ("a/b/c" . ,(search-path %load-path
  299. "guix/derivations.scm"))
  300. ("p/q" . ,(search-path %load-path "guix.scm"))
  301. ("p/z" . ,(search-path %load-path "guix/store.scm"))))
  302. (drv-path (imported-files %store files)))
  303. (and (build-derivations %store (list drv-path))
  304. (let ((dir (derivation-path->output-path drv-path)))
  305. (every (match-lambda
  306. ((path . source)
  307. (equal? (call-with-input-file (string-append dir "/" path)
  308. get-bytevector-all)
  309. (call-with-input-file source
  310. get-bytevector-all))))
  311. files)))))
  312. (test-assert "build-expression->derivation with modules"
  313. (let* ((builder `(begin
  314. (use-modules (guix build utils))
  315. (let ((out (assoc-ref %outputs "out")))
  316. (mkdir-p (string-append out "/guile/guix/nix"))
  317. #t)))
  318. (drv-path (build-expression->derivation %store
  319. "test-with-modules"
  320. (%current-system)
  321. builder '()
  322. #:modules
  323. '((guix build utils)))))
  324. (and (build-derivations %store (list drv-path))
  325. (let* ((p (derivation-path->output-path drv-path))
  326. (s (stat (string-append p "/guile/guix/nix"))))
  327. (eq? (stat:type s) 'directory)))))
  328. (test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
  329. 0
  330. 1))
  331. (test-assert "build-expression->derivation for fixed-output derivation"
  332. (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
  333. (builder
  334. `(begin
  335. (use-modules (web client) (web uri)
  336. (rnrs io ports) (srfi srfi-11))
  337. (let-values (((resp bv)
  338. (http-get (string->uri ,url) #:decode-body? #f)))
  339. (call-with-output-file %output
  340. (lambda (p)
  341. (put-bytevector p bv))))))
  342. (drv-path (build-expression->derivation
  343. %store "hello-2.8.tar.gz" (%current-system) builder '()
  344. #:hash (nix-base32-string->bytevector
  345. "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
  346. #:hash-algo 'sha256))
  347. (succeeded? (build-derivations %store (list drv-path))))
  348. (and succeeded?
  349. (file-exists? (derivation-path->output-path drv-path)))))
  350. (test-end)
  351. (exit (= (test-runner-fail-count (test-runner-current)) 0))
  352. ;;; Local Variables:
  353. ;;; eval: (put 'test-assert 'scheme-indent-function 1)
  354. ;;; eval: (put 'guard 'scheme-indent-function 1)
  355. ;;; End: