1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 store)
22 #:use-module (guix utils)
23 #:use-module (guix monads)
24 #:use-module (gcrypt hash)
25 #:use-module (guix base32)
26 #:use-module (guix packages)
27 #:use-module (guix derivations)
28 #:use-module (guix serialization)
29 #:use-module (guix build utils)
30 #:use-module (guix gexp)
31 #:use-module (gnu packages)
32 #:use-module (gnu packages bootstrap)
33 #:use-module (ice-9 match)
34 #:use-module (rnrs bytevectors)
35 #:use-module (rnrs io ports)
36 #:use-module (web uri)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-11)
39 #:use-module (srfi srfi-26)
40 #:use-module (srfi srfi-34)
41 #:use-module (srfi srfi-64))
43 ;; Test the (guix store) module.
46 (open-connection-for-tests))
49 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
54 (test-assert "open-connection with file:// URI"
55 (let ((store (open-connection (string-append "file://"
56 (%daemon-socket-uri)))))
57 (and (add-text-to-store store "foo" "bar")
59 (close-connection store)
62 (test-equal "connection handshake error"
64 (let ((port (%make-void-port "rw")))
65 (guard (c ((nix-connection-error? c)
66 (and (eq? port (nix-connection-error-file c))
67 (nix-connection-error-code c))))
68 (open-connection #f #:port port)
71 (test-equal "store-path-hash-part"
72 "283gqy39v3g9dxjy26rynl0zls82fmcg"
74 (string-append (%store-prefix)
75 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
77 (test-equal "store-path-hash-part #f"
80 (string-append (%store-prefix)
81 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
83 (test-equal "store-path-package-name"
85 (store-path-package-name
86 (string-append (%store-prefix)
87 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
89 (test-equal "store-path-package-name #f"
91 (store-path-package-name
92 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
94 (test-assert "direct-store-path?"
95 (and (direct-store-path?
96 (string-append (%store-prefix)
97 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
98 (not (direct-store-path?
101 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
102 (not (direct-store-path? (%store-prefix)))))
104 (test-skip (if %store 0 13))
106 (test-equal "add-data-to-store"
108 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
111 (test-assert "valid-path? live"
112 (let ((p (add-text-to-store %store "hello" "hello, world")))
113 (valid-path? %store p)))
115 (test-assert "valid-path? false"
116 (not (valid-path? %store
117 (string-append (%store-prefix) "/"
118 (make-string 32 #\e) "-foobar"))))
120 (test-assert "valid-path? error"
122 (guard (c ((nix-protocol-error? c) #t))
123 (valid-path? s "foo")
126 (test-assert "valid-path? recovery"
127 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
128 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
130 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
133 (let-syntax ((true-if-error (syntax-rules ()
135 (guard (c ((nix-protocol-error? c) #t))
137 (and (true-if-error (valid-path? s "foo"))
138 (true-if-error (valid-path? s "bar"))
139 (true-if-error (valid-path? s "baz"))
140 (true-if-error (valid-path? s "chbouib"))
141 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
143 (test-assert "hash-part->path"
144 (let ((p (add-text-to-store %store "hello" "hello, world")))
145 (equal? (hash-part->path %store (store-path-hash-part p))
148 (test-assert "dead-paths"
149 (let ((p (add-text-to-store %store "random-text" (random-text))))
150 (->bool (member p (dead-paths %store)))))
152 ;; FIXME: Find a test for `live-paths'.
154 ;; (test-assert "temporary root is in live-paths"
155 ;; (let* ((p1 (add-text-to-store %store "random-text"
156 ;; (random-text) '()))
157 ;; (b (add-text-to-store %store "link-builder"
158 ;; (format #f "echo ~a > $out" p1)
160 ;; (d1 (derivation %store "link"
161 ;; "/bin/sh" `("-e" ,b)
162 ;; #:inputs `((,b) (,p1))))
163 ;; (p2 (derivation->output-path d1)))
164 ;; (and (add-temp-root %store p2)
165 ;; (build-derivations %store (list d1))
166 ;; (valid-path? %store p1)
167 ;; (member (pk p2) (live-paths %store)))))
169 (test-assert "permanent root"
170 (let* ((p (with-store store
171 (let ((p (add-text-to-store store "random-text"
173 (add-permanent-root p)
174 (add-permanent-root p) ; should not throw
176 (and (member p (live-paths %store))
178 (remove-permanent-root p)
179 (->bool (member p (dead-paths %store)))))))
181 (test-assert "dead path can be explicitly collected"
182 (let ((p (add-text-to-store %store "random-text"
184 (let-values (((paths freed) (delete-paths %store (list p))))
185 (and (equal? paths (list p))
186 ;; XXX: On some file systems (notably Btrfs), freed
187 ;; may return 0. See <https://bugs.gnu.org/29363>.
189 (not (file-exists? p))))))
191 (test-assert "add-text-to-store vs. delete-paths"
192 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
193 ;; is no longer valid.
195 (let* ((text (random-text))
196 (path (add-text-to-store store "delete-me" text))
197 (deleted (delete-paths store (list path)))
198 (path2 (add-text-to-store store "delete-me" text)))
199 (and (string=? path path2)
200 (equal? deleted (list path))
201 (valid-path? store path)
202 (file-exists? path)))))
204 (test-assert "add-to-store vs. delete-paths"
207 (let* ((file (search-path %load-path "guix.scm"))
208 (path (add-to-store store "delete-me" #t "sha256" file))
209 (deleted (delete-paths store (list path)))
210 (path2 (add-to-store store "delete-me" #t "sha256" file)))
211 (and (string=? path path2)
212 (equal? deleted (list path))
213 (valid-path? store path)
214 (file-exists? path)))))
216 (test-equal "add-file-tree-to-store"
219 ("./bar" directory #t)
220 ("./foo" directory #t)
221 ("./foo/a" regular "file a")
222 ("./foo/b" symlink "a")
223 ("./foo/c" directory #t)
224 ("./foo/c/p" regular "file p")
225 ("./foo/c/q" directory #t)
226 ("./foo/c/q/x" regular
227 ,(string-append "#!" %shell "\nexit 42"))
228 ("./foo/c/q/y" symlink "..")
229 ("./foo/c/q/z" directory #t))
230 (let* ((tree `("file-tree" directory
232 ("a" regular (data "file a"))
235 ("p" regular (data ,(string->utf8 "file p")))
238 (data ,(string-append "#!" %shell "\nexit 42")))
242 (result (add-file-tree-to-store %store tree)))
243 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
244 (with-directory-excursion result
246 (let ((type (stat:type (lstat file))))
249 ((or 'regular 'executable)
250 (call-with-input-file file
252 ('symlink (readlink file))
254 (find-files "." #:directories? #t))))))
256 (test-equal "add-file-tree-to-store, flat"
258 (let* ((tree `("flat-file" regular (data "Hello, world!")))
259 (result (add-file-tree-to-store %store tree)))
260 (and (file-exists? result)
261 (call-with-input-file result get-string-all))))
263 (test-assert "references"
264 (let* ((t1 (add-text-to-store %store "random1"
266 (t2 (add-text-to-store %store "random2"
267 (random-text) (list t1))))
268 (and (equal? (list t1) (references %store t2))
269 (equal? (list t2) (referrers %store t1))
270 (null? (references %store t1))
271 (null? (referrers %store t2)))))
273 (test-assert "references/substitutes missing reference info"
275 (set-build-options s #:use-substitutes? #f)
276 (guard (c ((nix-protocol-error? c) #t))
277 (let* ((b (add-to-store s "bash" #t "sha256"
278 (search-bootstrap-binary "bash"
280 (d (derivation s "the-thing" b '("--help")
282 (references/substitutes s (list (derivation->output-path d) b))
285 (test-assert "references/substitutes with substitute info"
287 (set-build-options s #:use-substitutes? #t)
288 (let* ((t1 (add-text-to-store s "random1" (random-text)))
289 (t2 (add-text-to-store s "random2" (random-text)
291 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
292 (b (add-to-store s "bash" #t "sha256"
293 (search-bootstrap-binary "bash"
295 (d (derivation s "the-thing" b `("-e" ,t3)
296 #:inputs `((,b) (,t3) (,t2))
297 #:env-vars `(("t2" . ,t2))))
298 (o (derivation->output-path d)))
299 (with-derivation-narinfo d
300 (sha256 => (sha256 (string->utf8 t2)))
301 (references => (list t2))
303 (equal? (references/substitutes s (list o t3 t2 t1))
309 (test-equal "substitutable-path-info when substitutes are turned off"
312 (set-build-options s #:use-substitutes? #f)
313 (let* ((b (add-to-store s "bash" #t "sha256"
314 (search-bootstrap-binary "bash"
316 (d (derivation s "the-thing" b '("--version")
318 (o (derivation->output-path d)))
319 (with-derivation-narinfo d
320 (substitutable-path-info s (list o))))))
322 (test-equal "substitutable-paths when substitutes are turned off"
325 (set-build-options s #:use-substitutes? #f)
326 (let* ((b (add-to-store s "bash" #t "sha256"
327 (search-bootstrap-binary "bash"
329 (d (derivation s "the-thing" b '("--version")
331 (o (derivation->output-path d)))
332 (with-derivation-narinfo d
333 (substitutable-paths s (list o))))))
335 (test-assert "requisites"
336 (let* ((t1 (add-text-to-store %store "random1"
338 (t2 (add-text-to-store %store "random2"
339 (random-text) (list t1)))
340 (t3 (add-text-to-store %store "random3"
341 (random-text) (list t2)))
342 (t4 (add-text-to-store %store "random4"
343 (random-text) (list t1 t3))))
345 (and (= (length x) (length y))
348 (and (same? (requisites %store (list t1)) (list t1))
349 (same? (requisites %store (list t2)) (list t1 t2))
350 (same? (requisites %store (list t3)) (list t1 t2 t3))
351 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
352 (same? (requisites %store (list t1 t2 t3 t4))
353 (list t1 t2 t3 t4)))))
355 (test-assert "derivers"
356 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
357 (s (add-to-store %store "bash" #t "sha256"
358 (search-bootstrap-binary "bash"
360 (d (derivation %store "the-thing"
362 #:env-vars `(("foo" . ,(random-text)))
363 #:inputs `((,b) (,s))))
364 (o (derivation->output-path d)))
365 (and (build-derivations %store (list d))
366 (equal? (query-derivation-outputs %store (derivation-file-name d))
368 (equal? (valid-derivers %store o)
369 (list (derivation-file-name d))))))
371 (test-assert "topologically-sorted, one item"
372 (let* ((a (add-text-to-store %store "a" "a"))
373 (b (add-text-to-store %store "b" "b" (list a)))
374 (c (add-text-to-store %store "c" "c" (list b)))
375 (d (add-text-to-store %store "d" "d" (list c)))
376 (s (topologically-sorted %store (list d))))
377 (equal? s (list a b c d))))
379 (test-assert "topologically-sorted, several items"
380 (let* ((a (add-text-to-store %store "a" "a"))
381 (b (add-text-to-store %store "b" "b" (list a)))
382 (c (add-text-to-store %store "c" "c" (list b)))
383 (d (add-text-to-store %store "d" "d" (list c)))
384 (s1 (topologically-sorted %store (list d a c b)))
385 (s2 (topologically-sorted %store (list b d c a b d))))
386 (equal? s1 s2 (list a b c d))))
388 (test-assert "topologically-sorted, more difficult"
389 (let* ((a (add-text-to-store %store "a" "a"))
390 (b (add-text-to-store %store "b" "b" (list a)))
391 (c (add-text-to-store %store "c" "c" (list b)))
392 (d (add-text-to-store %store "d" "d" (list c)))
393 (w (add-text-to-store %store "w" "w"))
394 (x (add-text-to-store %store "x" "x" (list w)))
395 (y (add-text-to-store %store "y" "y" (list x d)))
396 (s1 (topologically-sorted %store (list y)))
397 (s2 (topologically-sorted %store (list c y)))
398 (s3 (topologically-sorted %store (cons y (references %store y)))))
399 ;; The order in which 'references' returns the references of Y is
400 ;; unspecified, so accommodate.
401 (let* ((x-then-d? (equal? (references %store y) (list x d))))
405 (list a b c d w x y)))
409 (list a b c d w x y)))
410 (lset= string=? s1 s3)))))
412 (test-assert "current-build-output-port, UTF-8"
413 ;; Are UTF-8 strings in the build log properly interpreted?
415 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
416 (call-with-output-string
418 (parameterize ((current-build-output-port port))
419 (let* ((s "Here’s a Greek letter: λ.")
420 (d (build-expression->derivation
421 %store "foo" `(display ,s)
423 (package-derivation s %bootstrap-guile (%current-system)))))
424 (guard (c ((nix-protocol-error? c) #t))
425 (build-derivations %store (list d))))))))
426 "Here’s a Greek letter: λ."))
428 (test-assert "current-build-output-port, UTF-8 + garbage"
429 ;; What about a mixture of UTF-8 + garbage?
431 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
432 (call-with-output-string
434 (parameterize ((current-build-output-port port))
435 (let ((d (build-expression->derivation
438 (use-modules (rnrs io ports))
439 (display "garbage: ")
440 (put-bytevector (current-output-port) #vu8(128))
441 (display "lambda: λ\n"))
443 (package-derivation %store %bootstrap-guile))))
444 (guard (c ((nix-protocol-error? c) #t))
445 (build-derivations %store (list d))))))))
447 (guile-2.2 "garbage: �lambda: λ")
448 (else "garbage: ?lambda: λ"))))
450 (test-assert "log-file, derivation"
451 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
452 (s (add-to-store %store "bash" #t "sha256"
453 (search-bootstrap-binary "bash"
455 (d (derivation %store "the-thing"
457 #:env-vars `(("foo" . ,(random-text)))
458 #:inputs `((,b) (,s)))))
459 (and (build-derivations %store (list d))
460 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
462 (test-assert "log-file, output file name"
463 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
464 (s (add-to-store %store "bash" #t "sha256"
465 (search-bootstrap-binary "bash"
467 (d (derivation %store "the-thing"
469 #:env-vars `(("foo" . ,(random-text)))
470 #:inputs `((,b) (,s))))
471 (o (derivation->output-path d)))
472 (and (build-derivations %store (list d))
473 (file-exists? (pk (log-file %store o)))
474 (string=? (log-file %store (derivation-file-name d))
475 (log-file %store o)))))
477 (test-assert "no substitutes"
479 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
480 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
481 (o (map derivation->output-path (list d1 d2))))
482 (set-build-options s #:use-substitutes? #f)
483 (and (not (has-substitutes? s (derivation-file-name d1)))
484 (not (has-substitutes? s (derivation-file-name d2)))
485 (null? (substitutable-paths s o))
486 (null? (substitutable-path-info s o))))))
488 (test-assert "build-things with output path"
490 (let* ((c (random-text)) ;contents of the output
491 (d (build-expression->derivation
493 `(call-with-output-file %output
497 (package-derivation s %bootstrap-guile (%current-system))))
498 (o (derivation->output-path d)))
499 (set-build-options s #:use-substitutes? #f)
501 ;; Pass 'build-things' the output file name, O. However, since there
502 ;; are no substitutes for O, it will just do nothing.
503 (build-things s (list o))
504 (not (valid-path? s o)))))
506 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
508 (test-assert "substitute query"
510 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
511 (o (derivation->output-path d)))
512 ;; Create fake substituter data, to be read by 'guix substitute'.
513 (with-derivation-narinfo d
514 ;; Remove entry from the local cache.
516 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
517 "/guix/substitute")))
519 ;; Make sure 'guix substitute' correctly communicates the above
521 (set-build-options s #:use-substitutes? #t
522 #:substitute-urls (%test-substitute-urls))
523 (and (has-substitutes? s o)
524 (equal? (list o) (substitutable-paths s (list o)))
525 (match (pk 'spi (substitutable-path-info s (list o)))
526 (((? substitutable? s))
527 (and (string=? (substitutable-deriver s)
528 (derivation-file-name d))
529 (null? (substitutable-references s))
530 (equal? (substitutable-nar-size s) 1234)))))))))
532 (test-assert "substitute query, alternating URLs"
533 (let* ((d (with-store s
534 (package-derivation s %bootstrap-guile (%current-system))))
535 (o (derivation->output-path d)))
536 (with-derivation-narinfo d
537 ;; Remove entry from the local cache.
539 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
540 "/guix/substitute")))
542 ;; Note: We reconnect to the daemon to force a new instance of 'guix
543 ;; substitute' to be used; otherwise the #:substitute-urls of
544 ;; 'set-build-options' would have no effect.
546 (and (with-store s ;the right substitute URL
547 (set-build-options s #:use-substitutes? #t
548 #:substitute-urls (%test-substitute-urls))
549 (has-substitutes? s o))
550 (with-store s ;the wrong one
551 (set-build-options s #:use-substitutes? #t
552 #:substitute-urls (list
553 "http://does-not-exist"))
554 (not (has-substitutes? s o)))
555 (with-store s ;the right one again
556 (set-build-options s #:use-substitutes? #t
557 #:substitute-urls (%test-substitute-urls))
558 (has-substitutes? s o))
559 (with-store s ;empty list of URLs
560 (set-build-options s #:use-substitutes? #t
561 #:substitute-urls '())
562 (not (has-substitutes? s o)))))))
564 (test-assert "substitute"
566 (let* ((c (random-text)) ; contents of the output
567 (d (build-expression->derivation
569 `(call-with-output-file %output
571 (exit 1) ; would actually fail
574 (package-derivation s %bootstrap-guile (%current-system))))
575 (o (derivation->output-path d)))
576 (with-derivation-substitute d c
577 (set-build-options s #:use-substitutes? #t
578 #:substitute-urls (%test-substitute-urls))
579 (and (has-substitutes? s o)
580 (build-derivations s (list d))
581 (equal? c (call-with-input-file o get-string-all)))))))
583 (test-assert "substitute + build-things with output path"
585 (let* ((c (random-text)) ;contents of the output
586 (d (build-expression->derivation
588 `(call-with-output-file %output
590 (exit 1) ;would actually fail
593 (package-derivation s %bootstrap-guile (%current-system))))
594 (o (derivation->output-path d)))
595 (with-derivation-substitute d c
596 (set-build-options s #:use-substitutes? #t
597 #:substitute-urls (%test-substitute-urls))
598 (and (has-substitutes? s o)
599 (build-things s (list o)) ;give the output path
601 (equal? c (call-with-input-file o get-string-all)))))))
603 (test-assert "substitute, corrupt output hash"
604 ;; Tweak the substituter into installing a substitute whose hash doesn't
605 ;; match the one announced in the narinfo. The daemon must notice this and
608 (let* ((c "hello, world") ; contents of the output
609 (d (build-expression->derivation
610 s "corrupt-substitute"
613 (package-derivation s %bootstrap-guile (%current-system))))
614 (o (derivation->output-path d)))
615 (with-derivation-substitute d c
616 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
618 ;; Make sure we use 'guix substitute'.
620 #:use-substitutes? #t
622 #:substitute-urls (%test-substitute-urls))
623 (and (has-substitutes? s o)
624 (guard (c ((nix-protocol-error? c)
625 ;; XXX: the daemon writes "hash mismatch in downloaded
626 ;; path", but the actual error returned to the client
627 ;; doesn't mention that.
629 (not (zero? (nix-protocol-error-status c)))))
630 (build-derivations s (list d))
633 (test-assert "substitute --fallback"
635 (let* ((t (random-text)) ; contents of the output
636 (d (build-expression->derivation
637 s "substitute-me-not"
638 `(call-with-output-file %output
642 (package-derivation s %bootstrap-guile (%current-system))))
643 (o (derivation->output-path d)))
644 ;; Create fake substituter data, to be read by 'guix substitute'.
645 (with-derivation-narinfo d
646 ;; Make sure we use 'guix substitute'.
647 (set-build-options s #:use-substitutes? #t
648 #:substitute-urls (%test-substitute-urls))
649 (and (has-substitutes? s o)
650 (guard (c ((nix-protocol-error? c)
651 ;; The substituter failed as expected. Now make
652 ;; sure that #:fallback? #t works correctly.
654 #:use-substitutes? #t
656 (%test-substitute-urls)
658 (and (build-derivations s (list d))
659 (equal? t (call-with-input-file o
662 (build-derivations s (list d))
665 (test-assert "export/import several paths"
666 (let* ((texts (unfold (cut >= <> 10)
667 (lambda _ (random-text))
670 (files (map (cut add-text-to-store %store "text" <>) texts))
671 (dump (call-with-bytevector-output-port
672 (cut export-paths %store files <>))))
673 (delete-paths %store files)
674 (and (every (negate file-exists?) files)
675 (let* ((source (open-bytevector-input-port dump))
676 (imported (import-paths %store source)))
677 (and (equal? imported files)
678 (every file-exists? files)
681 (call-with-input-file file
685 (test-assert "export/import paths, ensure topological order"
686 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
687 (file1 (add-text-to-store %store "foo" (random-text)
689 (file2 (add-text-to-store %store "bar" (random-text)
691 (files (list file1 file2))
692 (dump1 (call-with-bytevector-output-port
693 (cute export-paths %store (list file1 file2) <>)))
694 (dump2 (call-with-bytevector-output-port
695 (cute export-paths %store (list file2 file1) <>))))
696 (delete-paths %store files)
697 (and (every (negate file-exists?) files)
698 (bytevector=? dump1 dump2)
699 (let* ((source (open-bytevector-input-port dump1))
700 (imported (import-paths %store source)))
701 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
702 (and (equal? imported (list file1 file2))
703 (every file-exists? files)
704 (equal? (list file0) (references %store file1))
705 (equal? (list file1) (references %store file2)))))))
707 (test-assert "export/import incomplete"
708 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
709 (file1 (add-text-to-store %store "foo" (random-text)
711 (file2 (add-text-to-store %store "bar" (random-text)
713 (dump (call-with-bytevector-output-port
714 (cute export-paths %store (list file2) <>))))
715 (delete-paths %store (list file0 file1 file2))
716 (guard (c ((nix-protocol-error? c)
717 (and (not (zero? (nix-protocol-error-status c)))
718 (string-contains (nix-protocol-error-message c)
720 ;; Here we get an exception because DUMP does not include FILE0 and
721 ;; FILE1, which are dependencies of FILE2.
722 (import-paths %store (open-bytevector-input-port dump)))))
724 (test-assert "export/import recursive"
725 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
726 (file1 (add-text-to-store %store "foo" (random-text)
728 (file2 (add-text-to-store %store "bar" (random-text)
730 (dump (call-with-bytevector-output-port
731 (cute export-paths %store (list file2) <>
733 (delete-paths %store (list file0 file1 file2))
734 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
735 (and (equal? imported (list file0 file1 file2))
736 (every file-exists? (list file0 file1 file2))
737 (equal? (list file0) (references %store file1))
738 (equal? (list file1) (references %store file2))))))
740 (test-assert "write-file & export-path yield the same result"
741 ;; Here we compare 'write-file' and the daemon's own implementation.
742 ;; 'write-file' is the reference because we know it sorts file
743 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
744 ;; currently happen to be sorted as a side-effect of some unrelated
745 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
747 (run-with-store %store
748 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
749 (out1 -> (derivation->output-path drv1))
750 (data -> (unfold (cut >= <> 26)
752 (random-bytevector 128))
756 (use-modules (rnrs io ports) (srfi srfi-1))
762 (+ i (char->integer #\a)))))
764 (define (touch file data)
765 (call-with-output-file file
767 (put-bytevector port data))))
772 ;; The files must be different so they have
773 ;; different inode numbers, and the inode
774 ;; order must differ from the lexicographic
777 (append (drop letters 10)
781 (drv2 (gexp->derivation "bunch" build))
782 (out2 -> (derivation->output-path drv2))
783 (item-info -> (store-lift query-path-info)))
785 (built-derivations (list drv1 drv2))
787 (lambda (item result)
789 (let-values (((port get) (open-sha256-port)))
790 (write-file item port)
794 ;; 'query-path-info' returns a hash produced by using the
795 ;; daemon's C++ 'dump' function, which is the implementation
797 (>>= (item-info item)
801 (bytevector=? (path-info-hash info) ref-hash))))))
804 #:guile-for-build (%guile-for-build)))
806 (test-assert "import corrupt path"
807 (let* ((text (random-text))
808 (file (add-text-to-store %store "text" text))
809 (dump (call-with-bytevector-output-port
810 (cut export-paths %store (list file) <>))))
811 (delete-paths %store (list file))
813 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
814 ;; the file contents in DUMP, regardless of the store prefix.
816 (byte (bytevector-u8-ref dump index)))
817 (bytevector-u8-set! dump index (logxor #xff byte)))
819 (and (not (file-exists? file))
820 (guard (c ((nix-protocol-error? c)
822 (and (not (zero? (nix-protocol-error-status c)))
823 (string-contains (nix-protocol-error-message c)
825 (let* ((source (open-bytevector-input-port dump))
826 (imported (import-paths %store source)))
827 (pk 'corrupt-imported imported)
830 (test-assert "verify-store"
831 (let* ((text (random-text))
832 (file1 (add-text-to-store %store "foo" text))
833 (file2 (add-text-to-store %store "bar" (random-text)
835 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
838 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
840 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
841 ;; without actually creating the file. ;
842 (call-with-output-file file1
844 (display text port)))
845 (pk 'verify3 (verify-store %store)))))) ;OK again
847 (test-assert "verify-store + check-contents"
848 ;; XXX: This test is I/O intensive.
850 (let* ((text (random-text))
851 (drv (build-expression->derivation
853 `(let ((out (assoc-ref %outputs "out")))
854 (call-with-output-file out
856 (display ,text port)))
859 (package-derivation s %bootstrap-guile (%current-system))))
860 (file (derivation->output-path drv)))
861 (with-derivation-substitute drv text
862 (and (build-derivations s (list drv))
863 (verify-store s #:check-contents? #t) ;should be OK
866 (call-with-output-file file
868 (display "corrupt!" port)))
871 ;; Make sure the corruption is detected. We don't test repairing
872 ;; because only "trusted" users are allowed to do it, but we
873 ;; don't expose that notion of trusted users that nix-daemon
874 ;; supports because it seems dubious and redundant with what the
875 ;; OS provides (in Nix "trusted" users have additional
876 ;; privileges, such as overriding the set of substitute URLs, but
877 ;; we instead want to allow anyone to modify them, provided
878 ;; substitutes are signed by a root-approved key.)
879 (not (verify-store s #:check-contents? #t))
881 ;; Delete the corrupt item to leave the store in a clean state.
882 (delete-paths s (list file)))))))
884 (test-assert "build-things, check mode"
886 (call-with-temporary-output-file
887 (lambda (entropy entropy-port)
888 (write (random-text) entropy-port)
889 (force-output entropy-port)
890 (let* ((drv (build-expression->derivation
891 store "non-deterministic"
893 (use-modules (rnrs io ports))
894 (let ((out (assoc-ref %outputs "out")))
895 (call-with-output-file out
897 ;; Rely on the fact that tests do not use the
898 ;; chroot, and thus ENTROPY is readable.
899 (display (call-with-input-file ,entropy
904 (package-derivation store %bootstrap-guile (%current-system))))
905 (file (derivation->output-path drv)))
906 (and (build-things store (list (derivation-file-name drv)))
908 (write (random-text) entropy-port)
909 (force-output entropy-port)
910 (guard (c ((nix-protocol-error? c)
911 (pk 'determinism-exception c)
912 (and (not (zero? (nix-protocol-error-status c)))
913 (string-contains (nix-protocol-error-message c)
915 ;; This one will produce a different result. Since we're in
916 ;; 'check' mode, this must fail.
917 (build-things store (list (derivation-file-name drv))
921 (test-assert "build multiple times"
923 ;; Ask to build twice.
924 (set-build-options store #:rounds 2 #:use-substitutes? #f)
926 (call-with-temporary-output-file
927 (lambda (entropy entropy-port)
928 (write (random-text) entropy-port)
929 (force-output entropy-port)
930 (let* ((drv (build-expression->derivation
931 store "non-deterministic"
933 (use-modules (rnrs io ports))
934 (let ((out (assoc-ref %outputs "out")))
935 (call-with-output-file out
937 ;; Rely on the fact that tests do not use the
938 ;; chroot, and thus ENTROPY is accessible.
939 (display (call-with-input-file ,entropy
942 (call-with-output-file ,entropy
944 (write 'foobar port)))))
947 (package-derivation store %bootstrap-guile (%current-system))))
948 (file (derivation->output-path drv)))
949 (guard (c ((nix-protocol-error? c)
950 (pk 'multiple-build c)
951 (and (not (zero? (nix-protocol-error-status c)))
952 (string-contains (nix-protocol-error-message c)
954 ;; This one will produce a different result on the second run.
955 (current-build-output-port (current-error-port))
956 (build-things store (list (derivation-file-name drv)))
959 (test-equal "store-lower"
961 (let* ((add (store-lower text-file))
962 (file (add %store "foo" "Lowered.")))
963 (call-with-input-file file get-string-all)))
965 (test-equal "current-system"
967 (parameterize ((%current-system "frob"))
968 (run-with-store %store
970 (set-current-system "bar")
974 (test-assert "query-path-info"
975 (let* ((ref (add-text-to-store %store "ref" "foo"))
976 (item (add-text-to-store %store "item" "bar" (list ref)))
977 (info (query-path-info %store item)))
978 (and (equal? (path-info-references info) (list ref))
979 (equal? (path-info-hash info)
982 (call-with-output-string (cut write-file item <>))))))))
984 (test-assert "path-info-deriver"
985 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
986 (s (add-to-store %store "bash" #t "sha256"
987 (search-bootstrap-binary "bash"
989 (d (derivation %store "the-thing"
991 #:env-vars `(("foo" . ,(random-text)))
992 #:inputs `((,b) (,s))))
993 (o (derivation->output-path d)))
994 (and (build-derivations %store (list d))
995 (not (path-info-deriver (query-path-info %store b)))
996 (string=? (derivation-file-name d)
997 (path-info-deriver (query-path-info %store o))))))
999 (test-equal "build-cores"
1002 (let* ((build (add-text-to-store store "build.sh"
1003 "echo $NIX_BUILD_CORES > $out"))
1004 (bash (add-to-store store "bash" #t "sha256"
1005 (search-bootstrap-binary "bash"
1006 (%current-system))))
1007 (drv1 (derivation store "the-thing" bash
1009 #:inputs `((,bash) (,build))
1010 #:env-vars `(("x" . ,(random-text)))))
1011 (drv2 (derivation store "the-thing" bash
1013 #:inputs `((,bash) (,build))
1014 #:env-vars `(("x" . ,(random-text))))))
1015 (and (build-derivations store (list drv1))
1017 (set-build-options store #:build-cores 42)
1018 (build-derivations store (list drv2)))
1019 (list (call-with-input-file (derivation->output-path drv1)
1021 (call-with-input-file (derivation->output-path drv2)