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)
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-data-to-store"
120 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
123 (test-assert "valid-path? live"
124 (let ((p (add-text-to-store %store "hello" "hello, world")))
125 (valid-path? %store p)))
127 (test-assert "valid-path? false"
128 (not (valid-path? %store
129 (string-append (%store-prefix) "/"
130 (make-string 32 #\e) "-foobar"))))
132 (test-assert "valid-path? error"
134 (guard (c ((store-protocol-error? c) #t))
135 (valid-path? s "foo")
138 (test-assert "valid-path? recovery"
139 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
140 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
142 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
145 (let-syntax ((true-if-error (syntax-rules ()
147 (guard (c ((store-protocol-error? c) #t))
149 (and (true-if-error (valid-path? s "foo"))
150 (true-if-error (valid-path? s "bar"))
151 (true-if-error (valid-path? s "baz"))
152 (true-if-error (valid-path? s "chbouib"))
153 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
155 (test-assert "hash-part->path"
156 (let ((p (add-text-to-store %store "hello" "hello, world")))
157 (equal? (hash-part->path %store (store-path-hash-part p))
160 (test-assert "dead-paths"
161 (let ((p (add-text-to-store %store "random-text" (random-text))))
162 (->bool (member p (dead-paths %store)))))
164 ;; FIXME: Find a test for `live-paths'.
166 ;; (test-assert "temporary root is in live-paths"
167 ;; (let* ((p1 (add-text-to-store %store "random-text"
168 ;; (random-text) '()))
169 ;; (b (add-text-to-store %store "link-builder"
170 ;; (format #f "echo ~a > $out" p1)
172 ;; (d1 (derivation %store "link"
173 ;; "/bin/sh" `("-e" ,b)
174 ;; #:inputs `((,b) (,p1))))
175 ;; (p2 (derivation->output-path d1)))
176 ;; (and (add-temp-root %store p2)
177 ;; (build-derivations %store (list d1))
178 ;; (valid-path? %store p1)
179 ;; (member (pk p2) (live-paths %store)))))
181 (test-assert "permanent root"
182 (let* ((p (with-store store
183 (let ((p (add-text-to-store store "random-text"
185 (add-permanent-root p)
186 (add-permanent-root p) ; should not throw
188 (and (member p (live-paths %store))
190 (remove-permanent-root p)
191 (->bool (member p (dead-paths %store)))))))
193 (test-assert "dead path can be explicitly collected"
194 (let ((p (add-text-to-store %store "random-text"
196 (let-values (((paths freed) (delete-paths %store (list p))))
197 (and (equal? paths (list p))
198 ;; XXX: On some file systems (notably Btrfs), freed
199 ;; may return 0. See <https://bugs.gnu.org/29363>.
201 (not (file-exists? p))))))
203 (test-assert "add-text-to-store vs. delete-paths"
204 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
205 ;; is no longer valid.
207 (let* ((text (random-text))
208 (path (add-text-to-store store "delete-me" text))
209 (deleted (delete-paths store (list path)))
210 (path2 (add-text-to-store store "delete-me" text)))
211 (and (string=? path path2)
212 (equal? deleted (list path))
213 (valid-path? store path)
214 (file-exists? path)))))
216 (test-assert "add-to-store vs. delete-paths"
219 (let* ((file (search-path %load-path "guix.scm"))
220 (path (add-to-store store "delete-me" #t "sha256" file))
221 (deleted (delete-paths store (list path)))
222 (path2 (add-to-store store "delete-me" #t "sha256" file)))
223 (and (string=? path path2)
224 (equal? deleted (list path))
225 (valid-path? store path)
226 (file-exists? path)))))
228 (test-equal "add-file-tree-to-store"
231 ("./bar" directory #t)
232 ("./foo" directory #t)
233 ("./foo/a" regular "file a")
234 ("./foo/b" symlink "a")
235 ("./foo/c" directory #t)
236 ("./foo/c/p" regular "file p")
237 ("./foo/c/q" directory #t)
238 ("./foo/c/q/x" regular
239 ,(string-append "#!" %shell "\nexit 42"))
240 ("./foo/c/q/y" symlink "..")
241 ("./foo/c/q/z" directory #t))
242 (let* ((tree `("file-tree" directory
244 ("a" regular (data "file a"))
247 ("p" regular (data ,(string->utf8 "file p")))
250 (data ,(string-append "#!" %shell "\nexit 42")))
254 (result (add-file-tree-to-store %store tree)))
255 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
256 (with-directory-excursion result
258 (let ((type (stat:type (lstat file))))
261 ((or 'regular 'executable)
262 (call-with-input-file file
264 ('symlink (readlink file))
266 (find-files "." #:directories? #t))))))
268 (test-equal "add-file-tree-to-store, flat"
270 (let* ((tree `("flat-file" regular (data "Hello, world!")))
271 (result (add-file-tree-to-store %store tree)))
272 (and (file-exists? result)
273 (call-with-input-file result get-string-all))))
275 (test-assert "references"
276 (let* ((t1 (add-text-to-store %store "random1"
278 (t2 (add-text-to-store %store "random2"
279 (random-text) (list t1))))
280 (and (equal? (list t1) (references %store t2))
281 (equal? (list t2) (referrers %store t1))
282 (null? (references %store t1))
283 (null? (referrers %store t2)))))
285 (test-assert "references/substitutes missing reference info"
287 (set-build-options s #:use-substitutes? #f)
288 (guard (c ((store-protocol-error? c) #t))
289 (let* ((b (add-to-store s "bash" #t "sha256"
290 (search-bootstrap-binary "bash"
292 (d (derivation s "the-thing" b '("--help")
294 (references/substitutes s (list (derivation->output-path d) b))
297 (test-assert "references/substitutes with substitute info"
299 (set-build-options s #:use-substitutes? #t)
300 (let* ((t1 (add-text-to-store s "random1" (random-text)))
301 (t2 (add-text-to-store s "random2" (random-text)
303 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
304 (b (add-to-store s "bash" #t "sha256"
305 (search-bootstrap-binary "bash"
307 (d (derivation s "the-thing" b `("-e" ,t3)
308 #:inputs `((,b) (,t3) (,t2))
309 #:env-vars `(("t2" . ,t2))))
310 (o (derivation->output-path d)))
311 (with-derivation-narinfo d
312 (sha256 => (sha256 (string->utf8 t2)))
313 (references => (list t2))
315 (equal? (references/substitutes s (list o t3 t2 t1))
321 (test-equal "substitutable-path-info when substitutes are turned off"
324 (set-build-options s #:use-substitutes? #f)
325 (let* ((b (add-to-store s "bash" #t "sha256"
326 (search-bootstrap-binary "bash"
328 (d (derivation s "the-thing" b '("--version")
330 (o (derivation->output-path d)))
331 (with-derivation-narinfo d
332 (substitutable-path-info s (list o))))))
334 (test-equal "substitutable-paths when substitutes are turned off"
337 (set-build-options s #:use-substitutes? #f)
338 (let* ((b (add-to-store s "bash" #t "sha256"
339 (search-bootstrap-binary "bash"
341 (d (derivation s "the-thing" b '("--version")
343 (o (derivation->output-path d)))
344 (with-derivation-narinfo d
345 (substitutable-paths s (list o))))))
347 (test-assert "requisites"
348 (let* ((t1 (add-text-to-store %store "random1"
350 (t2 (add-text-to-store %store "random2"
351 (random-text) (list t1)))
352 (t3 (add-text-to-store %store "random3"
353 (random-text) (list t2)))
354 (t4 (add-text-to-store %store "random4"
355 (random-text) (list t1 t3))))
357 (and (= (length x) (length y))
360 (and (same? (requisites %store (list t1)) (list t1))
361 (same? (requisites %store (list t2)) (list t1 t2))
362 (same? (requisites %store (list t3)) (list t1 t2 t3))
363 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
364 (same? (requisites %store (list t1 t2 t3 t4))
365 (list t1 t2 t3 t4)))))
367 (test-assert "derivers"
368 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
369 (s (add-to-store %store "bash" #t "sha256"
370 (search-bootstrap-binary "bash"
372 (d (derivation %store "the-thing"
374 #:env-vars `(("foo" . ,(random-text)))
375 #:inputs `((,b) (,s))))
376 (o (derivation->output-path d)))
377 (and (build-derivations %store (list d))
378 (equal? (query-derivation-outputs %store (derivation-file-name d))
380 (equal? (valid-derivers %store o)
381 (list (derivation-file-name d))))))
383 (test-equal "with-build-handler"
385 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
386 (s (add-to-store %store "bash" #t "sha256"
387 (search-bootstrap-binary "bash"
389 (d1 (derivation %store "the-thing"
391 #:env-vars `(("foo" . ,(random-text)))
392 #:sources (list b s)))
393 (d2 (derivation %store "the-thing"
395 #:env-vars `(("foo" . ,(random-text))
397 #:sources (list b s)))
398 (o1 (derivation->output-path d1))
399 (o2 (derivation->output-path d2)))
402 (lambda (continue store things mode)
405 (set! counter (+ 1 counter))
406 (if (string=? drv (derivation-file-name d1))
408 (and (string=? drv (derivation-file-name d2))
411 (build-derivations %store (list d1))
412 (build-derivations %store (list d2))
415 (test-equal "with-build-handler + with-store"
417 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
418 ;; even though 'with-build-handler' is outside the dynamic extent of
420 (with-build-handler (lambda (continue store things mode)
423 (and (string-suffix? "thingie.drv" drv)
425 (store-connection-socket store)))
428 (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
429 (s (add-to-store store "bash" #t "sha256"
430 (search-bootstrap-binary "bash"
432 (d (derivation store "thingie"
434 #:env-vars `(("foo" . ,(random-text)))
435 #:sources (list b s))))
436 (build-derivations store (list d))
438 ;; Here STORE's socket should still be open.
439 (and (valid-path? store (derivation->output-path d))
442 (test-assert "map/accumulate-builds"
443 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
444 (s (add-to-store %store "bash" #t "sha256"
445 (search-bootstrap-binary "bash"
447 (d1 (derivation %store "the-thing"
449 #:env-vars `(("foo" . ,(random-text)))
450 #:sources (list b s)))
451 (d2 (derivation %store "the-thing"
453 #:env-vars `(("foo" . ,(random-text))
455 #:sources (list b s))))
456 (with-build-handler (lambda (continue store things mode)
457 (equal? (map derivation-file-name (list d1 d2))
459 (map/accumulate-builds %store
461 (build-derivations %store (list drv))
462 (add-to-store %store "content-addressed"
464 (derivation->output-path drv)))
467 (test-assert "mapm/accumulate-builds"
468 (let* ((d1 (run-with-store %store
469 (gexp->derivation "foo" #~(mkdir #$output))))
470 (d2 (run-with-store %store
471 (gexp->derivation "bar" #~(mkdir #$output)))))
472 (with-build-handler (lambda (continue store things mode)
473 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
475 (run-with-store %store
476 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
478 (test-equal "mapm/accumulate-builds, %current-target-system"
479 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
480 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
482 (run-with-store %store
483 (mlet %store-monad ((lst1 (mapm %store-monad
485 (current-target-system))
487 (lst2 (mapm/accumulate-builds
489 (current-target-system))
491 (return (list lst1 lst2)))
493 #:target "i586-pc-gnu"))
495 (test-assert "topologically-sorted, one item"
496 (let* ((a (add-text-to-store %store "a" "a"))
497 (b (add-text-to-store %store "b" "b" (list a)))
498 (c (add-text-to-store %store "c" "c" (list b)))
499 (d (add-text-to-store %store "d" "d" (list c)))
500 (s (topologically-sorted %store (list d))))
501 (equal? s (list a b c d))))
503 (test-assert "topologically-sorted, several items"
504 (let* ((a (add-text-to-store %store "a" "a"))
505 (b (add-text-to-store %store "b" "b" (list a)))
506 (c (add-text-to-store %store "c" "c" (list b)))
507 (d (add-text-to-store %store "d" "d" (list c)))
508 (s1 (topologically-sorted %store (list d a c b)))
509 (s2 (topologically-sorted %store (list b d c a b d))))
510 (equal? s1 s2 (list a b c d))))
512 (test-assert "topologically-sorted, more difficult"
513 (let* ((a (add-text-to-store %store "a" "a"))
514 (b (add-text-to-store %store "b" "b" (list a)))
515 (c (add-text-to-store %store "c" "c" (list b)))
516 (d (add-text-to-store %store "d" "d" (list c)))
517 (w (add-text-to-store %store "w" "w"))
518 (x (add-text-to-store %store "x" "x" (list w)))
519 (y (add-text-to-store %store "y" "y" (list x d)))
520 (s1 (topologically-sorted %store (list y)))
521 (s2 (topologically-sorted %store (list c y)))
522 (s3 (topologically-sorted %store (cons y (references %store y)))))
523 ;; The order in which 'references' returns the references of Y is
524 ;; unspecified, so accommodate.
525 (let* ((x-then-d? (equal? (references %store y) (list x d))))
529 (list a b c d w x y)))
533 (list a b c d w x y)))
534 (lset= string=? s1 s3)))))
536 (test-assert "current-build-output-port, UTF-8"
537 ;; Are UTF-8 strings in the build log properly interpreted?
539 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
540 (call-with-output-string
542 (parameterize ((current-build-output-port port))
543 (let* ((s "Here’s a Greek letter: λ.")
544 (d (build-expression->derivation
545 %store "foo" `(display ,s)
547 (package-derivation s %bootstrap-guile (%current-system)))))
548 (guard (c ((store-protocol-error? c) #t))
549 (build-derivations %store (list d))))))))
550 "Here’s a Greek letter: λ."))
552 (test-assert "current-build-output-port, UTF-8 + garbage"
553 ;; What about a mixture of UTF-8 + garbage?
555 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
556 (call-with-output-string
558 (parameterize ((current-build-output-port port))
559 (let ((d (build-expression->derivation
562 (use-modules (rnrs io ports))
563 (display "garbage: ")
564 (put-bytevector (current-output-port) #vu8(128))
565 (display "lambda: λ\n"))
567 (package-derivation %store %bootstrap-guile))))
568 (guard (c ((store-protocol-error? c) #t))
569 (build-derivations %store (list d))))))))
570 "garbage: �lambda: λ"))
572 (test-assert "log-file, derivation"
573 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
574 (s (add-to-store %store "bash" #t "sha256"
575 (search-bootstrap-binary "bash"
577 (d (derivation %store "the-thing"
579 #:env-vars `(("foo" . ,(random-text)))
580 #:inputs `((,b) (,s)))))
581 (and (build-derivations %store (list d))
582 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
584 (test-assert "log-file, output file name"
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 (o (derivation->output-path d)))
594 (and (build-derivations %store (list d))
595 (file-exists? (pk (log-file %store o)))
596 (string=? (log-file %store (derivation-file-name d))
597 (log-file %store o)))))
599 (test-assert "no substitutes"
601 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
602 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
603 (o (map derivation->output-path (list d1 d2))))
604 (set-build-options s #:use-substitutes? #f)
605 (and (not (has-substitutes? s (derivation-file-name d1)))
606 (not (has-substitutes? s (derivation-file-name d2)))
607 (null? (substitutable-paths s o))
608 (null? (substitutable-path-info s o))))))
610 (test-assert "build-things with output path"
612 (let* ((c (random-text)) ;contents of the output
613 (d (build-expression->derivation
615 `(call-with-output-file %output
619 (package-derivation s %bootstrap-guile (%current-system))))
620 (o (derivation->output-path d)))
621 (set-build-options s #:use-substitutes? #f)
623 ;; Pass 'build-things' the output file name, O. However, since there
624 ;; are no substitutes for O, it will just do nothing.
625 (build-things s (list o))
626 (not (valid-path? s o)))))
628 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
630 (test-assert "substitute query"
632 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
633 (o (derivation->output-path d)))
634 ;; Create fake substituter data, to be read by 'guix substitute'.
635 (with-derivation-narinfo d
636 ;; Remove entry from the local cache.
638 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
639 "/guix/substitute")))
641 ;; Make sure 'guix substitute' correctly communicates the above
643 (set-build-options s #:use-substitutes? #t
644 #:substitute-urls (%test-substitute-urls))
645 (and (has-substitutes? s o)
646 (equal? (list o) (substitutable-paths s (list o)))
647 (match (pk 'spi (substitutable-path-info s (list o)))
648 (((? substitutable? s))
649 (and (string=? (substitutable-deriver s)
650 (derivation-file-name d))
651 (null? (substitutable-references s))
652 (equal? (substitutable-nar-size s) 1234)))))))))
654 (test-assert "substitute query, alternating URLs"
655 (let* ((d (with-store s
656 (package-derivation s %bootstrap-guile (%current-system))))
657 (o (derivation->output-path d)))
658 (with-derivation-narinfo d
659 ;; Remove entry from the local cache.
661 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
662 "/guix/substitute")))
664 ;; Note: We reconnect to the daemon to force a new instance of 'guix
665 ;; substitute' to be used; otherwise the #:substitute-urls of
666 ;; 'set-build-options' would have no effect.
668 (and (with-store s ;the right substitute URL
669 (set-build-options s #:use-substitutes? #t
670 #:substitute-urls (%test-substitute-urls))
671 (has-substitutes? s o))
672 (with-store s ;the wrong one
673 (set-build-options s #:use-substitutes? #t
674 #:substitute-urls (list
675 "http://does-not-exist"))
676 (not (has-substitutes? s o)))
677 (with-store s ;the right one again
678 (set-build-options s #:use-substitutes? #t
679 #:substitute-urls (%test-substitute-urls))
680 (has-substitutes? s o))
681 (with-store s ;empty list of URLs
682 (set-build-options s #:use-substitutes? #t
683 #:substitute-urls '())
684 (not (has-substitutes? s o)))))))
686 (test-assert "substitute"
688 (let* ((c (random-text)) ; contents of the output
689 (d (build-expression->derivation
691 `(call-with-output-file %output
693 (exit 1) ; would actually fail
696 (package-derivation s %bootstrap-guile (%current-system))))
697 (o (derivation->output-path d)))
698 (with-derivation-substitute d c
699 (set-build-options s #:use-substitutes? #t
700 #:substitute-urls (%test-substitute-urls))
701 (and (has-substitutes? s o)
702 (build-derivations s (list d))
703 (equal? c (call-with-input-file o get-string-all)))))))
705 (test-assert "substitute + build-things with output path"
707 (let* ((c (random-text)) ;contents of the output
708 (d (build-expression->derivation
710 `(call-with-output-file %output
712 (exit 1) ;would actually fail
715 (package-derivation s %bootstrap-guile (%current-system))))
716 (o (derivation->output-path d)))
717 (with-derivation-substitute d c
718 (set-build-options s #:use-substitutes? #t
719 #:substitute-urls (%test-substitute-urls))
720 (and (has-substitutes? s o)
721 (build-things s (list o)) ;give the output path
723 (equal? c (call-with-input-file o get-string-all)))))))
725 (test-assert "substitute + build-things with specific output"
727 (let* ((c (random-text)) ;contents of the output
728 (d (build-expression->derivation
729 s "substitute-me" `(begin ,c (exit 1)) ;would fail
730 #:outputs '("out" "one" "two")
732 (package-derivation s %bootstrap-guile (%current-system))))
733 (o (derivation->output-path d)))
734 (with-derivation-substitute d c
735 (set-build-options s #:use-substitutes? #t
736 #:substitute-urls (%test-substitute-urls))
737 (and (has-substitutes? s o)
739 ;; Ask for nothing but the "out" output of D.
740 (build-things s `((,(derivation-file-name d) . "out")))
743 (equal? c (call-with-input-file o get-string-all)))))))
745 (test-assert "substitute, corrupt output hash"
746 ;; Tweak the substituter into installing a substitute whose hash doesn't
747 ;; match the one announced in the narinfo. The daemon must notice this and
750 (let* ((c "hello, world") ; contents of the output
751 (d (build-expression->derivation
752 s "corrupt-substitute"
755 (package-derivation s %bootstrap-guile (%current-system))))
756 (o (derivation->output-path d)))
757 (with-derivation-substitute d c
758 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
760 ;; Make sure we use 'guix substitute'.
762 #:use-substitutes? #t
764 #:substitute-urls (%test-substitute-urls))
765 (and (has-substitutes? s o)
766 (guard (c ((store-protocol-error? c)
767 ;; XXX: the daemon writes "hash mismatch in downloaded
768 ;; path", but the actual error returned to the client
769 ;; doesn't mention that.
771 (not (zero? (store-protocol-error-status c)))))
772 (build-derivations s (list d))
775 (test-assert "substitute --fallback"
777 (let* ((t (random-text)) ; contents of the output
778 (d (build-expression->derivation
779 s "substitute-me-not"
780 `(call-with-output-file %output
784 (package-derivation s %bootstrap-guile (%current-system))))
785 (o (derivation->output-path d)))
786 ;; Create fake substituter data, to be read by 'guix substitute'.
787 (with-derivation-narinfo d
788 ;; Make sure we use 'guix substitute'.
789 (set-build-options s #:use-substitutes? #t
790 #:substitute-urls (%test-substitute-urls))
791 (and (has-substitutes? s o)
792 (guard (c ((store-protocol-error? c)
793 ;; The substituter failed as expected. Now make
794 ;; sure that #:fallback? #t works correctly.
796 #:use-substitutes? #t
798 (%test-substitute-urls)
800 (and (build-derivations s (list d))
801 (equal? t (call-with-input-file o
804 (build-derivations s (list d))
807 (test-assert "export/import several paths"
808 (let* ((texts (unfold (cut >= <> 10)
809 (lambda _ (random-text))
812 (files (map (cut add-text-to-store %store "text" <>) texts))
813 (dump (call-with-bytevector-output-port
814 (cut export-paths %store files <>))))
815 (delete-paths %store files)
816 (and (every (negate file-exists?) files)
817 (let* ((source (open-bytevector-input-port dump))
818 (imported (import-paths %store source)))
819 (and (equal? imported files)
820 (every file-exists? files)
823 (call-with-input-file file
827 (test-assert "export/import paths, ensure topological order"
828 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
829 (file1 (add-text-to-store %store "foo" (random-text)
831 (file2 (add-text-to-store %store "bar" (random-text)
833 (files (list file1 file2))
834 (dump1 (call-with-bytevector-output-port
835 (cute export-paths %store (list file1 file2) <>)))
836 (dump2 (call-with-bytevector-output-port
837 (cute export-paths %store (list file2 file1) <>))))
838 (delete-paths %store files)
839 (and (every (negate file-exists?) files)
840 (bytevector=? dump1 dump2)
841 (let* ((source (open-bytevector-input-port dump1))
842 (imported (import-paths %store source)))
843 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
844 (and (equal? imported (list file1 file2))
845 (every file-exists? files)
846 (equal? (list file0) (references %store file1))
847 (equal? (list file1) (references %store file2)))))))
849 (test-assert "export/import incomplete"
850 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
851 (file1 (add-text-to-store %store "foo" (random-text)
853 (file2 (add-text-to-store %store "bar" (random-text)
855 (dump (call-with-bytevector-output-port
856 (cute export-paths %store (list file2) <>))))
857 (delete-paths %store (list file0 file1 file2))
858 (guard (c ((store-protocol-error? c)
859 (and (not (zero? (store-protocol-error-status c)))
860 (string-contains (store-protocol-error-message c)
862 ;; Here we get an exception because DUMP does not include FILE0 and
863 ;; FILE1, which are dependencies of FILE2.
864 (import-paths %store (open-bytevector-input-port dump)))))
866 (test-assert "export/import recursive"
867 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
868 (file1 (add-text-to-store %store "foo" (random-text)
870 (file2 (add-text-to-store %store "bar" (random-text)
872 (dump (call-with-bytevector-output-port
873 (cute export-paths %store (list file2) <>
875 (delete-paths %store (list file0 file1 file2))
876 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
877 (and (equal? imported (list file0 file1 file2))
878 (every file-exists? (list file0 file1 file2))
879 (equal? (list file0) (references %store file1))
880 (equal? (list file1) (references %store file2))))))
882 (test-assert "write-file & export-path yield the same result"
883 ;; Here we compare 'write-file' and the daemon's own implementation.
884 ;; 'write-file' is the reference because we know it sorts file
885 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
886 ;; currently happen to be sorted as a side-effect of some unrelated
887 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
889 (run-with-store %store
890 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
891 (out1 -> (derivation->output-path drv1))
892 (data -> (unfold (cut >= <> 26)
894 (random-bytevector 128))
898 (use-modules (rnrs io ports) (srfi srfi-1))
904 (+ i (char->integer #\a)))))
906 (define (touch file data)
907 (call-with-output-file file
909 (put-bytevector port data))))
914 ;; The files must be different so they have
915 ;; different inode numbers, and the inode
916 ;; order must differ from the lexicographic
919 (append (drop letters 10)
923 (drv2 (gexp->derivation "bunch" build))
924 (out2 -> (derivation->output-path drv2))
925 (item-info -> (store-lift query-path-info)))
927 (built-derivations (list drv1 drv2))
929 (lambda (item result)
931 (let-values (((port get) (open-sha256-port)))
932 (write-file item port)
936 ;; 'query-path-info' returns a hash produced by using the
937 ;; daemon's C++ 'dump' function, which is the implementation
939 (>>= (item-info item)
943 (bytevector=? (path-info-hash info) ref-hash))))))
946 #:guile-for-build (%guile-for-build)))
948 (test-assert "import corrupt path"
949 (let* ((text (random-text))
950 (file (add-text-to-store %store "text" text))
951 (dump (call-with-bytevector-output-port
952 (cut export-paths %store (list file) <>))))
953 (delete-paths %store (list file))
955 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
956 ;; the file contents in DUMP, regardless of the store prefix.
958 (byte (bytevector-u8-ref dump index)))
959 (bytevector-u8-set! dump index (logxor #xff byte)))
961 (and (not (file-exists? file))
962 (guard (c ((store-protocol-error? c)
964 (and (not (zero? (store-protocol-error-status c)))
965 (string-contains (store-protocol-error-message c)
967 (let* ((source (open-bytevector-input-port dump))
968 (imported (import-paths %store source)))
969 (pk 'corrupt-imported imported)
972 (test-assert "verify-store"
973 (let* ((text (random-text))
974 (file1 (add-text-to-store %store "foo" text))
975 (file2 (add-text-to-store %store "bar" (random-text)
977 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
980 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
982 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
983 ;; without actually creating the file. ;
984 (call-with-output-file file1
986 (display text port)))
987 (pk 'verify3 (verify-store %store)))))) ;OK again
989 (test-assert "verify-store + check-contents"
990 ;; XXX: This test is I/O intensive.
992 (let* ((text (random-text))
993 (drv (build-expression->derivation
995 `(let ((out (assoc-ref %outputs "out")))
996 (call-with-output-file out
998 (display ,text port)))
1001 (package-derivation s %bootstrap-guile (%current-system))))
1002 (file (derivation->output-path drv)))
1003 (with-derivation-substitute drv text
1004 (and (build-derivations s (list drv))
1005 (verify-store s #:check-contents? #t) ;should be OK
1008 (call-with-output-file file
1010 (display "corrupt!" port)))
1013 ;; Make sure the corruption is detected. We don't test repairing
1014 ;; because only "trusted" users are allowed to do it, but we
1015 ;; don't expose that notion of trusted users that nix-daemon
1016 ;; supports because it seems dubious and redundant with what the
1017 ;; OS provides (in Nix "trusted" users have additional
1018 ;; privileges, such as overriding the set of substitute URLs, but
1019 ;; we instead want to allow anyone to modify them, provided
1020 ;; substitutes are signed by a root-approved key.)
1021 (not (verify-store s #:check-contents? #t))
1023 ;; Delete the corrupt item to leave the store in a clean state.
1024 (delete-paths s (list file)))))))
1026 (test-assert "build-things, check mode"
1028 (call-with-temporary-output-file
1029 (lambda (entropy entropy-port)
1030 (write (random-text) entropy-port)
1031 (force-output entropy-port)
1032 (let* ((drv (build-expression->derivation
1033 store "non-deterministic"
1035 (use-modules (rnrs io ports))
1036 (let ((out (assoc-ref %outputs "out")))
1037 (call-with-output-file out
1039 ;; Rely on the fact that tests do not use the
1040 ;; chroot, and thus ENTROPY is readable.
1041 (display (call-with-input-file ,entropy
1046 (package-derivation store %bootstrap-guile (%current-system))))
1047 (file (derivation->output-path drv)))
1048 (and (build-things store (list (derivation-file-name drv)))
1050 (write (random-text) entropy-port)
1051 (force-output entropy-port)
1052 (guard (c ((store-protocol-error? c)
1053 (pk 'determinism-exception c)
1054 (and (not (zero? (store-protocol-error-status c)))
1055 (string-contains (store-protocol-error-message c)
1057 ;; This one will produce a different result. Since we're in
1058 ;; 'check' mode, this must fail.
1059 (build-things store (list (derivation-file-name drv))
1063 (test-assert "build-succeeded trace in check mode"
1065 (call-with-output-string
1067 (let ((d (build-expression->derivation
1068 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1070 (package-derivation %store %bootstrap-guile))))
1071 (build-derivations %store (list d))
1072 (parameterize ((current-build-output-port port))
1073 (build-derivations %store (list d) (build-mode check))))))
1074 "@ build-succeeded"))
1076 (test-assert "build multiple times"
1078 ;; Ask to build twice.
1079 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1081 (call-with-temporary-output-file
1082 (lambda (entropy entropy-port)
1083 (write (random-text) entropy-port)
1084 (force-output entropy-port)
1085 (let* ((drv (build-expression->derivation
1086 store "non-deterministic"
1088 (use-modules (rnrs io ports))
1089 (let ((out (assoc-ref %outputs "out")))
1090 (call-with-output-file out
1092 ;; Rely on the fact that tests do not use the
1093 ;; chroot, and thus ENTROPY is accessible.
1094 (display (call-with-input-file ,entropy
1097 (call-with-output-file ,entropy
1099 (write 'foobar port)))))
1102 (package-derivation store %bootstrap-guile (%current-system))))
1103 (file (derivation->output-path drv)))
1104 (guard (c ((store-protocol-error? c)
1105 (pk 'multiple-build c)
1106 (and (not (zero? (store-protocol-error-status c)))
1107 (string-contains (store-protocol-error-message c)
1109 ;; This one will produce a different result on the second run.
1110 (current-build-output-port (current-error-port))
1111 (build-things store (list (derivation-file-name drv)))
1114 (test-equal "store-lower"
1116 (let* ((add (store-lower text-file))
1117 (file (add %store "foo" "Lowered.")))
1118 (call-with-input-file file get-string-all)))
1120 (test-equal "current-system"
1122 (parameterize ((%current-system "frob"))
1123 (run-with-store %store
1124 (mbegin %store-monad
1125 (set-current-system "bar")
1129 (test-assert "query-path-info"
1130 (let* ((ref (add-text-to-store %store "ref" "foo"))
1131 (item (add-text-to-store %store "item" "bar" (list ref)))
1132 (info (query-path-info %store item)))
1133 (and (equal? (path-info-references info) (list ref))
1134 (equal? (path-info-hash info)
1137 (call-with-output-string (cut write-file item <>))))))))
1139 (test-assert "path-info-deriver"
1140 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1141 (s (add-to-store %store "bash" #t "sha256"
1142 (search-bootstrap-binary "bash"
1143 (%current-system))))
1144 (d (derivation %store "the-thing"
1146 #:env-vars `(("foo" . ,(random-text)))
1147 #:inputs `((,b) (,s))))
1148 (o (derivation->output-path d)))
1149 (and (build-derivations %store (list d))
1150 (not (path-info-deriver (query-path-info %store b)))
1151 (string=? (derivation-file-name d)
1152 (path-info-deriver (query-path-info %store o))))))
1154 (test-equal "build-cores"
1157 (let* ((build (add-text-to-store store "build.sh"
1158 "echo $NIX_BUILD_CORES > $out"))
1159 (bash (add-to-store store "bash" #t "sha256"
1160 (search-bootstrap-binary "bash"
1161 (%current-system))))
1162 (drv1 (derivation store "the-thing" bash
1164 #:inputs `((,bash) (,build))
1165 #:env-vars `(("x" . ,(random-text)))))
1166 (drv2 (derivation store "the-thing" bash
1168 #:inputs `((,bash) (,build))
1169 #:env-vars `(("x" . ,(random-text))))))
1170 (and (build-derivations store (list drv1))
1172 (set-build-options store #:build-cores 42)
1173 (build-derivations store (list drv2)))
1174 (list (call-with-input-file (derivation->output-path drv1)
1176 (call-with-input-file (derivation->output-path drv2)
1179 (test-equal "multiplexed-build-output"
1180 '("Hello from first." "Hello from second.")
1182 (let* ((build (add-text-to-store store "build.sh"
1183 "echo Hello from $NAME.; echo > $out"))
1184 (bash (add-to-store store "bash" #t "sha256"
1185 (search-bootstrap-binary "bash"
1186 (%current-system))))
1187 (drv1 (derivation store "one" bash
1189 #:inputs `((,bash) (,build))
1190 #:env-vars `(("NAME" . "first")
1191 ("x" . ,(random-text)))))
1192 (drv2 (derivation store "two" bash
1194 #:inputs `((,bash) (,build))
1195 #:env-vars `(("NAME" . "second")
1196 ("x" . ,(random-text))))))
1197 (set-build-options store
1198 #:print-build-trace #t
1199 #:multiplexed-build-output? #t
1200 #:max-build-jobs 10)
1201 (let ((port (open-output-string)))
1202 ;; Send the build log to PORT.
1203 (parameterize ((current-build-output-port port))
1204 (build-derivations store (list drv1 drv2)))
1206 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1207 ;; traces that allow us to retrieve each builder's output (we assume
1208 ;; there's exactly one "build-output" trace for each builder, which is
1210 (let* ((log (get-output-string port))
1211 (started (fold-matches
1212 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1215 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1217 (output (fold-matches
1218 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1220 (drv-pid (lambda (name)
1222 (let ((drv (match:substring m 1))
1223 (pid (string->number
1224 (match:substring m 4))))
1225 (and (string-suffix? name drv) pid)))))
1226 (pid-log (lambda (pid)
1228 (let ((n (string->number
1229 (match:substring m 1)))
1230 (len (string->number
1231 (match:substring m 2)))
1232 (str (match:substring m 3)))
1234 (= (string-length str) (- len 1))
1236 (pid1 (any (drv-pid "one.drv") started))
1237 (pid2 (any (drv-pid "two.drv") started)))
1238 (list (any (pid-log pid1) output)
1239 (any (pid-log pid2) output)))))))