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>
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 ((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))
47 ;; Test the (guix store) module.
50 (open-connection-for-tests))
53 (or (getenv "SHELL") (getenv "CONFIG_SHELL") "/bin/sh"))
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")
63 (close-connection store)
66 (test-equal "connection handshake error"
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)
75 (test-equal "store-path-hash-part"
76 "283gqy39v3g9dxjy26rynl0zls82fmcg"
78 (string-append (%store-prefix)
79 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
81 (test-equal "store-path-hash-part #f"
84 (string-append (%store-prefix)
85 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
87 (test-equal "store-path-package-name"
89 (store-path-package-name
90 (string-append (%store-prefix)
91 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
93 (test-equal "store-path-package-name #f"
95 (store-path-package-name
96 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
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?
105 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
106 (not (direct-store-path? (%store-prefix)))))
108 (test-skip (if %store 0 15))
110 (test-equal "profiles/per-user exists and is not writable"
112 (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
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))))
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)
130 '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
132 (test-equal "add-data-to-store"
134 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
137 (test-assert "valid-path? live"
138 (let ((p (add-text-to-store %store "hello" "hello, world")))
139 (valid-path? %store p)))
141 (test-assert "valid-path? false"
142 (not (valid-path? %store
143 (string-append (%store-prefix) "/"
144 (make-string 32 #\e) "-foobar"))))
146 (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
151 (add-text-to-store s "foo" "bar")
155 (test-assert "valid-path? error"
157 (guard (c ((store-protocol-error? c) #t))
158 (valid-path? s "foo")
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
165 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
168 (let-syntax ((true-if-error (syntax-rules ()
170 (guard (c ((store-protocol-error? c) #t))
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"))))))
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))
183 (test-assert "dead-paths"
184 (let ((p (add-text-to-store %store "random-text" (random-text))))
185 (->bool (member p (dead-paths %store)))))
187 ;; FIXME: Find a test for `live-paths'.
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)
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)))))
204 (test-assert "add-indirect-root and find-roots"
205 (call-with-temporary-directory
207 (let* ((item (add-text-to-store %store "something" (random-text)))
208 (root (string-append directory "/gc-root")))
210 (add-indirect-root %store root)
211 (let ((result (member (cons root item) (find-roots %store))))
215 (test-assert "permanent root"
216 (let* ((p (with-store store
217 (let ((p (add-text-to-store store "random-text"
219 (add-permanent-root p)
220 (add-permanent-root p) ; should not throw
222 (and (member p (live-paths %store))
224 (remove-permanent-root p)
225 (->bool (member p (dead-paths %store)))))))
227 (test-assert "dead path can be explicitly collected"
228 (let ((p (add-text-to-store %store "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>.
235 (not (file-exists? p))))))
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.
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)
252 (file-exists? path2)))))
254 (test-equal "add-file-tree-to-store"
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
270 ("a" regular (data "file a"))
273 ("p" regular (data ,(string->utf8 "file p")))
276 (data ,(string-append "#!" %shell "\nexit 42")))
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
284 (let ((type (stat:type (lstat file))))
287 ((or 'regular 'executable)
288 (call-with-input-file file
290 ('symlink (readlink file))
292 (find-files "." #:directories? #t))))))
294 (test-equal "add-file-tree-to-store, flat"
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))))
301 (test-assert "references"
302 (let* ((t1 (add-text-to-store %store "random1"
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)))))
311 (test-equal "substitutable-path-info when substitutes are turned off"
314 (set-build-options s #:use-substitutes? #f)
315 (let* ((b (add-to-store s "bash" #t "sha256"
316 (search-bootstrap-binary "bash"
318 (d (derivation s "the-thing" b '("--version")
320 (o (derivation->output-path d)))
321 (with-derivation-narinfo d
322 (substitutable-path-info s (list o))))))
324 (test-equal "substitutable-paths when substitutes are turned off"
327 (set-build-options s #:use-substitutes? #f)
328 (let* ((b (add-to-store s "bash" #t "sha256"
329 (search-bootstrap-binary "bash"
331 (d (derivation s "the-thing" b '("--version")
333 (o (derivation->output-path d)))
334 (with-derivation-narinfo d
335 (substitutable-paths s (list o))))))
337 (test-assert "requisites"
338 (let* ((t1 (add-text-to-store %store "random1"
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))))
347 (and (= (length x) (length y))
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)))))
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"
362 (d (derivation %store "the-thing"
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))
370 (equal? (valid-derivers %store o)
371 (list (derivation-file-name d))))))
373 (test-equal "with-build-handler"
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"
379 (d1 (derivation %store "the-thing"
381 #:env-vars `(("foo" . ,(random-text)))
382 #:sources (list b s)))
383 (d2 (derivation %store "the-thing"
385 #:env-vars `(("foo" . ,(random-text))
387 #:sources (list b s)))
388 (o1 (derivation->output-path d1))
389 (o2 (derivation->output-path d2)))
392 (lambda (continue store things mode)
395 (set! counter (+ 1 counter))
396 (if (string=? drv (derivation-file-name d1))
398 (and (string=? drv (derivation-file-name d2))
401 (build-derivations %store (list d1))
402 (build-derivations %store (list d2))
405 (test-equal "with-build-handler + with-store"
407 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
408 ;; even though 'with-build-handler' is outside the dynamic extent of
410 (with-build-handler (lambda (continue store things mode)
413 (and (string-suffix? "thingie.drv" drv)
415 (store-connection-socket 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"
422 (d (derivation store "thingie"
424 #:env-vars `(("foo" . ,(random-text)))
425 #:sources (list b s))))
426 (build-derivations store (list d))
428 ;; Here STORE's socket should still be open.
429 (and (valid-path? store (derivation->output-path d))
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"
437 (d1 (derivation %store "the-thing"
439 #:env-vars `(("foo" . ,(random-text)))
440 #:sources (list b s)))
441 (d2 (derivation %store "the-thing"
443 #:env-vars `(("foo" . ,(random-text))
445 #:sources (list b s))))
446 (with-build-handler (lambda (continue store things mode)
447 (equal? (map derivation-file-name (list d1 d2))
449 (map/accumulate-builds %store
451 (build-derivations %store (list drv))
452 (add-to-store %store "content-addressed"
454 (derivation->output-path drv)))
457 (test-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
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"
467 (derivation %store (string-append "the-thing-"
470 #:env-vars `(("foo" . ,(random-text)))
472 #:properties `((n . ,i))))
476 (with-build-handler (lambda (continue store things mode)
477 (set! calls (cons things calls))
479 (map/accumulate-builds %store
481 (build-derivations %store (list d))
482 (assq-ref (derivation-properties d) 'n))
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)
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)))
501 (run-with-store %store
502 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
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
508 (run-with-store %store
509 (mlet %store-monad ((lst1 (mapm %store-monad
511 (current-target-system))
513 (lst2 (mapm/accumulate-builds
515 (current-target-system))
517 (return (list lst1 lst2)))
519 #:target "i586-pc-gnu"))
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))))
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))))
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))))
555 (list a b c d w x y)))
559 (list a b c d w x y)))
560 (lset= string=? s1 s3)))))
562 (test-assert "current-build-output-port, UTF-8"
563 ;; Are UTF-8 strings in the build log properly interpreted?
565 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
566 (call-with-output-string
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)
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: λ."))
579 (test-assert "current-build-output-port, UTF-8 + garbage"
580 ;; What about a mixture of UTF-8 + garbage?
582 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
583 (call-with-output-string
585 (parameterize ((current-build-output-port port))
586 (let ((d (build-expression->derivation
589 (use-modules (rnrs io ports))
590 (display "garbage: ")
591 (put-bytevector (current-output-port) #vu8(128))
592 (display "lambda: λ\n"))
594 (package-derivation %store %bootstrap-guile))))
595 (guard (c ((store-protocol-error? c) #t))
596 (build-derivations %store (list d))))))))
597 "garbage: �lambda: λ"))
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"
604 (d (derivation %store "the-thing"
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)))))))
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"
616 (d (derivation %store "the-thing"
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)))))
626 (test-assert "no substitutes"
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))))))
637 (test-assert "build-things with output path"
639 (let* ((c (random-text)) ;contents of the output
640 (d (build-expression->derivation
642 `(call-with-output-file %output
646 (package-derivation s %bootstrap-guile (%current-system))))
647 (o (derivation->output-path d)))
648 (set-build-options s #:use-substitutes? #f)
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)))))
655 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
657 (test-assert "substitute query"
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.
665 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
666 "/guix/substitute")))
668 ;; Make sure 'guix substitute' correctly communicates the above
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)))))))))
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.
688 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
689 "/guix/substitute")))
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.
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)))))))
713 (test-assert "substitute"
715 (let* ((c (random-text)) ; contents of the output
716 (d (build-expression->derivation
718 `(call-with-output-file %output
720 (exit 1) ; would actually fail
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))
731 (equal? c (call-with-input-file o get-string-all)))))))
733 (test-assert "substitute, deduplication"
735 (let* ((c (random-text)) ; contents of the output
736 (g (package-derivation s %bootstrap-guile))
737 (d1 (build-expression->derivation s "substitute-me"
739 #:guile-for-build g))
740 (d2 (build-expression->derivation s "build-me"
741 `(call-with-output-file %output
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
754 (equal? c (call-with-input-file o1 get-string-all))
755 (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
757 (test-assert "substitute + build-things with output path"
759 (let* ((c (random-text)) ;contents of the output
760 (d (build-expression->derivation
762 `(call-with-output-file %output
764 (exit 1) ;would actually fail
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
776 (equal? c (call-with-input-file o get-string-all)))))))
778 (test-assert "substitute + build-things with specific output"
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")
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)
792 ;; Ask for nothing but the "out" output of D.
793 (build-things s `((,(derivation-file-name d) . "out")))
797 (equal? c (call-with-input-file o get-string-all)))))))
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
804 (let* ((c "hello, world") ; contents of the output
805 (d (build-expression->derivation
806 s "corrupt-substitute"
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
814 ;; Make sure we use 'guix substitute'.
816 #:use-substitutes? #t
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.
825 (not (zero? (store-protocol-error-status c)))))
826 (build-derivations s (list d))
829 (test-assert "substitute, corrupt output hash, build trace"
830 ;; Likewise, and check the build trace.
832 (let* ((c "hello, world") ; contents of the output
833 (d (build-expression->derivation
834 s "corrupt-substitute"
837 (package-derivation s %bootstrap-guile (%current-system))))
838 (o (derivation->output-path d)))
839 ;; Make sure we use 'guix substitute'.
841 #:print-build-trace #t
842 #:use-substitutes? #t
844 #:substitute-urls (%test-substitute-urls))
846 (with-derivation-substitute d c
847 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
850 (call-with-output-string
852 (parameterize ((current-build-output-port port))
853 (guard (c ((store-protocol-error? c) #t))
854 (build-derivations s (list d))
858 (let-values (((port get-hash)
859 (gcrypt:open-hash-port
860 (gcrypt:hash-algorithm gcrypt:sha256))))
861 (write-file-tree "foo" port
864 (values 'regular (string-length c)))
867 (open-input-string c)))
869 (bytevector->nix-base32-string (get-hash))))
871 (define expected-hash
872 (bytevector->nix-base32-string (make-bytevector 32 0)))
875 (string-append "@ hash-mismatch " o " sha256 "
876 expected-hash " " actual-hash "\n"))
879 (string-append "@ substituter-failed " o))
881 (and (string-contains output mismatch)
882 (string-contains output failure))))))
884 (test-assert "substitute --fallback"
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
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.
905 #:use-substitutes? #t
907 (%test-substitute-urls)
909 (and (build-derivations s (list d))
910 (equal? t (call-with-input-file o
913 (build-derivations s (list d))
916 (test-assert "export/import several paths"
917 (let* ((texts (unfold (cut >= <> 10)
918 (lambda _ (random-text))
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)
932 (call-with-input-file file
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)
940 (file2 (add-text-to-store %store "bar" (random-text)
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)))))))
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)
962 (file2 (add-text-to-store %store "bar" (random-text)
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)
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)))))
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)
979 (file2 (add-text-to-store %store "bar" (random-text)
981 (dump (call-with-bytevector-output-port
982 (cute export-paths %store (list file2) <>
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))))))
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
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)
1003 (random-bytevector 128))
1007 (use-modules (rnrs io ports) (srfi srfi-1))
1013 (+ i (char->integer #\a)))))
1015 (define (touch file data)
1016 (call-with-output-file file
1018 (put-bytevector port data))))
1023 ;; The files must be different so they have
1024 ;; different inode numbers, and the inode
1025 ;; order must differ from the lexicographic
1028 (append (drop letters 10)
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))
1038 (lambda (item result)
1040 (let-values (((port get) (gcrypt:open-sha256-port)))
1041 (write-file item port)
1045 ;; 'query-path-info' returns a hash produced by using the
1046 ;; daemon's C++ 'dump' function, which is the implementation
1048 (>>= (item-info item)
1052 (bytevector=? (path-info-hash info) ref-hash))))))
1055 #:guile-for-build (%guile-for-build)))
1058 (test-assert "import not signed"
1059 (let* ((text (random-text))
1060 (file (add-file-tree-to-store %store
1062 ("text" regular (data ,text))
1063 ("link" symlink "text"))))
1064 (dump (call-with-bytevector-output-port
1066 (write-int 1 port) ;start
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
1075 (write-int 0 port))))) ;done
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)
1087 (test-assert "import signed by unauthorized key"
1088 (let* ((text (random-text))
1089 (file (add-file-tree-to-store %store
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
1098 (write-int 1 port) ;start
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
1108 (gcrypt:bytevector->hash-data
1109 (gcrypt:sha256 #vu8(0 1 2))
1111 (gcrypt:find-sexp-token key 'private-key)
1112 (gcrypt:find-sexp-token key 'public-key)))
1115 (write-int 0 port))))) ;done
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)
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))
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.
1137 (byte (bytevector-u8-ref dump index)))
1138 (bytevector-u8-set! dump index (logxor #xff byte)))
1140 (and (not (file-exists? file))
1141 (guard (c ((store-protocol-error? c)
1143 (and (not (zero? (store-protocol-error-status c)))
1144 (string-contains (store-protocol-error-message c)
1146 (let* ((source (open-bytevector-input-port dump))
1147 (imported (import-paths %store source)))
1148 (pk 'corrupt-imported imported)
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)
1156 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1159 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
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
1165 (display text port)))
1166 (pk 'verify3 (verify-store %store)))))) ;OK again
1168 (test-assert "verify-store + check-contents"
1169 ;; XXX: This test is I/O intensive.
1171 (let* ((text (random-text))
1172 (drv (build-expression->derivation
1174 `(let ((out (assoc-ref %outputs "out")))
1175 (call-with-output-file out
1177 (display ,text port)))
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
1187 (call-with-output-file file
1189 (display "corrupt!" port)))
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))
1202 ;; Delete the corrupt item to leave the store in a clean state.
1203 (delete-paths s (list file)))))))
1205 (test-assert "build-things, check mode"
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"
1214 (use-modules (rnrs io ports))
1215 (let ((out (assoc-ref %outputs "out")))
1216 (call-with-output-file out
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
1225 (package-derivation store %bootstrap-guile (%current-system))))
1226 (file (derivation->output-path drv)))
1227 (and (build-things store (list (derivation-file-name drv)))
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)
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))
1242 (test-assert "build-succeeded trace in check mode"
1244 (call-with-output-string
1246 (let ((d (build-expression->derivation
1247 %store "foo" '(mkdir (assoc-ref %outputs "out"))
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"))
1255 (test-assert "build multiple times"
1257 ;; Ask to build twice.
1258 (set-build-options store #:rounds 2 #:use-substitutes? #f)
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"
1267 (use-modules (rnrs io ports))
1268 (let ((out (assoc-ref %outputs "out")))
1269 (call-with-output-file out
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
1276 (call-with-output-file ,entropy
1278 (write 'foobar port)))))
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)
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)))
1293 (test-equal "store-lower"
1295 (let* ((add (store-lower text-file))
1296 (file (add %store "foo" "Lowered.")))
1297 (call-with-input-file file get-string-all)))
1299 (test-equal "current-system"
1301 (parameterize ((%current-system "frob"))
1302 (run-with-store %store
1303 (mbegin %store-monad
1304 (set-current-system "bar")
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)
1316 (call-with-output-string (cut write-file item <>))))))))
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"
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))))))
1333 (test-equal "build-cores"
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
1343 #:inputs `((,bash) (,build))
1344 #:env-vars `(("x" . ,(random-text)))))
1345 (drv2 (derivation store "the-thing" bash
1347 #:inputs `((,bash) (,build))
1348 #:env-vars `(("x" . ,(random-text))))))
1349 (and (build-derivations store (list drv1))
1351 (set-build-options store #:build-cores 42)
1352 (build-derivations store (list drv2)))
1353 (list (call-with-input-file (derivation->output-path drv1)
1355 (call-with-input-file (derivation->output-path drv2)
1358 (test-equal "multiplexed-build-output"
1359 '("Hello from first." "Hello from second.")
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
1368 #:inputs `((,bash) (,build))
1369 #:env-vars `(("NAME" . "first")
1370 ("x" . ,(random-text)))))
1371 (drv2 (derivation store "two" bash
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)))
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
1389 (let* ((log (get-output-string port))
1390 (started (fold-matches
1391 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1394 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1396 (output (fold-matches
1397 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1399 (drv-pid (lambda (name)
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)
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)))
1413 (= (string-length str) (- len 1))
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)))))))