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 ((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")))
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 "permanent root"
205 (let* ((p (with-store store
206 (let ((p (add-text-to-store store "random-text"
208 (add-permanent-root p)
209 (add-permanent-root p) ; should not throw
211 (and (member p (live-paths %store))
213 (remove-permanent-root p)
214 (->bool (member p (dead-paths %store)))))))
216 (test-assert "dead path can be explicitly collected"
217 (let ((p (add-text-to-store %store "random-text"
219 (let-values (((paths freed) (delete-paths %store (list p))))
220 (and (equal? paths (list p))
221 ;; XXX: On some file systems (notably Btrfs), freed
222 ;; may return 0. See <https://bugs.gnu.org/29363>.
224 (not (file-exists? p))))))
226 (test-assert "add-text-to-store/add-to-store vs. delete-paths"
227 ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
228 ;; store item without noticing that it is no longer valid.
230 (let* ((text (random-text))
231 (file (search-path %load-path "guix.scm"))
232 (path1 (add-text-to-store store "delete-me" text))
233 (path2 (add-to-store store "delete-me" #t "sha256" file))
234 (deleted (delete-paths store (list path1 path2))))
235 (and (string=? path1 (add-text-to-store store "delete-me" text))
236 (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
237 (lset= string=? deleted (list path1 path2))
238 (valid-path? store path1)
239 (valid-path? store path2)
241 (file-exists? path2)))))
243 (test-equal "add-file-tree-to-store"
246 ("./bar" directory #t)
247 ("./foo" directory #t)
248 ("./foo/a" regular "file a")
249 ("./foo/b" symlink "a")
250 ("./foo/c" directory #t)
251 ("./foo/c/p" regular "file p")
252 ("./foo/c/q" directory #t)
253 ("./foo/c/q/x" regular
254 ,(string-append "#!" %shell "\nexit 42"))
255 ("./foo/c/q/y" symlink "..")
256 ("./foo/c/q/z" directory #t))
257 (let* ((tree `("file-tree" directory
259 ("a" regular (data "file a"))
262 ("p" regular (data ,(string->utf8 "file p")))
265 (data ,(string-append "#!" %shell "\nexit 42")))
269 (result (add-file-tree-to-store %store tree)))
270 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
271 (with-directory-excursion result
273 (let ((type (stat:type (lstat file))))
276 ((or 'regular 'executable)
277 (call-with-input-file file
279 ('symlink (readlink file))
281 (find-files "." #:directories? #t))))))
283 (test-equal "add-file-tree-to-store, flat"
285 (let* ((tree `("flat-file" regular (data "Hello, world!")))
286 (result (add-file-tree-to-store %store tree)))
287 (and (file-exists? result)
288 (call-with-input-file result get-string-all))))
290 (test-assert "references"
291 (let* ((t1 (add-text-to-store %store "random1"
293 (t2 (add-text-to-store %store "random2"
294 (random-text) (list t1))))
295 (and (equal? (list t1) (references %store t2))
296 (equal? (list t2) (referrers %store t1))
297 (null? (references %store t1))
298 (null? (referrers %store t2)))))
300 (test-assert "references/substitutes missing reference info"
302 (set-build-options s #:use-substitutes? #f)
303 (guard (c ((store-protocol-error? c) #t))
304 (let* ((b (add-to-store s "bash" #t "sha256"
305 (search-bootstrap-binary "bash"
307 (d (derivation s "the-thing" b '("--help")
309 (references/substitutes s (list (derivation->output-path d) b))
312 (test-assert "references/substitutes with substitute info"
314 (set-build-options s #:use-substitutes? #t)
315 (let* ((t1 (add-text-to-store s "random1" (random-text)))
316 (t2 (add-text-to-store s "random2" (random-text)
318 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
319 (b (add-to-store s "bash" #t "sha256"
320 (search-bootstrap-binary "bash"
322 (d (derivation s "the-thing" b `("-e" ,t3)
323 #:inputs `((,b) (,t3) (,t2))
324 #:env-vars `(("t2" . ,t2))))
325 (o (derivation->output-path d)))
326 (with-derivation-narinfo d
327 (sha256 => (gcrypt:sha256 (string->utf8 t2)))
328 (references => (list t2))
330 (equal? (references/substitutes s (list o t3 t2 t1))
336 (test-equal "substitutable-path-info when substitutes are turned off"
339 (set-build-options s #:use-substitutes? #f)
340 (let* ((b (add-to-store s "bash" #t "sha256"
341 (search-bootstrap-binary "bash"
343 (d (derivation s "the-thing" b '("--version")
345 (o (derivation->output-path d)))
346 (with-derivation-narinfo d
347 (substitutable-path-info s (list o))))))
349 (test-equal "substitutable-paths when substitutes are turned off"
352 (set-build-options s #:use-substitutes? #f)
353 (let* ((b (add-to-store s "bash" #t "sha256"
354 (search-bootstrap-binary "bash"
356 (d (derivation s "the-thing" b '("--version")
358 (o (derivation->output-path d)))
359 (with-derivation-narinfo d
360 (substitutable-paths s (list o))))))
362 (test-assert "requisites"
363 (let* ((t1 (add-text-to-store %store "random1"
365 (t2 (add-text-to-store %store "random2"
366 (random-text) (list t1)))
367 (t3 (add-text-to-store %store "random3"
368 (random-text) (list t2)))
369 (t4 (add-text-to-store %store "random4"
370 (random-text) (list t1 t3))))
372 (and (= (length x) (length y))
375 (and (same? (requisites %store (list t1)) (list t1))
376 (same? (requisites %store (list t2)) (list t1 t2))
377 (same? (requisites %store (list t3)) (list t1 t2 t3))
378 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
379 (same? (requisites %store (list t1 t2 t3 t4))
380 (list t1 t2 t3 t4)))))
382 (test-assert "derivers"
383 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
384 (s (add-to-store %store "bash" #t "sha256"
385 (search-bootstrap-binary "bash"
387 (d (derivation %store "the-thing"
389 #:env-vars `(("foo" . ,(random-text)))
390 #:inputs `((,b) (,s))))
391 (o (derivation->output-path d)))
392 (and (build-derivations %store (list d))
393 (equal? (query-derivation-outputs %store (derivation-file-name d))
395 (equal? (valid-derivers %store o)
396 (list (derivation-file-name d))))))
398 (test-equal "with-build-handler"
400 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
401 (s (add-to-store %store "bash" #t "sha256"
402 (search-bootstrap-binary "bash"
404 (d1 (derivation %store "the-thing"
406 #:env-vars `(("foo" . ,(random-text)))
407 #:sources (list b s)))
408 (d2 (derivation %store "the-thing"
410 #:env-vars `(("foo" . ,(random-text))
412 #:sources (list b s)))
413 (o1 (derivation->output-path d1))
414 (o2 (derivation->output-path d2)))
417 (lambda (continue store things mode)
420 (set! counter (+ 1 counter))
421 (if (string=? drv (derivation-file-name d1))
423 (and (string=? drv (derivation-file-name d2))
426 (build-derivations %store (list d1))
427 (build-derivations %store (list d2))
430 (test-equal "with-build-handler + with-store"
432 ;; Check that STORE remains valid when the build handler invokes CONTINUE,
433 ;; even though 'with-build-handler' is outside the dynamic extent of
435 (with-build-handler (lambda (continue store things mode)
438 (and (string-suffix? "thingie.drv" drv)
440 (store-connection-socket store)))
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 (d (derivation store "thingie"
449 #:env-vars `(("foo" . ,(random-text)))
450 #:sources (list b s))))
451 (build-derivations store (list d))
453 ;; Here STORE's socket should still be open.
454 (and (valid-path? store (derivation->output-path d))
457 (test-assert "map/accumulate-builds"
458 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
459 (s (add-to-store %store "bash" #t "sha256"
460 (search-bootstrap-binary "bash"
462 (d1 (derivation %store "the-thing"
464 #:env-vars `(("foo" . ,(random-text)))
465 #:sources (list b s)))
466 (d2 (derivation %store "the-thing"
468 #:env-vars `(("foo" . ,(random-text))
470 #:sources (list b s))))
471 (with-build-handler (lambda (continue store things mode)
472 (equal? (map derivation-file-name (list d1 d2))
474 (map/accumulate-builds %store
476 (build-derivations %store (list drv))
477 (add-to-store %store "content-addressed"
479 (derivation->output-path drv)))
482 (test-assert "mapm/accumulate-builds"
483 (let* ((d1 (run-with-store %store
484 (gexp->derivation "foo" #~(mkdir #$output))))
485 (d2 (run-with-store %store
486 (gexp->derivation "bar" #~(mkdir #$output)))))
487 (with-build-handler (lambda (continue store things mode)
488 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
490 (run-with-store %store
491 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
493 (test-equal "mapm/accumulate-builds, %current-target-system"
494 (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
495 ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
497 (run-with-store %store
498 (mlet %store-monad ((lst1 (mapm %store-monad
500 (current-target-system))
502 (lst2 (mapm/accumulate-builds
504 (current-target-system))
506 (return (list lst1 lst2)))
508 #:target "i586-pc-gnu"))
510 (test-assert "topologically-sorted, one item"
511 (let* ((a (add-text-to-store %store "a" "a"))
512 (b (add-text-to-store %store "b" "b" (list a)))
513 (c (add-text-to-store %store "c" "c" (list b)))
514 (d (add-text-to-store %store "d" "d" (list c)))
515 (s (topologically-sorted %store (list d))))
516 (equal? s (list a b c d))))
518 (test-assert "topologically-sorted, several items"
519 (let* ((a (add-text-to-store %store "a" "a"))
520 (b (add-text-to-store %store "b" "b" (list a)))
521 (c (add-text-to-store %store "c" "c" (list b)))
522 (d (add-text-to-store %store "d" "d" (list c)))
523 (s1 (topologically-sorted %store (list d a c b)))
524 (s2 (topologically-sorted %store (list b d c a b d))))
525 (equal? s1 s2 (list a b c d))))
527 (test-assert "topologically-sorted, more difficult"
528 (let* ((a (add-text-to-store %store "a" "a"))
529 (b (add-text-to-store %store "b" "b" (list a)))
530 (c (add-text-to-store %store "c" "c" (list b)))
531 (d (add-text-to-store %store "d" "d" (list c)))
532 (w (add-text-to-store %store "w" "w"))
533 (x (add-text-to-store %store "x" "x" (list w)))
534 (y (add-text-to-store %store "y" "y" (list x d)))
535 (s1 (topologically-sorted %store (list y)))
536 (s2 (topologically-sorted %store (list c y)))
537 (s3 (topologically-sorted %store (cons y (references %store y)))))
538 ;; The order in which 'references' returns the references of Y is
539 ;; unspecified, so accommodate.
540 (let* ((x-then-d? (equal? (references %store y) (list x d))))
544 (list a b c d w x y)))
548 (list a b c d w x y)))
549 (lset= string=? s1 s3)))))
551 (test-assert "current-build-output-port, UTF-8"
552 ;; Are UTF-8 strings in the build log properly interpreted?
554 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
555 (call-with-output-string
557 (parameterize ((current-build-output-port port))
558 (let* ((s "Here’s a Greek letter: λ.")
559 (d (build-expression->derivation
560 %store "foo" `(display ,s)
562 (package-derivation s %bootstrap-guile (%current-system)))))
563 (guard (c ((store-protocol-error? c) #t))
564 (build-derivations %store (list d))))))))
565 "Here’s a Greek letter: λ."))
567 (test-assert "current-build-output-port, UTF-8 + garbage"
568 ;; What about a mixture of UTF-8 + garbage?
570 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
571 (call-with-output-string
573 (parameterize ((current-build-output-port port))
574 (let ((d (build-expression->derivation
577 (use-modules (rnrs io ports))
578 (display "garbage: ")
579 (put-bytevector (current-output-port) #vu8(128))
580 (display "lambda: λ\n"))
582 (package-derivation %store %bootstrap-guile))))
583 (guard (c ((store-protocol-error? c) #t))
584 (build-derivations %store (list d))))))))
585 "garbage: �lambda: λ"))
587 (test-assert "log-file, derivation"
588 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
589 (s (add-to-store %store "bash" #t "sha256"
590 (search-bootstrap-binary "bash"
592 (d (derivation %store "the-thing"
594 #:env-vars `(("foo" . ,(random-text)))
595 #:inputs `((,b) (,s)))))
596 (and (build-derivations %store (list d))
597 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
599 (test-assert "log-file, output file name"
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 (o (derivation->output-path d)))
609 (and (build-derivations %store (list d))
610 (file-exists? (pk (log-file %store o)))
611 (string=? (log-file %store (derivation-file-name d))
612 (log-file %store o)))))
614 (test-assert "no substitutes"
616 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
617 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
618 (o (map derivation->output-path (list d1 d2))))
619 (set-build-options s #:use-substitutes? #f)
620 (and (not (has-substitutes? s (derivation-file-name d1)))
621 (not (has-substitutes? s (derivation-file-name d2)))
622 (null? (substitutable-paths s o))
623 (null? (substitutable-path-info s o))))))
625 (test-assert "build-things with output path"
627 (let* ((c (random-text)) ;contents of the output
628 (d (build-expression->derivation
630 `(call-with-output-file %output
634 (package-derivation s %bootstrap-guile (%current-system))))
635 (o (derivation->output-path d)))
636 (set-build-options s #:use-substitutes? #f)
638 ;; Pass 'build-things' the output file name, O. However, since there
639 ;; are no substitutes for O, it will just do nothing.
640 (build-things s (list o))
641 (not (valid-path? s o)))))
643 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
645 (test-assert "substitute query"
647 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
648 (o (derivation->output-path d)))
649 ;; Create fake substituter data, to be read by 'guix substitute'.
650 (with-derivation-narinfo d
651 ;; Remove entry from the local cache.
653 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
654 "/guix/substitute")))
656 ;; Make sure 'guix substitute' correctly communicates the above
658 (set-build-options s #:use-substitutes? #t
659 #:substitute-urls (%test-substitute-urls))
660 (and (has-substitutes? s o)
661 (equal? (list o) (substitutable-paths s (list o)))
662 (match (pk 'spi (substitutable-path-info s (list o)))
663 (((? substitutable? s))
664 (and (string=? (substitutable-deriver s)
665 (derivation-file-name d))
666 (null? (substitutable-references s))
667 (equal? (substitutable-nar-size s) 1234)))))))))
669 (test-assert "substitute query, alternating URLs"
670 (let* ((d (with-store s
671 (package-derivation s %bootstrap-guile (%current-system))))
672 (o (derivation->output-path d)))
673 (with-derivation-narinfo d
674 ;; Remove entry from the local cache.
676 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
677 "/guix/substitute")))
679 ;; Note: We reconnect to the daemon to force a new instance of 'guix
680 ;; substitute' to be used; otherwise the #:substitute-urls of
681 ;; 'set-build-options' would have no effect.
683 (and (with-store s ;the right substitute URL
684 (set-build-options s #:use-substitutes? #t
685 #:substitute-urls (%test-substitute-urls))
686 (has-substitutes? s o))
687 (with-store s ;the wrong one
688 (set-build-options s #:use-substitutes? #t
689 #:substitute-urls (list
690 "http://does-not-exist"))
691 (not (has-substitutes? s o)))
692 (with-store s ;the right one again
693 (set-build-options s #:use-substitutes? #t
694 #:substitute-urls (%test-substitute-urls))
695 (has-substitutes? s o))
696 (with-store s ;empty list of URLs
697 (set-build-options s #:use-substitutes? #t
698 #:substitute-urls '())
699 (not (has-substitutes? s o)))))))
701 (test-assert "substitute"
703 (let* ((c (random-text)) ; contents of the output
704 (d (build-expression->derivation
706 `(call-with-output-file %output
708 (exit 1) ; would actually fail
711 (package-derivation s %bootstrap-guile (%current-system))))
712 (o (derivation->output-path d)))
713 (with-derivation-substitute d c
714 (set-build-options s #:use-substitutes? #t
715 #:substitute-urls (%test-substitute-urls))
716 (and (has-substitutes? s o)
717 (build-derivations s (list d))
718 (equal? c (call-with-input-file o get-string-all)))))))
720 (test-assert "substitute + build-things with output path"
722 (let* ((c (random-text)) ;contents of the output
723 (d (build-expression->derivation
725 `(call-with-output-file %output
727 (exit 1) ;would actually fail
730 (package-derivation s %bootstrap-guile (%current-system))))
731 (o (derivation->output-path d)))
732 (with-derivation-substitute d c
733 (set-build-options s #:use-substitutes? #t
734 #:substitute-urls (%test-substitute-urls))
735 (and (has-substitutes? s o)
736 (build-things s (list o)) ;give the output path
738 (equal? c (call-with-input-file o get-string-all)))))))
740 (test-assert "substitute + build-things with specific output"
742 (let* ((c (random-text)) ;contents of the output
743 (d (build-expression->derivation
744 s "substitute-me" `(begin ,c (exit 1)) ;would fail
745 #:outputs '("out" "one" "two")
747 (package-derivation s %bootstrap-guile (%current-system))))
748 (o (derivation->output-path d)))
749 (with-derivation-substitute d c
750 (set-build-options s #:use-substitutes? #t
751 #:substitute-urls (%test-substitute-urls))
752 (and (has-substitutes? s o)
754 ;; Ask for nothing but the "out" output of D.
755 (build-things s `((,(derivation-file-name d) . "out")))
758 (equal? c (call-with-input-file o get-string-all)))))))
760 (test-assert "substitute, corrupt output hash"
761 ;; Tweak the substituter into installing a substitute whose hash doesn't
762 ;; match the one announced in the narinfo. The daemon must notice this and
765 (let* ((c "hello, world") ; contents of the output
766 (d (build-expression->derivation
767 s "corrupt-substitute"
770 (package-derivation s %bootstrap-guile (%current-system))))
771 (o (derivation->output-path d)))
772 (with-derivation-substitute d c
773 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
775 ;; Make sure we use 'guix substitute'.
777 #:use-substitutes? #t
779 #:substitute-urls (%test-substitute-urls))
780 (and (has-substitutes? s o)
781 (guard (c ((store-protocol-error? c)
782 ;; XXX: the daemon writes "hash mismatch in downloaded
783 ;; path", but the actual error returned to the client
784 ;; doesn't mention that.
786 (not (zero? (store-protocol-error-status c)))))
787 (build-derivations s (list d))
790 (test-assert "substitute --fallback"
792 (let* ((t (random-text)) ; contents of the output
793 (d (build-expression->derivation
794 s "substitute-me-not"
795 `(call-with-output-file %output
799 (package-derivation s %bootstrap-guile (%current-system))))
800 (o (derivation->output-path d)))
801 ;; Create fake substituter data, to be read by 'guix substitute'.
802 (with-derivation-narinfo d
803 ;; Make sure we use 'guix substitute'.
804 (set-build-options s #:use-substitutes? #t
805 #:substitute-urls (%test-substitute-urls))
806 (and (has-substitutes? s o)
807 (guard (c ((store-protocol-error? c)
808 ;; The substituter failed as expected. Now make
809 ;; sure that #:fallback? #t works correctly.
811 #:use-substitutes? #t
813 (%test-substitute-urls)
815 (and (build-derivations s (list d))
816 (equal? t (call-with-input-file o
819 (build-derivations s (list d))
822 (test-assert "export/import several paths"
823 (let* ((texts (unfold (cut >= <> 10)
824 (lambda _ (random-text))
827 (files (map (cut add-text-to-store %store "text" <>) texts))
828 (dump (call-with-bytevector-output-port
829 (cut export-paths %store files <>))))
830 (delete-paths %store files)
831 (and (every (negate file-exists?) files)
832 (let* ((source (open-bytevector-input-port dump))
833 (imported (import-paths %store source)))
834 (and (equal? imported files)
835 (every file-exists? files)
838 (call-with-input-file file
842 (test-assert "export/import paths, ensure topological order"
843 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
844 (file1 (add-text-to-store %store "foo" (random-text)
846 (file2 (add-text-to-store %store "bar" (random-text)
848 (files (list file1 file2))
849 (dump1 (call-with-bytevector-output-port
850 (cute export-paths %store (list file1 file2) <>)))
851 (dump2 (call-with-bytevector-output-port
852 (cute export-paths %store (list file2 file1) <>))))
853 (delete-paths %store files)
854 (and (every (negate file-exists?) files)
855 (bytevector=? dump1 dump2)
856 (let* ((source (open-bytevector-input-port dump1))
857 (imported (import-paths %store source)))
858 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
859 (and (equal? imported (list file1 file2))
860 (every file-exists? files)
861 (equal? (list file0) (references %store file1))
862 (equal? (list file1) (references %store file2)))))))
864 (test-assert "export/import incomplete"
865 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
866 (file1 (add-text-to-store %store "foo" (random-text)
868 (file2 (add-text-to-store %store "bar" (random-text)
870 (dump (call-with-bytevector-output-port
871 (cute export-paths %store (list file2) <>))))
872 (delete-paths %store (list file0 file1 file2))
873 (guard (c ((store-protocol-error? c)
874 (and (not (zero? (store-protocol-error-status c)))
875 (string-contains (store-protocol-error-message c)
877 ;; Here we get an exception because DUMP does not include FILE0 and
878 ;; FILE1, which are dependencies of FILE2.
879 (import-paths %store (open-bytevector-input-port dump)))))
881 (test-assert "export/import recursive"
882 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
883 (file1 (add-text-to-store %store "foo" (random-text)
885 (file2 (add-text-to-store %store "bar" (random-text)
887 (dump (call-with-bytevector-output-port
888 (cute export-paths %store (list file2) <>
890 (delete-paths %store (list file0 file1 file2))
891 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
892 (and (equal? imported (list file0 file1 file2))
893 (every file-exists? (list file0 file1 file2))
894 (equal? (list file0) (references %store file1))
895 (equal? (list file1) (references %store file2))))))
897 (test-assert "write-file & export-path yield the same result"
898 ;; Here we compare 'write-file' and the daemon's own implementation.
899 ;; 'write-file' is the reference because we know it sorts file
900 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
901 ;; currently happen to be sorted as a side-effect of some unrelated
902 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
904 (run-with-store %store
905 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
906 (out1 -> (derivation->output-path drv1))
907 (data -> (unfold (cut >= <> 26)
909 (random-bytevector 128))
913 (use-modules (rnrs io ports) (srfi srfi-1))
919 (+ i (char->integer #\a)))))
921 (define (touch file data)
922 (call-with-output-file file
924 (put-bytevector port data))))
929 ;; The files must be different so they have
930 ;; different inode numbers, and the inode
931 ;; order must differ from the lexicographic
934 (append (drop letters 10)
938 (drv2 (gexp->derivation "bunch" build))
939 (out2 -> (derivation->output-path drv2))
940 (item-info -> (store-lift query-path-info)))
942 (built-derivations (list drv1 drv2))
944 (lambda (item result)
946 (let-values (((port get) (gcrypt:open-sha256-port)))
947 (write-file item port)
951 ;; 'query-path-info' returns a hash produced by using the
952 ;; daemon's C++ 'dump' function, which is the implementation
954 (>>= (item-info item)
958 (bytevector=? (path-info-hash info) ref-hash))))))
961 #:guile-for-build (%guile-for-build)))
964 (test-assert "import not signed"
965 (let* ((text (random-text))
966 (file (add-file-tree-to-store %store
968 ("text" regular (data ,text))
969 ("link" symlink "text"))))
970 (dump (call-with-bytevector-output-port
972 (write-int 1 port) ;start
974 (write-file file port) ;contents
975 (write-int #x4558494e port) ;%export-magic
976 (write-string file port) ;store item
977 (write-string-list '() port) ;references
978 (write-string "" port) ;deriver
979 (write-int 0 port) ;not signed
981 (write-int 0 port))))) ;done
983 ;; Ensure 'import-paths' raises an exception.
984 (guard (c ((store-protocol-error? c)
985 (and (not (zero? (store-protocol-error-status c)))
986 (string-contains (store-protocol-error-message c)
987 "lacks a signature"))))
988 (let* ((source (open-bytevector-input-port dump))
989 (imported (import-paths %store source)))
990 (pk 'unsigned-imported imported)
993 (test-assert "import signed by unauthorized key"
994 (let* ((text (random-text))
995 (file (add-file-tree-to-store %store
997 ("text" regular (data ,text))
998 ("link" symlink "text"))))
999 (key (gcrypt:generate-key
1000 (gcrypt:string->canonical-sexp
1001 "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
1002 (dump (call-with-bytevector-output-port
1004 (write-int 1 port) ;start
1006 (write-file file port) ;contents
1007 (write-int #x4558494e port) ;%export-magic
1008 (write-string file port) ;store item
1009 (write-string-list '() port) ;references
1010 (write-string "" port) ;deriver
1011 (write-int 1 port) ;signed
1012 (write-string (gcrypt:canonical-sexp->string
1014 (gcrypt:bytevector->hash-data
1015 (gcrypt:sha256 #vu8(0 1 2))
1017 (gcrypt:find-sexp-token key 'private-key)
1018 (gcrypt:find-sexp-token key 'public-key)))
1021 (write-int 0 port))))) ;done
1023 ;; Ensure 'import-paths' raises an exception.
1024 (guard (c ((store-protocol-error? c)
1025 (and (not (zero? (store-protocol-error-status c)))
1026 (string-contains (store-protocol-error-message c)
1027 "unauthorized public key"))))
1028 (let* ((source (open-bytevector-input-port dump))
1029 (imported (import-paths %store source)))
1030 (pk 'unauthorized-imported imported)
1033 (test-assert "import corrupt path"
1034 (let* ((text (random-text))
1035 (file (add-text-to-store %store "text" text))
1036 (dump (call-with-bytevector-output-port
1037 (cut export-paths %store (list file) <>))))
1038 (delete-paths %store (list file))
1040 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
1041 ;; the file contents in DUMP, regardless of the store prefix.
1043 (byte (bytevector-u8-ref dump index)))
1044 (bytevector-u8-set! dump index (logxor #xff byte)))
1046 (and (not (file-exists? file))
1047 (guard (c ((store-protocol-error? c)
1049 (and (not (zero? (store-protocol-error-status c)))
1050 (string-contains (store-protocol-error-message c)
1052 (let* ((source (open-bytevector-input-port dump))
1053 (imported (import-paths %store source)))
1054 (pk 'corrupt-imported imported)
1057 (test-assert "verify-store"
1058 (let* ((text (random-text))
1059 (file1 (add-text-to-store %store "foo" text))
1060 (file2 (add-text-to-store %store "bar" (random-text)
1062 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
1065 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
1067 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
1068 ;; without actually creating the file. ;
1069 (call-with-output-file file1
1071 (display text port)))
1072 (pk 'verify3 (verify-store %store)))))) ;OK again
1074 (test-assert "verify-store + check-contents"
1075 ;; XXX: This test is I/O intensive.
1077 (let* ((text (random-text))
1078 (drv (build-expression->derivation
1080 `(let ((out (assoc-ref %outputs "out")))
1081 (call-with-output-file out
1083 (display ,text port)))
1086 (package-derivation s %bootstrap-guile (%current-system))))
1087 (file (derivation->output-path drv)))
1088 (with-derivation-substitute drv text
1089 (and (build-derivations s (list drv))
1090 (verify-store s #:check-contents? #t) ;should be OK
1093 (call-with-output-file file
1095 (display "corrupt!" port)))
1098 ;; Make sure the corruption is detected. We don't test repairing
1099 ;; because only "trusted" users are allowed to do it, but we
1100 ;; don't expose that notion of trusted users that nix-daemon
1101 ;; supports because it seems dubious and redundant with what the
1102 ;; OS provides (in Nix "trusted" users have additional
1103 ;; privileges, such as overriding the set of substitute URLs, but
1104 ;; we instead want to allow anyone to modify them, provided
1105 ;; substitutes are signed by a root-approved key.)
1106 (not (verify-store s #:check-contents? #t))
1108 ;; Delete the corrupt item to leave the store in a clean state.
1109 (delete-paths s (list file)))))))
1111 (test-assert "build-things, check mode"
1113 (call-with-temporary-output-file
1114 (lambda (entropy entropy-port)
1115 (write (random-text) entropy-port)
1116 (force-output entropy-port)
1117 (let* ((drv (build-expression->derivation
1118 store "non-deterministic"
1120 (use-modules (rnrs io ports))
1121 (let ((out (assoc-ref %outputs "out")))
1122 (call-with-output-file out
1124 ;; Rely on the fact that tests do not use the
1125 ;; chroot, and thus ENTROPY is readable.
1126 (display (call-with-input-file ,entropy
1131 (package-derivation store %bootstrap-guile (%current-system))))
1132 (file (derivation->output-path drv)))
1133 (and (build-things store (list (derivation-file-name drv)))
1135 (write (random-text) entropy-port)
1136 (force-output entropy-port)
1137 (guard (c ((store-protocol-error? c)
1138 (pk 'determinism-exception c)
1139 (and (not (zero? (store-protocol-error-status c)))
1140 (string-contains (store-protocol-error-message c)
1142 ;; This one will produce a different result. Since we're in
1143 ;; 'check' mode, this must fail.
1144 (build-things store (list (derivation-file-name drv))
1148 (test-assert "build-succeeded trace in check mode"
1150 (call-with-output-string
1152 (let ((d (build-expression->derivation
1153 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1155 (package-derivation %store %bootstrap-guile))))
1156 (build-derivations %store (list d))
1157 (parameterize ((current-build-output-port port))
1158 (build-derivations %store (list d) (build-mode check))))))
1159 "@ build-succeeded"))
1161 (test-assert "build multiple times"
1163 ;; Ask to build twice.
1164 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1166 (call-with-temporary-output-file
1167 (lambda (entropy entropy-port)
1168 (write (random-text) entropy-port)
1169 (force-output entropy-port)
1170 (let* ((drv (build-expression->derivation
1171 store "non-deterministic"
1173 (use-modules (rnrs io ports))
1174 (let ((out (assoc-ref %outputs "out")))
1175 (call-with-output-file out
1177 ;; Rely on the fact that tests do not use the
1178 ;; chroot, and thus ENTROPY is accessible.
1179 (display (call-with-input-file ,entropy
1182 (call-with-output-file ,entropy
1184 (write 'foobar port)))))
1187 (package-derivation store %bootstrap-guile (%current-system))))
1188 (file (derivation->output-path drv)))
1189 (guard (c ((store-protocol-error? c)
1190 (pk 'multiple-build c)
1191 (and (not (zero? (store-protocol-error-status c)))
1192 (string-contains (store-protocol-error-message c)
1194 ;; This one will produce a different result on the second run.
1195 (current-build-output-port (current-error-port))
1196 (build-things store (list (derivation-file-name drv)))
1199 (test-equal "store-lower"
1201 (let* ((add (store-lower text-file))
1202 (file (add %store "foo" "Lowered.")))
1203 (call-with-input-file file get-string-all)))
1205 (test-equal "current-system"
1207 (parameterize ((%current-system "frob"))
1208 (run-with-store %store
1209 (mbegin %store-monad
1210 (set-current-system "bar")
1214 (test-assert "query-path-info"
1215 (let* ((ref (add-text-to-store %store "ref" "foo"))
1216 (item (add-text-to-store %store "item" "bar" (list ref)))
1217 (info (query-path-info %store item)))
1218 (and (equal? (path-info-references info) (list ref))
1219 (equal? (path-info-hash info)
1222 (call-with-output-string (cut write-file item <>))))))))
1224 (test-assert "path-info-deriver"
1225 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1226 (s (add-to-store %store "bash" #t "sha256"
1227 (search-bootstrap-binary "bash"
1228 (%current-system))))
1229 (d (derivation %store "the-thing"
1231 #:env-vars `(("foo" . ,(random-text)))
1232 #:inputs `((,b) (,s))))
1233 (o (derivation->output-path d)))
1234 (and (build-derivations %store (list d))
1235 (not (path-info-deriver (query-path-info %store b)))
1236 (string=? (derivation-file-name d)
1237 (path-info-deriver (query-path-info %store o))))))
1239 (test-equal "build-cores"
1242 (let* ((build (add-text-to-store store "build.sh"
1243 "echo $NIX_BUILD_CORES > $out"))
1244 (bash (add-to-store store "bash" #t "sha256"
1245 (search-bootstrap-binary "bash"
1246 (%current-system))))
1247 (drv1 (derivation store "the-thing" bash
1249 #:inputs `((,bash) (,build))
1250 #:env-vars `(("x" . ,(random-text)))))
1251 (drv2 (derivation store "the-thing" bash
1253 #:inputs `((,bash) (,build))
1254 #:env-vars `(("x" . ,(random-text))))))
1255 (and (build-derivations store (list drv1))
1257 (set-build-options store #:build-cores 42)
1258 (build-derivations store (list drv2)))
1259 (list (call-with-input-file (derivation->output-path drv1)
1261 (call-with-input-file (derivation->output-path drv2)
1264 (test-equal "multiplexed-build-output"
1265 '("Hello from first." "Hello from second.")
1267 (let* ((build (add-text-to-store store "build.sh"
1268 "echo Hello from $NAME.; echo > $out"))
1269 (bash (add-to-store store "bash" #t "sha256"
1270 (search-bootstrap-binary "bash"
1271 (%current-system))))
1272 (drv1 (derivation store "one" bash
1274 #:inputs `((,bash) (,build))
1275 #:env-vars `(("NAME" . "first")
1276 ("x" . ,(random-text)))))
1277 (drv2 (derivation store "two" bash
1279 #:inputs `((,bash) (,build))
1280 #:env-vars `(("NAME" . "second")
1281 ("x" . ,(random-text))))))
1282 (set-build-options store
1283 #:print-build-trace #t
1284 #:multiplexed-build-output? #t
1285 #:max-build-jobs 10)
1286 (let ((port (open-output-string)))
1287 ;; Send the build log to PORT.
1288 (parameterize ((current-build-output-port port))
1289 (build-derivations store (list drv1 drv2)))
1291 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1292 ;; traces that allow us to retrieve each builder's output (we assume
1293 ;; there's exactly one "build-output" trace for each builder, which is
1295 (let* ((log (get-output-string port))
1296 (started (fold-matches
1297 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1300 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1302 (output (fold-matches
1303 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1305 (drv-pid (lambda (name)
1307 (let ((drv (match:substring m 1))
1308 (pid (string->number
1309 (match:substring m 4))))
1310 (and (string-suffix? name drv) pid)))))
1311 (pid-log (lambda (pid)
1313 (let ((n (string->number
1314 (match:substring m 1)))
1315 (len (string->number
1316 (match:substring m 2)))
1317 (str (match:substring m 3)))
1319 (= (string-length str) (- len 1))
1321 (pid1 (any (drv-pid "one.drv") started))
1322 (pid2 (any (drv-pid "two.drv") started)))
1323 (list (any (pid-log pid1) output)
1324 (any (pid-log pid2) output)))))))