tests: Disable grafting by default for most tests.
[jackhill/guix/guix.git] / tests / derivations.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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 (define-module (test-derivations)
20 #:use-module (guix derivations)
21 #:use-module (guix grafts)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix hash)
25 #:use-module (guix base32)
26 #:use-module (guix tests)
27 #:use-module ((guix packages) #:select (package-derivation base32))
28 #:use-module ((guix build utils) #:select (executable-file?))
29 #:use-module ((gnu packages) #:select (search-bootstrap-binary))
30 #:use-module (gnu packages bootstrap)
31 #:use-module ((gnu packages guile) #:select (guile-1.8))
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-11)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-64)
37 #:use-module (rnrs io ports)
38 #:use-module (rnrs bytevectors)
39 #:use-module (web uri)
40 #:use-module (ice-9 rdelim)
41 #:use-module (ice-9 regex)
42 #:use-module (ice-9 ftw)
43 #:use-module (ice-9 match))
44
45 (define %store
46 (open-connection-for-tests))
47
48 ;; Globally disable grafts because they can trigger early builds.
49 (%graft? #f)
50
51 (define (bootstrap-binary name)
52 (let ((bin (search-bootstrap-binary name (%current-system))))
53 (and %store
54 (add-to-store %store name #t "sha256" bin))))
55
56 (define %bash
57 (bootstrap-binary "bash"))
58 (define %mkdir
59 (bootstrap-binary "mkdir"))
60
61 (define* (directory-contents dir #:optional (slurp get-bytevector-all))
62 "Return an alist representing the contents of DIR."
63 (define prefix-len (string-length dir))
64 (sort (file-system-fold (const #t) ; enter?
65 (lambda (path stat result) ; leaf
66 (alist-cons (string-drop path prefix-len)
67 (call-with-input-file path slurp)
68 result))
69 (lambda (path stat result) result) ; down
70 (lambda (path stat result) result) ; up
71 (lambda (path stat result) result) ; skip
72 (lambda (path stat errno result) result) ; error
73 '()
74 dir)
75 (lambda (e1 e2)
76 (string<? (car e1) (car e2)))))
77
78 \f
79 (test-begin "derivations")
80
81 (test-assert "parse & export"
82 (let* ((f (search-path %load-path "tests/test.drv"))
83 (b1 (call-with-input-file f get-bytevector-all))
84 (d1 (read-derivation (open-bytevector-input-port b1)))
85 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
86 (d2 (read-derivation (open-bytevector-input-port b2))))
87 (and (equal? b1 b2)
88 (equal? d1 d2))))
89
90 (test-skip (if %store 0 12))
91
92 (test-assert "add-to-store, flat"
93 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
94 (drv (add-to-store %store "flat-test" #f "sha256" file)))
95 (and (eq? 'regular (stat:type (stat drv)))
96 (valid-path? %store drv)
97 (equal? (call-with-input-file file get-bytevector-all)
98 (call-with-input-file drv get-bytevector-all)))))
99
100 (test-assert "add-to-store, recursive"
101 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
102 (drv (add-to-store %store "dir-tree-test" #t "sha256" dir)))
103 (and (eq? 'directory (stat:type (stat drv)))
104 (valid-path? %store drv)
105 (equal? (directory-contents dir)
106 (directory-contents drv)))))
107
108 (test-assert "derivation with no inputs"
109 (let* ((builder (add-text-to-store %store "my-builder.sh"
110 "echo hello, world\n"
111 '()))
112 (drv (derivation %store "foo"
113 %bash `("-e" ,builder)
114 #:env-vars '(("HOME" . "/homeless")))))
115 (and (store-path? (derivation-file-name drv))
116 (valid-path? %store (derivation-file-name drv)))))
117
118 (test-assert "build derivation with 1 source"
119 (let* ((builder (add-text-to-store %store "my-builder.sh"
120 "echo hello, world > \"$out\"\n"
121 '()))
122 (drv (derivation %store "foo"
123 %bash `(,builder)
124 #:env-vars '(("HOME" . "/homeless")
125 ("zzz" . "Z!")
126 ("AAA" . "A!"))
127 #:inputs `((,%bash) (,builder))))
128 (succeeded?
129 (build-derivations %store (list drv))))
130 (and succeeded?
131 (let ((path (derivation->output-path drv)))
132 (and (valid-path? %store path)
133 (string=? (call-with-input-file path read-line)
134 "hello, world"))))))
135
136 (test-assert "derivation with local file as input"
137 (let* ((builder (add-text-to-store
138 %store "my-builder.sh"
139 "(while read line ; do echo \"$line\" ; done) < $in > $out"
140 '()))
141 (input (search-path %load-path "ice-9/boot-9.scm"))
142 (input* (add-to-store %store (basename input)
143 #t "sha256" input))
144 (drv (derivation %store "derivation-with-input-file"
145 %bash `(,builder)
146
147 ;; Cheat to pass the actual file name to the
148 ;; builder.
149 #:env-vars `(("in" . ,input*))
150
151 #:inputs `((,%bash)
152 (,builder)
153 (,input))))) ; ← local file name
154 (and (build-derivations %store (list drv))
155 ;; Note: we can't compare the files because the above trick alters
156 ;; the contents.
157 (valid-path? %store (derivation->output-path drv)))))
158
159 (test-assert "derivation fails but keep going"
160 ;; In keep-going mode, 'build-derivations' should fail because of D1, but it
161 ;; must return only after D2 has succeeded.
162 (with-store store
163 (let* ((d1 (derivation %store "fails"
164 %bash `("-c" "false")
165 #:inputs `((,%bash))))
166 (d2 (build-expression->derivation %store "sleep-then-succeed"
167 `(begin
168 ,(random-text)
169 ;; XXX: Hopefully that's long
170 ;; enough that D1 has already
171 ;; failed.
172 (sleep 2)
173 (mkdir %output)))))
174 (set-build-options %store
175 #:use-substitutes? #f
176 #:keep-going? #t)
177 (guard (c ((nix-protocol-error? c)
178 (and (= 100 (nix-protocol-error-status c))
179 (string-contains (nix-protocol-error-message c)
180 (derivation-file-name d1))
181 (not (valid-path? %store (derivation->output-path d1)))
182 (valid-path? %store (derivation->output-path d2)))))
183 (build-derivations %store (list d1 d2))
184 #f))))
185
186 (test-assert "identical files are deduplicated"
187 (let* ((build1 (add-text-to-store %store "one.sh"
188 "echo hello, world > \"$out\"\n"
189 '()))
190 (build2 (add-text-to-store %store "two.sh"
191 "# Hey!\necho hello, world > \"$out\"\n"
192 '()))
193 (drv1 (derivation %store "foo"
194 %bash `(,build1)
195 #:inputs `((,%bash) (,build1))))
196 (drv2 (derivation %store "bar"
197 %bash `(,build2)
198 #:inputs `((,%bash) (,build2)))))
199 (and (build-derivations %store (list drv1 drv2))
200 (let ((file1 (derivation->output-path drv1))
201 (file2 (derivation->output-path drv2)))
202 (and (valid-path? %store file1) (valid-path? %store file2)
203 (string=? (call-with-input-file file1 get-string-all)
204 "hello, world\n")
205 (= (stat:ino (lstat file1))
206 (stat:ino (lstat file2))))))))
207
208 (test-equal "derivation-name"
209 "foo-0.0"
210 (let ((drv (derivation %store "foo-0.0" %bash '())))
211 (derivation-name drv)))
212
213 (test-equal "derivation-output-names"
214 '(("out") ("bar" "chbouib"))
215 (let ((drv1 (derivation %store "foo-0.0" %bash '()))
216 (drv2 (derivation %store "foo-0.0" %bash '()
217 #:outputs '("bar" "chbouib"))))
218 (list (derivation-output-names drv1)
219 (derivation-output-names drv2))))
220
221 (test-assert "offloadable-derivation?"
222 (and (offloadable-derivation? (derivation %store "foo" %bash '()))
223 (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
224 (derivation %store "foo" %bash '()
225 #:substitutable? #f))
226 (not (offloadable-derivation?
227 (derivation %store "foo" %bash '()
228 #:local-build? #t)))))
229
230 (test-assert "substitutable-derivation?"
231 (and (substitutable-derivation? (derivation %store "foo" %bash '()))
232 (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
233 (derivation %store "foo" %bash '()
234 #:local-build? #t))
235 (not (substitutable-derivation?
236 (derivation %store "foo" %bash '()
237 #:substitutable? #f)))))
238
239 (test-assert "fixed-output-derivation?"
240 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
241 "echo -n hello > $out" '()))
242 (hash (sha256 (string->utf8 "hello")))
243 (drv (derivation %store "fixed"
244 %bash `(,builder)
245 #:inputs `((,builder))
246 #:hash hash #:hash-algo 'sha256)))
247 (fixed-output-derivation? drv)))
248
249 (test-assert "fixed-output derivation"
250 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
251 "echo -n hello > $out" '()))
252 (hash (sha256 (string->utf8 "hello")))
253 (drv (derivation %store "fixed"
254 %bash `(,builder)
255 #:inputs `((,builder)) ; optional
256 #:hash hash #:hash-algo 'sha256))
257 (succeeded? (build-derivations %store (list drv))))
258 (and succeeded?
259 (let ((p (derivation->output-path drv)))
260 (and (equal? (string->utf8 "hello")
261 (call-with-input-file p get-bytevector-all))
262 (bytevector? (query-path-hash %store p)))))))
263
264 (test-assert "fixed-output derivation: output paths are equal"
265 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
266 "echo -n hello > $out" '()))
267 (builder2 (add-text-to-store %store "fixed-builder2.sh"
268 "echo hey; echo -n hello > $out" '()))
269 (hash (sha256 (string->utf8 "hello")))
270 (drv1 (derivation %store "fixed"
271 %bash `(,builder1)
272 #:hash hash #:hash-algo 'sha256))
273 (drv2 (derivation %store "fixed"
274 %bash `(,builder2)
275 #:hash hash #:hash-algo 'sha256))
276 (succeeded? (build-derivations %store (list drv1 drv2))))
277 (and succeeded?
278 (equal? (derivation->output-path drv1)
279 (derivation->output-path drv2)))))
280
281 (test-assert "fixed-output derivation, recursive"
282 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
283 "echo -n hello > $out" '()))
284 (hash (sha256 (string->utf8 "hello")))
285 (drv (derivation %store "fixed-rec"
286 %bash `(,builder)
287 #:inputs `((,builder))
288 #:hash (base32 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
289 #:hash-algo 'sha256
290 #:recursive? #t))
291 (succeeded? (build-derivations %store (list drv))))
292 (and succeeded?
293 (let ((p (derivation->output-path drv)))
294 (and (equal? (string->utf8 "hello")
295 (call-with-input-file p get-bytevector-all))
296 (bytevector? (query-path-hash %store p)))))))
297
298 (test-assert "derivation with a fixed-output input"
299 ;; A derivation D using a fixed-output derivation F doesn't has the same
300 ;; output path when passed F or F', as long as F and F' have the same output
301 ;; path.
302 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
303 "echo -n hello > $out" '()))
304 (builder2 (add-text-to-store %store "fixed-builder2.sh"
305 "echo hey; echo -n hello > $out" '()))
306 (hash (sha256 (string->utf8 "hello")))
307 (fixed1 (derivation %store "fixed"
308 %bash `(,builder1)
309 #:hash hash #:hash-algo 'sha256))
310 (fixed2 (derivation %store "fixed"
311 %bash `(,builder2)
312 #:hash hash #:hash-algo 'sha256))
313 (fixed-out (derivation->output-path fixed1))
314 (builder3 (add-text-to-store
315 %store "final-builder.sh"
316 ;; Use Bash hackery to avoid Coreutils.
317 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
318 (final1 (derivation %store "final"
319 %bash `(,builder3)
320 #:env-vars `(("in" . ,fixed-out))
321 #:inputs `((,%bash) (,builder3) (,fixed1))))
322 (final2 (derivation %store "final"
323 %bash `(,builder3)
324 #:env-vars `(("in" . ,fixed-out))
325 #:inputs `((,%bash) (,builder3) (,fixed2))))
326 (succeeded? (build-derivations %store
327 (list final1 final2))))
328 (and succeeded?
329 (equal? (derivation->output-path final1)
330 (derivation->output-path final2)))))
331
332 (test-assert "multiple-output derivation"
333 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
334 "echo one > $out ; echo two > $second"
335 '()))
336 (drv (derivation %store "fixed"
337 %bash `(,builder)
338 #:env-vars '(("HOME" . "/homeless")
339 ("zzz" . "Z!")
340 ("AAA" . "A!"))
341 #:inputs `((,%bash) (,builder))
342 #:outputs '("out" "second")))
343 (succeeded? (build-derivations %store (list drv))))
344 (and succeeded?
345 (let ((one (derivation->output-path drv "out"))
346 (two (derivation->output-path drv "second")))
347 (and (lset= equal?
348 (derivation->output-paths drv)
349 `(("out" . ,one) ("second" . ,two)))
350 (eq? 'one (call-with-input-file one read))
351 (eq? 'two (call-with-input-file two read)))))))
352
353 (test-assert "multiple-output derivation, non-alphabetic order"
354 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
355 ;; path computation must reorder them first.
356 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
357 "echo one > $out ; echo two > $AAA"
358 '()))
359 (drv (derivation %store "fixed"
360 %bash `(,builder)
361 #:inputs `((,%bash) (,builder))
362 #:outputs '("out" "AAA")))
363 (succeeded? (build-derivations %store (list drv))))
364 (and succeeded?
365 (let ((one (derivation->output-path drv "out"))
366 (two (derivation->output-path drv "AAA")))
367 (and (eq? 'one (call-with-input-file one read))
368 (eq? 'two (call-with-input-file two read)))))))
369
370 (test-assert "multiple-output derivation, derivation-path->output-path"
371 (let* ((builder (add-text-to-store %store "builder.sh"
372 "echo one > $out ; echo two > $second"
373 '()))
374 (drv (derivation %store "multiple"
375 %bash `(,builder)
376 #:outputs '("out" "second")))
377 (drv-file (derivation-file-name drv))
378 (one (derivation->output-path drv "out"))
379 (two (derivation->output-path drv "second"))
380 (first (derivation-path->output-path drv-file "out"))
381 (second (derivation-path->output-path drv-file "second")))
382 (and (not (string=? one two))
383 (string-suffix? "-second" two)
384 (string=? first one)
385 (string=? second two))))
386
387 (test-assert "user of multiple-output derivation"
388 ;; Check whether specifying several inputs coming from the same
389 ;; multiple-output derivation works.
390 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
391 "echo one > $out ; echo two > $two"
392 '()))
393 (mdrv (derivation %store "multiple-output"
394 %bash `(,builder1)
395 #:inputs `((,%bash) (,builder1))
396 #:outputs '("out" "two")))
397 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
398 "read x < $one;
399 read y < $two;
400 echo \"($x $y)\" > $out"
401 '()))
402 (udrv (derivation %store "multiple-output-user"
403 %bash `(,builder2)
404 #:env-vars `(("one"
405 . ,(derivation->output-path
406 mdrv "out"))
407 ("two"
408 . ,(derivation->output-path
409 mdrv "two")))
410 #:inputs `((,%bash)
411 (,builder2)
412 ;; two occurrences of MDRV:
413 (,mdrv)
414 (,mdrv "two")))))
415 (and (build-derivations %store (list (pk 'udrv udrv)))
416 (let ((p (derivation->output-path udrv)))
417 (and (valid-path? %store p)
418 (equal? '(one two) (call-with-input-file p read)))))))
419
420 (test-assert "derivation with #:references-graphs"
421 (let* ((input1 (add-text-to-store %store "foo" "hello"
422 (list %bash)))
423 (input2 (add-text-to-store %store "bar"
424 (number->string (random 7777))
425 (list input1)))
426 (builder (add-text-to-store %store "build-graph"
427 (format #f "
428 ~a $out
429 (while read l ; do echo $l ; done) < bash > $out/bash
430 (while read l ; do echo $l ; done) < input1 > $out/input1
431 (while read l ; do echo $l ; done) < input2 > $out/input2"
432 %mkdir)
433 (list %mkdir)))
434 (drv (derivation %store "closure-graphs"
435 %bash `(,builder)
436 #:references-graphs
437 `(("bash" . ,%bash)
438 ("input1" . ,input1)
439 ("input2" . ,input2))
440 #:inputs `((,%bash) (,builder))))
441 (out (derivation->output-path drv)))
442 (define (deps path . deps)
443 (let ((count (length deps)))
444 (string-append path "\n\n" (number->string count) "\n"
445 (string-join (sort deps string<?) "\n")
446 (if (zero? count) "" "\n"))))
447
448 (and (build-derivations %store (list drv))
449 (equal? (directory-contents out get-string-all)
450 `(("/bash" . ,(string-append %bash "\n\n0\n"))
451 ("/input1" . ,(if (string>? input1 %bash)
452 (string-append (deps %bash)
453 (deps input1 %bash))
454 (string-append (deps input1 %bash)
455 (deps %bash))))
456 ("/input2" . ,(string-concatenate
457 (map cdr
458 (sort
459 (map (lambda (p d)
460 (cons p (apply deps p d)))
461 (list %bash input1 input2)
462 (list '() (list %bash) (list input1)))
463 (lambda (x y)
464 (match x
465 ((p1 . _)
466 (match y
467 ((p2 . _)
468 (string<? p1 p2)))))))))))))))
469
470 (test-assert "derivation #:allowed-references, ok"
471 (let ((drv (derivation %store "allowed" %bash
472 '("-c" "echo hello > $out")
473 #:inputs `((,%bash))
474 #:allowed-references '())))
475 (build-derivations %store (list drv))))
476
477 (test-assert "derivation #:allowed-references, not allowed"
478 (let* ((txt (add-text-to-store %store "foo" "Hello, world."))
479 (drv (derivation %store "disallowed" %bash
480 `("-c" ,(string-append "echo " txt "> $out"))
481 #:inputs `((,%bash) (,txt))
482 #:allowed-references '())))
483 (guard (c ((nix-protocol-error? c)
484 ;; There's no specific error message to check for.
485 #t))
486 (build-derivations %store (list drv))
487 #f)))
488
489 (test-assert "derivation #:allowed-references, self allowed"
490 (let ((drv (derivation %store "allowed" %bash
491 '("-c" "echo $out > $out")
492 #:inputs `((,%bash))
493 #:allowed-references '("out"))))
494 (build-derivations %store (list drv))))
495
496 (test-assert "derivation #:allowed-references, self not allowed"
497 (let ((drv (derivation %store "disallowed" %bash
498 `("-c" ,"echo $out > $out")
499 #:inputs `((,%bash))
500 #:allowed-references '())))
501 (guard (c ((nix-protocol-error? c)
502 ;; There's no specific error message to check for.
503 #t))
504 (build-derivations %store (list drv))
505 #f)))
506
507 ;; Here we should get the value of $NIX_STATE_DIR that the daemon sees, which
508 ;; is a unique value for each test process; this value is the same as the one
509 ;; we see in the process executing this file since it is set by 'test-env'.
510 (test-equal "derivation #:leaked-env-vars"
511 (getenv "NIX_STATE_DIR")
512 (let* ((value (getenv "NIX_STATE_DIR"))
513 (drv (derivation %store "leaked-env-vars" %bash
514 '("-c" "echo -n $NIX_STATE_DIR > $out")
515 #:hash (sha256 (string->utf8 value))
516 #:hash-algo 'sha256
517 #:inputs `((,%bash))
518 #:leaked-env-vars '("NIX_STATE_DIR"))))
519 (and (build-derivations %store (list drv))
520 (call-with-input-file (derivation->output-path drv)
521 get-string-all))))
522
523 \f
524 (define %coreutils
525 (false-if-exception
526 (and (network-reachable?)
527 (package-derivation %store %bootstrap-coreutils&co))))
528
529 (test-skip (if %coreutils 0 1))
530
531 (test-assert "build derivation with coreutils"
532 (let* ((builder
533 (add-text-to-store %store "build-with-coreutils.sh"
534 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
535 '()))
536 (drv
537 (derivation %store "foo"
538 %bash `(,builder)
539 #:env-vars `(("PATH" .
540 ,(string-append
541 (derivation->output-path %coreutils)
542 "/bin")))
543 #:inputs `((,builder)
544 (,%coreutils))))
545 (succeeded?
546 (build-derivations %store (list drv))))
547 (and succeeded?
548 (let ((p (derivation->output-path drv)))
549 (and (valid-path? %store p)
550 (file-exists? (string-append p "/good")))))))
551
552 (test-skip (if (%guile-for-build) 0 8))
553
554 (test-assert "build-expression->derivation and derivation-prerequisites"
555 (let ((drv (build-expression->derivation %store "fail" #f)))
556 (any (match-lambda
557 (($ <derivation-input> path)
558 (string=? path (derivation-file-name (%guile-for-build)))))
559 (derivation-prerequisites drv))))
560
561 (test-assert "derivation-prerequisites and valid-derivation-input?"
562 (let* ((a (build-expression->derivation %store "a" '(mkdir %output)))
563 (b (build-expression->derivation %store "b" `(list ,(random-text))))
564 (c (build-expression->derivation %store "c" `(mkdir %output)
565 #:inputs `(("a" ,a) ("b" ,b)))))
566 ;; Make sure both A and %BOOTSTRAP-GUILE are built (the latter could have
567 ;; be removed by tests/guix-gc.sh.)
568 (build-derivations %store
569 (list a (package-derivation %store %bootstrap-guile)))
570
571 (match (derivation-prerequisites c
572 (cut valid-derivation-input? %store
573 <>))
574 ((($ <derivation-input> file ("out")))
575 (string=? file (derivation-file-name b)))
576 (x
577 (pk 'fail x #f)))))
578
579 (test-assert "build-expression->derivation without inputs"
580 (let* ((builder '(begin
581 (mkdir %output)
582 (call-with-output-file (string-append %output "/test")
583 (lambda (p)
584 (display '(hello guix) p)))))
585 (drv (build-expression->derivation %store "goo" builder))
586 (succeeded? (build-derivations %store (list drv))))
587 (and succeeded?
588 (let ((p (derivation->output-path drv)))
589 (equal? '(hello guix)
590 (call-with-input-file (string-append p "/test") read))))))
591
592 (test-assert "build-expression->derivation and max-silent-time"
593 (let* ((store (let ((s (open-connection)))
594 (set-build-options s #:max-silent-time 1)
595 s))
596 (builder '(begin (sleep 100) (mkdir %output) #t))
597 (drv (build-expression->derivation store "silent" builder))
598 (out-path (derivation->output-path drv)))
599 (guard (c ((nix-protocol-error? c)
600 (and (string-contains (nix-protocol-error-message c)
601 "failed")
602 (not (valid-path? store out-path)))))
603 (build-derivations store (list drv))
604 #f)))
605
606 (test-assert "build-expression->derivation and timeout"
607 (let* ((store (let ((s (open-connection)))
608 (set-build-options s #:timeout 1)
609 s))
610 (builder '(begin (sleep 100) (mkdir %output) #t))
611 (drv (build-expression->derivation store "slow" builder))
612 (out-path (derivation->output-path drv)))
613 (guard (c ((nix-protocol-error? c)
614 (and (string-contains (nix-protocol-error-message c)
615 "failed")
616 (not (valid-path? store out-path)))))
617 (build-derivations store (list drv))
618 #f)))
619
620 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
621 (let ((drv (build-expression->derivation %store "fail" #f)))
622 ;; The only direct dependency is (%guile-for-build) and it's already
623 ;; built.
624 (null? (derivation-prerequisites-to-build %store drv))))
625
626 (test-assert "derivation-prerequisites-to-build when outputs already present"
627 (let* ((builder '(begin (mkdir %output) #t))
628 (input-drv (build-expression->derivation %store "input" builder))
629 (input-path (derivation-output-path
630 (assoc-ref (derivation-outputs input-drv)
631 "out")))
632 (drv (build-expression->derivation %store "something" builder
633 #:inputs
634 `(("i" ,input-drv))))
635 (output (derivation->output-path drv)))
636 ;; Make sure these things are not already built.
637 (when (valid-path? %store input-path)
638 (delete-paths %store (list input-path)))
639 (when (valid-path? %store output)
640 (delete-paths %store (list output)))
641
642 (and (equal? (map derivation-input-path
643 (derivation-prerequisites-to-build %store drv))
644 (list (derivation-file-name input-drv)))
645
646 ;; Build DRV and delete its input.
647 (build-derivations %store (list drv))
648 (delete-paths %store (list input-path))
649 (not (valid-path? %store input-path))
650
651 ;; Now INPUT-PATH is missing, yet it shouldn't be listed as a
652 ;; prerequisite to build because DRV itself is already built.
653 (null? (derivation-prerequisites-to-build %store drv)))))
654
655 (test-assert "derivation-prerequisites-to-build and substitutes"
656 (let* ((store (open-connection))
657 (drv (build-expression->derivation store "prereq-subst"
658 (random 1000)))
659 (output (derivation->output-path drv)))
660
661 ;; Make sure substitutes are usable.
662 (set-build-options store #:use-substitutes? #t
663 #:substitute-urls (%test-substitute-urls))
664
665 (with-derivation-narinfo drv
666 (let-values (((build download)
667 (derivation-prerequisites-to-build store drv))
668 ((build* download*)
669 (derivation-prerequisites-to-build store drv
670 #:substitutable?
671 (const #f))))
672 (and (null? build)
673 (equal? download (list output))
674 (null? download*)
675 (null? build*))))))
676
677 (test-assert "derivation-prerequisites-to-build and substitutes, non-substitutable build"
678 (let* ((store (open-connection))
679 (drv (build-expression->derivation store "prereq-no-subst"
680 (random 1000)
681 #:substitutable? #f))
682 (output (derivation->output-path drv)))
683
684 ;; Make sure substitutes are usable.
685 (set-build-options store #:use-substitutes? #t
686 #:substitute-urls (%test-substitute-urls))
687
688 (with-derivation-narinfo drv
689 (let-values (((build download)
690 (derivation-prerequisites-to-build store drv)))
691 ;; Despite being available as a substitute, DRV will be built locally
692 ;; due to #:substitutable? #f.
693 (and (null? download)
694 (match build
695 (((? derivation-input? input))
696 (string=? (derivation-input-path input)
697 (derivation-file-name drv)))))))))
698
699 (test-assert "derivation-prerequisites-to-build and substitutes, local build"
700 (with-store store
701 (let* ((drv (build-expression->derivation store "prereq-subst-local"
702 (random 1000)
703 #:local-build? #t))
704 (output (derivation->output-path drv)))
705
706 ;; Make sure substitutes are usable.
707 (set-build-options store #:use-substitutes? #t
708 #:substitute-urls (%test-substitute-urls))
709
710 (with-derivation-narinfo drv
711 (let-values (((build download)
712 (derivation-prerequisites-to-build store drv)))
713 ;; #:local-build? is *not* synonymous with #:substitutable?, so we
714 ;; must be able to substitute DRV's output.
715 ;; See <http://bugs.gnu.org/18747>.
716 (and (null? build)
717 (match download
718 (((? string? item))
719 (string=? item (derivation->output-path drv))))))))))
720
721 (test-assert "derivation-prerequisites-to-build in 'check' mode"
722 (with-store store
723 (let* ((dep (build-expression->derivation store "dep"
724 `(begin ,(random-text)
725 (mkdir %output))))
726 (drv (build-expression->derivation store "to-check"
727 '(mkdir %output)
728 #:inputs `(("dep" ,dep)))))
729 (build-derivations store (list drv))
730 (delete-paths store (list (derivation->output-path dep)))
731
732 ;; In 'check' mode, DEP must be rebuilt.
733 (and (null? (derivation-prerequisites-to-build store drv))
734 (match (derivation-prerequisites-to-build store drv
735 #:mode (build-mode
736 check))
737 ((input)
738 (string=? (derivation-input-path input)
739 (derivation-file-name dep))))))))
740
741 (test-assert "build-expression->derivation with expression returning #f"
742 (let* ((builder '(begin
743 (mkdir %output)
744 #f)) ; fail!
745 (drv (build-expression->derivation %store "fail" builder))
746 (out-path (derivation->output-path drv)))
747 (guard (c ((nix-protocol-error? c)
748 ;; Note that the output path may exist at this point, but it
749 ;; is invalid.
750 (and (string-match "build .* failed"
751 (nix-protocol-error-message c))
752 (not (valid-path? %store out-path)))))
753 (build-derivations %store (list drv))
754 #f)))
755
756 (test-assert "build-expression->derivation with two outputs"
757 (let* ((builder '(begin
758 (call-with-output-file (assoc-ref %outputs "out")
759 (lambda (p)
760 (display '(hello) p)))
761 (call-with-output-file (assoc-ref %outputs "second")
762 (lambda (p)
763 (display '(world) p)))))
764 (drv (build-expression->derivation %store "double" builder
765 #:outputs '("out"
766 "second")))
767 (succeeded? (build-derivations %store (list drv))))
768 (and succeeded?
769 (let ((one (derivation->output-path drv))
770 (two (derivation->output-path drv "second")))
771 (and (equal? '(hello) (call-with-input-file one read))
772 (equal? '(world) (call-with-input-file two read)))))))
773
774 (test-skip (if %coreutils 0 1))
775 (test-assert "build-expression->derivation with one input"
776 (let* ((builder '(call-with-output-file %output
777 (lambda (p)
778 (let ((cu (assoc-ref %build-inputs "cu")))
779 (close 1)
780 (dup2 (port->fdes p) 1)
781 (execl (string-append cu "/bin/uname")
782 "uname" "-a")))))
783 (drv (build-expression->derivation %store "uname" builder
784 #:inputs
785 `(("cu" ,%coreutils))))
786 (succeeded? (build-derivations %store (list drv))))
787 (and succeeded?
788 (let ((p (derivation->output-path drv)))
789 (string-contains (call-with-input-file p read-line) "GNU")))))
790
791 (test-assert "build-expression->derivation with modules"
792 (let* ((builder `(begin
793 (use-modules (guix build utils))
794 (let ((out (assoc-ref %outputs "out")))
795 (mkdir-p (string-append out "/guile/guix/nix"))
796 #t)))
797 (drv (build-expression->derivation %store "test-with-modules"
798 builder
799 #:modules
800 '((guix build utils)))))
801 (and (build-derivations %store (list drv))
802 (let* ((p (derivation->output-path drv))
803 (s (stat (string-append p "/guile/guix/nix"))))
804 (eq? (stat:type s) 'directory)))))
805
806 (test-assert "build-expression->derivation: same fixed-output path"
807 (let* ((builder1 '(call-with-output-file %output
808 (lambda (p)
809 (write "hello" p))))
810 (builder2 '(call-with-output-file (pk 'difference-here! %output)
811 (lambda (p)
812 (write "hello" p))))
813 (hash (sha256 (string->utf8 "hello")))
814 (input1 (build-expression->derivation %store "fixed" builder1
815 #:hash hash
816 #:hash-algo 'sha256))
817 (input2 (build-expression->derivation %store "fixed" builder2
818 #:hash hash
819 #:hash-algo 'sha256))
820 (succeeded? (build-derivations %store (list input1 input2))))
821 (and succeeded?
822 (not (string=? (derivation-file-name input1)
823 (derivation-file-name input2)))
824 (string=? (derivation->output-path input1)
825 (derivation->output-path input2)))))
826
827 (test-assert "build-expression->derivation with a fixed-output input"
828 (let* ((builder1 '(call-with-output-file %output
829 (lambda (p)
830 (write "hello" p))))
831 (builder2 '(call-with-output-file (pk 'difference-here! %output)
832 (lambda (p)
833 (write "hello" p))))
834 (hash (sha256 (string->utf8 "hello")))
835 (input1 (build-expression->derivation %store "fixed" builder1
836 #:hash hash
837 #:hash-algo 'sha256))
838 (input2 (build-expression->derivation %store "fixed" builder2
839 #:hash hash
840 #:hash-algo 'sha256))
841 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
842 (call-with-output-file %output
843 (lambda (out)
844 (format #f "My input is ~a.~%" input)))))
845 (final1 (build-expression->derivation %store "final" builder3
846 #:inputs
847 `(("input" ,input1))))
848 (final2 (build-expression->derivation %store "final" builder3
849 #:inputs
850 `(("input" ,input2)))))
851 (and (string=? (derivation->output-path final1)
852 (derivation->output-path final2))
853 (string=? (derivation->output-path final1)
854 (derivation-path->output-path
855 (derivation-file-name final1)))
856 (build-derivations %store (list final1 final2)))))
857
858 (test-assert "build-expression->derivation produces recursive fixed-output"
859 (let* ((builder '(begin
860 (use-modules (srfi srfi-26))
861 (mkdir %output)
862 (chdir %output)
863 (call-with-output-file "exe"
864 (cut display "executable" <>))
865 (chmod "exe" #o777)
866 (symlink "exe" "symlink")
867 (mkdir "subdir")))
868 (drv (build-expression->derivation %store "fixed-rec" builder
869 #:hash-algo 'sha256
870 #:hash (base32
871 "10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p")
872 #:recursive? #t)))
873 (and (build-derivations %store (list drv))
874 (let* ((dir (derivation->output-path drv))
875 (exe (string-append dir "/exe"))
876 (link (string-append dir "/symlink"))
877 (subdir (string-append dir "/subdir")))
878 (and (executable-file? exe)
879 (string=? "executable"
880 (call-with-input-file exe get-string-all))
881 (string=? "exe" (readlink link))
882 (file-is-directory? subdir))))))
883
884 (test-assert "build-expression->derivation uses recursive fixed-output"
885 (let* ((builder '(call-with-output-file %output
886 (lambda (port)
887 (display "hello" port))))
888 (fixed (build-expression->derivation %store "small-fixed-rec"
889 builder
890 #:hash-algo 'sha256
891 #:hash (base32
892 "0sg9f58l1jj88w6pdrfdpj5x9b1zrwszk84j81zvby36q9whhhqa")
893 #:recursive? #t))
894 (in (derivation->output-path fixed))
895 (builder `(begin
896 (mkdir %output)
897 (chdir %output)
898 (symlink ,in "symlink")))
899 (drv (build-expression->derivation %store "fixed-rec-user"
900 builder
901 #:inputs `(("fixed" ,fixed)))))
902 (and (build-derivations %store (list drv))
903 (let ((out (derivation->output-path drv)))
904 (string=? (readlink (string-append out "/symlink")) in)))))
905
906 (test-assert "build-expression->derivation with #:references-graphs"
907 (let* ((input (add-text-to-store %store "foo" "hello"
908 (list %bash %mkdir)))
909 (builder '(copy-file "input" %output))
910 (drv (build-expression->derivation %store "references-graphs"
911 builder
912 #:references-graphs
913 `(("input" . ,input))))
914 (out (derivation->output-path drv)))
915 (define (deps path . deps)
916 (let ((count (length deps)))
917 (string-append path "\n\n" (number->string count) "\n"
918 (string-join (sort deps string<?) "\n")
919 (if (zero? count) "" "\n"))))
920
921 (and (build-derivations %store (list drv))
922 (equal? (call-with-input-file out get-string-all)
923 (string-concatenate
924 (map cdr
925 (sort (map (lambda (p d)
926 (cons p (apply deps p d)))
927 (list input %bash %mkdir)
928 (list (list %bash %mkdir)
929 '() '()))
930 (lambda (x y)
931 (match x
932 ((p1 . _)
933 (match y
934 ((p2 . _)
935 (string<? p1 p2)))))))))))))
936
937 (test-equal "map-derivation"
938 "hello"
939 (let* ((joke (package-derivation %store guile-1.8))
940 (good (package-derivation %store %bootstrap-guile))
941 (drv1 (build-expression->derivation %store "original-drv1"
942 #f ; systematically fail
943 #:guile-for-build joke))
944 (drv2 (build-expression->derivation %store "original-drv2"
945 '(call-with-output-file %output
946 (lambda (p)
947 (display "hello" p)))))
948 (drv3 (build-expression->derivation %store "drv-to-remap"
949 '(let ((in (assoc-ref
950 %build-inputs "in")))
951 (copy-file in %output))
952 #:inputs `(("in" ,drv1))
953 #:guile-for-build joke))
954 (drv4 (map-derivation %store drv3 `((,drv1 . ,drv2)
955 (,joke . ,good))))
956 (out (derivation->output-path drv4)))
957 (and (build-derivations %store (list (pk 'remapped drv4)))
958 (call-with-input-file out get-string-all))))
959
960 (test-equal "map-derivation, sources"
961 "hello"
962 (let* ((script1 (add-text-to-store %store "fail.sh" "exit 1"))
963 (script2 (add-text-to-store %store "hi.sh" "echo -n hello > $out"))
964 (bash-full (package-derivation %store (@ (gnu packages bash) bash)))
965 (drv1 (derivation %store "drv-to-remap"
966
967 ;; XXX: This wouldn't work in practice, but if
968 ;; we append "/bin/bash" then we can't replace
969 ;; it with the bootstrap bash, which is a
970 ;; single file.
971 (derivation->output-path bash-full)
972
973 `("-e" ,script1)
974 #:inputs `((,bash-full) (,script1))))
975 (drv2 (map-derivation %store drv1
976 `((,bash-full . ,%bash)
977 (,script1 . ,script2))))
978 (out (derivation->output-path drv2)))
979 (and (build-derivations %store (list (pk 'remapped* drv2)))
980 (call-with-input-file out get-string-all))))
981
982 (test-end)
983
984 \f
985 (exit (= (test-runner-fail-count (test-runner-current)) 0))