Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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
20 (define-module (test-derivations)
21 #:use-module (guix derivations)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix base32)
25 #:use-module ((guix packages) #:select (package-derivation))
26 #:use-module ((distro) #:select (search-bootstrap-binary))
27 #:use-module (distro packages bootstrap)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-11)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-34)
32 #:use-module (srfi srfi-64)
33 #:use-module (rnrs io ports)
34 #:use-module (rnrs bytevectors)
35 #:use-module (ice-9 rdelim)
36 #:use-module (ice-9 regex)
37 #:use-module (ice-9 ftw)
38 #:use-module (ice-9 match))
39
40 (define %store
41 (false-if-exception (open-connection)))
42
43 (when %store
44 ;; Make sure we build everything by ourselves.
45 (set-build-options %store #:use-substitutes? #f)
46
47 ;; By default, use %BOOTSTRAP-GUILE for the current system.
48 (let ((drv (package-derivation %store %bootstrap-guile)))
49 (%guile-for-build drv)))
50
51 (define %bash
52 (let ((bash (search-bootstrap-binary "bash" (%current-system))))
53 (and %store
54 (add-to-store %store "bash" #t #t "sha256" bash))))
55
56 (define (directory-contents dir)
57 "Return an alist representing the contents of DIR."
58 (define prefix-len (string-length dir))
59 (sort (file-system-fold (const #t) ; enter?
60 (lambda (path stat result) ; leaf
61 (alist-cons (string-drop path prefix-len)
62 (call-with-input-file path
63 get-bytevector-all)
64 result))
65 (lambda (path stat result) result) ; down
66 (lambda (path stat result) result) ; up
67 (lambda (path stat result) result) ; skip
68 (lambda (path stat errno result) result) ; error
69 '()
70 dir)
71 (lambda (e1 e2)
72 (string<? (car e1) (car e2)))))
73
74 (test-begin "derivations")
75
76 (test-assert "parse & export"
77 (let* ((f (search-path %load-path "tests/test.drv"))
78 (b1 (call-with-input-file f get-bytevector-all))
79 (d1 (read-derivation (open-bytevector-input-port b1)))
80 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
81 (d2 (read-derivation (open-bytevector-input-port b2))))
82 (and (equal? b1 b2)
83 (equal? d1 d2))))
84
85 (test-skip (if %store 0 11))
86
87 (test-assert "add-to-store, flat"
88 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
89 (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
90 (and (eq? 'regular (stat:type (stat drv)))
91 (valid-path? %store drv)
92 (equal? (call-with-input-file file get-bytevector-all)
93 (call-with-input-file drv get-bytevector-all)))))
94
95 (test-assert "add-to-store, recursive"
96 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
97 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
98 (and (eq? 'directory (stat:type (stat drv)))
99 (valid-path? %store drv)
100 (equal? (directory-contents dir)
101 (directory-contents drv)))))
102
103 (test-assert "derivation with no inputs"
104 (let* ((builder (add-text-to-store %store "my-builder.sh"
105 "echo hello, world\n"
106 '()))
107 (drv-path (derivation %store "foo" (%current-system)
108 %bash `("-e" ,builder)
109 '(("HOME" . "/homeless")) '())))
110 (and (store-path? drv-path)
111 (valid-path? %store drv-path))))
112
113 (test-assert "build derivation with 1 source"
114 (let*-values (((builder)
115 (add-text-to-store %store "my-builder.sh"
116 "echo hello, world > \"$out\"\n"
117 '()))
118 ((drv-path drv)
119 (derivation %store "foo" (%current-system)
120 %bash `(,builder)
121 '(("HOME" . "/homeless")
122 ("zzz" . "Z!")
123 ("AAA" . "A!"))
124 `((,builder))))
125 ((succeeded?)
126 (build-derivations %store (list drv-path))))
127 (and succeeded?
128 (let ((path (derivation-output-path
129 (assoc-ref (derivation-outputs drv) "out"))))
130 (and (valid-path? %store path)
131 (string=? (call-with-input-file path read-line)
132 "hello, world"))))))
133
134 (test-assert "derivation with local file as input"
135 (let* ((builder (add-text-to-store
136 %store "my-builder.sh"
137 "(while read line ; do echo $line ; done) < $in > $out"
138 '()))
139 (input (search-path %load-path "ice-9/boot-9.scm"))
140 (drv-path (derivation %store "derivation-with-input-file"
141 (%current-system)
142 %bash `(,builder)
143 `(("in"
144 ;; Cheat to pass the actual file
145 ;; name to the builder.
146 . ,(add-to-store %store
147 (basename input)
148 #t #t "sha256"
149 input)))
150 `((,builder)
151 (,input))))) ; ← local file name
152 (and (build-derivations %store (list drv-path))
153 (let ((p (derivation-path->output-path drv-path)))
154 (and (call-with-input-file p get-bytevector-all)
155 (call-with-input-file input get-bytevector-all))))))
156
157 (test-assert "fixed-output derivation"
158 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
159 "echo -n hello > $out" '()))
160 (hash (sha256 (string->utf8 "hello")))
161 (drv-path (derivation %store "fixed" (%current-system)
162 %bash `(,builder)
163 '()
164 `((,builder)) ; optional
165 #:hash hash #:hash-algo 'sha256))
166 (succeeded? (build-derivations %store (list drv-path))))
167 (and succeeded?
168 (let ((p (derivation-path->output-path drv-path)))
169 (and (equal? (string->utf8 "hello")
170 (call-with-input-file p get-bytevector-all))
171 (bytevector? (query-path-hash %store p)))))))
172
173 (test-assert "fixed-output derivation: output paths are equal"
174 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
175 "echo -n hello > $out" '()))
176 (builder2 (add-text-to-store %store "fixed-builder2.sh"
177 "echo hey; echo -n hello > $out" '()))
178 (hash (sha256 (string->utf8 "hello")))
179 (drv-path1 (derivation %store "fixed" (%current-system)
180 %bash `(,builder1)
181 '() `()
182 #:hash hash #:hash-algo 'sha256))
183 (drv-path2 (derivation %store "fixed" (%current-system)
184 %bash `(,builder2)
185 '() `()
186 #:hash hash #:hash-algo 'sha256))
187 (succeeded? (build-derivations %store
188 (list drv-path1 drv-path2))))
189 (and succeeded?
190 (equal? (derivation-path->output-path drv-path1)
191 (derivation-path->output-path drv-path2)))))
192
193 (test-assert "derivation with a fixed-output input"
194 ;; A derivation D using a fixed-output derivation F doesn't has the same
195 ;; output path when passed F or F', as long as F and F' have the same output
196 ;; path.
197 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
198 "echo -n hello > $out" '()))
199 (builder2 (add-text-to-store %store "fixed-builder2.sh"
200 "echo hey; echo -n hello > $out" '()))
201 (hash (sha256 (string->utf8 "hello")))
202 (fixed1 (derivation %store "fixed" (%current-system)
203 %bash `(,builder1)
204 '() `()
205 #:hash hash #:hash-algo 'sha256))
206 (fixed2 (derivation %store "fixed" (%current-system)
207 %bash `(,builder2)
208 '() `()
209 #:hash hash #:hash-algo 'sha256))
210 (fixed-out (derivation-path->output-path fixed1))
211 (builder3 (add-text-to-store
212 %store "final-builder.sh"
213 ;; Use Bash hackery to avoid Coreutils.
214 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
215 (final1 (derivation %store "final" (%current-system)
216 %bash `(,builder3)
217 `(("in" . ,fixed-out))
218 `((,builder3) (,fixed1))))
219 (final2 (derivation %store "final" (%current-system)
220 %bash `(,builder3)
221 `(("in" . ,fixed-out))
222 `((,builder3) (,fixed2))))
223 (succeeded? (build-derivations %store
224 (list final1 final2))))
225 (and succeeded?
226 (equal? (derivation-path->output-path final1)
227 (derivation-path->output-path final2)))))
228
229 (test-assert "multiple-output derivation"
230 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
231 "echo one > $out ; echo two > $second"
232 '()))
233 (drv-path (derivation %store "fixed" (%current-system)
234 %bash `(,builder)
235 '(("HOME" . "/homeless")
236 ("zzz" . "Z!")
237 ("AAA" . "A!"))
238 `((,builder))
239 #:outputs '("out" "second")))
240 (succeeded? (build-derivations %store (list drv-path))))
241 (and succeeded?
242 (let ((one (derivation-path->output-path drv-path "out"))
243 (two (derivation-path->output-path drv-path "second")))
244 (and (lset= equal?
245 (derivation-path->output-paths drv-path)
246 `(("out" . ,one) ("second" . ,two)))
247 (eq? 'one (call-with-input-file one read))
248 (eq? 'two (call-with-input-file two read)))))))
249
250 (test-assert "multiple-output derivation, non-alphabetic order"
251 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
252 ;; path computation must reorder them first.
253 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
254 "echo one > $out ; echo two > $AAA"
255 '()))
256 (drv-path (derivation %store "fixed" (%current-system)
257 %bash `(,builder)
258 '()
259 `((,builder))
260 #:outputs '("out" "AAA")))
261 (succeeded? (build-derivations %store (list drv-path))))
262 (and succeeded?
263 (let ((one (derivation-path->output-path drv-path "out"))
264 (two (derivation-path->output-path drv-path "AAA")))
265 (and (eq? 'one (call-with-input-file one read))
266 (eq? 'two (call-with-input-file two read)))))))
267
268 (test-assert "user of multiple-output derivation"
269 ;; Check whether specifying several inputs coming from the same
270 ;; multiple-output derivation works.
271 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
272 "echo one > $out ; echo two > $two"
273 '()))
274 (mdrv (derivation %store "multiple-output" (%current-system)
275 %bash `(,builder1)
276 '()
277 `((,builder1))
278 #:outputs '("out" "two")))
279 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
280 "read x < $one;
281 read y < $two;
282 echo \"($x $y)\" > $out"
283 '()))
284 (udrv (derivation %store "multiple-output-user"
285 (%current-system)
286 %bash `(,builder2)
287 `(("one" . ,(derivation-path->output-path
288 mdrv "out"))
289 ("two" . ,(derivation-path->output-path
290 mdrv "two")))
291 `((,builder2)
292 ;; two occurrences of MDRV:
293 (,mdrv)
294 (,mdrv "two")))))
295 (and (build-derivations %store (list (pk 'udrv udrv)))
296 (let ((p (derivation-path->output-path udrv)))
297 (and (valid-path? %store p)
298 (equal? '(one two) (call-with-input-file p read)))))))
299
300 \f
301 (define %coreutils
302 (false-if-exception
303 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
304 (or (package-derivation %store %bootstrap-coreutils&co)
305 (nixpkgs-derivation "coreutils")))))
306
307 (test-skip (if %coreutils 0 1))
308
309 (test-assert "build derivation with coreutils"
310 (let* ((builder
311 (add-text-to-store %store "build-with-coreutils.sh"
312 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
313 '()))
314 (drv-path
315 (derivation %store "foo" (%current-system)
316 %bash `(,builder)
317 `(("PATH" .
318 ,(string-append
319 (derivation-path->output-path %coreutils)
320 "/bin")))
321 `((,builder)
322 (,%coreutils))))
323 (succeeded?
324 (build-derivations %store (list drv-path))))
325 (and succeeded?
326 (let ((p (derivation-path->output-path drv-path)))
327 (and (valid-path? %store p)
328 (file-exists? (string-append p "/good")))))))
329
330 (test-skip (if (%guile-for-build) 0 7))
331
332 (test-assert "build-expression->derivation and derivation-prerequisites"
333 (let-values (((drv-path drv)
334 (build-expression->derivation %store "fail" (%current-system)
335 #f '())))
336 (any (match-lambda
337 (($ <derivation-input> path)
338 (string=? path (%guile-for-build))))
339 (derivation-prerequisites drv))))
340
341 (test-assert "build-expression->derivation without inputs"
342 (let* ((builder '(begin
343 (mkdir %output)
344 (call-with-output-file (string-append %output "/test")
345 (lambda (p)
346 (display '(hello guix) p)))))
347 (drv-path (build-expression->derivation %store "goo" (%current-system)
348 builder '()))
349 (succeeded? (build-derivations %store (list drv-path))))
350 (and succeeded?
351 (let ((p (derivation-path->output-path drv-path)))
352 (equal? '(hello guix)
353 (call-with-input-file (string-append p "/test") read))))))
354
355 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
356 (let-values (((drv-path drv)
357 (build-expression->derivation %store "fail" (%current-system)
358 #f '())))
359 ;; The only direct dependency is (%guile-for-build) and it's already
360 ;; built.
361 (null? (derivation-prerequisites-to-build %store drv))))
362
363 (test-assert "build-expression->derivation with expression returning #f"
364 (let* ((builder '(begin
365 (mkdir %output)
366 #f)) ; fail!
367 (drv-path (build-expression->derivation %store "fail" (%current-system)
368 builder '()))
369 (out-path (derivation-path->output-path drv-path)))
370 (guard (c ((nix-protocol-error? c)
371 ;; Note that the output path may exist at this point, but it
372 ;; is invalid.
373 (and (string-match "build .* failed"
374 (nix-protocol-error-message c))
375 (not (valid-path? %store out-path)))))
376 (build-derivations %store (list drv-path))
377 #f)))
378
379 (test-assert "build-expression->derivation with two outputs"
380 (let* ((builder '(begin
381 (call-with-output-file (assoc-ref %outputs "out")
382 (lambda (p)
383 (display '(hello) p)))
384 (call-with-output-file (assoc-ref %outputs "second")
385 (lambda (p)
386 (display '(world) p)))))
387 (drv-path (build-expression->derivation %store "double"
388 (%current-system)
389 builder '()
390 #:outputs '("out"
391 "second")))
392 (succeeded? (build-derivations %store (list drv-path))))
393 (and succeeded?
394 (let ((one (derivation-path->output-path drv-path))
395 (two (derivation-path->output-path drv-path "second")))
396 (and (equal? '(hello) (call-with-input-file one read))
397 (equal? '(world) (call-with-input-file two read)))))))
398
399 (test-skip (if %coreutils 0 1))
400 (test-assert "build-expression->derivation with one input"
401 (let* ((builder '(call-with-output-file %output
402 (lambda (p)
403 (let ((cu (assoc-ref %build-inputs "cu")))
404 (close 1)
405 (dup2 (port->fdes p) 1)
406 (execl (string-append cu "/bin/uname")
407 "uname" "-a")))))
408 (drv-path (build-expression->derivation %store "uname" (%current-system)
409 builder
410 `(("cu" ,%coreutils))))
411 (succeeded? (build-derivations %store (list drv-path))))
412 (and succeeded?
413 (let ((p (derivation-path->output-path drv-path)))
414 (string-contains (call-with-input-file p read-line) "GNU")))))
415
416 (test-assert "imported-files"
417 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
418 ("a/b/c" . ,(search-path %load-path
419 "guix/derivations.scm"))
420 ("p/q" . ,(search-path %load-path "guix.scm"))
421 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
422 (drv-path (imported-files %store files)))
423 (and (build-derivations %store (list drv-path))
424 (let ((dir (derivation-path->output-path drv-path)))
425 (every (match-lambda
426 ((path . source)
427 (equal? (call-with-input-file (string-append dir "/" path)
428 get-bytevector-all)
429 (call-with-input-file source
430 get-bytevector-all))))
431 files)))))
432
433 (test-assert "build-expression->derivation with modules"
434 (let* ((builder `(begin
435 (use-modules (guix build utils))
436 (let ((out (assoc-ref %outputs "out")))
437 (mkdir-p (string-append out "/guile/guix/nix"))
438 #t)))
439 (drv-path (build-expression->derivation %store
440 "test-with-modules"
441 (%current-system)
442 builder '()
443 #:modules
444 '((guix build utils)))))
445 (and (build-derivations %store (list drv-path))
446 (let* ((p (derivation-path->output-path drv-path))
447 (s (stat (string-append p "/guile/guix/nix"))))
448 (eq? (stat:type s) 'directory)))))
449
450 (test-assert "build-expression->derivation: same fixed-output path"
451 (let* ((builder1 '(call-with-output-file %output
452 (lambda (p)
453 (write "hello" p))))
454 (builder2 '(call-with-output-file (pk 'difference-here! %output)
455 (lambda (p)
456 (write "hello" p))))
457 (hash (sha256 (string->utf8 "hello")))
458 (input1 (build-expression->derivation %store "fixed"
459 (%current-system)
460 builder1 '()
461 #:hash hash
462 #:hash-algo 'sha256))
463 (input2 (build-expression->derivation %store "fixed"
464 (%current-system)
465 builder2 '()
466 #:hash hash
467 #:hash-algo 'sha256))
468 (succeeded? (build-derivations %store (list input1 input2))))
469 (and succeeded?
470 (not (string=? input1 input2))
471 (string=? (derivation-path->output-path input1)
472 (derivation-path->output-path input2)))))
473
474 (test-assert "build-expression->derivation with a fixed-output input"
475 (let* ((builder1 '(call-with-output-file %output
476 (lambda (p)
477 (write "hello" p))))
478 (builder2 '(call-with-output-file (pk 'difference-here! %output)
479 (lambda (p)
480 (write "hello" p))))
481 (hash (sha256 (string->utf8 "hello")))
482 (input1 (build-expression->derivation %store "fixed"
483 (%current-system)
484 builder1 '()
485 #:hash hash
486 #:hash-algo 'sha256))
487 (input2 (build-expression->derivation %store "fixed"
488 (%current-system)
489 builder2 '()
490 #:hash hash
491 #:hash-algo 'sha256))
492 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
493 (call-with-output-file %output
494 (lambda (out)
495 (format #f "My input is ~a.~%" input)))))
496 (final1 (build-expression->derivation %store "final"
497 (%current-system)
498 builder3
499 `(("input" ,input1))))
500 (final2 (build-expression->derivation %store "final"
501 (%current-system)
502 builder3
503 `(("input" ,input2)))))
504 (and (string=? (derivation-path->output-path final1)
505 (derivation-path->output-path final2))
506 (build-derivations %store (list final1 final2)))))
507
508 (test-end)
509
510 \f
511 (exit (= (test-runner-fail-count (test-runner-current)) 0))
512
513 ;;; Local Variables:
514 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
515 ;;; eval: (put 'guard 'scheme-indent-function 1)
516 ;;; End: