1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 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/>.
20 (define-module (test-store)
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 nar)
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 (false-if-exception (open-connection)))
46 ;; Make sure we build everything by ourselves.
47 (set-build-options %store #:use-substitutes? #f))
50 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
53 (number->string (random (expt 2 256) %seed) 16))
58 (test-equal "store-path-hash-part"
59 "283gqy39v3g9dxjy26rynl0zls82fmcg"
61 (string-append (%store-prefix)
62 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
64 (test-equal "store-path-hash-part #f"
67 (string-append (%store-prefix)
68 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
70 (test-equal "store-path-package-name"
72 (store-path-package-name
73 (string-append (%store-prefix)
74 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
76 (test-equal "store-path-package-name #f"
78 (store-path-package-name
79 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
81 (test-assert "direct-store-path?"
82 (and (direct-store-path?
83 (string-append (%store-prefix)
84 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
85 (not (direct-store-path?
88 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
89 (not (direct-store-path? (%store-prefix)))))
91 (test-skip (if %store 0 13))
93 (test-assert "valid-path? live"
94 (let ((p (add-text-to-store %store "hello" "hello, world")))
95 (valid-path? %store p)))
97 (test-assert "valid-path? false"
98 (not (valid-path? %store
99 (string-append (%store-prefix) "/"
100 (make-string 32 #\e) "-foobar"))))
102 (test-assert "valid-path? error"
104 (guard (c ((nix-protocol-error? c) #t))
105 (valid-path? s "foo")
108 (test-assert "valid-path? recovery"
109 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
110 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
112 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
115 (let-syntax ((true-if-error (syntax-rules ()
117 (guard (c ((nix-protocol-error? c) #t))
119 (and (true-if-error (valid-path? s "foo"))
120 (true-if-error (valid-path? s "bar"))
121 (true-if-error (valid-path? s "baz"))
122 (true-if-error (valid-path? s "chbouib"))
123 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
125 (test-assert "hash-part->path"
126 (let ((p (add-text-to-store %store "hello" "hello, world")))
127 (equal? (hash-part->path %store (store-path-hash-part p))
130 (test-assert "dead-paths"
131 (let ((p (add-text-to-store %store "random-text" (random-text))))
132 (member p (dead-paths %store))))
134 ;; FIXME: Find a test for `live-paths'.
136 ;; (test-assert "temporary root is in live-paths"
137 ;; (let* ((p1 (add-text-to-store %store "random-text"
138 ;; (random-text) '()))
139 ;; (b (add-text-to-store %store "link-builder"
140 ;; (format #f "echo ~a > $out" p1)
142 ;; (d1 (derivation %store "link"
143 ;; "/bin/sh" `("-e" ,b)
144 ;; #:inputs `((,b) (,p1))))
145 ;; (p2 (derivation->output-path d1)))
146 ;; (and (add-temp-root %store p2)
147 ;; (build-derivations %store (list d1))
148 ;; (valid-path? %store p1)
149 ;; (member (pk p2) (live-paths %store)))))
151 (test-assert "permanent root"
152 (let* ((p (with-store store
153 (let ((p (add-text-to-store store "random-text"
155 (add-permanent-root p)
156 (add-permanent-root p) ; should not throw
158 (and (member p (live-paths %store))
160 (remove-permanent-root p)
161 (->bool (member p (dead-paths %store)))))))
163 (test-assert "dead path can be explicitly collected"
164 (let ((p (add-text-to-store %store "random-text"
166 (let-values (((paths freed) (delete-paths %store (list p))))
167 (and (equal? paths (list p))
169 (not (file-exists? p))))))
171 (test-assert "references"
172 (let* ((t1 (add-text-to-store %store "random1"
174 (t2 (add-text-to-store %store "random2"
175 (random-text) (list t1))))
176 (and (equal? (list t1) (references %store t2))
177 (equal? (list t2) (referrers %store t1))
178 (null? (references %store t1))
179 (null? (referrers %store t2)))))
181 (test-assert "requisites"
182 (let* ((t1 (add-text-to-store %store "random1"
184 (t2 (add-text-to-store %store "random2"
185 (random-text) (list t1)))
186 (t3 (add-text-to-store %store "random3"
187 (random-text) (list t2)))
188 (t4 (add-text-to-store %store "random4"
189 (random-text) (list t1 t3))))
191 (and (= (length x) (length y))
194 (and (same? (requisites %store t1) (list t1))
195 (same? (requisites %store t2) (list t1 t2))
196 (same? (requisites %store t3) (list t1 t2 t3))
197 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
199 (test-assert "derivers"
200 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
201 (s (add-to-store %store "bash" #t "sha256"
202 (search-bootstrap-binary "bash"
204 (d (derivation %store "the-thing"
206 #:env-vars `(("foo" . ,(random-text)))
207 #:inputs `((,b) (,s))))
208 (o (derivation->output-path d)))
209 (and (build-derivations %store (list d))
210 (equal? (query-derivation-outputs %store (derivation-file-name d))
212 (equal? (valid-derivers %store o)
213 (list (derivation-file-name d))))))
215 (test-assert "topologically-sorted, one item"
216 (let* ((a (add-text-to-store %store "a" "a"))
217 (b (add-text-to-store %store "b" "b" (list a)))
218 (c (add-text-to-store %store "c" "c" (list b)))
219 (d (add-text-to-store %store "d" "d" (list c)))
220 (s (topologically-sorted %store (list d))))
221 (equal? s (list a b c d))))
223 (test-assert "topologically-sorted, several items"
224 (let* ((a (add-text-to-store %store "a" "a"))
225 (b (add-text-to-store %store "b" "b" (list a)))
226 (c (add-text-to-store %store "c" "c" (list b)))
227 (d (add-text-to-store %store "d" "d" (list c)))
228 (s1 (topologically-sorted %store (list d a c b)))
229 (s2 (topologically-sorted %store (list b d c a b d))))
230 (equal? s1 s2 (list a b c d))))
232 (test-assert "topologically-sorted, more difficult"
233 (let* ((a (add-text-to-store %store "a" "a"))
234 (b (add-text-to-store %store "b" "b" (list a)))
235 (c (add-text-to-store %store "c" "c" (list b)))
236 (d (add-text-to-store %store "d" "d" (list c)))
237 (w (add-text-to-store %store "w" "w"))
238 (x (add-text-to-store %store "x" "x" (list w)))
239 (y (add-text-to-store %store "y" "y" (list x d)))
240 (s1 (topologically-sorted %store (list y)))
241 (s2 (topologically-sorted %store (list c y)))
242 (s3 (topologically-sorted %store (cons y (references %store y)))))
243 ;; The order in which 'references' returns the references of Y is
244 ;; unspecified, so accommodate.
245 (let* ((x-then-d? (equal? (references %store y) (list x d))))
249 (list a b c d w x y)))
253 (list a b c d w x y)))
254 (lset= string=? s1 s3)))))
256 (test-assert "log-file, derivation"
257 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
258 (s (add-to-store %store "bash" #t "sha256"
259 (search-bootstrap-binary "bash"
261 (d (derivation %store "the-thing"
263 #:env-vars `(("foo" . ,(random-text)))
264 #:inputs `((,b) (,s)))))
265 (and (build-derivations %store (list d))
266 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
268 (test-assert "log-file, output file name"
269 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
270 (s (add-to-store %store "bash" #t "sha256"
271 (search-bootstrap-binary "bash"
273 (d (derivation %store "the-thing"
275 #:env-vars `(("foo" . ,(random-text)))
276 #:inputs `((,b) (,s))))
277 (o (derivation->output-path d)))
278 (and (build-derivations %store (list d))
279 (file-exists? (pk (log-file %store o)))
280 (string=? (log-file %store (derivation-file-name d))
281 (log-file %store o)))))
283 (test-assert "no substitutes"
284 (let* ((s (open-connection))
285 (d1 (package-derivation s %bootstrap-guile (%current-system)))
286 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
287 (o (map derivation->output-path (list d1 d2))))
288 (set-build-options s #:use-substitutes? #f)
289 (and (not (has-substitutes? s (derivation-file-name d1)))
290 (not (has-substitutes? s (derivation-file-name d2)))
291 (null? (substitutable-paths s o))
292 (null? (substitutable-path-info s o)))))
294 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
296 (test-assert "substitute query"
297 (let* ((s (open-connection))
298 (d (package-derivation s %bootstrap-guile (%current-system)))
299 (o (derivation->output-path d))
300 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
301 (compose uri-path string->uri))))
302 ;; Create fake substituter data, to be read by `substitute-binary'.
303 (call-with-output-file (string-append dir "/nix-cache-info")
305 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
307 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
310 (format p "StorePath: ~a
318 (string-append dir "/example.nar") ; URL
319 (%current-system) ; System
321 (derivation-file-name d))))) ; Deriver
323 ;; Remove entry from the local cache.
325 (delete-file (string-append (getenv "XDG_CACHE_HOME")
326 "/guix/substitute-binary/"
327 (store-path-hash-part o))))
329 ;; Make sure `substitute-binary' correctly communicates the above data.
330 (set-build-options s #:use-substitutes? #t)
331 (and (has-substitutes? s o)
332 (equal? (list o) (substitutable-paths s (list o)))
333 (match (pk 'spi (substitutable-path-info s (list o)))
334 (((? substitutable? s))
335 (and (string=? (substitutable-deriver s) (derivation-file-name d))
336 (null? (substitutable-references s))
337 (equal? (substitutable-nar-size s) 1234)))))))
339 (test-assert "substitute"
340 (let* ((s (open-connection))
341 (c (random-text)) ; contents of the output
342 (d (build-expression->derivation
344 `(call-with-output-file %output
346 (exit 1) ; would actually fail
349 (package-derivation s %bootstrap-guile (%current-system))))
350 (o (derivation->output-path d))
351 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
352 (compose uri-path string->uri))))
353 ;; Create fake substituter data, to be read by `substitute-binary'.
354 (call-with-output-file (string-append dir "/nix-cache-info")
356 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
358 (call-with-output-file (string-append dir "/example.out")
361 (call-with-output-file (string-append dir "/example.nar")
363 (write-file (string-append dir "/example.out") p)))
364 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
367 (format p "StorePath: ~a
376 "example.nar" ; relative URL
377 (call-with-input-file (string-append dir "/example.nar")
378 (compose bytevector->nix-base32-string sha256
380 (%current-system) ; System
382 (derivation-file-name d))))) ; Deriver
384 ;; Make sure we use `substitute-binary'.
385 (set-build-options s #:use-substitutes? #t)
386 (and (has-substitutes? s o)
387 (build-derivations s (list d))
388 (equal? c (call-with-input-file o get-string-all)))))
390 (test-assert "substitute, corrupt output hash"
391 ;; Tweak the substituter into installing a substitute whose hash doesn't
392 ;; match the one announced in the narinfo. The daemon must notice this and
394 (let* ((s (open-connection))
395 (c "hello, world") ; contents of the output
396 (d (build-expression->derivation
397 s "corrupt-substitute"
400 (package-derivation s %bootstrap-guile (%current-system))))
401 (o (derivation->output-path d))
402 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
403 (compose uri-path string->uri))))
404 ;; Create fake substituter data, to be read by `substitute-binary'.
405 (call-with-output-file (string-append dir "/nix-cache-info")
407 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
409 (call-with-output-file (string-append dir "/example.out")
411 (display "The contents here do not match C." p)))
412 (call-with-output-file (string-append dir "/example.nar")
414 (write-file (string-append dir "/example.out") p)))
415 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
418 (format p "StorePath: ~a
427 "example.nar" ; relative URL
428 (bytevector->nix-base32-string
429 (sha256 (string->utf8 c)))
430 (%current-system) ; System
432 (derivation-file-name d))))) ; Deriver
434 ;; Make sure we use `substitute-binary'.
436 #:use-substitutes? #t
438 (and (has-substitutes? s o)
439 (guard (c ((nix-protocol-error? c)
440 ;; XXX: the daemon writes "hash mismatch in downloaded
441 ;; path", but the actual error returned to the client
442 ;; doesn't mention that.
444 (not (zero? (nix-protocol-error-status c)))))
445 (build-derivations s (list d))
448 (test-assert "substitute --fallback"
449 (let* ((s (open-connection))
450 (t (random-text)) ; contents of the output
451 (d (build-expression->derivation
452 s "substitute-me-not"
453 `(call-with-output-file %output
457 (package-derivation s %bootstrap-guile (%current-system))))
458 (o (derivation->output-path d))
459 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
460 (compose uri-path string->uri))))
461 ;; Create fake substituter data, to be read by `substitute-binary'.
462 (call-with-output-file (string-append dir "/nix-cache-info")
464 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
466 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
469 (format p "StorePath: ~a
473 NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
478 "does-not-exist.nar" ; relative URL
479 (%current-system) ; System
481 (derivation-file-name d))))) ; Deriver
483 ;; Make sure we use `substitute-binary'.
484 (set-build-options s #:use-substitutes? #t)
485 (and (has-substitutes? s o)
486 (guard (c ((nix-protocol-error? c)
487 ;; The substituter failed as expected. Now make sure that
488 ;; #:fallback? #t works correctly.
490 #:use-substitutes? #t
492 (and (build-derivations s (list d))
493 (equal? t (call-with-input-file o get-string-all)))))
495 (build-derivations s (list d))
498 (test-assert "export/import several paths"
499 (let* ((texts (unfold (cut >= <> 10)
500 (lambda _ (random-text))
503 (files (map (cut add-text-to-store %store "text" <>) texts))
504 (dump (call-with-bytevector-output-port
505 (cut export-paths %store files <>))))
506 (delete-paths %store files)
507 (and (every (negate file-exists?) files)
508 (let* ((source (open-bytevector-input-port dump))
509 (imported (import-paths %store source)))
510 (and (equal? imported files)
511 (every file-exists? files)
514 (call-with-input-file file
518 (test-assert "export/import paths, ensure topological order"
519 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
520 (file1 (add-text-to-store %store "foo" (random-text)
522 (file2 (add-text-to-store %store "bar" (random-text)
524 (files (list file1 file2))
525 (dump1 (call-with-bytevector-output-port
526 (cute export-paths %store (list file1 file2) <>)))
527 (dump2 (call-with-bytevector-output-port
528 (cute export-paths %store (list file2 file1) <>))))
529 (delete-paths %store files)
530 (and (every (negate file-exists?) files)
531 (bytevector=? dump1 dump2)
532 (let* ((source (open-bytevector-input-port dump1))
533 (imported (import-paths %store source)))
534 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
535 (and (equal? imported (list file1 file2))
536 (every file-exists? files)
537 (equal? (list file0) (references %store file1))
538 (equal? (list file1) (references %store file2)))))))
540 (test-assert "import corrupt path"
541 (let* ((text (random-text))
542 (file (add-text-to-store %store "text" text))
543 (dump (call-with-bytevector-output-port
544 (cut export-paths %store (list file) <>))))
545 (delete-paths %store (list file))
547 ;; Flip a bit in the stream's payload.
548 (let* ((index (quotient (bytevector-length dump) 4))
549 (byte (bytevector-u8-ref dump index)))
550 (bytevector-u8-set! dump index (logxor #xff byte)))
552 (and (not (file-exists? file))
553 (guard (c ((nix-protocol-error? c)
555 (and (not (zero? (nix-protocol-error-status c)))
556 (string-contains (nix-protocol-error-message c)
558 (let* ((source (open-bytevector-input-port dump))
559 (imported (import-paths %store source)))
560 (pk 'corrupt-imported imported)
563 (test-assert "register-path"
564 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
566 (when (valid-path? %store file)
567 (delete-paths %store (list file)))
568 (false-if-exception (delete-file file))
570 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
571 (drv (string-append file ".drv")))
572 (call-with-output-file file
573 (cut display "This is a fake store item.\n" <>))
575 #:references (list ref)
578 (and (valid-path? %store file)
579 (equal? (references %store file) (list ref))
580 (null? (valid-derivers %store file))
581 (null? (referrers %store file))))))
586 (exit (= (test-runner-fail-count (test-runner-current)) 0))