1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 (guix base32)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29 #:use-module (guix serialization)
30 #:use-module (guix build utils)
31 #:use-module (guix gexp)
32 #:use-module (gnu packages)
33 #:use-module (gnu packages bootstrap)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (rnrs bytevectors)
37 #:use-module (rnrs io ports)
38 #:use-module (web uri)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-64))
45 ;; Test the (guix store) module.
48 (open-connection-for-tests))
51 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
56 (test-assert "open-connection with file:// URI"
57 (let ((store (open-connection (string-append "file://"
58 (%daemon-socket-uri)))))
59 (and (add-text-to-store store "foo" "bar")
61 (close-connection store)
64 (test-equal "connection handshake error"
66 (let ((port (%make-void-port "rw")))
67 (guard (c ((store-connection-error? c)
68 (and (eq? port (store-connection-error-file c))
69 (store-connection-error-code c))))
70 (open-connection #f #:port port)
73 (test-equal "store-path-hash-part"
74 "283gqy39v3g9dxjy26rynl0zls82fmcg"
76 (string-append (%store-prefix)
77 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
79 (test-equal "store-path-hash-part #f"
82 (string-append (%store-prefix)
83 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
85 (test-equal "store-path-package-name"
87 (store-path-package-name
88 (string-append (%store-prefix)
89 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
91 (test-equal "store-path-package-name #f"
93 (store-path-package-name
94 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
96 (test-assert "direct-store-path?"
97 (and (direct-store-path?
98 (string-append (%store-prefix)
99 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
100 (not (direct-store-path?
103 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
104 (not (direct-store-path? (%store-prefix)))))
106 (test-skip (if %store 0 15))
108 (test-equal "profiles/per-user exists and is not writable"
110 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
112 (test-equal "profiles/per-user/$USER exists"
113 (list (getuid) #o755)
114 (let ((s (stat (string-append %state-directory "/profiles/per-user/"
115 (passwd:name (getpwuid (getuid)))))))
116 (list (stat:uid s) (stat:perms s))))
118 (test-equal "add-to-store"
119 '("sha1" "sha256" "sha512")
120 (let* ((file (search-path %load-path "guix.scm"))
121 (content (call-with-input-file file get-bytevector-all)))
122 (map (lambda (hash-algo)
123 (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
124 (and (direct-store-path? file)
125 (bytevector=? (call-with-input-file file get-bytevector-all)
128 '("sha1" "sha256" "sha512"))))
130 (test-equal "add-data-to-store"
132 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
135 (test-assert "valid-path? live"
136 (let ((p (add-text-to-store %store "hello" "hello, world")))
137 (valid-path? %store p)))
139 (test-assert "valid-path? false"
140 (not (valid-path? %store
141 (string-append (%store-prefix) "/"
142 (make-string 32 #\e) "-foobar"))))
144 (test-assert "valid-path? error"
146 (guard (c ((store-protocol-error? c) #t))
147 (valid-path? s "foo")
150 (test-assert "valid-path? recovery"
151 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
152 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
154 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
157 (let-syntax ((true-if-error (syntax-rules ()
159 (guard (c ((store-protocol-error? c) #t))
161 (and (true-if-error (valid-path? s "foo"))
162 (true-if-error (valid-path? s "bar"))
163 (true-if-error (valid-path? s "baz"))
164 (true-if-error (valid-path? s "chbouib"))
165 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
167 (test-assert "hash-part->path"
168 (let ((p (add-text-to-store %store "hello" "hello, world")))
169 (equal? (hash-part->path %store (store-path-hash-part p))
172 (test-assert "dead-paths"
173 (let ((p (add-text-to-store %store "random-text" (random-text))))
174 (->bool (member p (dead-paths %store)))))
176 ;; FIXME: Find a test for `live-paths'.
178 ;; (test-assert "temporary root is in live-paths"
179 ;; (let* ((p1 (add-text-to-store %store "random-text"
180 ;; (random-text) '()))
181 ;; (b (add-text-to-store %store "link-builder"
182 ;; (format #f "echo ~a > $out" p1)
184 ;; (d1 (derivation %store "link"
185 ;; "/bin/sh" `("-e" ,b)
186 ;; #:inputs `((,b) (,p1))))
187 ;; (p2 (derivation->output-path d1)))
188 ;; (and (add-temp-root %store p2)
189 ;; (build-derivations %store (list d1))
190 ;; (valid-path? %store p1)
191 ;; (member (pk p2) (live-paths %store)))))
193 (test-assert "permanent root"
194 (let* ((p (with-store store
195 (let ((p (add-text-to-store store "random-text"
197 (add-permanent-root p)
198 (add-permanent-root p) ; should not throw
200 (and (member p (live-paths %store))
202 (remove-permanent-root p)
203 (->bool (member p (dead-paths %store)))))))
205 (test-assert "dead path can be explicitly collected"
206 (let ((p (add-text-to-store %store "random-text"
208 (let-values (((paths freed) (delete-paths %store (list p))))
209 (and (equal? paths (list p))
210 ;; XXX: On some file systems (notably Btrfs), freed
211 ;; may return 0. See <https://bugs.gnu.org/29363>.
213 (not (file-exists? p))))))
215 (test-assert "add-text-to-store vs. delete-paths"
216 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
217 ;; is no longer valid.
219 (let* ((text (random-text))
220 (path (add-text-to-store store "delete-me" text))
221 (deleted (delete-paths store (list path)))
222 (path2 (add-text-to-store store "delete-me" text)))
223 (and (string=? path path2)
224 (equal? deleted (list path))
225 (valid-path? store path)
226 (file-exists? path)))))
228 (test-assert "add-to-store vs. delete-paths"
231 (let* ((file (search-path %load-path "guix.scm"))
232 (path (add-to-store store "delete-me" #t "sha256" file))
233 (deleted (delete-paths store (list path)))
234 (path2 (add-to-store store "delete-me" #t "sha256" file)))
235 (and (string=? path path2)
236 (equal? deleted (list path))
237 (valid-path? store path)
238 (file-exists? path)))))
240 (test-equal "add-file-tree-to-store"
243 ("./bar" directory #t)
244 ("./foo" directory #t)
245 ("./foo/a" regular "file a")
246 ("./foo/b" symlink "a")
247 ("./foo/c" directory #t)
248 ("./foo/c/p" regular "file p")
249 ("./foo/c/q" directory #t)
250 ("./foo/c/q/x" regular
251 ,(string-append "#!" %shell "\nexit 42"))
252 ("./foo/c/q/y" symlink "..")
253 ("./foo/c/q/z" directory #t))
254 (let* ((tree `("file-tree" directory
256 ("a" regular (data "file a"))
259 ("p" regular (data ,(string->utf8 "file p")))
262 (data ,(string-append "#!" %shell "\nexit 42")))
266 (result (add-file-tree-to-store %store tree)))
267 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
268 (with-directory-excursion result
270 (let ((type (stat:type (lstat file))))
273 ((or 'regular 'executable)
274 (call-with-input-file file
276 ('symlink (readlink file))
278 (find-files "." #:directories? #t))))))
280 (test-equal "add-file-tree-to-store, flat"
282 (let* ((tree `("flat-file" regular (data "Hello, world!")))
283 (result (add-file-tree-to-store %store tree)))
284 (and (file-exists? result)
285 (call-with-input-file result get-string-all))))
287 (test-assert "references"
288 (let* ((t1 (add-text-to-store %store "random1"
290 (t2 (add-text-to-store %store "random2"
291 (random-text) (list t1))))
292 (and (equal? (list t1) (references %store t2))
293 (equal? (list t2) (referrers %store t1))
294 (null? (references %store t1))
295 (null? (referrers %store t2)))))
297 (test-assert "references/substitutes missing reference info"
299 (set-build-options s #:use-substitutes? #f)
300 (guard (c ((store-protocol-error? c) #t))
301 (let* ((b (add-to-store s "bash" #t "sha256"
302 (search-bootstrap-binary "bash"
304 (d (derivation s "the-thing" b '("--help")
306 (references/substitutes s (list (derivation->output-path d) b))
309 (test-assert "references/substitutes with substitute info"
311 (set-build-options s #:use-substitutes? #t)
312 (let* ((t1 (add-text-to-store s "random1" (random-text)))
313 (t2 (add-text-to-store s "random2" (random-text)
315 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
316 (b (add-to-store s "bash" #t "sha256"
317 (search-bootstrap-binary "bash"
319 (d (derivation s "the-thing" b `("-e" ,t3)
320 #:inputs `((,b) (,t3) (,t2))
321 #:env-vars `(("t2" . ,t2))))
322 (o (derivation->output-path d)))
323 (with-derivation-narinfo d
324 (sha256 => (gcrypt:sha256 (string->utf8 t2)))
325 (references => (list t2))
327 (equal? (references/substitutes s (list o t3 t2 t1))
333 (test-equal "substitutable-path-info when substitutes are turned off"
336 (set-build-options s #:use-substitutes? #f)
337 (let* ((b (add-to-store s "bash" #t "sha256"
338 (search-bootstrap-binary "bash"
340 (d (derivation s "the-thing" b '("--version")
342 (o (derivation->output-path d)))
343 (with-derivation-narinfo d
344 (substitutable-path-info s (list o))))))
346 (test-equal "substitutable-paths when substitutes are turned off"
349 (set-build-options s #:use-substitutes? #f)
350 (let* ((b (add-to-store s "bash" #t "sha256"
351 (search-bootstrap-binary "bash"
353 (d (derivation s "the-thing" b '("--version")
355 (o (derivation->output-path d)))
356 (with-derivation-narinfo d
357 (substitutable-paths s (list o))))))
359 (test-assert "requisites"
360 (let* ((t1 (add-text-to-store %store "random1"
362 (t2 (add-text-to-store %store "random2"
363 (random-text) (list t1)))
364 (t3 (add-text-to-store %store "random3"
365 (random-text) (list t2)))
366 (t4 (add-text-to-store %store "random4"
367 (random-text) (list t1 t3))))
369 (and (= (length x) (length y))
372 (and (same? (requisites %store (list t1)) (list t1))
373 (same? (requisites %store (list t2)) (list t1 t2))
374 (same? (requisites %store (list t3)) (list t1 t2 t3))
375 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
376 (same? (requisites %store (list t1 t2 t3 t4))
377 (list t1 t2 t3 t4)))))
379 (test-assert "derivers"
380 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
381 (s (add-to-store %store "bash" #t "sha256"
382 (search-bootstrap-binary "bash"
384 (d (derivation %store "the-thing"
386 #:env-vars `(("foo" . ,(random-text)))
387 #:inputs `((,b) (,s))))
388 (o (derivation->output-path d)))
389 (and (build-derivations %store (list d))
390 (equal? (query-derivation-outputs %store (derivation-file-name d))
392 (equal? (valid-derivers %store o)
393 (list (derivation-file-name d))))))
395 (test-equal "with-build-handler"
397 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
398 (s (add-to-store %store "bash" #t "sha256"
399 (search-bootstrap-binary "bash"
401 (d1 (derivation %store "the-thing"
403 #:env-vars `(("foo" . ,(random-text)))
404 #:sources (list b s)))
405 (d2 (derivation %store "the-thing"
407 #:env-vars `(("foo" . ,(random-text))
409 #:sources (list b s)))
410 (o1 (derivation->output-path d1))
411 (o2 (derivation->output-path d2)))
414 (lambda (continue store things mode)
417 (set! counter (+ 1 counter))
418 (if (string=? drv (derivation-file-name d1))
420 (and (string=? drv (derivation-file-name d2))
423 (build-derivations %store (list d1))
424 (build-derivations %store (list d2))
427 (test-equal "with-build-handler + with-store"
429 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
430 ;; even though 'with-build-handler' is outside the dynamic extent of
432 (with-build-handler (lambda (continue store things mode)
435 (and (string-suffix? "thingie.drv" drv)
437 (store-connection-socket store)))
440 (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
441 (s (add-to-store store "bash" #t "sha256"
442 (search-bootstrap-binary "bash"
444 (d (derivation store "thingie"
446 #:env-vars `(("foo" . ,(random-text)))
447 #:sources (list b s))))
448 (build-derivations store (list d))
450 ;; Here STORE's socket should still be open.
451 (and (valid-path? store (derivation->output-path d))
454 (test-assert "map/accumulate-builds"
455 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
456 (s (add-to-store %store "bash" #t "sha256"
457 (search-bootstrap-binary "bash"
459 (d1 (derivation %store "the-thing"
461 #:env-vars `(("foo" . ,(random-text)))
462 #:sources (list b s)))
463 (d2 (derivation %store "the-thing"
465 #:env-vars `(("foo" . ,(random-text))
467 #:sources (list b s))))
468 (with-build-handler (lambda (continue store things mode)
469 (equal? (map derivation-file-name (list d1 d2))
471 (map/accumulate-builds %store
473 (build-derivations %store (list drv))
474 (add-to-store %store "content-addressed"
476 (derivation->output-path drv)))
479 (test-assert "mapm/accumulate-builds"
480 (let* ((d1 (run-with-store %store
481 (gexp->derivation "foo" #~(mkdir #$output))))
482 (d2 (run-with-store %store
483 (gexp->derivation "bar" #~(mkdir #$output)))))
484 (with-build-handler (lambda (continue store things mode)
485 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
487 (run-with-store %store
488 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
490 (test-equal "mapm/accumulate-builds, %current-target-system"
491 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
492 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
494 (run-with-store %store
495 (mlet %store-monad ((lst1 (mapm %store-monad
497 (current-target-system))
499 (lst2 (mapm/accumulate-builds
501 (current-target-system))
503 (return (list lst1 lst2)))
505 #:target "i586-pc-gnu"))
507 (test-assert "topologically-sorted, one item"
508 (let* ((a (add-text-to-store %store "a" "a"))
509 (b (add-text-to-store %store "b" "b" (list a)))
510 (c (add-text-to-store %store "c" "c" (list b)))
511 (d (add-text-to-store %store "d" "d" (list c)))
512 (s (topologically-sorted %store (list d))))
513 (equal? s (list a b c d))))
515 (test-assert "topologically-sorted, several items"
516 (let* ((a (add-text-to-store %store "a" "a"))
517 (b (add-text-to-store %store "b" "b" (list a)))
518 (c (add-text-to-store %store "c" "c" (list b)))
519 (d (add-text-to-store %store "d" "d" (list c)))
520 (s1 (topologically-sorted %store (list d a c b)))
521 (s2 (topologically-sorted %store (list b d c a b d))))
522 (equal? s1 s2 (list a b c d))))
524 (test-assert "topologically-sorted, more difficult"
525 (let* ((a (add-text-to-store %store "a" "a"))
526 (b (add-text-to-store %store "b" "b" (list a)))
527 (c (add-text-to-store %store "c" "c" (list b)))
528 (d (add-text-to-store %store "d" "d" (list c)))
529 (w (add-text-to-store %store "w" "w"))
530 (x (add-text-to-store %store "x" "x" (list w)))
531 (y (add-text-to-store %store "y" "y" (list x d)))
532 (s1 (topologically-sorted %store (list y)))
533 (s2 (topologically-sorted %store (list c y)))
534 (s3 (topologically-sorted %store (cons y (references %store y)))))
535 ;; The order in which 'references' returns the references of Y is
536 ;; unspecified, so accommodate.
537 (let* ((x-then-d? (equal? (references %store y) (list x d))))
541 (list a b c d w x y)))
545 (list a b c d w x y)))
546 (lset= string=? s1 s3)))))
548 (test-assert "current-build-output-port, UTF-8"
549 ;; Are UTF-8 strings in the build log properly interpreted?
551 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
552 (call-with-output-string
554 (parameterize ((current-build-output-port port))
555 (let* ((s "Here’s a Greek letter: λ.")
556 (d (build-expression->derivation
557 %store "foo" `(display ,s)
559 (package-derivation s %bootstrap-guile (%current-system)))))
560 (guard (c ((store-protocol-error? c) #t))
561 (build-derivations %store (list d))))))))
562 "Here’s a Greek letter: λ."))
564 (test-assert "current-build-output-port, UTF-8 + garbage"
565 ;; What about a mixture of UTF-8 + garbage?
567 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
568 (call-with-output-string
570 (parameterize ((current-build-output-port port))
571 (let ((d (build-expression->derivation
574 (use-modules (rnrs io ports))
575 (display "garbage: ")
576 (put-bytevector (current-output-port) #vu8(128))
577 (display "lambda: λ\n"))
579 (package-derivation %store %bootstrap-guile))))
580 (guard (c ((store-protocol-error? c) #t))
581 (build-derivations %store (list d))))))))
582 "garbage: �lambda: λ"))
584 (test-assert "log-file, derivation"
585 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
586 (s (add-to-store %store "bash" #t "sha256"
587 (search-bootstrap-binary "bash"
589 (d (derivation %store "the-thing"
591 #:env-vars `(("foo" . ,(random-text)))
592 #:inputs `((,b) (,s)))))
593 (and (build-derivations %store (list d))
594 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
596 (test-assert "log-file, output file name"
597 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
598 (s (add-to-store %store "bash" #t "sha256"
599 (search-bootstrap-binary "bash"
601 (d (derivation %store "the-thing"
603 #:env-vars `(("foo" . ,(random-text)))
604 #:inputs `((,b) (,s))))
605 (o (derivation->output-path d)))
606 (and (build-derivations %store (list d))
607 (file-exists? (pk (log-file %store o)))
608 (string=? (log-file %store (derivation-file-name d))
609 (log-file %store o)))))
611 (test-assert "no substitutes"
613 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
614 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
615 (o (map derivation->output-path (list d1 d2))))
616 (set-build-options s #:use-substitutes? #f)
617 (and (not (has-substitutes? s (derivation-file-name d1)))
618 (not (has-substitutes? s (derivation-file-name d2)))
619 (null? (substitutable-paths s o))
620 (null? (substitutable-path-info s o))))))
622 (test-assert "build-things with output path"
624 (let* ((c (random-text)) ;contents of the output
625 (d (build-expression->derivation
627 `(call-with-output-file %output
631 (package-derivation s %bootstrap-guile (%current-system))))
632 (o (derivation->output-path d)))
633 (set-build-options s #:use-substitutes? #f)
635 ;; Pass 'build-things' the output file name, O. However, since there
636 ;; are no substitutes for O, it will just do nothing.
637 (build-things s (list o))
638 (not (valid-path? s o)))))
640 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
642 (test-assert "substitute query"
644 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
645 (o (derivation->output-path d)))
646 ;; Create fake substituter data, to be read by 'guix substitute'.
647 (with-derivation-narinfo d
648 ;; Remove entry from the local cache.
650 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
651 "/guix/substitute")))
653 ;; Make sure 'guix substitute' correctly communicates the above
655 (set-build-options s #:use-substitutes? #t
656 #:substitute-urls (%test-substitute-urls))
657 (and (has-substitutes? s o)
658 (equal? (list o) (substitutable-paths s (list o)))
659 (match (pk 'spi (substitutable-path-info s (list o)))
660 (((? substitutable? s))
661 (and (string=? (substitutable-deriver s)
662 (derivation-file-name d))
663 (null? (substitutable-references s))
664 (equal? (substitutable-nar-size s) 1234)))))))))
666 (test-assert "substitute query, alternating URLs"
667 (let* ((d (with-store s
668 (package-derivation s %bootstrap-guile (%current-system))))
669 (o (derivation->output-path d)))
670 (with-derivation-narinfo d
671 ;; Remove entry from the local cache.
673 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
674 "/guix/substitute")))
676 ;; Note: We reconnect to the daemon to force a new instance of 'guix
677 ;; substitute' to be used; otherwise the #:substitute-urls of
678 ;; 'set-build-options' would have no effect.
680 (and (with-store s ;the right substitute URL
681 (set-build-options s #:use-substitutes? #t
682 #:substitute-urls (%test-substitute-urls))
683 (has-substitutes? s o))
684 (with-store s ;the wrong one
685 (set-build-options s #:use-substitutes? #t
686 #:substitute-urls (list
687 "http://does-not-exist"))
688 (not (has-substitutes? s o)))
689 (with-store s ;the right one again
690 (set-build-options s #:use-substitutes? #t
691 #:substitute-urls (%test-substitute-urls))
692 (has-substitutes? s o))
693 (with-store s ;empty list of URLs
694 (set-build-options s #:use-substitutes? #t
695 #:substitute-urls '())
696 (not (has-substitutes? s o)))))))
698 (test-assert "substitute"
700 (let* ((c (random-text)) ; contents of the output
701 (d (build-expression->derivation
703 `(call-with-output-file %output
705 (exit 1) ; would actually fail
708 (package-derivation s %bootstrap-guile (%current-system))))
709 (o (derivation->output-path d)))
710 (with-derivation-substitute d c
711 (set-build-options s #:use-substitutes? #t
712 #:substitute-urls (%test-substitute-urls))
713 (and (has-substitutes? s o)
714 (build-derivations s (list d))
715 (equal? c (call-with-input-file o get-string-all)))))))
717 (test-assert "substitute + build-things with output path"
719 (let* ((c (random-text)) ;contents of the output
720 (d (build-expression->derivation
722 `(call-with-output-file %output
724 (exit 1) ;would actually fail
727 (package-derivation s %bootstrap-guile (%current-system))))
728 (o (derivation->output-path d)))
729 (with-derivation-substitute d c
730 (set-build-options s #:use-substitutes? #t
731 #:substitute-urls (%test-substitute-urls))
732 (and (has-substitutes? s o)
733 (build-things s (list o)) ;give the output path
735 (equal? c (call-with-input-file o get-string-all)))))))
737 (test-assert "substitute + build-things with specific output"
739 (let* ((c (random-text)) ;contents of the output
740 (d (build-expression->derivation
741 s "substitute-me" `(begin ,c (exit 1)) ;would fail
742 #:outputs '("out" "one" "two")
744 (package-derivation s %bootstrap-guile (%current-system))))
745 (o (derivation->output-path d)))
746 (with-derivation-substitute d c
747 (set-build-options s #:use-substitutes? #t
748 #:substitute-urls (%test-substitute-urls))
749 (and (has-substitutes? s o)
751 ;; Ask for nothing but the "out" output of D.
752 (build-things s `((,(derivation-file-name d) . "out")))
755 (equal? c (call-with-input-file o get-string-all)))))))
757 (test-assert "substitute, corrupt output hash"
758 ;; Tweak the substituter into installing a substitute whose hash doesn't
759 ;; match the one announced in the narinfo. The daemon must notice this and
762 (let* ((c "hello, world") ; contents of the output
763 (d (build-expression->derivation
764 s "corrupt-substitute"
767 (package-derivation s %bootstrap-guile (%current-system))))
768 (o (derivation->output-path d)))
769 (with-derivation-substitute d c
770 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
772 ;; Make sure we use 'guix substitute'.
774 #:use-substitutes? #t
776 #:substitute-urls (%test-substitute-urls))
777 (and (has-substitutes? s o)
778 (guard (c ((store-protocol-error? c)
779 ;; XXX: the daemon writes "hash mismatch in downloaded
780 ;; path", but the actual error returned to the client
781 ;; doesn't mention that.
783 (not (zero? (store-protocol-error-status c)))))
784 (build-derivations s (list d))
787 (test-assert "substitute --fallback"
789 (let* ((t (random-text)) ; contents of the output
790 (d (build-expression->derivation
791 s "substitute-me-not"
792 `(call-with-output-file %output
796 (package-derivation s %bootstrap-guile (%current-system))))
797 (o (derivation->output-path d)))
798 ;; Create fake substituter data, to be read by 'guix substitute'.
799 (with-derivation-narinfo d
800 ;; Make sure we use 'guix substitute'.
801 (set-build-options s #:use-substitutes? #t
802 #:substitute-urls (%test-substitute-urls))
803 (and (has-substitutes? s o)
804 (guard (c ((store-protocol-error? c)
805 ;; The substituter failed as expected. Now make
806 ;; sure that #:fallback? #t works correctly.
808 #:use-substitutes? #t
810 (%test-substitute-urls)
812 (and (build-derivations s (list d))
813 (equal? t (call-with-input-file o
816 (build-derivations s (list d))
819 (test-assert "export/import several paths"
820 (let* ((texts (unfold (cut >= <> 10)
821 (lambda _ (random-text))
824 (files (map (cut add-text-to-store %store "text" <>) texts))
825 (dump (call-with-bytevector-output-port
826 (cut export-paths %store files <>))))
827 (delete-paths %store files)
828 (and (every (negate file-exists?) files)
829 (let* ((source (open-bytevector-input-port dump))
830 (imported (import-paths %store source)))
831 (and (equal? imported files)
832 (every file-exists? files)
835 (call-with-input-file file
839 (test-assert "export/import paths, ensure topological order"
840 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
841 (file1 (add-text-to-store %store "foo" (random-text)
843 (file2 (add-text-to-store %store "bar" (random-text)
845 (files (list file1 file2))
846 (dump1 (call-with-bytevector-output-port
847 (cute export-paths %store (list file1 file2) <>)))
848 (dump2 (call-with-bytevector-output-port
849 (cute export-paths %store (list file2 file1) <>))))
850 (delete-paths %store files)
851 (and (every (negate file-exists?) files)
852 (bytevector=? dump1 dump2)
853 (let* ((source (open-bytevector-input-port dump1))
854 (imported (import-paths %store source)))
855 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
856 (and (equal? imported (list file1 file2))
857 (every file-exists? files)
858 (equal? (list file0) (references %store file1))
859 (equal? (list file1) (references %store file2)))))))
861 (test-assert "export/import incomplete"
862 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
863 (file1 (add-text-to-store %store "foo" (random-text)
865 (file2 (add-text-to-store %store "bar" (random-text)
867 (dump (call-with-bytevector-output-port
868 (cute export-paths %store (list file2) <>))))
869 (delete-paths %store (list file0 file1 file2))
870 (guard (c ((store-protocol-error? c)
871 (and (not (zero? (store-protocol-error-status c)))
872 (string-contains (store-protocol-error-message c)
874 ;; Here we get an exception because DUMP does not include FILE0 and
875 ;; FILE1, which are dependencies of FILE2.
876 (import-paths %store (open-bytevector-input-port dump)))))
878 (test-assert "export/import recursive"
879 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
880 (file1 (add-text-to-store %store "foo" (random-text)
882 (file2 (add-text-to-store %store "bar" (random-text)
884 (dump (call-with-bytevector-output-port
885 (cute export-paths %store (list file2) <>
887 (delete-paths %store (list file0 file1 file2))
888 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
889 (and (equal? imported (list file0 file1 file2))
890 (every file-exists? (list file0 file1 file2))
891 (equal? (list file0) (references %store file1))
892 (equal? (list file1) (references %store file2))))))
894 (test-assert "write-file & export-path yield the same result"
895 ;; Here we compare 'write-file' and the daemon's own implementation.
896 ;; 'write-file' is the reference because we know it sorts file
897 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
898 ;; currently happen to be sorted as a side-effect of some unrelated
899 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
901 (run-with-store %store
902 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
903 (out1 -> (derivation->output-path drv1))
904 (data -> (unfold (cut >= <> 26)
906 (random-bytevector 128))
910 (use-modules (rnrs io ports) (srfi srfi-1))
916 (+ i (char->integer #\a)))))
918 (define (touch file data)
919 (call-with-output-file file
921 (put-bytevector port data))))
926 ;; The files must be different so they have
927 ;; different inode numbers, and the inode
928 ;; order must differ from the lexicographic
931 (append (drop letters 10)
935 (drv2 (gexp->derivation "bunch" build))
936 (out2 -> (derivation->output-path drv2))
937 (item-info -> (store-lift query-path-info)))
939 (built-derivations (list drv1 drv2))
941 (lambda (item result)
943 (let-values (((port get) (gcrypt:open-sha256-port)))
944 (write-file item port)
948 ;; 'query-path-info' returns a hash produced by using the
949 ;; daemon's C++ 'dump' function, which is the implementation
951 (>>= (item-info item)
955 (bytevector=? (path-info-hash info) ref-hash))))))
958 #:guile-for-build (%guile-for-build)))
960 (test-assert "import corrupt path"
961 (let* ((text (random-text))
962 (file (add-text-to-store %store "text" text))
963 (dump (call-with-bytevector-output-port
964 (cut export-paths %store (list file) <>))))
965 (delete-paths %store (list file))
967 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
968 ;; the file contents in DUMP, regardless of the store prefix.
970 (byte (bytevector-u8-ref dump index)))
971 (bytevector-u8-set! dump index (logxor #xff byte)))
973 (and (not (file-exists? file))
974 (guard (c ((store-protocol-error? c)
976 (and (not (zero? (store-protocol-error-status c)))
977 (string-contains (store-protocol-error-message c)
979 (let* ((source (open-bytevector-input-port dump))
980 (imported (import-paths %store source)))
981 (pk 'corrupt-imported imported)
984 (test-assert "verify-store"
985 (let* ((text (random-text))
986 (file1 (add-text-to-store %store "foo" text))
987 (file2 (add-text-to-store %store "bar" (random-text)
989 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
992 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
994 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
995 ;; without actually creating the file. ;
996 (call-with-output-file file1
998 (display text port)))
999 (pk 'verify3 (verify-store %store)))))) ;OK again
1001 (test-assert "verify-store + check-contents"
1002 ;; XXX: This test is I/O intensive.
1004 (let* ((text (random-text))
1005 (drv (build-expression->derivation
1007 `(let ((out (assoc-ref %outputs "out")))
1008 (call-with-output-file out
1010 (display ,text port)))
1013 (package-derivation s %bootstrap-guile (%current-system))))
1014 (file (derivation->output-path drv)))
1015 (with-derivation-substitute drv text
1016 (and (build-derivations s (list drv))
1017 (verify-store s #:check-contents? #t) ;should be OK
1020 (call-with-output-file file
1022 (display "corrupt!" port)))
1025 ;; Make sure the corruption is detected. We don't test repairing
1026 ;; because only "trusted" users are allowed to do it, but we
1027 ;; don't expose that notion of trusted users that nix-daemon
1028 ;; supports because it seems dubious and redundant with what the
1029 ;; OS provides (in Nix "trusted" users have additional
1030 ;; privileges, such as overriding the set of substitute URLs, but
1031 ;; we instead want to allow anyone to modify them, provided
1032 ;; substitutes are signed by a root-approved key.)
1033 (not (verify-store s #:check-contents? #t))
1035 ;; Delete the corrupt item to leave the store in a clean state.
1036 (delete-paths s (list file)))))))
1038 (test-assert "build-things, check mode"
1040 (call-with-temporary-output-file
1041 (lambda (entropy entropy-port)
1042 (write (random-text) entropy-port)
1043 (force-output entropy-port)
1044 (let* ((drv (build-expression->derivation
1045 store "non-deterministic"
1047 (use-modules (rnrs io ports))
1048 (let ((out (assoc-ref %outputs "out")))
1049 (call-with-output-file out
1051 ;; Rely on the fact that tests do not use the
1052 ;; chroot, and thus ENTROPY is readable.
1053 (display (call-with-input-file ,entropy
1058 (package-derivation store %bootstrap-guile (%current-system))))
1059 (file (derivation->output-path drv)))
1060 (and (build-things store (list (derivation-file-name drv)))
1062 (write (random-text) entropy-port)
1063 (force-output entropy-port)
1064 (guard (c ((store-protocol-error? c)
1065 (pk 'determinism-exception c)
1066 (and (not (zero? (store-protocol-error-status c)))
1067 (string-contains (store-protocol-error-message c)
1069 ;; This one will produce a different result. Since we're in
1070 ;; 'check' mode, this must fail.
1071 (build-things store (list (derivation-file-name drv))
1075 (test-assert "build-succeeded trace in check mode"
1077 (call-with-output-string
1079 (let ((d (build-expression->derivation
1080 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1082 (package-derivation %store %bootstrap-guile))))
1083 (build-derivations %store (list d))
1084 (parameterize ((current-build-output-port port))
1085 (build-derivations %store (list d) (build-mode check))))))
1086 "@ build-succeeded"))
1088 (test-assert "build multiple times"
1090 ;; Ask to build twice.
1091 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1093 (call-with-temporary-output-file
1094 (lambda (entropy entropy-port)
1095 (write (random-text) entropy-port)
1096 (force-output entropy-port)
1097 (let* ((drv (build-expression->derivation
1098 store "non-deterministic"
1100 (use-modules (rnrs io ports))
1101 (let ((out (assoc-ref %outputs "out")))
1102 (call-with-output-file out
1104 ;; Rely on the fact that tests do not use the
1105 ;; chroot, and thus ENTROPY is accessible.
1106 (display (call-with-input-file ,entropy
1109 (call-with-output-file ,entropy
1111 (write 'foobar port)))))
1114 (package-derivation store %bootstrap-guile (%current-system))))
1115 (file (derivation->output-path drv)))
1116 (guard (c ((store-protocol-error? c)
1117 (pk 'multiple-build c)
1118 (and (not (zero? (store-protocol-error-status c)))
1119 (string-contains (store-protocol-error-message c)
1121 ;; This one will produce a different result on the second run.
1122 (current-build-output-port (current-error-port))
1123 (build-things store (list (derivation-file-name drv)))
1126 (test-equal "store-lower"
1128 (let* ((add (store-lower text-file))
1129 (file (add %store "foo" "Lowered.")))
1130 (call-with-input-file file get-string-all)))
1132 (test-equal "current-system"
1134 (parameterize ((%current-system "frob"))
1135 (run-with-store %store
1136 (mbegin %store-monad
1137 (set-current-system "bar")
1141 (test-assert "query-path-info"
1142 (let* ((ref (add-text-to-store %store "ref" "foo"))
1143 (item (add-text-to-store %store "item" "bar" (list ref)))
1144 (info (query-path-info %store item)))
1145 (and (equal? (path-info-references info) (list ref))
1146 (equal? (path-info-hash info)
1149 (call-with-output-string (cut write-file item <>))))))))
1151 (test-assert "path-info-deriver"
1152 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1153 (s (add-to-store %store "bash" #t "sha256"
1154 (search-bootstrap-binary "bash"
1155 (%current-system))))
1156 (d (derivation %store "the-thing"
1158 #:env-vars `(("foo" . ,(random-text)))
1159 #:inputs `((,b) (,s))))
1160 (o (derivation->output-path d)))
1161 (and (build-derivations %store (list d))
1162 (not (path-info-deriver (query-path-info %store b)))
1163 (string=? (derivation-file-name d)
1164 (path-info-deriver (query-path-info %store o))))))
1166 (test-equal "build-cores"
1169 (let* ((build (add-text-to-store store "build.sh"
1170 "echo $NIX_BUILD_CORES > $out"))
1171 (bash (add-to-store store "bash" #t "sha256"
1172 (search-bootstrap-binary "bash"
1173 (%current-system))))
1174 (drv1 (derivation store "the-thing" bash
1176 #:inputs `((,bash) (,build))
1177 #:env-vars `(("x" . ,(random-text)))))
1178 (drv2 (derivation store "the-thing" bash
1180 #:inputs `((,bash) (,build))
1181 #:env-vars `(("x" . ,(random-text))))))
1182 (and (build-derivations store (list drv1))
1184 (set-build-options store #:build-cores 42)
1185 (build-derivations store (list drv2)))
1186 (list (call-with-input-file (derivation->output-path drv1)
1188 (call-with-input-file (derivation->output-path drv2)
1191 (test-equal "multiplexed-build-output"
1192 '("Hello from first." "Hello from second.")
1194 (let* ((build (add-text-to-store store "build.sh"
1195 "echo Hello from $NAME.; echo > $out"))
1196 (bash (add-to-store store "bash" #t "sha256"
1197 (search-bootstrap-binary "bash"
1198 (%current-system))))
1199 (drv1 (derivation store "one" bash
1201 #:inputs `((,bash) (,build))
1202 #:env-vars `(("NAME" . "first")
1203 ("x" . ,(random-text)))))
1204 (drv2 (derivation store "two" bash
1206 #:inputs `((,bash) (,build))
1207 #:env-vars `(("NAME" . "second")
1208 ("x" . ,(random-text))))))
1209 (set-build-options store
1210 #:print-build-trace #t
1211 #:multiplexed-build-output? #t
1212 #:max-build-jobs 10)
1213 (let ((port (open-output-string)))
1214 ;; Send the build log to PORT.
1215 (parameterize ((current-build-output-port port))
1216 (build-derivations store (list drv1 drv2)))
1218 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1219 ;; traces that allow us to retrieve each builder's output (we assume
1220 ;; there's exactly one "build-output" trace for each builder, which is
1222 (let* ((log (get-output-string port))
1223 (started (fold-matches
1224 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1227 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1229 (output (fold-matches
1230 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1232 (drv-pid (lambda (name)
1234 (let ((drv (match:substring m 1))
1235 (pid (string->number
1236 (match:substring m 4))))
1237 (and (string-suffix? name drv) pid)))))
1238 (pid-log (lambda (pid)
1240 (let ((n (string->number
1241 (match:substring m 1)))
1242 (len (string->number
1243 (match:substring m 2)))
1244 (str (match:substring m 3)))
1246 (= (string-length str) (- len 1))
1248 (pid1 (any (drv-pid "one.drv") started))
1249 (pid2 (any (drv-pid "two.drv") started)))
1250 (list (any (pid-log pid1) output)
1251 (any (pid-log pid2) output)))))))