gnu: roffit: Adjust install phase.
[jackhill/guix/guix.git] / tests / store.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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-store)
20 #:use-module (guix tests)
21 #:use-module (guix config)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix monads)
25 #:use-module ((gcrypt hash) #:prefix gcrypt:)
26 #:use-module ((gcrypt pk-crypto) #:prefix gcrypt:)
27 #:use-module (guix pki)
28 #:use-module (guix base32)
29 #:use-module (guix packages)
30 #:use-module (guix derivations)
31 #:use-module (guix serialization)
32 #:use-module (guix build utils)
33 #:use-module (guix gexp)
34 #:use-module (gnu packages)
35 #:use-module (gnu packages bootstrap)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 regex)
38 #:use-module (rnrs bytevectors)
39 #:use-module (rnrs io ports)
40 #:use-module (web uri)
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-11)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-64))
46
47 ;; Test the (guix store) module.
48
49 (define %store
50 (open-connection-for-tests))
51
52 (define %shell
53 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
54
55 \f
56 (test-begin "store")
57
58 (test-assert "open-connection with file:// URI"
59 (let ((store (open-connection (string-append "file://"
60 (%daemon-socket-uri)))))
61 (and (add-text-to-store store "foo" "bar")
62 (begin
63 (close-connection store)
64 #t))))
65
66 (test-equal "connection handshake error"
67 EPROTO
68 (let ((port (%make-void-port "rw")))
69 (guard (c ((store-connection-error? c)
70 (and (eq? port (store-connection-error-file c))
71 (store-connection-error-code c))))
72 (open-connection #f #:port port)
73 'broken)))
74
75 (test-equal "store-path-hash-part"
76 "283gqy39v3g9dxjy26rynl0zls82fmcg"
77 (store-path-hash-part
78 (string-append (%store-prefix)
79 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
80
81 (test-equal "store-path-hash-part #f"
82 #f
83 (store-path-hash-part
84 (string-append (%store-prefix)
85 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
86
87 (test-equal "store-path-package-name"
88 "guile-2.0.7"
89 (store-path-package-name
90 (string-append (%store-prefix)
91 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
92
93 (test-equal "store-path-package-name #f"
94 #f
95 (store-path-package-name
96 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
97
98 (test-assert "direct-store-path?"
99 (and (direct-store-path?
100 (string-append (%store-prefix)
101 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
102 (not (direct-store-path?
103 (string-append
104 (%store-prefix)
105 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
106 (not (direct-store-path? (%store-prefix)))))
107
108 (test-skip (if %store 0 15))
109
110 (test-equal "profiles/per-user exists and is not writable"
111 #o755
112 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
113
114 (test-equal "profiles/per-user/$USER exists"
115 (list (getuid) #o755)
116 (let ((s (stat (string-append %state-directory "/profiles/per-user/"
117 (passwd:name (getpwuid (getuid)))))))
118 (list (stat:uid s) (stat:perms s))))
119
120 (test-equal "add-to-store"
121 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256")
122 (let* ((file (search-path %load-path "guix.scm"))
123 (content (call-with-input-file file get-bytevector-all)))
124 (map (lambda (hash-algo)
125 (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
126 (and (direct-store-path? file)
127 (bytevector=? (call-with-input-file file get-bytevector-all)
128 content)
129 hash-algo)))
130 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
131
132 (test-equal "add-data-to-store"
133 #vu8(1 2 3 4 5)
134 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
135 get-bytevector-all))
136
137 (test-assert "valid-path? live"
138 (let ((p (add-text-to-store %store "hello" "hello, world")))
139 (valid-path? %store p)))
140
141 (test-assert "valid-path? false"
142 (not (valid-path? %store
143 (string-append (%store-prefix) "/"
144 (make-string 32 #\e) "-foobar"))))
145
146 (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
147 '(1 2 3)
148 (call-with-values
149 (lambda ()
150 (with-store s
151 (add-text-to-store s "foo" "bar")
152 (values 1 2 3)))
153 list))
154
155 (test-assert "valid-path? error"
156 (with-store s
157 (guard (c ((store-protocol-error? c) #t))
158 (valid-path? s "foo")
159 #f)))
160
161 (test-assert "valid-path? recovery"
162 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
163 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
164 ;; file name. See
165 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
166 ;; details.
167 (with-store s
168 (let-syntax ((true-if-error (syntax-rules ()
169 ((_ exp)
170 (guard (c ((store-protocol-error? c) #t))
171 exp #f)))))
172 (and (true-if-error (valid-path? s "foo"))
173 (true-if-error (valid-path? s "bar"))
174 (true-if-error (valid-path? s "baz"))
175 (true-if-error (valid-path? s "chbouib"))
176 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
177
178 (test-assert "hash-part->path"
179 (let ((p (add-text-to-store %store "hello" "hello, world")))
180 (equal? (hash-part->path %store (store-path-hash-part p))
181 p)))
182
183 (test-assert "dead-paths"
184 (let ((p (add-text-to-store %store "random-text" (random-text))))
185 (->bool (member p (dead-paths %store)))))
186
187 ;; FIXME: Find a test for `live-paths'.
188 ;;
189 ;; (test-assert "temporary root is in live-paths"
190 ;; (let* ((p1 (add-text-to-store %store "random-text"
191 ;; (random-text) '()))
192 ;; (b (add-text-to-store %store "link-builder"
193 ;; (format #f "echo ~a > $out" p1)
194 ;; '()))
195 ;; (d1 (derivation %store "link"
196 ;; "/bin/sh" `("-e" ,b)
197 ;; #:inputs `((,b) (,p1))))
198 ;; (p2 (derivation->output-path d1)))
199 ;; (and (add-temp-root %store p2)
200 ;; (build-derivations %store (list d1))
201 ;; (valid-path? %store p1)
202 ;; (member (pk p2) (live-paths %store)))))
203
204 (test-assert "permanent root"
205 (let* ((p (with-store store
206 (let ((p (add-text-to-store store "random-text"
207 (random-text))))
208 (add-permanent-root p)
209 (add-permanent-root p) ; should not throw
210 p))))
211 (and (member p (live-paths %store))
212 (begin
213 (remove-permanent-root p)
214 (->bool (member p (dead-paths %store)))))))
215
216 (test-assert "dead path can be explicitly collected"
217 (let ((p (add-text-to-store %store "random-text"
218 (random-text) '())))
219 (let-values (((paths freed) (delete-paths %store (list p))))
220 (and (equal? paths (list p))
221 ;; XXX: On some file systems (notably Btrfs), freed
222 ;; may return 0. See <https://bugs.gnu.org/29363>.
223 ;;(> freed 0)
224 (not (file-exists? p))))))
225
226 (test-assert "add-text-to-store/add-to-store vs. delete-paths"
227 ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
228 ;; store item without noticing that it is no longer valid.
229 (with-store store
230 (let* ((text (random-text))
231 (file (search-path %load-path "guix.scm"))
232 (path1 (add-text-to-store store "delete-me" text))
233 (path2 (add-to-store store "delete-me" #t "sha256" file))
234 (deleted (delete-paths store (list path1 path2))))
235 (and (string=? path1 (add-text-to-store store "delete-me" text))
236 (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
237 (lset= string=? deleted (list path1 path2))
238 (valid-path? store path1)
239 (valid-path? store path2)
240 (file-exists? path1)
241 (file-exists? path2)))))
242
243 (test-equal "add-file-tree-to-store"
244 `(42
245 ("." directory #t)
246 ("./bar" directory #t)
247 ("./foo" directory #t)
248 ("./foo/a" regular "file a")
249 ("./foo/b" symlink "a")
250 ("./foo/c" directory #t)
251 ("./foo/c/p" regular "file p")
252 ("./foo/c/q" directory #t)
253 ("./foo/c/q/x" regular
254 ,(string-append "#!" %shell "\nexit 42"))
255 ("./foo/c/q/y" symlink "..")
256 ("./foo/c/q/z" directory #t))
257 (let* ((tree `("file-tree" directory
258 ("foo" directory
259 ("a" regular (data "file a"))
260 ("b" symlink "a")
261 ("c" directory
262 ("p" regular (data ,(string->utf8 "file p")))
263 ("q" directory
264 ("x" executable
265 (data ,(string-append "#!" %shell "\nexit 42")))
266 ("y" symlink "..")
267 ("z" directory))))
268 ("bar" directory)))
269 (result (add-file-tree-to-store %store tree)))
270 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
271 (with-directory-excursion result
272 (map (lambda (file)
273 (let ((type (stat:type (lstat file))))
274 `(,file ,type
275 ,(match type
276 ((or 'regular 'executable)
277 (call-with-input-file file
278 get-string-all))
279 ('symlink (readlink file))
280 ('directory #t)))))
281 (find-files "." #:directories? #t))))))
282
283 (test-equal "add-file-tree-to-store, flat"
284 "Hello, world!"
285 (let* ((tree `("flat-file" regular (data "Hello, world!")))
286 (result (add-file-tree-to-store %store tree)))
287 (and (file-exists? result)
288 (call-with-input-file result get-string-all))))
289
290 (test-assert "references"
291 (let* ((t1 (add-text-to-store %store "random1"
292 (random-text)))
293 (t2 (add-text-to-store %store "random2"
294 (random-text) (list t1))))
295 (and (equal? (list t1) (references %store t2))
296 (equal? (list t2) (referrers %store t1))
297 (null? (references %store t1))
298 (null? (referrers %store t2)))))
299
300 (test-assert "references/substitutes missing reference info"
301 (with-store s
302 (set-build-options s #:use-substitutes? #f)
303 (guard (c ((store-protocol-error? c) #t))
304 (let* ((b (add-to-store s "bash" #t "sha256"
305 (search-bootstrap-binary "bash"
306 (%current-system))))
307 (d (derivation s "the-thing" b '("--help")
308 #:inputs `((,b)))))
309 (references/substitutes s (list (derivation->output-path d) b))
310 #f))))
311
312 (test-assert "references/substitutes with substitute info"
313 (with-store s
314 (set-build-options s #:use-substitutes? #t)
315 (let* ((t1 (add-text-to-store s "random1" (random-text)))
316 (t2 (add-text-to-store s "random2" (random-text)
317 (list t1)))
318 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
319 (b (add-to-store s "bash" #t "sha256"
320 (search-bootstrap-binary "bash"
321 (%current-system))))
322 (d (derivation s "the-thing" b `("-e" ,t3)
323 #:inputs `((,b) (,t3) (,t2))
324 #:env-vars `(("t2" . ,t2))))
325 (o (derivation->output-path d)))
326 (with-derivation-narinfo d
327 (sha256 => (gcrypt:sha256 (string->utf8 t2)))
328 (references => (list t2))
329
330 (equal? (references/substitutes s (list o t3 t2 t1))
331 `((,t2) ;refs of O
332 () ;refs of T3
333 (,t1) ;refs of T2
334 ())))))) ;refs of T1
335
336 (test-equal "substitutable-path-info when substitutes are turned off"
337 '()
338 (with-store s
339 (set-build-options s #:use-substitutes? #f)
340 (let* ((b (add-to-store s "bash" #t "sha256"
341 (search-bootstrap-binary "bash"
342 (%current-system))))
343 (d (derivation s "the-thing" b '("--version")
344 #:inputs `((,b))))
345 (o (derivation->output-path d)))
346 (with-derivation-narinfo d
347 (substitutable-path-info s (list o))))))
348
349 (test-equal "substitutable-paths when substitutes are turned off"
350 '()
351 (with-store s
352 (set-build-options s #:use-substitutes? #f)
353 (let* ((b (add-to-store s "bash" #t "sha256"
354 (search-bootstrap-binary "bash"
355 (%current-system))))
356 (d (derivation s "the-thing" b '("--version")
357 #:inputs `((,b))))
358 (o (derivation->output-path d)))
359 (with-derivation-narinfo d
360 (substitutable-paths s (list o))))))
361
362 (test-assert "requisites"
363 (let* ((t1 (add-text-to-store %store "random1"
364 (random-text) '()))
365 (t2 (add-text-to-store %store "random2"
366 (random-text) (list t1)))
367 (t3 (add-text-to-store %store "random3"
368 (random-text) (list t2)))
369 (t4 (add-text-to-store %store "random4"
370 (random-text) (list t1 t3))))
371 (define (same? x y)
372 (and (= (length x) (length y))
373 (lset= equal? x y)))
374
375 (and (same? (requisites %store (list t1)) (list t1))
376 (same? (requisites %store (list t2)) (list t1 t2))
377 (same? (requisites %store (list t3)) (list t1 t2 t3))
378 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
379 (same? (requisites %store (list t1 t2 t3 t4))
380 (list t1 t2 t3 t4)))))
381
382 (test-assert "derivers"
383 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
384 (s (add-to-store %store "bash" #t "sha256"
385 (search-bootstrap-binary "bash"
386 (%current-system))))
387 (d (derivation %store "the-thing"
388 s `("-e" ,b)
389 #:env-vars `(("foo" . ,(random-text)))
390 #:inputs `((,b) (,s))))
391 (o (derivation->output-path d)))
392 (and (build-derivations %store (list d))
393 (equal? (query-derivation-outputs %store (derivation-file-name d))
394 (list o))
395 (equal? (valid-derivers %store o)
396 (list (derivation-file-name d))))))
397
398 (test-equal "with-build-handler"
399 'success
400 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
401 (s (add-to-store %store "bash" #t "sha256"
402 (search-bootstrap-binary "bash"
403 (%current-system))))
404 (d1 (derivation %store "the-thing"
405 s `("-e" ,b)
406 #:env-vars `(("foo" . ,(random-text)))
407 #:sources (list b s)))
408 (d2 (derivation %store "the-thing"
409 s `("-e" ,b)
410 #:env-vars `(("foo" . ,(random-text))
411 ("bar" . "baz"))
412 #:sources (list b s)))
413 (o1 (derivation->output-path d1))
414 (o2 (derivation->output-path d2)))
415 (with-build-handler
416 (let ((counter 0))
417 (lambda (continue store things mode)
418 (match things
419 ((drv)
420 (set! counter (+ 1 counter))
421 (if (string=? drv (derivation-file-name d1))
422 (continue #t)
423 (and (string=? drv (derivation-file-name d2))
424 (= counter 2)
425 'success))))))
426 (build-derivations %store (list d1))
427 (build-derivations %store (list d2))
428 'fail)))
429
430 (test-equal "with-build-handler + with-store"
431 'success
432 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
433 ;; even though 'with-build-handler' is outside the dynamic extent of
434 ;; 'with-store'.
435 (with-build-handler (lambda (continue store things mode)
436 (match things
437 ((drv)
438 (and (string-suffix? "thingie.drv" drv)
439 (not (port-closed?
440 (store-connection-socket store)))
441 (continue #t)))))
442 (with-store store
443 (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
444 (s (add-to-store store "bash" #t "sha256"
445 (search-bootstrap-binary "bash"
446 (%current-system))))
447 (d (derivation store "thingie"
448 s `("-e" ,b)
449 #:env-vars `(("foo" . ,(random-text)))
450 #:sources (list b s))))
451 (build-derivations store (list d))
452
453 ;; Here STORE's socket should still be open.
454 (and (valid-path? store (derivation->output-path d))
455 'success)))))
456
457 (test-assert "map/accumulate-builds"
458 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
459 (s (add-to-store %store "bash" #t "sha256"
460 (search-bootstrap-binary "bash"
461 (%current-system))))
462 (d1 (derivation %store "the-thing"
463 s `("-e" ,b)
464 #:env-vars `(("foo" . ,(random-text)))
465 #:sources (list b s)))
466 (d2 (derivation %store "the-thing"
467 s `("-e" ,b)
468 #:env-vars `(("foo" . ,(random-text))
469 ("bar" . "baz"))
470 #:sources (list b s))))
471 (with-build-handler (lambda (continue store things mode)
472 (equal? (map derivation-file-name (list d1 d2))
473 things))
474 (map/accumulate-builds %store
475 (lambda (drv)
476 (build-derivations %store (list drv))
477 (add-to-store %store "content-addressed"
478 #t "sha256"
479 (derivation->output-path drv)))
480 (list d1 d2)))))
481
482 (test-assert "mapm/accumulate-builds"
483 (let* ((d1 (run-with-store %store
484 (gexp->derivation "foo" #~(mkdir #$output))))
485 (d2 (run-with-store %store
486 (gexp->derivation "bar" #~(mkdir #$output)))))
487 (with-build-handler (lambda (continue store things mode)
488 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
489 (pk 'XX things)))
490 (run-with-store %store
491 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
492
493 (test-equal "mapm/accumulate-builds, %current-target-system"
494 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
495 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
496 ;; right #:target.
497 (run-with-store %store
498 (mlet %store-monad ((lst1 (mapm %store-monad
499 (lambda _
500 (current-target-system))
501 '(a b)))
502 (lst2 (mapm/accumulate-builds
503 (lambda _
504 (current-target-system))
505 '(a b))))
506 (return (list lst1 lst2)))
507 #:system system
508 #:target "i586-pc-gnu"))
509
510 (test-assert "topologically-sorted, one item"
511 (let* ((a (add-text-to-store %store "a" "a"))
512 (b (add-text-to-store %store "b" "b" (list a)))
513 (c (add-text-to-store %store "c" "c" (list b)))
514 (d (add-text-to-store %store "d" "d" (list c)))
515 (s (topologically-sorted %store (list d))))
516 (equal? s (list a b c d))))
517
518 (test-assert "topologically-sorted, several items"
519 (let* ((a (add-text-to-store %store "a" "a"))
520 (b (add-text-to-store %store "b" "b" (list a)))
521 (c (add-text-to-store %store "c" "c" (list b)))
522 (d (add-text-to-store %store "d" "d" (list c)))
523 (s1 (topologically-sorted %store (list d a c b)))
524 (s2 (topologically-sorted %store (list b d c a b d))))
525 (equal? s1 s2 (list a b c d))))
526
527 (test-assert "topologically-sorted, more difficult"
528 (let* ((a (add-text-to-store %store "a" "a"))
529 (b (add-text-to-store %store "b" "b" (list a)))
530 (c (add-text-to-store %store "c" "c" (list b)))
531 (d (add-text-to-store %store "d" "d" (list c)))
532 (w (add-text-to-store %store "w" "w"))
533 (x (add-text-to-store %store "x" "x" (list w)))
534 (y (add-text-to-store %store "y" "y" (list x d)))
535 (s1 (topologically-sorted %store (list y)))
536 (s2 (topologically-sorted %store (list c y)))
537 (s3 (topologically-sorted %store (cons y (references %store y)))))
538 ;; The order in which 'references' returns the references of Y is
539 ;; unspecified, so accommodate.
540 (let* ((x-then-d? (equal? (references %store y) (list x d))))
541 (and (equal? s1
542 (if x-then-d?
543 (list w x a b c d y)
544 (list a b c d w x y)))
545 (equal? s2
546 (if x-then-d?
547 (list a b c w x d y)
548 (list a b c d w x y)))
549 (lset= string=? s1 s3)))))
550
551 (test-assert "current-build-output-port, UTF-8"
552 ;; Are UTF-8 strings in the build log properly interpreted?
553 (string-contains
554 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
555 (call-with-output-string
556 (lambda (port)
557 (parameterize ((current-build-output-port port))
558 (let* ((s "Here’s a Greek letter: λ.")
559 (d (build-expression->derivation
560 %store "foo" `(display ,s)
561 #:guile-for-build
562 (package-derivation s %bootstrap-guile (%current-system)))))
563 (guard (c ((store-protocol-error? c) #t))
564 (build-derivations %store (list d))))))))
565 "Here’s a Greek letter: λ."))
566
567 (test-assert "current-build-output-port, UTF-8 + garbage"
568 ;; What about a mixture of UTF-8 + garbage?
569 (string-contains
570 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
571 (call-with-output-string
572 (lambda (port)
573 (parameterize ((current-build-output-port port))
574 (let ((d (build-expression->derivation
575 %store "foo"
576 `(begin
577 (use-modules (rnrs io ports))
578 (display "garbage: ")
579 (put-bytevector (current-output-port) #vu8(128))
580 (display "lambda: λ\n"))
581 #:guile-for-build
582 (package-derivation %store %bootstrap-guile))))
583 (guard (c ((store-protocol-error? c) #t))
584 (build-derivations %store (list d))))))))
585 "garbage: �lambda: λ"))
586
587 (test-assert "log-file, derivation"
588 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
589 (s (add-to-store %store "bash" #t "sha256"
590 (search-bootstrap-binary "bash"
591 (%current-system))))
592 (d (derivation %store "the-thing"
593 s `("-e" ,b)
594 #:env-vars `(("foo" . ,(random-text)))
595 #:inputs `((,b) (,s)))))
596 (and (build-derivations %store (list d))
597 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
598
599 (test-assert "log-file, output file name"
600 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
601 (s (add-to-store %store "bash" #t "sha256"
602 (search-bootstrap-binary "bash"
603 (%current-system))))
604 (d (derivation %store "the-thing"
605 s `("-e" ,b)
606 #:env-vars `(("foo" . ,(random-text)))
607 #:inputs `((,b) (,s))))
608 (o (derivation->output-path d)))
609 (and (build-derivations %store (list d))
610 (file-exists? (pk (log-file %store o)))
611 (string=? (log-file %store (derivation-file-name d))
612 (log-file %store o)))))
613
614 (test-assert "no substitutes"
615 (with-store s
616 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
617 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
618 (o (map derivation->output-path (list d1 d2))))
619 (set-build-options s #:use-substitutes? #f)
620 (and (not (has-substitutes? s (derivation-file-name d1)))
621 (not (has-substitutes? s (derivation-file-name d2)))
622 (null? (substitutable-paths s o))
623 (null? (substitutable-path-info s o))))))
624
625 (test-assert "build-things with output path"
626 (with-store s
627 (let* ((c (random-text)) ;contents of the output
628 (d (build-expression->derivation
629 s "substitute-me"
630 `(call-with-output-file %output
631 (lambda (p)
632 (display ,c p)))
633 #:guile-for-build
634 (package-derivation s %bootstrap-guile (%current-system))))
635 (o (derivation->output-path d)))
636 (set-build-options s #:use-substitutes? #f)
637
638 ;; Pass 'build-things' the output file name, O. However, since there
639 ;; are no substitutes for O, it will just do nothing.
640 (build-things s (list o))
641 (not (valid-path? s o)))))
642
643 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
644
645 (test-assert "substitute query"
646 (with-store s
647 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
648 (o (derivation->output-path d)))
649 ;; Create fake substituter data, to be read by 'guix substitute'.
650 (with-derivation-narinfo d
651 ;; Remove entry from the local cache.
652 (false-if-exception
653 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
654 "/guix/substitute")))
655
656 ;; Make sure 'guix substitute' correctly communicates the above
657 ;; data.
658 (set-build-options s #:use-substitutes? #t
659 #:substitute-urls (%test-substitute-urls))
660 (and (has-substitutes? s o)
661 (equal? (list o) (substitutable-paths s (list o)))
662 (match (pk 'spi (substitutable-path-info s (list o)))
663 (((? substitutable? s))
664 (and (string=? (substitutable-deriver s)
665 (derivation-file-name d))
666 (null? (substitutable-references s))
667 (equal? (substitutable-nar-size s) 1234)))))))))
668
669 (test-assert "substitute query, alternating URLs"
670 (let* ((d (with-store s
671 (package-derivation s %bootstrap-guile (%current-system))))
672 (o (derivation->output-path d)))
673 (with-derivation-narinfo d
674 ;; Remove entry from the local cache.
675 (false-if-exception
676 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
677 "/guix/substitute")))
678
679 ;; Note: We reconnect to the daemon to force a new instance of 'guix
680 ;; substitute' to be used; otherwise the #:substitute-urls of
681 ;; 'set-build-options' would have no effect.
682
683 (and (with-store s ;the right substitute URL
684 (set-build-options s #:use-substitutes? #t
685 #:substitute-urls (%test-substitute-urls))
686 (has-substitutes? s o))
687 (with-store s ;the wrong one
688 (set-build-options s #:use-substitutes? #t
689 #:substitute-urls (list
690 "http://does-not-exist"))
691 (not (has-substitutes? s o)))
692 (with-store s ;the right one again
693 (set-build-options s #:use-substitutes? #t
694 #:substitute-urls (%test-substitute-urls))
695 (has-substitutes? s o))
696 (with-store s ;empty list of URLs
697 (set-build-options s #:use-substitutes? #t
698 #:substitute-urls '())
699 (not (has-substitutes? s o)))))))
700
701 (test-assert "substitute"
702 (with-store s
703 (let* ((c (random-text)) ; contents of the output
704 (d (build-expression->derivation
705 s "substitute-me"
706 `(call-with-output-file %output
707 (lambda (p)
708 (exit 1) ; would actually fail
709 (display ,c p)))
710 #:guile-for-build
711 (package-derivation s %bootstrap-guile (%current-system))))
712 (o (derivation->output-path d)))
713 (with-derivation-substitute d c
714 (set-build-options s #:use-substitutes? #t
715 #:substitute-urls (%test-substitute-urls))
716 (and (has-substitutes? s o)
717 (build-derivations s (list d))
718 (equal? c (call-with-input-file o get-string-all)))))))
719
720 (test-assert "substitute + build-things with output path"
721 (with-store s
722 (let* ((c (random-text)) ;contents of the output
723 (d (build-expression->derivation
724 s "substitute-me"
725 `(call-with-output-file %output
726 (lambda (p)
727 (exit 1) ;would actually fail
728 (display ,c p)))
729 #:guile-for-build
730 (package-derivation s %bootstrap-guile (%current-system))))
731 (o (derivation->output-path d)))
732 (with-derivation-substitute d c
733 (set-build-options s #:use-substitutes? #t
734 #:substitute-urls (%test-substitute-urls))
735 (and (has-substitutes? s o)
736 (build-things s (list o)) ;give the output path
737 (valid-path? s o)
738 (equal? c (call-with-input-file o get-string-all)))))))
739
740 (test-assert "substitute + build-things with specific output"
741 (with-store s
742 (let* ((c (random-text)) ;contents of the output
743 (d (build-expression->derivation
744 s "substitute-me" `(begin ,c (exit 1)) ;would fail
745 #:outputs '("out" "one" "two")
746 #:guile-for-build
747 (package-derivation s %bootstrap-guile (%current-system))))
748 (o (derivation->output-path d)))
749 (with-derivation-substitute d c
750 (set-build-options s #:use-substitutes? #t
751 #:substitute-urls (%test-substitute-urls))
752 (and (has-substitutes? s o)
753
754 ;; Ask for nothing but the "out" output of D.
755 (build-things s `((,(derivation-file-name d) . "out")))
756
757 (valid-path? s o)
758 (equal? c (call-with-input-file o get-string-all)))))))
759
760 (test-assert "substitute, corrupt output hash"
761 ;; Tweak the substituter into installing a substitute whose hash doesn't
762 ;; match the one announced in the narinfo. The daemon must notice this and
763 ;; raise an error.
764 (with-store s
765 (let* ((c "hello, world") ; contents of the output
766 (d (build-expression->derivation
767 s "corrupt-substitute"
768 `(mkdir %output)
769 #:guile-for-build
770 (package-derivation s %bootstrap-guile (%current-system))))
771 (o (derivation->output-path d)))
772 (with-derivation-substitute d c
773 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
774
775 ;; Make sure we use 'guix substitute'.
776 (set-build-options s
777 #:use-substitutes? #t
778 #:fallback? #f
779 #:substitute-urls (%test-substitute-urls))
780 (and (has-substitutes? s o)
781 (guard (c ((store-protocol-error? c)
782 ;; XXX: the daemon writes "hash mismatch in downloaded
783 ;; path", but the actual error returned to the client
784 ;; doesn't mention that.
785 (pk 'corrupt c)
786 (not (zero? (store-protocol-error-status c)))))
787 (build-derivations s (list d))
788 #f))))))
789
790 (test-assert "substitute --fallback"
791 (with-store s
792 (let* ((t (random-text)) ; contents of the output
793 (d (build-expression->derivation
794 s "substitute-me-not"
795 `(call-with-output-file %output
796 (lambda (p)
797 (display ,t p)))
798 #:guile-for-build
799 (package-derivation s %bootstrap-guile (%current-system))))
800 (o (derivation->output-path d)))
801 ;; Create fake substituter data, to be read by 'guix substitute'.
802 (with-derivation-narinfo d
803 ;; Make sure we use 'guix substitute'.
804 (set-build-options s #:use-substitutes? #t
805 #:substitute-urls (%test-substitute-urls))
806 (and (has-substitutes? s o)
807 (guard (c ((store-protocol-error? c)
808 ;; The substituter failed as expected. Now make
809 ;; sure that #:fallback? #t works correctly.
810 (set-build-options s
811 #:use-substitutes? #t
812 #:substitute-urls
813 (%test-substitute-urls)
814 #:fallback? #t)
815 (and (build-derivations s (list d))
816 (equal? t (call-with-input-file o
817 get-string-all)))))
818 ;; Should fail.
819 (build-derivations s (list d))
820 #f))))))
821
822 (test-assert "export/import several paths"
823 (let* ((texts (unfold (cut >= <> 10)
824 (lambda _ (random-text))
825 1+
826 0))
827 (files (map (cut add-text-to-store %store "text" <>) texts))
828 (dump (call-with-bytevector-output-port
829 (cut export-paths %store files <>))))
830 (delete-paths %store files)
831 (and (every (negate file-exists?) files)
832 (let* ((source (open-bytevector-input-port dump))
833 (imported (import-paths %store source)))
834 (and (equal? imported files)
835 (every file-exists? files)
836 (equal? texts
837 (map (lambda (file)
838 (call-with-input-file file
839 get-string-all))
840 files)))))))
841
842 (test-assert "export/import paths, ensure topological order"
843 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
844 (file1 (add-text-to-store %store "foo" (random-text)
845 (list file0)))
846 (file2 (add-text-to-store %store "bar" (random-text)
847 (list file1)))
848 (files (list file1 file2))
849 (dump1 (call-with-bytevector-output-port
850 (cute export-paths %store (list file1 file2) <>)))
851 (dump2 (call-with-bytevector-output-port
852 (cute export-paths %store (list file2 file1) <>))))
853 (delete-paths %store files)
854 (and (every (negate file-exists?) files)
855 (bytevector=? dump1 dump2)
856 (let* ((source (open-bytevector-input-port dump1))
857 (imported (import-paths %store source)))
858 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
859 (and (equal? imported (list file1 file2))
860 (every file-exists? files)
861 (equal? (list file0) (references %store file1))
862 (equal? (list file1) (references %store file2)))))))
863
864 (test-assert "export/import incomplete"
865 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
866 (file1 (add-text-to-store %store "foo" (random-text)
867 (list file0)))
868 (file2 (add-text-to-store %store "bar" (random-text)
869 (list file1)))
870 (dump (call-with-bytevector-output-port
871 (cute export-paths %store (list file2) <>))))
872 (delete-paths %store (list file0 file1 file2))
873 (guard (c ((store-protocol-error? c)
874 (and (not (zero? (store-protocol-error-status c)))
875 (string-contains (store-protocol-error-message c)
876 "not valid"))))
877 ;; Here we get an exception because DUMP does not include FILE0 and
878 ;; FILE1, which are dependencies of FILE2.
879 (import-paths %store (open-bytevector-input-port dump)))))
880
881 (test-assert "export/import recursive"
882 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
883 (file1 (add-text-to-store %store "foo" (random-text)
884 (list file0)))
885 (file2 (add-text-to-store %store "bar" (random-text)
886 (list file1)))
887 (dump (call-with-bytevector-output-port
888 (cute export-paths %store (list file2) <>
889 #:recursive? #t))))
890 (delete-paths %store (list file0 file1 file2))
891 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
892 (and (equal? imported (list file0 file1 file2))
893 (every file-exists? (list file0 file1 file2))
894 (equal? (list file0) (references %store file1))
895 (equal? (list file1) (references %store file2))))))
896
897 (test-assert "write-file & export-path yield the same result"
898 ;; Here we compare 'write-file' and the daemon's own implementation.
899 ;; 'write-file' is the reference because we know it sorts file
900 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
901 ;; currently happen to be sorted as a side-effect of some unrelated
902 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
903 ;; changes there.
904 (run-with-store %store
905 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
906 (out1 -> (derivation->output-path drv1))
907 (data -> (unfold (cut >= <> 26)
908 (lambda (i)
909 (random-bytevector 128))
910 1+ 0))
911 (build
912 -> #~(begin
913 (use-modules (rnrs io ports) (srfi srfi-1))
914 (let ()
915 (define letters
916 (map (lambda (i)
917 (string
918 (integer->char
919 (+ i (char->integer #\a)))))
920 (iota 26)))
921 (define (touch file data)
922 (call-with-output-file file
923 (lambda (port)
924 (put-bytevector port data))))
925
926 (mkdir #$output)
927 (chdir #$output)
928
929 ;; The files must be different so they have
930 ;; different inode numbers, and the inode
931 ;; order must differ from the lexicographic
932 ;; order.
933 (for-each touch
934 (append (drop letters 10)
935 (take letters 10))
936 (list #$@data))
937 #t)))
938 (drv2 (gexp->derivation "bunch" build))
939 (out2 -> (derivation->output-path drv2))
940 (item-info -> (store-lift query-path-info)))
941 (mbegin %store-monad
942 (built-derivations (list drv1 drv2))
943 (foldm %store-monad
944 (lambda (item result)
945 (define ref-hash
946 (let-values (((port get) (gcrypt:open-sha256-port)))
947 (write-file item port)
948 (close-port port)
949 (get)))
950
951 ;; 'query-path-info' returns a hash produced by using the
952 ;; daemon's C++ 'dump' function, which is the implementation
953 ;; under test.
954 (>>= (item-info item)
955 (lambda (info)
956 (return
957 (and result
958 (bytevector=? (path-info-hash info) ref-hash))))))
959 #t
960 (list out1 out2))))
961 #:guile-for-build (%guile-for-build)))
962
963
964 (test-assert "import not signed"
965 (let* ((text (random-text))
966 (file (add-file-tree-to-store %store
967 `("tree" directory
968 ("text" regular (data ,text))
969 ("link" symlink "text"))))
970 (dump (call-with-bytevector-output-port
971 (lambda (port)
972 (write-int 1 port) ;start
973
974 (write-file file port) ;contents
975 (write-int #x4558494e port) ;%export-magic
976 (write-string file port) ;store item
977 (write-string-list '() port) ;references
978 (write-string "" port) ;deriver
979 (write-int 0 port) ;not signed
980
981 (write-int 0 port))))) ;done
982
983 ;; Ensure 'import-paths' raises an exception.
984 (guard (c ((store-protocol-error? c)
985 (and (not (zero? (store-protocol-error-status c)))
986 (string-contains (store-protocol-error-message c)
987 "lacks a signature"))))
988 (let* ((source (open-bytevector-input-port dump))
989 (imported (import-paths %store source)))
990 (pk 'unsigned-imported imported)
991 #f))))
992
993 (test-assert "import signed by unauthorized key"
994 (let* ((text (random-text))
995 (file (add-file-tree-to-store %store
996 `("tree" directory
997 ("text" regular (data ,text))
998 ("link" symlink "text"))))
999 (key (gcrypt:generate-key
1000 (gcrypt:string->canonical-sexp
1001 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1002 (dump (call-with-bytevector-output-port
1003 (lambda (port)
1004 (write-int 1 port) ;start
1005
1006 (write-file file port) ;contents
1007 (write-int #x4558494e port) ;%export-magic
1008 (write-string file port) ;store item
1009 (write-string-list '() port) ;references
1010 (write-string "" port) ;deriver
1011 (write-int 1 port) ;signed
1012 (write-string (gcrypt:canonical-sexp->string
1013 (signature-sexp
1014 (gcrypt:bytevector->hash-data
1015 (gcrypt:sha256 #vu8(0 1 2))
1016 #:key-type 'ecc)
1017 (gcrypt:find-sexp-token key 'private-key)
1018 (gcrypt:find-sexp-token key 'public-key)))
1019 port)
1020
1021 (write-int 0 port))))) ;done
1022
1023 ;; Ensure 'import-paths' raises an exception.
1024 (guard (c ((store-protocol-error? c)
1025 (and (not (zero? (store-protocol-error-status c)))
1026 (string-contains (store-protocol-error-message c)
1027 "unauthorized public key"))))
1028 (let* ((source (open-bytevector-input-port dump))
1029 (imported (import-paths %store source)))
1030 (pk 'unauthorized-imported imported)
1031 #f))))
1032
1033 (test-assert "import corrupt path"
1034 (let* ((text (random-text))
1035 (file (add-text-to-store %store "text" text))
1036 (dump (call-with-bytevector-output-port
1037 (cut export-paths %store (list file) <>))))
1038 (delete-paths %store (list file))
1039
1040 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1041 ;; the file contents in DUMP, regardless of the store prefix.
1042 (let* ((index #x70)
1043 (byte (bytevector-u8-ref dump index)))
1044 (bytevector-u8-set! dump index (logxor #xff byte)))
1045
1046 (and (not (file-exists? file))
1047 (guard (c ((store-protocol-error? c)
1048 (pk 'c c)
1049 (and (not (zero? (store-protocol-error-status c)))
1050 (string-contains (store-protocol-error-message c)
1051 "corrupt"))))
1052 (let* ((source (open-bytevector-input-port dump))
1053 (imported (import-paths %store source)))
1054 (pk 'corrupt-imported imported)
1055 #f)))))
1056
1057 (test-assert "verify-store"
1058 (let* ((text (random-text))
1059 (file1 (add-text-to-store %store "foo" text))
1060 (file2 (add-text-to-store %store "bar" (random-text)
1061 (list file1))))
1062 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1063 (begin
1064 (delete-file file1)
1065 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1066 (begin
1067 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1068 ;; without actually creating the file. ;
1069 (call-with-output-file file1
1070 (lambda (port)
1071 (display text port)))
1072 (pk 'verify3 (verify-store %store)))))) ;OK again
1073
1074 (test-assert "verify-store + check-contents"
1075 ;; XXX: This test is I/O intensive.
1076 (with-store s
1077 (let* ((text (random-text))
1078 (drv (build-expression->derivation
1079 s "corrupt"
1080 `(let ((out (assoc-ref %outputs "out")))
1081 (call-with-output-file out
1082 (lambda (port)
1083 (display ,text port)))
1084 #t)
1085 #:guile-for-build
1086 (package-derivation s %bootstrap-guile (%current-system))))
1087 (file (derivation->output-path drv)))
1088 (with-derivation-substitute drv text
1089 (and (build-derivations s (list drv))
1090 (verify-store s #:check-contents? #t) ;should be OK
1091 (begin
1092 (chmod file #o644)
1093 (call-with-output-file file
1094 (lambda (port)
1095 (display "corrupt!" port)))
1096 #t)
1097
1098 ;; Make sure the corruption is detected. We don't test repairing
1099 ;; because only "trusted" users are allowed to do it, but we
1100 ;; don't expose that notion of trusted users that nix-daemon
1101 ;; supports because it seems dubious and redundant with what the
1102 ;; OS provides (in Nix "trusted" users have additional
1103 ;; privileges, such as overriding the set of substitute URLs, but
1104 ;; we instead want to allow anyone to modify them, provided
1105 ;; substitutes are signed by a root-approved key.)
1106 (not (verify-store s #:check-contents? #t))
1107
1108 ;; Delete the corrupt item to leave the store in a clean state.
1109 (delete-paths s (list file)))))))
1110
1111 (test-assert "build-things, check mode"
1112 (with-store store
1113 (call-with-temporary-output-file
1114 (lambda (entropy entropy-port)
1115 (write (random-text) entropy-port)
1116 (force-output entropy-port)
1117 (let* ((drv (build-expression->derivation
1118 store "non-deterministic"
1119 `(begin
1120 (use-modules (rnrs io ports))
1121 (let ((out (assoc-ref %outputs "out")))
1122 (call-with-output-file out
1123 (lambda (port)
1124 ;; Rely on the fact that tests do not use the
1125 ;; chroot, and thus ENTROPY is readable.
1126 (display (call-with-input-file ,entropy
1127 get-string-all)
1128 port)))
1129 #t))
1130 #:guile-for-build
1131 (package-derivation store %bootstrap-guile (%current-system))))
1132 (file (derivation->output-path drv)))
1133 (and (build-things store (list (derivation-file-name drv)))
1134 (begin
1135 (write (random-text) entropy-port)
1136 (force-output entropy-port)
1137 (guard (c ((store-protocol-error? c)
1138 (pk 'determinism-exception c)
1139 (and (not (zero? (store-protocol-error-status c)))
1140 (string-contains (store-protocol-error-message c)
1141 "deterministic"))))
1142 ;; This one will produce a different result. Since we're in
1143 ;; 'check' mode, this must fail.
1144 (build-things store (list (derivation-file-name drv))
1145 (build-mode check))
1146 #f))))))))
1147
1148 (test-assert "build-succeeded trace in check mode"
1149 (string-contains
1150 (call-with-output-string
1151 (lambda (port)
1152 (let ((d (build-expression->derivation
1153 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1154 #:guile-for-build
1155 (package-derivation %store %bootstrap-guile))))
1156 (build-derivations %store (list d))
1157 (parameterize ((current-build-output-port port))
1158 (build-derivations %store (list d) (build-mode check))))))
1159 "@ build-succeeded"))
1160
1161 (test-assert "build multiple times"
1162 (with-store store
1163 ;; Ask to build twice.
1164 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1165
1166 (call-with-temporary-output-file
1167 (lambda (entropy entropy-port)
1168 (write (random-text) entropy-port)
1169 (force-output entropy-port)
1170 (let* ((drv (build-expression->derivation
1171 store "non-deterministic"
1172 `(begin
1173 (use-modules (rnrs io ports))
1174 (let ((out (assoc-ref %outputs "out")))
1175 (call-with-output-file out
1176 (lambda (port)
1177 ;; Rely on the fact that tests do not use the
1178 ;; chroot, and thus ENTROPY is accessible.
1179 (display (call-with-input-file ,entropy
1180 get-string-all)
1181 port)
1182 (call-with-output-file ,entropy
1183 (lambda (port)
1184 (write 'foobar port)))))
1185 #t))
1186 #:guile-for-build
1187 (package-derivation store %bootstrap-guile (%current-system))))
1188 (file (derivation->output-path drv)))
1189 (guard (c ((store-protocol-error? c)
1190 (pk 'multiple-build c)
1191 (and (not (zero? (store-protocol-error-status c)))
1192 (string-contains (store-protocol-error-message c)
1193 "deterministic"))))
1194 ;; This one will produce a different result on the second run.
1195 (current-build-output-port (current-error-port))
1196 (build-things store (list (derivation-file-name drv)))
1197 #f))))))
1198
1199 (test-equal "store-lower"
1200 "Lowered."
1201 (let* ((add (store-lower text-file))
1202 (file (add %store "foo" "Lowered.")))
1203 (call-with-input-file file get-string-all)))
1204
1205 (test-equal "current-system"
1206 "bar"
1207 (parameterize ((%current-system "frob"))
1208 (run-with-store %store
1209 (mbegin %store-monad
1210 (set-current-system "bar")
1211 (current-system))
1212 #:system "foo")))
1213
1214 (test-assert "query-path-info"
1215 (let* ((ref (add-text-to-store %store "ref" "foo"))
1216 (item (add-text-to-store %store "item" "bar" (list ref)))
1217 (info (query-path-info %store item)))
1218 (and (equal? (path-info-references info) (list ref))
1219 (equal? (path-info-hash info)
1220 (gcrypt:sha256
1221 (string->utf8
1222 (call-with-output-string (cut write-file item <>))))))))
1223
1224 (test-assert "path-info-deriver"
1225 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1226 (s (add-to-store %store "bash" #t "sha256"
1227 (search-bootstrap-binary "bash"
1228 (%current-system))))
1229 (d (derivation %store "the-thing"
1230 s `("-e" ,b)
1231 #:env-vars `(("foo" . ,(random-text)))
1232 #:inputs `((,b) (,s))))
1233 (o (derivation->output-path d)))
1234 (and (build-derivations %store (list d))
1235 (not (path-info-deriver (query-path-info %store b)))
1236 (string=? (derivation-file-name d)
1237 (path-info-deriver (query-path-info %store o))))))
1238
1239 (test-equal "build-cores"
1240 (list 0 42)
1241 (with-store store
1242 (let* ((build (add-text-to-store store "build.sh"
1243 "echo $NIX_BUILD_CORES > $out"))
1244 (bash (add-to-store store "bash" #t "sha256"
1245 (search-bootstrap-binary "bash"
1246 (%current-system))))
1247 (drv1 (derivation store "the-thing" bash
1248 `("-e" ,build)
1249 #:inputs `((,bash) (,build))
1250 #:env-vars `(("x" . ,(random-text)))))
1251 (drv2 (derivation store "the-thing" bash
1252 `("-e" ,build)
1253 #:inputs `((,bash) (,build))
1254 #:env-vars `(("x" . ,(random-text))))))
1255 (and (build-derivations store (list drv1))
1256 (begin
1257 (set-build-options store #:build-cores 42)
1258 (build-derivations store (list drv2)))
1259 (list (call-with-input-file (derivation->output-path drv1)
1260 read)
1261 (call-with-input-file (derivation->output-path drv2)
1262 read))))))
1263
1264 (test-equal "multiplexed-build-output"
1265 '("Hello from first." "Hello from second.")
1266 (with-store store
1267 (let* ((build (add-text-to-store store "build.sh"
1268 "echo Hello from $NAME.; echo > $out"))
1269 (bash (add-to-store store "bash" #t "sha256"
1270 (search-bootstrap-binary "bash"
1271 (%current-system))))
1272 (drv1 (derivation store "one" bash
1273 `("-e" ,build)
1274 #:inputs `((,bash) (,build))
1275 #:env-vars `(("NAME" . "first")
1276 ("x" . ,(random-text)))))
1277 (drv2 (derivation store "two" bash
1278 `("-e" ,build)
1279 #:inputs `((,bash) (,build))
1280 #:env-vars `(("NAME" . "second")
1281 ("x" . ,(random-text))))))
1282 (set-build-options store
1283 #:print-build-trace #t
1284 #:multiplexed-build-output? #t
1285 #:max-build-jobs 10)
1286 (let ((port (open-output-string)))
1287 ;; Send the build log to PORT.
1288 (parameterize ((current-build-output-port port))
1289 (build-derivations store (list drv1 drv2)))
1290
1291 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1292 ;; traces that allow us to retrieve each builder's output (we assume
1293 ;; there's exactly one "build-output" trace for each builder, which is
1294 ;; reasonable.)
1295 (let* ((log (get-output-string port))
1296 (started (fold-matches
1297 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1298 log '() cons))
1299 (done (fold-matches
1300 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1301 log '() cons))
1302 (output (fold-matches
1303 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1304 log '() cons))
1305 (drv-pid (lambda (name)
1306 (lambda (m)
1307 (let ((drv (match:substring m 1))
1308 (pid (string->number
1309 (match:substring m 4))))
1310 (and (string-suffix? name drv) pid)))))
1311 (pid-log (lambda (pid)
1312 (lambda (m)
1313 (let ((n (string->number
1314 (match:substring m 1)))
1315 (len (string->number
1316 (match:substring m 2)))
1317 (str (match:substring m 3)))
1318 (and (= pid n)
1319 (= (string-length str) (- len 1))
1320 str)))))
1321 (pid1 (any (drv-pid "one.drv") started))
1322 (pid2 (any (drv-pid "two.drv") started)))
1323 (list (any (pid-log pid1) output)
1324 (any (pid-log pid2) output)))))))
1325
1326 (test-end "store")