gnu: guix: Update to 0.7.
[jackhill/guix/guix.git] / tests / store.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19
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))
39
40 ;; Test the (guix store) module.
41
42 (define %store
43 (false-if-exception (open-connection)))
44
45 (when %store
46 ;; Make sure we build everything by ourselves.
47 (set-build-options %store #:use-substitutes? #f))
48
49 (define %seed
50 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
51
52 (define (random-text)
53 (number->string (random (expt 2 256) %seed) 16))
54
55 \f
56 (test-begin "store")
57
58 (test-equal "store-path-hash-part"
59 "283gqy39v3g9dxjy26rynl0zls82fmcg"
60 (store-path-hash-part
61 (string-append (%store-prefix)
62 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
63
64 (test-equal "store-path-hash-part #f"
65 #f
66 (store-path-hash-part
67 (string-append (%store-prefix)
68 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
69
70 (test-equal "store-path-package-name"
71 "guile-2.0.7"
72 (store-path-package-name
73 (string-append (%store-prefix)
74 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
75
76 (test-equal "store-path-package-name #f"
77 #f
78 (store-path-package-name
79 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
80
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?
86 (string-append
87 (%store-prefix)
88 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
89 (not (direct-store-path? (%store-prefix)))))
90
91 (test-skip (if %store 0 13))
92
93 (test-assert "valid-path? live"
94 (let ((p (add-text-to-store %store "hello" "hello, world")))
95 (valid-path? %store p)))
96
97 (test-assert "valid-path? false"
98 (not (valid-path? %store
99 (string-append (%store-prefix) "/"
100 (make-string 32 #\e) "-foobar"))))
101
102 (test-assert "valid-path? error"
103 (with-store s
104 (guard (c ((nix-protocol-error? c) #t))
105 (valid-path? s "foo")
106 #f)))
107
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
111 ;; file name. See
112 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
113 ;; details.
114 (with-store s
115 (let-syntax ((true-if-error (syntax-rules ()
116 ((_ exp)
117 (guard (c ((nix-protocol-error? c) #t))
118 exp #f)))))
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"))))))
124
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))
128 p)))
129
130 (test-assert "dead-paths"
131 (let ((p (add-text-to-store %store "random-text" (random-text))))
132 (member p (dead-paths %store))))
133
134 ;; FIXME: Find a test for `live-paths'.
135 ;;
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)
141 ;; '()))
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)))))
150
151 (test-assert "permanent root"
152 (let* ((p (with-store store
153 (let ((p (add-text-to-store store "random-text"
154 (random-text))))
155 (add-permanent-root p)
156 (add-permanent-root p) ; should not throw
157 p))))
158 (and (member p (live-paths %store))
159 (begin
160 (remove-permanent-root p)
161 (->bool (member p (dead-paths %store)))))))
162
163 (test-assert "dead path can be explicitly collected"
164 (let ((p (add-text-to-store %store "random-text"
165 (random-text) '())))
166 (let-values (((paths freed) (delete-paths %store (list p))))
167 (and (equal? paths (list p))
168 (> freed 0)
169 (not (file-exists? p))))))
170
171 (test-assert "references"
172 (let* ((t1 (add-text-to-store %store "random1"
173 (random-text)))
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)))))
180
181 (test-assert "requisites"
182 (let* ((t1 (add-text-to-store %store "random1"
183 (random-text) '()))
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))))
190 (define (same? x y)
191 (and (= (length x) (length y))
192 (lset= equal? x y)))
193
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)))))
198
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"
203 (%current-system))))
204 (d (derivation %store "the-thing"
205 s `("-e" ,b)
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))
211 (list o))
212 (equal? (valid-derivers %store o)
213 (list (derivation-file-name d))))))
214
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))))
222
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))))
231
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))))
246 (and (equal? s1
247 (if x-then-d?
248 (list w x a b c d y)
249 (list a b c d w x y)))
250 (equal? s2
251 (if x-then-d?
252 (list a b c w x d y)
253 (list a b c d w x y)))
254 (lset= string=? s1 s3)))))
255
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"
260 (%current-system))))
261 (d (derivation %store "the-thing"
262 s `("-e" ,b)
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)))))))
267
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"
272 (%current-system))))
273 (d (derivation %store "the-thing"
274 s `("-e" ,b)
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)))))
282
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)))))
293
294 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
295
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")
304 (lambda (p)
305 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
306 (%store-prefix))))
307 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
308 ".narinfo")
309 (lambda (p)
310 (format p "StorePath: ~a
311 URL: ~a
312 Compression: none
313 NarSize: 1234
314 References:
315 System: ~a
316 Deriver: ~a~%"
317 o ; StorePath
318 (string-append dir "/example.nar") ; URL
319 (%current-system) ; System
320 (basename
321 (derivation-file-name d))))) ; Deriver
322
323 ;; Remove entry from the local cache.
324 (false-if-exception
325 (delete-file (string-append (getenv "XDG_CACHE_HOME")
326 "/guix/substitute-binary/"
327 (store-path-hash-part o))))
328
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)))))))
338
339 (test-assert "substitute"
340 (let* ((s (open-connection))
341 (c (random-text)) ; contents of the output
342 (d (build-expression->derivation
343 s "substitute-me"
344 `(call-with-output-file %output
345 (lambda (p)
346 (exit 1) ; would actually fail
347 (display ,c p)))
348 #:guile-for-build
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")
355 (lambda (p)
356 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
357 (%store-prefix))))
358 (call-with-output-file (string-append dir "/example.out")
359 (lambda (p)
360 (display c p)))
361 (call-with-output-file (string-append dir "/example.nar")
362 (lambda (p)
363 (write-file (string-append dir "/example.out") p)))
364 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
365 ".narinfo")
366 (lambda (p)
367 (format p "StorePath: ~a
368 URL: ~a
369 Compression: none
370 NarSize: 1234
371 NarHash: sha256:~a
372 References:
373 System: ~a
374 Deriver: ~a~%"
375 o ; StorePath
376 "example.nar" ; relative URL
377 (call-with-input-file (string-append dir "/example.nar")
378 (compose bytevector->nix-base32-string sha256
379 get-bytevector-all))
380 (%current-system) ; System
381 (basename
382 (derivation-file-name d))))) ; Deriver
383
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)))))
389
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
393 ;; raise an error.
394 (let* ((s (open-connection))
395 (c "hello, world") ; contents of the output
396 (d (build-expression->derivation
397 s "corrupt-substitute"
398 `(mkdir %output)
399 #:guile-for-build
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")
406 (lambda (p)
407 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
408 (%store-prefix))))
409 (call-with-output-file (string-append dir "/example.out")
410 (lambda (p)
411 (display "The contents here do not match C." p)))
412 (call-with-output-file (string-append dir "/example.nar")
413 (lambda (p)
414 (write-file (string-append dir "/example.out") p)))
415 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
416 ".narinfo")
417 (lambda (p)
418 (format p "StorePath: ~a
419 URL: ~a
420 Compression: none
421 NarSize: 1234
422 NarHash: sha256:~a
423 References:
424 System: ~a
425 Deriver: ~a~%"
426 o ; StorePath
427 "example.nar" ; relative URL
428 (bytevector->nix-base32-string
429 (sha256 (string->utf8 c)))
430 (%current-system) ; System
431 (basename
432 (derivation-file-name d))))) ; Deriver
433
434 ;; Make sure we use `substitute-binary'.
435 (set-build-options s
436 #:use-substitutes? #t
437 #:fallback? #f)
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.
443 (pk 'corrupt c)
444 (not (zero? (nix-protocol-error-status c)))))
445 (build-derivations s (list d))
446 #f))))
447
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
454 (lambda (p)
455 (display ,t p)))
456 #:guile-for-build
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")
463 (lambda (p)
464 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
465 (%store-prefix))))
466 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
467 ".narinfo")
468 (lambda (p)
469 (format p "StorePath: ~a
470 URL: ~a
471 Compression: none
472 NarSize: 1234
473 NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
474 References:
475 System: ~a
476 Deriver: ~a~%"
477 o ; StorePath
478 "does-not-exist.nar" ; relative URL
479 (%current-system) ; System
480 (basename
481 (derivation-file-name d))))) ; Deriver
482
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.
489 (set-build-options s
490 #:use-substitutes? #t
491 #:fallback? #t)
492 (and (build-derivations s (list d))
493 (equal? t (call-with-input-file o get-string-all)))))
494 ;; Should fail.
495 (build-derivations s (list d))
496 #f))))
497
498 (test-assert "export/import several paths"
499 (let* ((texts (unfold (cut >= <> 10)
500 (lambda _ (random-text))
501 1+
502 0))
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)
512 (equal? texts
513 (map (lambda (file)
514 (call-with-input-file file
515 get-string-all))
516 files)))))))
517
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)
521 (list file0)))
522 (file2 (add-text-to-store %store "bar" (random-text)
523 (list file1)))
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)))))))
539
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))
546
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)))
551
552 (and (not (file-exists? file))
553 (guard (c ((nix-protocol-error? c)
554 (pk 'c c)
555 (and (not (zero? (nix-protocol-error-status c)))
556 (string-contains (nix-protocol-error-message c)
557 "corrupt"))))
558 (let* ((source (open-bytevector-input-port dump))
559 (imported (import-paths %store source)))
560 (pk 'corrupt-imported imported)
561 #f)))))
562
563 (test-assert "register-path"
564 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
565 "-fake")))
566 (when (valid-path? %store file)
567 (delete-paths %store (list file)))
568 (false-if-exception (delete-file file))
569
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" <>))
574 (register-path file
575 #:references (list ref)
576 #:deriver drv)
577
578 (and (valid-path? %store file)
579 (equal? (references %store file) (list ref))
580 (null? (valid-derivers %store file))
581 (null? (referrers %store file))))))
582
583 (test-end "store")
584
585 \f
586 (exit (= (test-runner-fail-count (test-runner-current)) 0))