gnu: Add UnionFS-FUSE.
[jackhill/guix/guix.git] / tests / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
6bfec3ed 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3259877d 3;;;
233e7676 4;;; This file is part of GNU Guix.
3259877d 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
3259877d
LC
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;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
3259877d
LC
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
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
3259877d
LC
18
19
20(define-module (test-store)
21 #:use-module (guix store)
22 #:use-module (guix utils)
72626a71 23 #:use-module (guix hash)
3259877d 24 #:use-module (guix base32)
0f3d2504
LC
25 #:use-module (guix packages)
26 #:use-module (guix derivations)
fe0cff14 27 #:use-module (guix nar)
fae31edc 28 #:use-module (gnu packages)
1ffa7090 29 #:use-module (gnu packages bootstrap)
3259877d 30 #:use-module (ice-9 match)
526382ff 31 #:use-module (rnrs bytevectors)
fe0cff14 32 #:use-module (rnrs io ports)
f65cf81a 33 #:use-module (web uri)
3259877d
LC
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
526382ff 36 #:use-module (srfi srfi-26)
c3eb878f 37 #:use-module (srfi srfi-34)
3259877d
LC
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
2c6ab6cc
LC
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
c61a5b4a
LC
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
9336e5b5
LC
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
3259877d
LC
90(test-skip (if %store 0 10))
91
92(test-assert "dead-paths"
cfbf9160 93 (let ((p (add-text-to-store %store "random-text" (random-text))))
3259877d
LC
94 (member p (dead-paths %store))))
95
96;; FIXME: Find a test for `live-paths'.
97;;
98;; (test-assert "temporary root is in live-paths"
99;; (let* ((p1 (add-text-to-store %store "random-text"
100;; (random-text) '()))
101;; (b (add-text-to-store %store "link-builder"
102;; (format #f "echo ~a > $out" p1)
103;; '()))
a987d2c0
LC
104;; (d1 (derivation %store "link"
105;; "/bin/sh" `("-e" ,b)
106;; #:inputs `((,b) (,p1))))
59688fc4 107;; (p2 (derivation->output-path d1)))
3259877d
LC
108;; (and (add-temp-root %store p2)
109;; (build-derivations %store (list d1))
110;; (valid-path? %store p1)
111;; (member (pk p2) (live-paths %store)))))
112
113(test-assert "dead path can be explicitly collected"
114 (let ((p (add-text-to-store %store "random-text"
115 (random-text) '())))
116 (let-values (((paths freed) (delete-paths %store (list p))))
117 (and (equal? paths (list p))
118 (> freed 0)
119 (not (file-exists? p))))))
120
fae31edc
LC
121(test-assert "references"
122 (let* ((t1 (add-text-to-store %store "random1"
cfbf9160 123 (random-text)))
fae31edc
LC
124 (t2 (add-text-to-store %store "random2"
125 (random-text) (list t1))))
126 (and (equal? (list t1) (references %store t2))
127 (equal? (list t2) (referrers %store t1))
128 (null? (references %store t1))
129 (null? (referrers %store t2)))))
130
3f1e6939
LC
131(test-assert "requisites"
132 (let* ((t1 (add-text-to-store %store "random1"
133 (random-text) '()))
134 (t2 (add-text-to-store %store "random2"
135 (random-text) (list t1)))
136 (t3 (add-text-to-store %store "random3"
137 (random-text) (list t2)))
138 (t4 (add-text-to-store %store "random4"
139 (random-text) (list t1 t3))))
140 (define (same? x y)
141 (and (= (length x) (length y))
142 (lset= equal? x y)))
143
144 (and (same? (requisites %store t1) (list t1))
145 (same? (requisites %store t2) (list t1 t2))
146 (same? (requisites %store t3) (list t1 t2 t3))
147 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
148
fae31edc
LC
149(test-assert "derivers"
150 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
151 (s (add-to-store %store "bash" #t "sha256"
152 (search-bootstrap-binary "bash"
153 (%current-system))))
a987d2c0
LC
154 (d (derivation %store "the-thing"
155 s `("-e" ,b)
156 #:env-vars `(("foo" . ,(random-text)))
157 #:inputs `((,b) (,s))))
59688fc4 158 (o (derivation->output-path d)))
fae31edc 159 (and (build-derivations %store (list d))
59688fc4 160 (equal? (query-derivation-outputs %store (derivation-file-name d))
fae31edc
LC
161 (list o))
162 (equal? (valid-derivers %store o)
59688fc4 163 (list (derivation-file-name d))))))
fae31edc 164
50add477
LC
165(test-assert "topologically-sorted, one item"
166 (let* ((a (add-text-to-store %store "a" "a"))
167 (b (add-text-to-store %store "b" "b" (list a)))
168 (c (add-text-to-store %store "c" "c" (list b)))
169 (d (add-text-to-store %store "d" "d" (list c)))
170 (s (topologically-sorted %store (list d))))
171 (equal? s (list a b c d))))
172
173(test-assert "topologically-sorted, several items"
174 (let* ((a (add-text-to-store %store "a" "a"))
175 (b (add-text-to-store %store "b" "b" (list a)))
176 (c (add-text-to-store %store "c" "c" (list b)))
177 (d (add-text-to-store %store "d" "d" (list c)))
178 (s1 (topologically-sorted %store (list d a c b)))
179 (s2 (topologically-sorted %store (list b d c a b d))))
180 (equal? s1 s2 (list a b c d))))
181
182(test-assert "topologically-sorted, more difficult"
183 (let* ((a (add-text-to-store %store "a" "a"))
184 (b (add-text-to-store %store "b" "b" (list a)))
185 (c (add-text-to-store %store "c" "c" (list b)))
186 (d (add-text-to-store %store "d" "d" (list c)))
187 (w (add-text-to-store %store "w" "w"))
188 (x (add-text-to-store %store "x" "x" (list w)))
189 (y (add-text-to-store %store "y" "y" (list x d)))
190 (s1 (topologically-sorted %store (list y)))
191 (s2 (topologically-sorted %store (list c y)))
192 (s3 (topologically-sorted %store (cons y (references %store y)))))
193 (and (equal? s1 (list w x a b c d y))
194 (equal? s2 (list a b c w x d y))
195 (lset= string=? s1 s3))))
196
eddd4077
LC
197(test-assert "log-file, derivation"
198 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
199 (s (add-to-store %store "bash" #t "sha256"
200 (search-bootstrap-binary "bash"
201 (%current-system))))
202 (d (derivation %store "the-thing"
203 s `("-e" ,b)
204 #:env-vars `(("foo" . ,(random-text)))
205 #:inputs `((,b) (,s)))))
206 (and (build-derivations %store (list d))
207 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
208
209(test-assert "log-file, output file name"
210 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
211 (s (add-to-store %store "bash" #t "sha256"
212 (search-bootstrap-binary "bash"
213 (%current-system))))
214 (d (derivation %store "the-thing"
215 s `("-e" ,b)
216 #:env-vars `(("foo" . ,(random-text)))
217 #:inputs `((,b) (,s))))
218 (o (derivation->output-path d)))
219 (and (build-derivations %store (list d))
220 (file-exists? (pk (log-file %store o)))
221 (string=? (log-file %store (derivation-file-name d))
222 (log-file %store o)))))
223
0f3d2504
LC
224(test-assert "no substitutes"
225 (let* ((s (open-connection))
226 (d1 (package-derivation s %bootstrap-guile (%current-system)))
227 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
59688fc4 228 (o (map derivation->output-path (list d1 d2))))
0f3d2504 229 (set-build-options s #:use-substitutes? #f)
59688fc4
LC
230 (and (not (has-substitutes? s (derivation-file-name d1)))
231 (not (has-substitutes? s (derivation-file-name d2)))
0f3d2504
LC
232 (null? (substitutable-paths s o))
233 (null? (substitutable-path-info s o)))))
234
f65cf81a
LC
235(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
236
237(test-assert "substitute query"
238 (let* ((s (open-connection))
239 (d (package-derivation s %bootstrap-guile (%current-system)))
59688fc4 240 (o (derivation->output-path d))
f65cf81a
LC
241 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
242 (compose uri-path string->uri))))
243 ;; Create fake substituter data, to be read by `substitute-binary'.
244 (call-with-output-file (string-append dir "/nix-cache-info")
245 (lambda (p)
246 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
fe0cff14 247 (%store-prefix))))
f65cf81a
LC
248 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
249 ".narinfo")
250 (lambda (p)
251 (format p "StorePath: ~a
252URL: ~a
253Compression: none
254NarSize: 1234
255References:
256System: ~a
257Deriver: ~a~%"
258 o ; StorePath
259 (string-append dir "/example.nar") ; URL
260 (%current-system) ; System
59688fc4
LC
261 (basename
262 (derivation-file-name d))))) ; Deriver
f65cf81a 263
eba783b7
LC
264 ;; Remove entry from the local cache.
265 (false-if-exception
266 (delete-file (string-append (getenv "XDG_CACHE_HOME")
267 "/guix/substitute-binary/"
268 (store-path-hash-part o))))
269
f65cf81a
LC
270 ;; Make sure `substitute-binary' correctly communicates the above data.
271 (set-build-options s #:use-substitutes? #t)
272 (and (has-substitutes? s o)
273 (equal? (list o) (substitutable-paths s (list o)))
274 (match (pk 'spi (substitutable-path-info s (list o)))
275 (((? substitutable? s))
59688fc4 276 (and (string=? (substitutable-deriver s) (derivation-file-name d))
f65cf81a
LC
277 (null? (substitutable-references s))
278 (equal? (substitutable-nar-size s) 1234)))))))
279
fe0cff14
LC
280(test-assert "substitute"
281 (let* ((s (open-connection))
282 (c (random-text)) ; contents of the output
283 (d (build-expression->derivation
dd1a5a15 284 s "substitute-me"
fe0cff14
LC
285 `(call-with-output-file %output
286 (lambda (p)
287 (exit 1) ; would actually fail
288 (display ,c p)))
fe0cff14
LC
289 #:guile-for-build
290 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 291 (o (derivation->output-path d))
fe0cff14
LC
292 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
293 (compose uri-path string->uri))))
294 ;; Create fake substituter data, to be read by `substitute-binary'.
295 (call-with-output-file (string-append dir "/nix-cache-info")
296 (lambda (p)
297 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
298 (%store-prefix))))
299 (call-with-output-file (string-append dir "/example.out")
300 (lambda (p)
301 (display c p)))
302 (call-with-output-file (string-append dir "/example.nar")
303 (lambda (p)
304 (write-file (string-append dir "/example.out") p)))
305 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
306 ".narinfo")
307 (lambda (p)
308 (format p "StorePath: ~a
309URL: ~a
310Compression: none
311NarSize: 1234
312NarHash: sha256:~a
313References:
314System: ~a
315Deriver: ~a~%"
316 o ; StorePath
317 "example.nar" ; relative URL
318 (call-with-input-file (string-append dir "/example.nar")
319 (compose bytevector->nix-base32-string sha256
320 get-bytevector-all))
321 (%current-system) ; System
59688fc4
LC
322 (basename
323 (derivation-file-name d))))) ; Deriver
fe0cff14
LC
324
325 ;; Make sure we use `substitute-binary'.
326 (set-build-options s #:use-substitutes? #t)
327 (and (has-substitutes? s o)
328 (build-derivations s (list d))
329 (equal? c (call-with-input-file o get-string-all)))))
330
c3eb878f
LC
331(test-assert "substitute --fallback"
332 (let* ((s (open-connection))
333 (t (random-text)) ; contents of the output
334 (d (build-expression->derivation
dd1a5a15 335 s "substitute-me-not"
c3eb878f
LC
336 `(call-with-output-file %output
337 (lambda (p)
338 (display ,t p)))
c3eb878f
LC
339 #:guile-for-build
340 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 341 (o (derivation->output-path d))
c3eb878f
LC
342 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
343 (compose uri-path string->uri))))
344 ;; Create fake substituter data, to be read by `substitute-binary'.
345 (call-with-output-file (string-append dir "/nix-cache-info")
346 (lambda (p)
347 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
348 (%store-prefix))))
349 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
350 ".narinfo")
351 (lambda (p)
352 (format p "StorePath: ~a
353URL: ~a
354Compression: none
355NarSize: 1234
356NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
357References:
358System: ~a
359Deriver: ~a~%"
360 o ; StorePath
361 "does-not-exist.nar" ; relative URL
362 (%current-system) ; System
59688fc4
LC
363 (basename
364 (derivation-file-name d))))) ; Deriver
c3eb878f
LC
365
366 ;; Make sure we use `substitute-binary'.
367 (set-build-options s #:use-substitutes? #t)
368 (and (has-substitutes? s o)
369 (guard (c ((nix-protocol-error? c)
370 ;; The substituter failed as expected. Now make sure that
371 ;; #:fallback? #t works correctly.
372 (set-build-options s
373 #:use-substitutes? #t
374 #:fallback? #t)
375 (and (build-derivations s (list d))
376 (equal? t (call-with-input-file o get-string-all)))))
377 ;; Should fail.
378 (build-derivations s (list d))
379 #f))))
380
526382ff
LC
381(test-assert "export/import several paths"
382 (let* ((texts (unfold (cut >= <> 10)
383 (lambda _ (random-text))
384 1+
385 0))
386 (files (map (cut add-text-to-store %store "text" <>) texts))
387 (dump (call-with-bytevector-output-port
388 (cut export-paths %store files <>))))
389 (delete-paths %store files)
390 (and (every (negate file-exists?) files)
391 (let* ((source (open-bytevector-input-port dump))
392 (imported (import-paths %store source)))
393 (and (equal? imported files)
394 (every file-exists? files)
395 (equal? texts
396 (map (lambda (file)
397 (call-with-input-file file
398 get-string-all))
399 files)))))))
400
99fbddf9 401(test-assert "export/import paths, ensure topological order"
cafb92d8
LC
402 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
403 (file1 (add-text-to-store %store "foo" (random-text)
404 (list file0)))
99fbddf9
LC
405 (file2 (add-text-to-store %store "bar" (random-text)
406 (list file1)))
407 (files (list file1 file2))
408 (dump1 (call-with-bytevector-output-port
409 (cute export-paths %store (list file1 file2) <>)))
410 (dump2 (call-with-bytevector-output-port
411 (cute export-paths %store (list file2 file1) <>))))
412 (delete-paths %store files)
413 (and (every (negate file-exists?) files)
414 (bytevector=? dump1 dump2)
415 (let* ((source (open-bytevector-input-port dump1))
416 (imported (import-paths %store source)))
cafb92d8 417 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
99fbddf9
LC
418 (and (equal? imported (list file1 file2))
419 (every file-exists? files)
cafb92d8 420 (equal? (list file0) (references %store file1))
99fbddf9
LC
421 (equal? (list file1) (references %store file2)))))))
422
526382ff
LC
423(test-assert "import corrupt path"
424 (let* ((text (random-text))
425 (file (add-text-to-store %store "text" text))
426 (dump (call-with-bytevector-output-port
427 (cut export-paths %store (list file) <>))))
428 (delete-paths %store (list file))
429
6df1fb89
LC
430 ;; Flip a bit in the stream's payload.
431 (let* ((index (quotient (bytevector-length dump) 4))
526382ff
LC
432 (byte (bytevector-u8-ref dump index)))
433 (bytevector-u8-set! dump index (logxor #xff byte)))
434
435 (and (not (file-exists? file))
436 (guard (c ((nix-protocol-error? c)
437 (pk 'c c)
438 (and (not (zero? (nix-protocol-error-status c)))
439 (string-contains (nix-protocol-error-message c)
440 "corrupt"))))
441 (let* ((source (open-bytevector-input-port dump))
442 (imported (import-paths %store source)))
443 (pk 'corrupt-imported imported)
444 #f)))))
445
6bfec3ed
LC
446(test-assert "register-path"
447 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
448 "-fake")))
449 (when (valid-path? %store file)
450 (delete-paths %store (list file)))
451 (false-if-exception (delete-file file))
452
453 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
454 (drv (string-append file ".drv")))
455 (call-with-output-file file
456 (cut display "This is a fake store item.\n" <>))
457 (register-path file
458 #:references (list ref)
459 #:deriver drv)
460
461 (and (valid-path? %store file)
462 (equal? (references %store file) (list ref))
463 (null? (valid-derivers %store file))
464 (null? (referrers %store file))))))
465
3259877d
LC
466(test-end "store")
467
468\f
469(exit (= (test-runner-fail-count (test-runner-current)) 0))