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