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