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