Update `HACKING'.
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
233e7676
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
341c6fdd 3;;;
233e7676 4;;; This file is part of GNU Guix.
341c6fdd 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
341c6fdd
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
341c6fdd
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
341c6fdd
LC
18
19
20(define-module (test-derivations)
21 #:use-module (guix derivations)
26bbbb95 22 #:use-module (guix store)
de4c3f26 23 #:use-module (guix utils)
ddc29a78 24 #:use-module (guix base32)
b272c474 25 #:use-module ((guix packages) #:select (package-derivation))
97d3998e 26 #:use-module ((distro) #:select (search-bootstrap-binary))
18633d4f 27 #:use-module (distro packages bootstrap)
b37eb5ed 28 #:use-module (srfi srfi-1)
fb3eec83 29 #:use-module (srfi srfi-11)
341c6fdd 30 #:use-module (srfi srfi-26)
99634e3f 31 #:use-module (srfi srfi-34)
341c6fdd 32 #:use-module (srfi srfi-64)
fb3eec83 33 #:use-module (rnrs io ports)
749c6567 34 #:use-module (rnrs bytevectors)
b37eb5ed 35 #:use-module (ice-9 rdelim)
db393b33 36 #:use-module (ice-9 regex)
99634e3f
LC
37 #:use-module (ice-9 ftw)
38 #:use-module (ice-9 match))
341c6fdd 39
26bbbb95
LC
40(define %store
41 (false-if-exception (open-connection)))
42
b272c474 43(when %store
81dbd783
LC
44 ;; Make sure we build everything by ourselves.
45 (set-build-options %store #:use-substitutes? #f)
46
b272c474
LC
47 ;; By default, use %BOOTSTRAP-GUILE for the current system.
48 (let ((drv (package-derivation %store %bootstrap-guile)))
49 (%guile-for-build drv)))
50
97d3998e
LC
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
b37eb5ed
LC
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
341c6fdd
LC
74(test-begin "derivations")
75
76(test-assert "parse & export"
33594aa4
LC
77 (let* ((f (search-path %load-path "tests/test.drv"))
78 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
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
29833b26 85(test-skip (if %store 0 11))
b37eb5ed 86
d1b1c424
LC
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)))
31ef99a8 91 (valid-path? %store drv)
d1b1c424
LC
92 (equal? (call-with-input-file file get-bytevector-all)
93 (call-with-input-file drv get-bytevector-all)))))
94
b37eb5ed
LC
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)))
31ef99a8 99 (valid-path? %store drv)
b37eb5ed
LC
100 (equal? (directory-contents dir)
101 (directory-contents drv)))))
26bbbb95
LC
102
103(test-assert "derivation with no inputs"
31ef99a8 104 (let* ((builder (add-text-to-store %store "my-builder.sh"
97d3998e 105 "echo hello, world\n"
31ef99a8 106 '()))
97d3998e
LC
107 (drv-path (derivation %store "foo" (%current-system)
108 %bash `("-e" ,builder)
109 '(("HOME" . "/homeless")) '())))
31ef99a8
LC
110 (and (store-path? drv-path)
111 (valid-path? %store drv-path))))
26bbbb95 112
fb3eec83
LC
113(test-assert "build derivation with 1 source"
114 (let*-values (((builder)
115 (add-text-to-store %store "my-builder.sh"
de4c3f26 116 "echo hello, world > \"$out\"\n"
fb3eec83
LC
117 '()))
118 ((drv-path drv)
98090557 119 (derivation %store "foo" (%current-system)
97d3998e 120 %bash `(,builder)
af7f9e5f
LC
121 '(("HOME" . "/homeless")
122 ("zzz" . "Z!")
123 ("AAA" . "A!"))
fb3eec83
LC
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"))))
31ef99a8
LC
130 (and (valid-path? %store path)
131 (string=? (call-with-input-file path read-line)
132 "hello, world"))))))
fb3eec83 133
860a6f1a
LC
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)
97d3998e 142 %bash `(,builder)
860a6f1a
LC
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
749c6567
LC
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")))
98090557 161 (drv-path (derivation %store "fixed" (%current-system)
97d3998e 162 %bash `(,builder)
813986ac
LC
163 '()
164 `((,builder)) ; optional
749c6567
LC
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)))
82058eff
LC
169 (and (equal? (string->utf8 "hello")
170 (call-with-input-file p get-bytevector-all))
171 (bytevector? (query-path-hash %store p)))))))
749c6567 172
813986ac
LC
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)
97d3998e 180 %bash `(,builder1)
813986ac
LC
181 '() `()
182 #:hash hash #:hash-algo 'sha256))
183 (drv-path2 (derivation %store "fixed" (%current-system)
97d3998e 184 %bash `(,builder2)
813986ac
LC
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)
97d3998e 203 %bash `(,builder1)
813986ac
LC
204 '() `()
205 #:hash hash #:hash-algo 'sha256))
206 (fixed2 (derivation %store "fixed" (%current-system)
97d3998e 207 %bash `(,builder2)
813986ac
LC
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)
97d3998e 216 %bash `(,builder3)
813986ac
LC
217 `(("in" . ,fixed-out))
218 `((,builder3) (,fixed1))))
219 (final2 (derivation %store "final" (%current-system)
97d3998e 220 %bash `(,builder3)
813986ac
LC
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
7946c4e7
LC
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 '()))
98090557 233 (drv-path (derivation %store "fixed" (%current-system)
97d3998e 234 %bash `(,builder)
7946c4e7
LC
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")))
7244a5f7
LC
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))
7946c4e7
LC
248 (eq? 'two (call-with-input-file two read)))))))
249
4b1786aa
LC
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)
97d3998e 257 %bash `(,builder)
4b1786aa
LC
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
d66ac374
LC
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)
97d3998e 275 %bash `(,builder1)
d66ac374
LC
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)
97d3998e 286 %bash `(,builder2)
d66ac374
LC
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
de4c3f26
LC
300\f
301(define %coreutils
8f3ecbd7 302 (false-if-exception
ad1ebab3
LC
303 (and (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)
304 (or (package-derivation %store %bootstrap-coreutils&co)
305 (nixpkgs-derivation "coreutils")))))
de4c3f26
LC
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
98090557 315 (derivation %store "foo" (%current-system)
97d3998e 316 %bash `(,builder)
de4c3f26
LC
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)))
31ef99a8
LC
327 (and (valid-path? %store p)
328 (file-exists? (string-append p "/good")))))))
de4c3f26 329
813986ac 330(test-skip (if (%guile-for-build) 0 7))
9a20830e
LC
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))))
d9085c23
LC
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)))))
98090557 347 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
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
9a20830e
LC
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
784bb1f3
LC
363(test-assert "derivation-prerequisites-to-build when outputs already present"
364 (let*-values (((builder)
365 '(begin (mkdir %output) #t))
366 ((input-drv-path input-drv)
367 (build-expression->derivation %store "input"
368 (%current-system)
369 builder '()))
370 ((input-path)
371 (derivation-output-path
372 (assoc-ref (derivation-outputs input-drv)
373 "out")))
374 ((drv-path drv)
375 (build-expression->derivation %store "something"
376 (%current-system)
377 builder
378 `(("i" ,input-drv-path))))
379 ((output)
380 (derivation-output-path
381 (assoc-ref (derivation-outputs drv) "out"))))
382 ;; Make sure these things are not already built.
383 (when (valid-path? %store input-path)
384 (delete-paths %store (list input-path)))
385 (when (valid-path? %store output)
386 (delete-paths %store (list output)))
387
388 (and (equal? (map derivation-input-path
389 (derivation-prerequisites-to-build %store drv))
390 (list input-drv-path))
391
392 ;; Build DRV and delete its input.
393 (build-derivations %store (list drv-path))
394 (delete-paths %store (list input-path))
395 (not (valid-path? %store input-path))
396
397 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
398 ;; prerequisite to build because DRV itself is already built.
399 (null? (derivation-prerequisites-to-build %store drv)))))
400
db393b33
LC
401(test-assert "build-expression->derivation with expression returning #f"
402 (let* ((builder '(begin
403 (mkdir %output)
404 #f)) ; fail!
405 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
406 builder '()))
407 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
408 (guard (c ((nix-protocol-error? c)
409 ;; Note that the output path may exist at this point, but it
410 ;; is invalid.
31ef99a8
LC
411 (and (string-match "build .* failed"
412 (nix-protocol-error-message c))
413 (not (valid-path? %store out-path)))))
db393b33
LC
414 (build-derivations %store (list drv-path))
415 #f)))
416
9bc07f4d
LC
417(test-assert "build-expression->derivation with two outputs"
418 (let* ((builder '(begin
419 (call-with-output-file (assoc-ref %outputs "out")
420 (lambda (p)
421 (display '(hello) p)))
422 (call-with-output-file (assoc-ref %outputs "second")
423 (lambda (p)
424 (display '(world) p)))))
425 (drv-path (build-expression->derivation %store "double"
98090557 426 (%current-system)
9bc07f4d
LC
427 builder '()
428 #:outputs '("out"
429 "second")))
430 (succeeded? (build-derivations %store (list drv-path))))
431 (and succeeded?
432 (let ((one (derivation-path->output-path drv-path))
433 (two (derivation-path->output-path drv-path "second")))
434 (and (equal? '(hello) (call-with-input-file one read))
435 (equal? '(world) (call-with-input-file two read)))))))
436
ad1ebab3 437(test-skip (if %coreutils 0 1))
d9085c23
LC
438(test-assert "build-expression->derivation with one input"
439 (let* ((builder '(call-with-output-file %output
440 (lambda (p)
441 (let ((cu (assoc-ref %build-inputs "cu")))
442 (close 1)
443 (dup2 (port->fdes p) 1)
444 (execl (string-append cu "/bin/uname")
445 "uname" "-a")))))
98090557 446 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 447 builder
2acb2cb6 448 `(("cu" ,%coreutils))))
d9085c23
LC
449 (succeeded? (build-derivations %store (list drv-path))))
450 (and succeeded?
451 (let ((p (derivation-path->output-path drv-path)))
452 (string-contains (call-with-input-file p read-line) "GNU")))))
453
99634e3f
LC
454(test-assert "imported-files"
455 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
456 ("a/b/c" . ,(search-path %load-path
457 "guix/derivations.scm"))
224f7ad6
LC
458 ("p/q" . ,(search-path %load-path "guix.scm"))
459 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
460 (drv-path (imported-files %store files)))
461 (and (build-derivations %store (list drv-path))
462 (let ((dir (derivation-path->output-path drv-path)))
463 (every (match-lambda
464 ((path . source)
465 (equal? (call-with-input-file (string-append dir "/" path)
466 get-bytevector-all)
467 (call-with-input-file source
468 get-bytevector-all))))
469 files)))))
470
d9024884
LC
471(test-assert "build-expression->derivation with modules"
472 (let* ((builder `(begin
473 (use-modules (guix build utils))
474 (let ((out (assoc-ref %outputs "out")))
475 (mkdir-p (string-append out "/guile/guix/nix"))
476 #t)))
477 (drv-path (build-expression->derivation %store
478 "test-with-modules"
479 (%current-system)
480 builder '()
481 #:modules
482 '((guix build utils)))))
483 (and (build-derivations %store (list drv-path))
484 (let* ((p (derivation-path->output-path drv-path))
485 (s (stat (string-append p "/guile/guix/nix"))))
486 (eq? (stat:type s) 'directory)))))
487
813986ac
LC
488(test-assert "build-expression->derivation: same fixed-output path"
489 (let* ((builder1 '(call-with-output-file %output
490 (lambda (p)
491 (write "hello" p))))
492 (builder2 '(call-with-output-file (pk 'difference-here! %output)
493 (lambda (p)
494 (write "hello" p))))
495 (hash (sha256 (string->utf8 "hello")))
496 (input1 (build-expression->derivation %store "fixed"
497 (%current-system)
498 builder1 '()
499 #:hash hash
500 #:hash-algo 'sha256))
501 (input2 (build-expression->derivation %store "fixed"
502 (%current-system)
503 builder2 '()
504 #:hash hash
505 #:hash-algo 'sha256))
506 (succeeded? (build-derivations %store (list input1 input2))))
507 (and succeeded?
508 (not (string=? input1 input2))
509 (string=? (derivation-path->output-path input1)
510 (derivation-path->output-path input2)))))
511
7bdd1f0e
LC
512(test-assert "build-expression->derivation with a fixed-output input"
513 (let* ((builder1 '(call-with-output-file %output
514 (lambda (p)
515 (write "hello" p))))
516 (builder2 '(call-with-output-file (pk 'difference-here! %output)
517 (lambda (p)
518 (write "hello" p))))
519 (hash (sha256 (string->utf8 "hello")))
520 (input1 (build-expression->derivation %store "fixed"
521 (%current-system)
522 builder1 '()
523 #:hash hash
524 #:hash-algo 'sha256))
525 (input2 (build-expression->derivation %store "fixed"
526 (%current-system)
527 builder2 '()
528 #:hash hash
529 #:hash-algo 'sha256))
530 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
531 (call-with-output-file %output
532 (lambda (out)
533 (format #f "My input is ~a.~%" input)))))
534 (final1 (build-expression->derivation %store "final"
535 (%current-system)
536 builder3
537 `(("input" ,input1))))
538 (final2 (build-expression->derivation %store "final"
539 (%current-system)
540 builder3
541 `(("input" ,input2)))))
542 (and (string=? (derivation-path->output-path final1)
543 (derivation-path->output-path final2))
544 (build-derivations %store (list final1 final2)))))
545
341c6fdd
LC
546(test-end)
547
548\f
549(exit (= (test-runner-fail-count (test-runner-current)) 0))
550
551;;; Local Variables:
552;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 553;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 554;;; End: