Merge remote-tracking branch 'origin/master' into core-updates-frozen.
[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-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
458 (iota 20)
459
460 ;; Make sure that, when the cutoff is reached, 'map/accumulate-builds' still
461 ;; returns the right result and calls the build handler by batches.
462 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
463 (s (add-to-store %store "bash" #t "sha256"
464 (search-bootstrap-binary "bash"
465 (%current-system))))
466 (d (map (lambda (i)
467 (derivation %store (string-append "the-thing-"
468 (number->string i))
469 s `("-e" ,b)
470 #:env-vars `(("foo" . ,(random-text)))
471 #:sources (list b s)
472 #:properties `((n . ,i))))
473 (iota 20)))
474 (calls '()))
475 (define lst
476 (with-build-handler (lambda (continue store things mode)
477 (set! calls (cons things calls))
478 (continue #f))
479 (map/accumulate-builds %store
480 (lambda (d)
481 (build-derivations %store (list d))
482 (assq-ref (derivation-properties d) 'n))
483 d
484 #:cutoff 7)))
485
486 (match (reverse calls)
487 (((batch1 ...) (batch2 ...) (batch3 ...))
488 (and (equal? (map derivation-file-name (take d 8)) batch1)
489 (equal? (map derivation-file-name (take (drop d 8) 8)) batch2)
490 (equal? (map derivation-file-name (drop d 16)) batch3)
491 lst)))))
492
493 (test-assert "mapm/accumulate-builds"
494 (let* ((d1 (run-with-store %store
495 (gexp->derivation "foo" #~(mkdir #$output))))
496 (d2 (run-with-store %store
497 (gexp->derivation "bar" #~(mkdir #$output)))))
498 (with-build-handler (lambda (continue store things mode)
499 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
500 (pk 'XX things)))
501 (run-with-store %store
502 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
503
504 (test-equal "mapm/accumulate-builds, %current-target-system"
505 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
506 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
507 ;; right #:target.
508 (run-with-store %store
509 (mlet %store-monad ((lst1 (mapm %store-monad
510 (lambda _
511 (current-target-system))
512 '(a b)))
513 (lst2 (mapm/accumulate-builds
514 (lambda _
515 (current-target-system))
516 '(a b))))
517 (return (list lst1 lst2)))
518 #:system system
519 #:target "i586-pc-gnu"))
520
521 (test-assert "topologically-sorted, one item"
522 (let* ((a (add-text-to-store %store "a" "a"))
523 (b (add-text-to-store %store "b" "b" (list a)))
524 (c (add-text-to-store %store "c" "c" (list b)))
525 (d (add-text-to-store %store "d" "d" (list c)))
526 (s (topologically-sorted %store (list d))))
527 (equal? s (list a b c d))))
528
529 (test-assert "topologically-sorted, several items"
530 (let* ((a (add-text-to-store %store "a" "a"))
531 (b (add-text-to-store %store "b" "b" (list a)))
532 (c (add-text-to-store %store "c" "c" (list b)))
533 (d (add-text-to-store %store "d" "d" (list c)))
534 (s1 (topologically-sorted %store (list d a c b)))
535 (s2 (topologically-sorted %store (list b d c a b d))))
536 (equal? s1 s2 (list a b c d))))
537
538 (test-assert "topologically-sorted, more difficult"
539 (let* ((a (add-text-to-store %store "a" "a"))
540 (b (add-text-to-store %store "b" "b" (list a)))
541 (c (add-text-to-store %store "c" "c" (list b)))
542 (d (add-text-to-store %store "d" "d" (list c)))
543 (w (add-text-to-store %store "w" "w"))
544 (x (add-text-to-store %store "x" "x" (list w)))
545 (y (add-text-to-store %store "y" "y" (list x d)))
546 (s1 (topologically-sorted %store (list y)))
547 (s2 (topologically-sorted %store (list c y)))
548 (s3 (topologically-sorted %store (cons y (references %store y)))))
549 ;; The order in which 'references' returns the references of Y is
550 ;; unspecified, so accommodate.
551 (let* ((x-then-d? (equal? (references %store y) (list x d))))
552 (and (equal? s1
553 (if x-then-d?
554 (list w x a b c d y)
555 (list a b c d w x y)))
556 (equal? s2
557 (if x-then-d?
558 (list a b c w x d y)
559 (list a b c d w x y)))
560 (lset= string=? s1 s3)))))
561
562 (test-assert "current-build-output-port, UTF-8"
563 ;; Are UTF-8 strings in the build log properly interpreted?
564 (string-contains
565 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
566 (call-with-output-string
567 (lambda (port)
568 (parameterize ((current-build-output-port port))
569 (let* ((s "Here’s a Greek letter: λ.")
570 (d (build-expression->derivation
571 %store "foo" `(display ,s)
572 #:guile-for-build
573 (package-derivation %store %bootstrap-guile
574 (%current-system)))))
575 (guard (c ((store-protocol-error? c) #t))
576 (build-derivations %store (list d))))))))
577 "Here’s a Greek letter: λ."))
578
579 (test-assert "current-build-output-port, UTF-8 + garbage"
580 ;; What about a mixture of UTF-8 + garbage?
581 (string-contains
582 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
583 (call-with-output-string
584 (lambda (port)
585 (parameterize ((current-build-output-port port))
586 (let ((d (build-expression->derivation
587 %store "foo"
588 `(begin
589 (use-modules (rnrs io ports))
590 (display "garbage: ")
591 (put-bytevector (current-output-port) #vu8(128))
592 (display "lambda: λ\n"))
593 #:guile-for-build
594 (package-derivation %store %bootstrap-guile))))
595 (guard (c ((store-protocol-error? c) #t))
596 (build-derivations %store (list d))))))))
597 "garbage: �lambda: λ"))
598
599 (test-assert "log-file, derivation"
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 (and (build-derivations %store (list d))
609 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
610
611 (test-assert "log-file, output file name"
612 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
613 (s (add-to-store %store "bash" #t "sha256"
614 (search-bootstrap-binary "bash"
615 (%current-system))))
616 (d (derivation %store "the-thing"
617 s `("-e" ,b)
618 #:env-vars `(("foo" . ,(random-text)))
619 #:inputs `((,b) (,s))))
620 (o (derivation->output-path d)))
621 (and (build-derivations %store (list d))
622 (file-exists? (pk (log-file %store o)))
623 (string=? (log-file %store (derivation-file-name d))
624 (log-file %store o)))))
625
626 (test-assert "no substitutes"
627 (with-store s
628 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
629 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
630 (o (map derivation->output-path (list d1 d2))))
631 (set-build-options s #:use-substitutes? #f)
632 (and (not (has-substitutes? s (derivation-file-name d1)))
633 (not (has-substitutes? s (derivation-file-name d2)))
634 (null? (substitutable-paths s o))
635 (null? (substitutable-path-info s o))))))
636
637 (test-assert "build-things with output path"
638 (with-store s
639 (let* ((c (random-text)) ;contents of the output
640 (d (build-expression->derivation
641 s "substitute-me"
642 `(call-with-output-file %output
643 (lambda (p)
644 (display ,c p)))
645 #:guile-for-build
646 (package-derivation s %bootstrap-guile (%current-system))))
647 (o (derivation->output-path d)))
648 (set-build-options s #:use-substitutes? #f)
649
650 ;; Pass 'build-things' the output file name, O. However, since there
651 ;; are no substitutes for O, it will just do nothing.
652 (build-things s (list o))
653 (not (valid-path? s o)))))
654
655 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
656
657 (test-assert "substitute query"
658 (with-store s
659 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
660 (o (derivation->output-path d)))
661 ;; Create fake substituter data, to be read by 'guix substitute'.
662 (with-derivation-narinfo d
663 ;; Remove entry from the local cache.
664 (false-if-exception
665 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
666 "/guix/substitute")))
667
668 ;; Make sure 'guix substitute' correctly communicates the above
669 ;; data.
670 (set-build-options s #:use-substitutes? #t
671 #:substitute-urls (%test-substitute-urls))
672 (and (has-substitutes? s o)
673 (equal? (list o) (substitutable-paths s (list o)))
674 (match (pk 'spi (substitutable-path-info s (list o)))
675 (((? substitutable? s))
676 (and (string=? (substitutable-deriver s)
677 (derivation-file-name d))
678 (null? (substitutable-references s))
679 (equal? (substitutable-nar-size s) 1234)))))))))
680
681 (test-assert "substitute query, alternating URLs"
682 (let* ((d (with-store s
683 (package-derivation s %bootstrap-guile (%current-system))))
684 (o (derivation->output-path d)))
685 (with-derivation-narinfo d
686 ;; Remove entry from the local cache.
687 (false-if-exception
688 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
689 "/guix/substitute")))
690
691 ;; Note: We reconnect to the daemon to force a new instance of 'guix
692 ;; substitute' to be used; otherwise the #:substitute-urls of
693 ;; 'set-build-options' would have no effect.
694
695 (and (with-store s ;the right substitute URL
696 (set-build-options s #:use-substitutes? #t
697 #:substitute-urls (%test-substitute-urls))
698 (has-substitutes? s o))
699 (with-store s ;the wrong one
700 (set-build-options s #:use-substitutes? #t
701 #:substitute-urls (list
702 "http://does-not-exist"))
703 (not (has-substitutes? s o)))
704 (with-store s ;the right one again
705 (set-build-options s #:use-substitutes? #t
706 #:substitute-urls (%test-substitute-urls))
707 (has-substitutes? s o))
708 (with-store s ;empty list of URLs
709 (set-build-options s #:use-substitutes? #t
710 #:substitute-urls '())
711 (not (has-substitutes? s o)))))))
712
713 (test-assert "substitute"
714 (with-store s
715 (let* ((c (random-text)) ; contents of the output
716 (d (build-expression->derivation
717 s "substitute-me"
718 `(call-with-output-file %output
719 (lambda (p)
720 (exit 1) ; would actually fail
721 (display ,c p)))
722 #:guile-for-build
723 (package-derivation s %bootstrap-guile (%current-system))))
724 (o (derivation->output-path d)))
725 (with-derivation-substitute d c
726 (set-build-options s #:use-substitutes? #t
727 #:substitute-urls (%test-substitute-urls))
728 (and (has-substitutes? s o)
729 (build-derivations s (list d))
730 (canonical-file? o)
731 (equal? c (call-with-input-file o get-string-all)))))))
732
733 (test-assert "substitute, deduplication"
734 (with-store s
735 (let* ((c (random-text)) ; contents of the output
736 (g (package-derivation s %bootstrap-guile))
737 (d1 (build-expression->derivation s "substitute-me"
738 `(begin ,c (exit 1))
739 #:guile-for-build g))
740 (d2 (build-expression->derivation s "build-me"
741 `(call-with-output-file %output
742 (lambda (p)
743 (display ,c p)))
744 #:guile-for-build g))
745 (o1 (derivation->output-path d1))
746 (o2 (derivation->output-path d2)))
747 (with-derivation-substitute d1 c
748 (set-build-options s #:use-substitutes? #t
749 #:substitute-urls (%test-substitute-urls))
750 (and (has-substitutes? s o1)
751 (build-derivations s (list d2)) ;build
752 (build-derivations s (list d1)) ;substitute
753 (canonical-file? o1)
754 (equal? c (call-with-input-file o1 get-string-all))
755 (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
756
757 (test-assert "substitute + build-things with output path"
758 (with-store s
759 (let* ((c (random-text)) ;contents of the output
760 (d (build-expression->derivation
761 s "substitute-me"
762 `(call-with-output-file %output
763 (lambda (p)
764 (exit 1) ;would actually fail
765 (display ,c p)))
766 #:guile-for-build
767 (package-derivation s %bootstrap-guile (%current-system))))
768 (o (derivation->output-path d)))
769 (with-derivation-substitute d c
770 (set-build-options s #:use-substitutes? #t
771 #:substitute-urls (%test-substitute-urls))
772 (and (has-substitutes? s o)
773 (build-things s (list o)) ;give the output path
774 (valid-path? s o)
775 (canonical-file? o)
776 (equal? c (call-with-input-file o get-string-all)))))))
777
778 (test-assert "substitute + build-things with specific output"
779 (with-store s
780 (let* ((c (random-text)) ;contents of the output
781 (d (build-expression->derivation
782 s "substitute-me" `(begin ,c (exit 1)) ;would fail
783 #:outputs '("out" "one" "two")
784 #:guile-for-build
785 (package-derivation s %bootstrap-guile (%current-system))))
786 (o (derivation->output-path d)))
787 (with-derivation-substitute d c
788 (set-build-options s #:use-substitutes? #t
789 #:substitute-urls (%test-substitute-urls))
790 (and (has-substitutes? s o)
791
792 ;; Ask for nothing but the "out" output of D.
793 (build-things s `((,(derivation-file-name d) . "out")))
794
795 (valid-path? s o)
796 (canonical-file? o)
797 (equal? c (call-with-input-file o get-string-all)))))))
798
799 (test-assert "substitute, corrupt output hash"
800 ;; Tweak the substituter into installing a substitute whose hash doesn't
801 ;; match the one announced in the narinfo. The daemon must notice this and
802 ;; raise an error.
803 (with-store s
804 (let* ((c "hello, world") ; contents of the output
805 (d (build-expression->derivation
806 s "corrupt-substitute"
807 `(mkdir %output)
808 #:guile-for-build
809 (package-derivation s %bootstrap-guile (%current-system))))
810 (o (derivation->output-path d)))
811 (with-derivation-substitute d c
812 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
813
814 ;; Make sure we use 'guix substitute'.
815 (set-build-options s
816 #:use-substitutes? #t
817 #:fallback? #f
818 #:substitute-urls (%test-substitute-urls))
819 (and (has-substitutes? s o)
820 (guard (c ((store-protocol-error? c)
821 ;; XXX: the daemon writes "hash mismatch in downloaded
822 ;; path", but the actual error returned to the client
823 ;; doesn't mention that.
824 (pk 'corrupt c)
825 (not (zero? (store-protocol-error-status c)))))
826 (build-derivations s (list d))
827 #f))))))
828
829 (test-assert "substitute, corrupt output hash, build trace"
830 ;; Likewise, and check the build trace.
831 (with-store s
832 (let* ((c "hello, world") ; contents of the output
833 (d (build-expression->derivation
834 s "corrupt-substitute"
835 `(mkdir %output)
836 #:guile-for-build
837 (package-derivation s %bootstrap-guile (%current-system))))
838 (o (derivation->output-path d)))
839 ;; Make sure we use 'guix substitute'.
840 (set-build-options s
841 #:print-build-trace #t
842 #:use-substitutes? #t
843 #:fallback? #f
844 #:substitute-urls (%test-substitute-urls))
845
846 (with-derivation-substitute d c
847 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
848
849 (define output
850 (call-with-output-string
851 (lambda (port)
852 (parameterize ((current-build-output-port port))
853 (guard (c ((store-protocol-error? c) #t))
854 (build-derivations s (list d))
855 #f)))))
856
857 (define actual-hash
858 (let-values (((port get-hash)
859 (gcrypt:open-hash-port
860 (gcrypt:hash-algorithm gcrypt:sha256))))
861 (write-file-tree "foo" port
862 #:file-type+size
863 (lambda _
864 (values 'regular (string-length c)))
865 #:file-port
866 (lambda _
867 (open-input-string c)))
868 (close-port port)
869 (bytevector->nix-base32-string (get-hash))))
870
871 (define expected-hash
872 (bytevector->nix-base32-string (make-bytevector 32 0)))
873
874 (define mismatch
875 (string-append "@ hash-mismatch " o " sha256 "
876 expected-hash " " actual-hash "\n"))
877
878 (define failure
879 (string-append "@ substituter-failed " o))
880
881 (and (string-contains output mismatch)
882 (string-contains output failure))))))
883
884 (test-assert "substitute --fallback"
885 (with-store s
886 (let* ((t (random-text)) ; contents of the output
887 (d (build-expression->derivation
888 s "substitute-me-not"
889 `(call-with-output-file %output
890 (lambda (p)
891 (display ,t p)))
892 #:guile-for-build
893 (package-derivation s %bootstrap-guile (%current-system))))
894 (o (derivation->output-path d)))
895 ;; Create fake substituter data, to be read by 'guix substitute'.
896 (with-derivation-narinfo d
897 ;; Make sure we use 'guix substitute'.
898 (set-build-options s #:use-substitutes? #t
899 #:substitute-urls (%test-substitute-urls))
900 (and (has-substitutes? s o)
901 (guard (c ((store-protocol-error? c)
902 ;; The substituter failed as expected. Now make
903 ;; sure that #:fallback? #t works correctly.
904 (set-build-options s
905 #:use-substitutes? #t
906 #:substitute-urls
907 (%test-substitute-urls)
908 #:fallback? #t)
909 (and (build-derivations s (list d))
910 (equal? t (call-with-input-file o
911 get-string-all)))))
912 ;; Should fail.
913 (build-derivations s (list d))
914 #f))))))
915
916 (test-assert "export/import several paths"
917 (let* ((texts (unfold (cut >= <> 10)
918 (lambda _ (random-text))
919 1+
920 0))
921 (files (map (cut add-text-to-store %store "text" <>) texts))
922 (dump (call-with-bytevector-output-port
923 (cut export-paths %store files <>))))
924 (delete-paths %store files)
925 (and (every (negate file-exists?) files)
926 (let* ((source (open-bytevector-input-port dump))
927 (imported (import-paths %store source)))
928 (and (equal? imported files)
929 (every file-exists? files)
930 (equal? texts
931 (map (lambda (file)
932 (call-with-input-file file
933 get-string-all))
934 files)))))))
935
936 (test-assert "export/import paths, ensure topological order"
937 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
938 (file1 (add-text-to-store %store "foo" (random-text)
939 (list file0)))
940 (file2 (add-text-to-store %store "bar" (random-text)
941 (list file1)))
942 (files (list file1 file2))
943 (dump1 (call-with-bytevector-output-port
944 (cute export-paths %store (list file1 file2) <>)))
945 (dump2 (call-with-bytevector-output-port
946 (cute export-paths %store (list file2 file1) <>))))
947 (delete-paths %store files)
948 (and (every (negate file-exists?) files)
949 (bytevector=? dump1 dump2)
950 (let* ((source (open-bytevector-input-port dump1))
951 (imported (import-paths %store source)))
952 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
953 (and (equal? imported (list file1 file2))
954 (every file-exists? files)
955 (equal? (list file0) (references %store file1))
956 (equal? (list file1) (references %store file2)))))))
957
958 (test-assert "export/import incomplete"
959 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
960 (file1 (add-text-to-store %store "foo" (random-text)
961 (list file0)))
962 (file2 (add-text-to-store %store "bar" (random-text)
963 (list file1)))
964 (dump (call-with-bytevector-output-port
965 (cute export-paths %store (list file2) <>))))
966 (delete-paths %store (list file0 file1 file2))
967 (guard (c ((store-protocol-error? c)
968 (and (not (zero? (store-protocol-error-status c)))
969 (string-contains (store-protocol-error-message c)
970 "not valid"))))
971 ;; Here we get an exception because DUMP does not include FILE0 and
972 ;; FILE1, which are dependencies of FILE2.
973 (import-paths %store (open-bytevector-input-port dump)))))
974
975 (test-assert "export/import recursive"
976 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
977 (file1 (add-text-to-store %store "foo" (random-text)
978 (list file0)))
979 (file2 (add-text-to-store %store "bar" (random-text)
980 (list file1)))
981 (dump (call-with-bytevector-output-port
982 (cute export-paths %store (list file2) <>
983 #:recursive? #t))))
984 (delete-paths %store (list file0 file1 file2))
985 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
986 (and (equal? imported (list file0 file1 file2))
987 (every file-exists? (list file0 file1 file2))
988 (equal? (list file0) (references %store file1))
989 (equal? (list file1) (references %store file2))))))
990
991 (test-assert "write-file & export-path yield the same result"
992 ;; Here we compare 'write-file' and the daemon's own implementation.
993 ;; 'write-file' is the reference because we know it sorts file
994 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
995 ;; currently happen to be sorted as a side-effect of some unrelated
996 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
997 ;; changes there.
998 (run-with-store %store
999 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
1000 (out1 -> (derivation->output-path drv1))
1001 (data -> (unfold (cut >= <> 26)
1002 (lambda (i)
1003 (random-bytevector 128))
1004 1+ 0))
1005 (build
1006 -> #~(begin
1007 (use-modules (rnrs io ports) (srfi srfi-1))
1008 (let ()
1009 (define letters
1010 (map (lambda (i)
1011 (string
1012 (integer->char
1013 (+ i (char->integer #\a)))))
1014 (iota 26)))
1015 (define (touch file data)
1016 (call-with-output-file file
1017 (lambda (port)
1018 (put-bytevector port data))))
1019
1020 (mkdir #$output)
1021 (chdir #$output)
1022
1023 ;; The files must be different so they have
1024 ;; different inode numbers, and the inode
1025 ;; order must differ from the lexicographic
1026 ;; order.
1027 (for-each touch
1028 (append (drop letters 10)
1029 (take letters 10))
1030 (list #$@data))
1031 #t)))
1032 (drv2 (gexp->derivation "bunch" build))
1033 (out2 -> (derivation->output-path drv2))
1034 (item-info -> (store-lift query-path-info)))
1035 (mbegin %store-monad
1036 (built-derivations (list drv1 drv2))
1037 (foldm %store-monad
1038 (lambda (item result)
1039 (define ref-hash
1040 (let-values (((port get) (gcrypt:open-sha256-port)))
1041 (write-file item port)
1042 (close-port port)
1043 (get)))
1044
1045 ;; 'query-path-info' returns a hash produced by using the
1046 ;; daemon's C++ 'dump' function, which is the implementation
1047 ;; under test.
1048 (>>= (item-info item)
1049 (lambda (info)
1050 (return
1051 (and result
1052 (bytevector=? (path-info-hash info) ref-hash))))))
1053 #t
1054 (list out1 out2))))
1055 #:guile-for-build (%guile-for-build)))
1056
1057
1058 (test-assert "import not signed"
1059 (let* ((text (random-text))
1060 (file (add-file-tree-to-store %store
1061 `("tree" directory
1062 ("text" regular (data ,text))
1063 ("link" symlink "text"))))
1064 (dump (call-with-bytevector-output-port
1065 (lambda (port)
1066 (write-int 1 port) ;start
1067
1068 (write-file file port) ;contents
1069 (write-int #x4558494e port) ;%export-magic
1070 (write-string file port) ;store item
1071 (write-string-list '() port) ;references
1072 (write-string "" port) ;deriver
1073 (write-int 0 port) ;not signed
1074
1075 (write-int 0 port))))) ;done
1076
1077 ;; Ensure 'import-paths' raises an exception.
1078 (guard (c ((store-protocol-error? c)
1079 (and (not (zero? (store-protocol-error-status c)))
1080 (string-contains (store-protocol-error-message c)
1081 "lacks a signature"))))
1082 (let* ((source (open-bytevector-input-port dump))
1083 (imported (import-paths %store source)))
1084 (pk 'unsigned-imported imported)
1085 #f))))
1086
1087 (test-assert "import signed by unauthorized key"
1088 (let* ((text (random-text))
1089 (file (add-file-tree-to-store %store
1090 `("tree" directory
1091 ("text" regular (data ,text))
1092 ("link" symlink "text"))))
1093 (key (gcrypt:generate-key
1094 (gcrypt:string->canonical-sexp
1095 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1096 (dump (call-with-bytevector-output-port
1097 (lambda (port)
1098 (write-int 1 port) ;start
1099
1100 (write-file file port) ;contents
1101 (write-int #x4558494e port) ;%export-magic
1102 (write-string file port) ;store item
1103 (write-string-list '() port) ;references
1104 (write-string "" port) ;deriver
1105 (write-int 1 port) ;signed
1106 (write-string (gcrypt:canonical-sexp->string
1107 (signature-sexp
1108 (gcrypt:bytevector->hash-data
1109 (gcrypt:sha256 #vu8(0 1 2))
1110 #:key-type 'ecc)
1111 (gcrypt:find-sexp-token key 'private-key)
1112 (gcrypt:find-sexp-token key 'public-key)))
1113 port)
1114
1115 (write-int 0 port))))) ;done
1116
1117 ;; Ensure 'import-paths' raises an exception.
1118 (guard (c ((store-protocol-error? c)
1119 (and (not (zero? (store-protocol-error-status c)))
1120 (string-contains (store-protocol-error-message c)
1121 "unauthorized public key"))))
1122 (let* ((source (open-bytevector-input-port dump))
1123 (imported (import-paths %store source)))
1124 (pk 'unauthorized-imported imported)
1125 #f))))
1126
1127 (test-assert "import corrupt path"
1128 (let* ((text (random-text))
1129 (file (add-text-to-store %store "text" text))
1130 (dump (call-with-bytevector-output-port
1131 (cut export-paths %store (list file) <>))))
1132 (delete-paths %store (list file))
1133
1134 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1135 ;; the file contents in DUMP, regardless of the store prefix.
1136 (let* ((index #x70)
1137 (byte (bytevector-u8-ref dump index)))
1138 (bytevector-u8-set! dump index (logxor #xff byte)))
1139
1140 (and (not (file-exists? file))
1141 (guard (c ((store-protocol-error? c)
1142 (pk 'c c)
1143 (and (not (zero? (store-protocol-error-status c)))
1144 (string-contains (store-protocol-error-message c)
1145 "corrupt"))))
1146 (let* ((source (open-bytevector-input-port dump))
1147 (imported (import-paths %store source)))
1148 (pk 'corrupt-imported imported)
1149 #f)))))
1150
1151 (test-assert "verify-store"
1152 (let* ((text (random-text))
1153 (file1 (add-text-to-store %store "foo" text))
1154 (file2 (add-text-to-store %store "bar" (random-text)
1155 (list file1))))
1156 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1157 (begin
1158 (delete-file file1)
1159 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1160 (begin
1161 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1162 ;; without actually creating the file. ;
1163 (call-with-output-file file1
1164 (lambda (port)
1165 (display text port)))
1166 (pk 'verify3 (verify-store %store)))))) ;OK again
1167
1168 (test-assert "verify-store + check-contents"
1169 ;; XXX: This test is I/O intensive.
1170 (with-store s
1171 (let* ((text (random-text))
1172 (drv (build-expression->derivation
1173 s "corrupt"
1174 `(let ((out (assoc-ref %outputs "out")))
1175 (call-with-output-file out
1176 (lambda (port)
1177 (display ,text port)))
1178 #t)
1179 #:guile-for-build
1180 (package-derivation s %bootstrap-guile (%current-system))))
1181 (file (derivation->output-path drv)))
1182 (with-derivation-substitute drv text
1183 (and (build-derivations s (list drv))
1184 (verify-store s #:check-contents? #t) ;should be OK
1185 (begin
1186 (chmod file #o644)
1187 (call-with-output-file file
1188 (lambda (port)
1189 (display "corrupt!" port)))
1190 #t)
1191
1192 ;; Make sure the corruption is detected. We don't test repairing
1193 ;; because only "trusted" users are allowed to do it, but we
1194 ;; don't expose that notion of trusted users that nix-daemon
1195 ;; supports because it seems dubious and redundant with what the
1196 ;; OS provides (in Nix "trusted" users have additional
1197 ;; privileges, such as overriding the set of substitute URLs, but
1198 ;; we instead want to allow anyone to modify them, provided
1199 ;; substitutes are signed by a root-approved key.)
1200 (not (verify-store s #:check-contents? #t))
1201
1202 ;; Delete the corrupt item to leave the store in a clean state.
1203 (delete-paths s (list file)))))))
1204
1205 (test-assert "build-things, check mode"
1206 (with-store store
1207 (call-with-temporary-output-file
1208 (lambda (entropy entropy-port)
1209 (write (random-text) entropy-port)
1210 (force-output entropy-port)
1211 (let* ((drv (build-expression->derivation
1212 store "non-deterministic"
1213 `(begin
1214 (use-modules (rnrs io ports))
1215 (let ((out (assoc-ref %outputs "out")))
1216 (call-with-output-file out
1217 (lambda (port)
1218 ;; Rely on the fact that tests do not use the
1219 ;; chroot, and thus ENTROPY is readable.
1220 (display (call-with-input-file ,entropy
1221 get-string-all)
1222 port)))
1223 #t))
1224 #:guile-for-build
1225 (package-derivation store %bootstrap-guile (%current-system))))
1226 (file (derivation->output-path drv)))
1227 (and (build-things store (list (derivation-file-name drv)))
1228 (begin
1229 (write (random-text) entropy-port)
1230 (force-output entropy-port)
1231 (guard (c ((store-protocol-error? c)
1232 (pk 'determinism-exception c)
1233 (and (not (zero? (store-protocol-error-status c)))
1234 (string-contains (store-protocol-error-message c)
1235 "deterministic"))))
1236 ;; This one will produce a different result. Since we're in
1237 ;; 'check' mode, this must fail.
1238 (build-things store (list (derivation-file-name drv))
1239 (build-mode check))
1240 #f))))))))
1241
1242 (test-assert "build-succeeded trace in check mode"
1243 (string-contains
1244 (call-with-output-string
1245 (lambda (port)
1246 (let ((d (build-expression->derivation
1247 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1248 #:guile-for-build
1249 (package-derivation %store %bootstrap-guile))))
1250 (build-derivations %store (list d))
1251 (parameterize ((current-build-output-port port))
1252 (build-derivations %store (list d) (build-mode check))))))
1253 "@ build-succeeded"))
1254
1255 (test-assert "build multiple times"
1256 (with-store store
1257 ;; Ask to build twice.
1258 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1259
1260 (call-with-temporary-output-file
1261 (lambda (entropy entropy-port)
1262 (write (random-text) entropy-port)
1263 (force-output entropy-port)
1264 (let* ((drv (build-expression->derivation
1265 store "non-deterministic"
1266 `(begin
1267 (use-modules (rnrs io ports))
1268 (let ((out (assoc-ref %outputs "out")))
1269 (call-with-output-file out
1270 (lambda (port)
1271 ;; Rely on the fact that tests do not use the
1272 ;; chroot, and thus ENTROPY is accessible.
1273 (display (call-with-input-file ,entropy
1274 get-string-all)
1275 port)
1276 (call-with-output-file ,entropy
1277 (lambda (port)
1278 (write 'foobar port)))))
1279 #t))
1280 #:guile-for-build
1281 (package-derivation store %bootstrap-guile (%current-system))))
1282 (file (derivation->output-path drv)))
1283 (guard (c ((store-protocol-error? c)
1284 (pk 'multiple-build c)
1285 (and (not (zero? (store-protocol-error-status c)))
1286 (string-contains (store-protocol-error-message c)
1287 "deterministic"))))
1288 ;; This one will produce a different result on the second run.
1289 (current-build-output-port (current-error-port))
1290 (build-things store (list (derivation-file-name drv)))
1291 #f))))))
1292
1293 (test-equal "store-lower"
1294 "Lowered."
1295 (let* ((add (store-lower text-file))
1296 (file (add %store "foo" "Lowered.")))
1297 (call-with-input-file file get-string-all)))
1298
1299 (test-equal "current-system"
1300 "bar"
1301 (parameterize ((%current-system "frob"))
1302 (run-with-store %store
1303 (mbegin %store-monad
1304 (set-current-system "bar")
1305 (current-system))
1306 #:system "foo")))
1307
1308 (test-assert "query-path-info"
1309 (let* ((ref (add-text-to-store %store "ref" "foo"))
1310 (item (add-text-to-store %store "item" "bar" (list ref)))
1311 (info (query-path-info %store item)))
1312 (and (equal? (path-info-references info) (list ref))
1313 (equal? (path-info-hash info)
1314 (gcrypt:sha256
1315 (string->utf8
1316 (call-with-output-string (cut write-file item <>))))))))
1317
1318 (test-assert "path-info-deriver"
1319 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1320 (s (add-to-store %store "bash" #t "sha256"
1321 (search-bootstrap-binary "bash"
1322 (%current-system))))
1323 (d (derivation %store "the-thing"
1324 s `("-e" ,b)
1325 #:env-vars `(("foo" . ,(random-text)))
1326 #:inputs `((,b) (,s))))
1327 (o (derivation->output-path d)))
1328 (and (build-derivations %store (list d))
1329 (not (path-info-deriver (query-path-info %store b)))
1330 (string=? (derivation-file-name d)
1331 (path-info-deriver (query-path-info %store o))))))
1332
1333 (test-equal "build-cores"
1334 (list 0 42)
1335 (with-store store
1336 (let* ((build (add-text-to-store store "build.sh"
1337 "echo $NIX_BUILD_CORES > $out"))
1338 (bash (add-to-store store "bash" #t "sha256"
1339 (search-bootstrap-binary "bash"
1340 (%current-system))))
1341 (drv1 (derivation store "the-thing" bash
1342 `("-e" ,build)
1343 #:inputs `((,bash) (,build))
1344 #:env-vars `(("x" . ,(random-text)))))
1345 (drv2 (derivation store "the-thing" bash
1346 `("-e" ,build)
1347 #:inputs `((,bash) (,build))
1348 #:env-vars `(("x" . ,(random-text))))))
1349 (and (build-derivations store (list drv1))
1350 (begin
1351 (set-build-options store #:build-cores 42)
1352 (build-derivations store (list drv2)))
1353 (list (call-with-input-file (derivation->output-path drv1)
1354 read)
1355 (call-with-input-file (derivation->output-path drv2)
1356 read))))))
1357
1358 (test-equal "multiplexed-build-output"
1359 '("Hello from first." "Hello from second.")
1360 (with-store store
1361 (let* ((build (add-text-to-store store "build.sh"
1362 "echo Hello from $NAME.; echo > $out"))
1363 (bash (add-to-store store "bash" #t "sha256"
1364 (search-bootstrap-binary "bash"
1365 (%current-system))))
1366 (drv1 (derivation store "one" bash
1367 `("-e" ,build)
1368 #:inputs `((,bash) (,build))
1369 #:env-vars `(("NAME" . "first")
1370 ("x" . ,(random-text)))))
1371 (drv2 (derivation store "two" bash
1372 `("-e" ,build)
1373 #:inputs `((,bash) (,build))
1374 #:env-vars `(("NAME" . "second")
1375 ("x" . ,(random-text))))))
1376 (set-build-options store
1377 #:print-build-trace #t
1378 #:multiplexed-build-output? #t
1379 #:max-build-jobs 10)
1380 (let ((port (open-output-string)))
1381 ;; Send the build log to PORT.
1382 (parameterize ((current-build-output-port port))
1383 (build-derivations store (list drv1 drv2)))
1384
1385 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1386 ;; traces that allow us to retrieve each builder's output (we assume
1387 ;; there's exactly one "build-output" trace for each builder, which is
1388 ;; reasonable.)
1389 (let* ((log (get-output-string port))
1390 (started (fold-matches
1391 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1392 log '() cons))
1393 (done (fold-matches
1394 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1395 log '() cons))
1396 (output (fold-matches
1397 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1398 log '() cons))
1399 (drv-pid (lambda (name)
1400 (lambda (m)
1401 (let ((drv (match:substring m 1))
1402 (pid (string->number
1403 (match:substring m 4))))
1404 (and (string-suffix? name drv) pid)))))
1405 (pid-log (lambda (pid)
1406 (lambda (m)
1407 (let ((n (string->number
1408 (match:substring m 1)))
1409 (len (string->number
1410 (match:substring m 2)))
1411 (str (match:substring m 3)))
1412 (and (= pid n)
1413 (= (string-length str) (- len 1))
1414 str)))))
1415 (pid1 (any (drv-pid "one.drv") started))
1416 (pid2 (any (drv-pid "two.drv") started)))
1417 (list (any (pid-log pid1) output)
1418 (any (pid-log pid2) output)))))))
1419
1420 (test-end "store")