1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 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 hash)
24 #:use-module (guix base32)
25 #:use-module (guix packages)
26 #:use-module (guix derivations)
27 #:use-module (guix serialization)
28 #:use-module (gnu packages)
29 #:use-module (gnu packages bootstrap)
30 #:use-module (ice-9 match)
31 #:use-module (rnrs bytevectors)
32 #:use-module (rnrs io ports)
33 #:use-module (web uri)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-64))
40 ;; Test the (guix store) module.
43 (open-connection-for-tests))
48 (test-equal "store-path-hash-part"
49 "283gqy39v3g9dxjy26rynl0zls82fmcg"
51 (string-append (%store-prefix)
52 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
54 (test-equal "store-path-hash-part #f"
57 (string-append (%store-prefix)
58 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
60 (test-equal "store-path-package-name"
62 (store-path-package-name
63 (string-append (%store-prefix)
64 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
66 (test-equal "store-path-package-name #f"
68 (store-path-package-name
69 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
71 (test-assert "direct-store-path?"
72 (and (direct-store-path?
73 (string-append (%store-prefix)
74 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
75 (not (direct-store-path?
78 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
79 (not (direct-store-path? (%store-prefix)))))
81 (test-skip (if %store 0 13))
83 (test-assert "valid-path? live"
84 (let ((p (add-text-to-store %store "hello" "hello, world")))
85 (valid-path? %store p)))
87 (test-assert "valid-path? false"
88 (not (valid-path? %store
89 (string-append (%store-prefix) "/"
90 (make-string 32 #\e) "-foobar"))))
92 (test-assert "valid-path? error"
94 (guard (c ((nix-protocol-error? c) #t))
98 (test-assert "valid-path? recovery"
99 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
100 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
102 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
105 (let-syntax ((true-if-error (syntax-rules ()
107 (guard (c ((nix-protocol-error? c) #t))
109 (and (true-if-error (valid-path? s "foo"))
110 (true-if-error (valid-path? s "bar"))
111 (true-if-error (valid-path? s "baz"))
112 (true-if-error (valid-path? s "chbouib"))
113 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
115 (test-assert "hash-part->path"
116 (let ((p (add-text-to-store %store "hello" "hello, world")))
117 (equal? (hash-part->path %store (store-path-hash-part p))
120 (test-assert "dead-paths"
121 (let ((p (add-text-to-store %store "random-text" (random-text))))
122 (member p (dead-paths %store))))
124 ;; FIXME: Find a test for `live-paths'.
126 ;; (test-assert "temporary root is in live-paths"
127 ;; (let* ((p1 (add-text-to-store %store "random-text"
128 ;; (random-text) '()))
129 ;; (b (add-text-to-store %store "link-builder"
130 ;; (format #f "echo ~a > $out" p1)
132 ;; (d1 (derivation %store "link"
133 ;; "/bin/sh" `("-e" ,b)
134 ;; #:inputs `((,b) (,p1))))
135 ;; (p2 (derivation->output-path d1)))
136 ;; (and (add-temp-root %store p2)
137 ;; (build-derivations %store (list d1))
138 ;; (valid-path? %store p1)
139 ;; (member (pk p2) (live-paths %store)))))
141 (test-assert "permanent root"
142 (let* ((p (with-store store
143 (let ((p (add-text-to-store store "random-text"
145 (add-permanent-root p)
146 (add-permanent-root p) ; should not throw
148 (and (member p (live-paths %store))
150 (remove-permanent-root p)
151 (->bool (member p (dead-paths %store)))))))
153 (test-assert "dead path can be explicitly collected"
154 (let ((p (add-text-to-store %store "random-text"
156 (let-values (((paths freed) (delete-paths %store (list p))))
157 (and (equal? paths (list p))
159 (not (file-exists? p))))))
161 (test-assert "add-text-to-store vs. delete-paths"
162 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
163 ;; is no longer valid.
165 (let* ((text (random-text))
166 (path (add-text-to-store store "delete-me" text))
167 (deleted (delete-paths store (list path)))
168 (path2 (add-text-to-store store "delete-me" text)))
169 (and (string=? path path2)
170 (equal? deleted (list path))
171 (valid-path? store path)
172 (file-exists? path)))))
174 (test-assert "add-to-store vs. delete-paths"
177 (let* ((file (search-path %load-path "guix.scm"))
178 (path (add-to-store store "delete-me" #t "sha256" file))
179 (deleted (delete-paths store (list path)))
180 (path2 (add-to-store store "delete-me" #t "sha256" file)))
181 (and (string=? path path2)
182 (equal? deleted (list path))
183 (valid-path? store path)
184 (file-exists? path)))))
186 (test-assert "references"
187 (let* ((t1 (add-text-to-store %store "random1"
189 (t2 (add-text-to-store %store "random2"
190 (random-text) (list t1))))
191 (and (equal? (list t1) (references %store t2))
192 (equal? (list t2) (referrers %store t1))
193 (null? (references %store t1))
194 (null? (referrers %store t2)))))
196 (test-assert "requisites"
197 (let* ((t1 (add-text-to-store %store "random1"
199 (t2 (add-text-to-store %store "random2"
200 (random-text) (list t1)))
201 (t3 (add-text-to-store %store "random3"
202 (random-text) (list t2)))
203 (t4 (add-text-to-store %store "random4"
204 (random-text) (list t1 t3))))
206 (and (= (length x) (length y))
209 (and (same? (requisites %store t1) (list t1))
210 (same? (requisites %store t2) (list t1 t2))
211 (same? (requisites %store t3) (list t1 t2 t3))
212 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
214 (test-assert "derivers"
215 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
216 (s (add-to-store %store "bash" #t "sha256"
217 (search-bootstrap-binary "bash"
219 (d (derivation %store "the-thing"
221 #:env-vars `(("foo" . ,(random-text)))
222 #:inputs `((,b) (,s))))
223 (o (derivation->output-path d)))
224 (and (build-derivations %store (list d))
225 (equal? (query-derivation-outputs %store (derivation-file-name d))
227 (equal? (valid-derivers %store o)
228 (list (derivation-file-name d))))))
230 (test-assert "topologically-sorted, one item"
231 (let* ((a (add-text-to-store %store "a" "a"))
232 (b (add-text-to-store %store "b" "b" (list a)))
233 (c (add-text-to-store %store "c" "c" (list b)))
234 (d (add-text-to-store %store "d" "d" (list c)))
235 (s (topologically-sorted %store (list d))))
236 (equal? s (list a b c d))))
238 (test-assert "topologically-sorted, several items"
239 (let* ((a (add-text-to-store %store "a" "a"))
240 (b (add-text-to-store %store "b" "b" (list a)))
241 (c (add-text-to-store %store "c" "c" (list b)))
242 (d (add-text-to-store %store "d" "d" (list c)))
243 (s1 (topologically-sorted %store (list d a c b)))
244 (s2 (topologically-sorted %store (list b d c a b d))))
245 (equal? s1 s2 (list a b c d))))
247 (test-assert "topologically-sorted, more difficult"
248 (let* ((a (add-text-to-store %store "a" "a"))
249 (b (add-text-to-store %store "b" "b" (list a)))
250 (c (add-text-to-store %store "c" "c" (list b)))
251 (d (add-text-to-store %store "d" "d" (list c)))
252 (w (add-text-to-store %store "w" "w"))
253 (x (add-text-to-store %store "x" "x" (list w)))
254 (y (add-text-to-store %store "y" "y" (list x d)))
255 (s1 (topologically-sorted %store (list y)))
256 (s2 (topologically-sorted %store (list c y)))
257 (s3 (topologically-sorted %store (cons y (references %store y)))))
258 ;; The order in which 'references' returns the references of Y is
259 ;; unspecified, so accommodate.
260 (let* ((x-then-d? (equal? (references %store y) (list x d))))
264 (list a b c d w x y)))
268 (list a b c d w x y)))
269 (lset= string=? s1 s3)))))
271 (test-assert "log-file, derivation"
272 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
273 (s (add-to-store %store "bash" #t "sha256"
274 (search-bootstrap-binary "bash"
276 (d (derivation %store "the-thing"
278 #:env-vars `(("foo" . ,(random-text)))
279 #:inputs `((,b) (,s)))))
280 (and (build-derivations %store (list d))
281 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
283 (test-assert "log-file, output file name"
284 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
285 (s (add-to-store %store "bash" #t "sha256"
286 (search-bootstrap-binary "bash"
288 (d (derivation %store "the-thing"
290 #:env-vars `(("foo" . ,(random-text)))
291 #:inputs `((,b) (,s))))
292 (o (derivation->output-path d)))
293 (and (build-derivations %store (list d))
294 (file-exists? (pk (log-file %store o)))
295 (string=? (log-file %store (derivation-file-name d))
296 (log-file %store o)))))
298 (test-assert "no substitutes"
299 (let* ((s (open-connection))
300 (d1 (package-derivation s %bootstrap-guile (%current-system)))
301 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
302 (o (map derivation->output-path (list d1 d2))))
303 (set-build-options s #:use-substitutes? #f)
304 (and (not (has-substitutes? s (derivation-file-name d1)))
305 (not (has-substitutes? s (derivation-file-name d2)))
306 (null? (substitutable-paths s o))
307 (null? (substitutable-path-info s o)))))
309 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
311 (test-assert "substitute query"
312 (let* ((s (open-connection))
313 (d (package-derivation s %bootstrap-guile (%current-system)))
314 (o (derivation->output-path d))
315 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
316 (compose uri-path string->uri))))
317 ;; Create fake substituter data, to be read by `substitute-binary'.
318 (call-with-output-file (string-append dir "/nix-cache-info")
320 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
322 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
325 (format p "StorePath: ~a
333 (string-append dir "/example.nar") ; URL
334 (%current-system) ; System
336 (derivation-file-name d))))) ; Deriver
338 ;; Remove entry from the local cache.
340 (delete-file (string-append (getenv "XDG_CACHE_HOME")
341 "/guix/substitute-binary/"
342 (store-path-hash-part o))))
344 ;; Make sure `substitute-binary' correctly communicates the above data.
345 (set-build-options s #:use-substitutes? #t)
346 (and (has-substitutes? s o)
347 (equal? (list o) (substitutable-paths s (list o)))
348 (match (pk 'spi (substitutable-path-info s (list o)))
349 (((? substitutable? s))
350 (and (string=? (substitutable-deriver s) (derivation-file-name d))
351 (null? (substitutable-references s))
352 (equal? (substitutable-nar-size s) 1234)))))))
354 (test-assert "substitute"
355 (let* ((s (open-connection))
356 (c (random-text)) ; contents of the output
357 (d (build-expression->derivation
359 `(call-with-output-file %output
361 (exit 1) ; would actually fail
364 (package-derivation s %bootstrap-guile (%current-system))))
365 (o (derivation->output-path d))
366 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
367 (compose uri-path string->uri))))
368 ;; Create fake substituter data, to be read by `substitute-binary'.
369 (call-with-output-file (string-append dir "/nix-cache-info")
371 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
373 (call-with-output-file (string-append dir "/example.out")
376 (call-with-output-file (string-append dir "/example.nar")
378 (write-file (string-append dir "/example.out") p)))
379 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
382 (format p "StorePath: ~a
391 "example.nar" ; relative URL
392 (call-with-input-file (string-append dir "/example.nar")
393 (compose bytevector->nix-base32-string sha256
395 (%current-system) ; System
397 (derivation-file-name d))))) ; Deriver
399 ;; Make sure we use `substitute-binary'.
400 (set-build-options s #:use-substitutes? #t)
401 (and (has-substitutes? s o)
402 (build-derivations s (list d))
403 (equal? c (call-with-input-file o get-string-all)))))
405 (test-assert "substitute, corrupt output hash"
406 ;; Tweak the substituter into installing a substitute whose hash doesn't
407 ;; match the one announced in the narinfo. The daemon must notice this and
409 (let* ((s (open-connection))
410 (c "hello, world") ; contents of the output
411 (d (build-expression->derivation
412 s "corrupt-substitute"
415 (package-derivation s %bootstrap-guile (%current-system))))
416 (o (derivation->output-path d))
417 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
418 (compose uri-path string->uri))))
419 ;; Create fake substituter data, to be read by `substitute-binary'.
420 (call-with-output-file (string-append dir "/nix-cache-info")
422 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
424 (call-with-output-file (string-append dir "/example.out")
426 (display "The contents here do not match C." p)))
427 (call-with-output-file (string-append dir "/example.nar")
429 (write-file (string-append dir "/example.out") p)))
430 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
433 (format p "StorePath: ~a
442 "example.nar" ; relative URL
443 (bytevector->nix-base32-string
444 (sha256 (string->utf8 c)))
445 (%current-system) ; System
447 (derivation-file-name d))))) ; Deriver
449 ;; Make sure we use `substitute-binary'.
451 #:use-substitutes? #t
453 (and (has-substitutes? s o)
454 (guard (c ((nix-protocol-error? c)
455 ;; XXX: the daemon writes "hash mismatch in downloaded
456 ;; path", but the actual error returned to the client
457 ;; doesn't mention that.
459 (not (zero? (nix-protocol-error-status c)))))
460 (build-derivations s (list d))
463 (test-assert "substitute --fallback"
464 (let* ((s (open-connection))
465 (t (random-text)) ; contents of the output
466 (d (build-expression->derivation
467 s "substitute-me-not"
468 `(call-with-output-file %output
472 (package-derivation s %bootstrap-guile (%current-system))))
473 (o (derivation->output-path d))
474 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
475 (compose uri-path string->uri))))
476 ;; Create fake substituter data, to be read by `substitute-binary'.
477 (call-with-output-file (string-append dir "/nix-cache-info")
479 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
481 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
484 (format p "StorePath: ~a
488 NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
493 "does-not-exist.nar" ; relative URL
494 (%current-system) ; System
496 (derivation-file-name d))))) ; Deriver
498 ;; Make sure we use `substitute-binary'.
499 (set-build-options s #:use-substitutes? #t)
500 (and (has-substitutes? s o)
501 (guard (c ((nix-protocol-error? c)
502 ;; The substituter failed as expected. Now make sure that
503 ;; #:fallback? #t works correctly.
505 #:use-substitutes? #t
507 (and (build-derivations s (list d))
508 (equal? t (call-with-input-file o get-string-all)))))
510 (build-derivations s (list d))
513 (test-assert "export/import several paths"
514 (let* ((texts (unfold (cut >= <> 10)
515 (lambda _ (random-text))
518 (files (map (cut add-text-to-store %store "text" <>) texts))
519 (dump (call-with-bytevector-output-port
520 (cut export-paths %store files <>))))
521 (delete-paths %store files)
522 (and (every (negate file-exists?) files)
523 (let* ((source (open-bytevector-input-port dump))
524 (imported (import-paths %store source)))
525 (and (equal? imported files)
526 (every file-exists? files)
529 (call-with-input-file file
533 (test-assert "export/import paths, ensure topological order"
534 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
535 (file1 (add-text-to-store %store "foo" (random-text)
537 (file2 (add-text-to-store %store "bar" (random-text)
539 (files (list file1 file2))
540 (dump1 (call-with-bytevector-output-port
541 (cute export-paths %store (list file1 file2) <>)))
542 (dump2 (call-with-bytevector-output-port
543 (cute export-paths %store (list file2 file1) <>))))
544 (delete-paths %store files)
545 (and (every (negate file-exists?) files)
546 (bytevector=? dump1 dump2)
547 (let* ((source (open-bytevector-input-port dump1))
548 (imported (import-paths %store source)))
549 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
550 (and (equal? imported (list file1 file2))
551 (every file-exists? files)
552 (equal? (list file0) (references %store file1))
553 (equal? (list file1) (references %store file2)))))))
555 (test-assert "export/import incomplete"
556 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
557 (file1 (add-text-to-store %store "foo" (random-text)
559 (file2 (add-text-to-store %store "bar" (random-text)
561 (dump (call-with-bytevector-output-port
562 (cute export-paths %store (list file2) <>))))
563 (delete-paths %store (list file0 file1 file2))
564 (guard (c ((nix-protocol-error? c)
565 (and (not (zero? (nix-protocol-error-status c)))
566 (string-contains (nix-protocol-error-message c)
568 ;; Here we get an exception because DUMP does not include FILE0 and
569 ;; FILE1, which are dependencies of FILE2.
570 (import-paths %store (open-bytevector-input-port dump)))))
572 (test-assert "export/import recursive"
573 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
574 (file1 (add-text-to-store %store "foo" (random-text)
576 (file2 (add-text-to-store %store "bar" (random-text)
578 (dump (call-with-bytevector-output-port
579 (cute export-paths %store (list file2) <>
581 (delete-paths %store (list file0 file1 file2))
582 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
583 (and (equal? imported (list file0 file1 file2))
584 (every file-exists? (list file0 file1 file2))
585 (equal? (list file0) (references %store file1))
586 (equal? (list file1) (references %store file2))))))
588 (test-assert "import corrupt path"
589 (let* ((text (random-text))
590 (file (add-text-to-store %store "text" text))
591 (dump (call-with-bytevector-output-port
592 (cut export-paths %store (list file) <>))))
593 (delete-paths %store (list file))
595 ;; Flip a bit in the stream's payload.
596 (let* ((index (quotient (bytevector-length dump) 4))
597 (byte (bytevector-u8-ref dump index)))
598 (bytevector-u8-set! dump index (logxor #xff byte)))
600 (and (not (file-exists? file))
601 (guard (c ((nix-protocol-error? c)
603 (and (not (zero? (nix-protocol-error-status c)))
604 (string-contains (nix-protocol-error-message c)
606 (let* ((source (open-bytevector-input-port dump))
607 (imported (import-paths %store source)))
608 (pk 'corrupt-imported imported)
611 (test-assert "register-path"
612 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
614 (when (valid-path? %store file)
615 (delete-paths %store (list file)))
616 (false-if-exception (delete-file file))
618 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
619 (drv (string-append file ".drv")))
620 (call-with-output-file file
621 (cut display "This is a fake store item.\n" <>))
623 #:references (list ref)
626 (and (valid-path? %store file)
627 (equal? (references %store file) (list ref))
628 (null? (valid-derivers %store file))
629 (null? (referrers %store file))))))
631 (test-equal "store-lower"
633 (let* ((add (store-lower text-file))
634 (file (add %store "foo" "Lowered.")))
635 (call-with-input-file file get-string-all)))
640 (exit (= (test-runner-fail-count (test-runner-current)) 0))