offload: Fix 'choose-build-machine' for several machines.
[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)))))
58cbbe4b
LC
193 ;; The order in which 'references' returns the references of Y is
194 ;; unspecified, so accommodate.
195 (let* ((x-then-d? (equal? (references %store y) (list x d))))
196 (and (equal? s1
197 (if x-then-d?
198 (list w x a b c d y)
199 (list a b c d w x y)))
200 (equal? s2
201 (if x-then-d?
202 (list a b c w x d y)
203 (list a b c d w x y)))
204 (lset= string=? s1 s3)))))
50add477 205
eddd4077
LC
206(test-assert "log-file, derivation"
207 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
208 (s (add-to-store %store "bash" #t "sha256"
209 (search-bootstrap-binary "bash"
210 (%current-system))))
211 (d (derivation %store "the-thing"
212 s `("-e" ,b)
213 #:env-vars `(("foo" . ,(random-text)))
214 #:inputs `((,b) (,s)))))
215 (and (build-derivations %store (list d))
216 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
217
218(test-assert "log-file, output file name"
219 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
220 (s (add-to-store %store "bash" #t "sha256"
221 (search-bootstrap-binary "bash"
222 (%current-system))))
223 (d (derivation %store "the-thing"
224 s `("-e" ,b)
225 #:env-vars `(("foo" . ,(random-text)))
226 #:inputs `((,b) (,s))))
227 (o (derivation->output-path d)))
228 (and (build-derivations %store (list d))
229 (file-exists? (pk (log-file %store o)))
230 (string=? (log-file %store (derivation-file-name d))
231 (log-file %store o)))))
232
0f3d2504
LC
233(test-assert "no substitutes"
234 (let* ((s (open-connection))
235 (d1 (package-derivation s %bootstrap-guile (%current-system)))
236 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
59688fc4 237 (o (map derivation->output-path (list d1 d2))))
0f3d2504 238 (set-build-options s #:use-substitutes? #f)
59688fc4
LC
239 (and (not (has-substitutes? s (derivation-file-name d1)))
240 (not (has-substitutes? s (derivation-file-name d2)))
0f3d2504
LC
241 (null? (substitutable-paths s o))
242 (null? (substitutable-path-info s o)))))
243
f65cf81a
LC
244(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
245
246(test-assert "substitute query"
247 (let* ((s (open-connection))
248 (d (package-derivation s %bootstrap-guile (%current-system)))
59688fc4 249 (o (derivation->output-path d))
f65cf81a
LC
250 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
251 (compose uri-path string->uri))))
252 ;; Create fake substituter data, to be read by `substitute-binary'.
253 (call-with-output-file (string-append dir "/nix-cache-info")
254 (lambda (p)
255 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
fe0cff14 256 (%store-prefix))))
f65cf81a
LC
257 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
258 ".narinfo")
259 (lambda (p)
260 (format p "StorePath: ~a
261URL: ~a
262Compression: none
263NarSize: 1234
264References:
265System: ~a
266Deriver: ~a~%"
267 o ; StorePath
268 (string-append dir "/example.nar") ; URL
269 (%current-system) ; System
59688fc4
LC
270 (basename
271 (derivation-file-name d))))) ; Deriver
f65cf81a 272
eba783b7
LC
273 ;; Remove entry from the local cache.
274 (false-if-exception
275 (delete-file (string-append (getenv "XDG_CACHE_HOME")
276 "/guix/substitute-binary/"
277 (store-path-hash-part o))))
278
f65cf81a
LC
279 ;; Make sure `substitute-binary' correctly communicates the above data.
280 (set-build-options s #:use-substitutes? #t)
281 (and (has-substitutes? s o)
282 (equal? (list o) (substitutable-paths s (list o)))
283 (match (pk 'spi (substitutable-path-info s (list o)))
284 (((? substitutable? s))
59688fc4 285 (and (string=? (substitutable-deriver s) (derivation-file-name d))
f65cf81a
LC
286 (null? (substitutable-references s))
287 (equal? (substitutable-nar-size s) 1234)))))))
288
fe0cff14
LC
289(test-assert "substitute"
290 (let* ((s (open-connection))
291 (c (random-text)) ; contents of the output
292 (d (build-expression->derivation
dd1a5a15 293 s "substitute-me"
fe0cff14
LC
294 `(call-with-output-file %output
295 (lambda (p)
296 (exit 1) ; would actually fail
297 (display ,c p)))
fe0cff14
LC
298 #:guile-for-build
299 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 300 (o (derivation->output-path d))
fe0cff14
LC
301 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
302 (compose uri-path string->uri))))
303 ;; Create fake substituter data, to be read by `substitute-binary'.
304 (call-with-output-file (string-append dir "/nix-cache-info")
305 (lambda (p)
306 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
307 (%store-prefix))))
308 (call-with-output-file (string-append dir "/example.out")
309 (lambda (p)
310 (display c p)))
311 (call-with-output-file (string-append dir "/example.nar")
312 (lambda (p)
313 (write-file (string-append dir "/example.out") p)))
314 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
315 ".narinfo")
316 (lambda (p)
317 (format p "StorePath: ~a
318URL: ~a
319Compression: none
320NarSize: 1234
321NarHash: sha256:~a
322References:
323System: ~a
324Deriver: ~a~%"
325 o ; StorePath
326 "example.nar" ; relative URL
327 (call-with-input-file (string-append dir "/example.nar")
328 (compose bytevector->nix-base32-string sha256
329 get-bytevector-all))
330 (%current-system) ; System
59688fc4
LC
331 (basename
332 (derivation-file-name d))))) ; Deriver
fe0cff14
LC
333
334 ;; Make sure we use `substitute-binary'.
335 (set-build-options s #:use-substitutes? #t)
336 (and (has-substitutes? s o)
337 (build-derivations s (list d))
338 (equal? c (call-with-input-file o get-string-all)))))
339
c3eb878f
LC
340(test-assert "substitute --fallback"
341 (let* ((s (open-connection))
342 (t (random-text)) ; contents of the output
343 (d (build-expression->derivation
dd1a5a15 344 s "substitute-me-not"
c3eb878f
LC
345 `(call-with-output-file %output
346 (lambda (p)
347 (display ,t p)))
c3eb878f
LC
348 #:guile-for-build
349 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 350 (o (derivation->output-path d))
c3eb878f
LC
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 "/" (store-path-hash-part o)
359 ".narinfo")
360 (lambda (p)
361 (format p "StorePath: ~a
362URL: ~a
363Compression: none
364NarSize: 1234
365NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
366References:
367System: ~a
368Deriver: ~a~%"
369 o ; StorePath
370 "does-not-exist.nar" ; relative URL
371 (%current-system) ; System
59688fc4
LC
372 (basename
373 (derivation-file-name d))))) ; Deriver
c3eb878f
LC
374
375 ;; Make sure we use `substitute-binary'.
376 (set-build-options s #:use-substitutes? #t)
377 (and (has-substitutes? s o)
378 (guard (c ((nix-protocol-error? c)
379 ;; The substituter failed as expected. Now make sure that
380 ;; #:fallback? #t works correctly.
381 (set-build-options s
382 #:use-substitutes? #t
383 #:fallback? #t)
384 (and (build-derivations s (list d))
385 (equal? t (call-with-input-file o get-string-all)))))
386 ;; Should fail.
387 (build-derivations s (list d))
388 #f))))
389
526382ff
LC
390(test-assert "export/import several paths"
391 (let* ((texts (unfold (cut >= <> 10)
392 (lambda _ (random-text))
393 1+
394 0))
395 (files (map (cut add-text-to-store %store "text" <>) texts))
396 (dump (call-with-bytevector-output-port
397 (cut export-paths %store files <>))))
398 (delete-paths %store files)
399 (and (every (negate file-exists?) files)
400 (let* ((source (open-bytevector-input-port dump))
401 (imported (import-paths %store source)))
402 (and (equal? imported files)
403 (every file-exists? files)
404 (equal? texts
405 (map (lambda (file)
406 (call-with-input-file file
407 get-string-all))
408 files)))))))
409
99fbddf9 410(test-assert "export/import paths, ensure topological order"
cafb92d8
LC
411 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
412 (file1 (add-text-to-store %store "foo" (random-text)
413 (list file0)))
99fbddf9
LC
414 (file2 (add-text-to-store %store "bar" (random-text)
415 (list file1)))
416 (files (list file1 file2))
417 (dump1 (call-with-bytevector-output-port
418 (cute export-paths %store (list file1 file2) <>)))
419 (dump2 (call-with-bytevector-output-port
420 (cute export-paths %store (list file2 file1) <>))))
421 (delete-paths %store files)
422 (and (every (negate file-exists?) files)
423 (bytevector=? dump1 dump2)
424 (let* ((source (open-bytevector-input-port dump1))
425 (imported (import-paths %store source)))
cafb92d8 426 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
99fbddf9
LC
427 (and (equal? imported (list file1 file2))
428 (every file-exists? files)
cafb92d8 429 (equal? (list file0) (references %store file1))
99fbddf9
LC
430 (equal? (list file1) (references %store file2)))))))
431
526382ff
LC
432(test-assert "import corrupt path"
433 (let* ((text (random-text))
434 (file (add-text-to-store %store "text" text))
435 (dump (call-with-bytevector-output-port
436 (cut export-paths %store (list file) <>))))
437 (delete-paths %store (list file))
438
6df1fb89
LC
439 ;; Flip a bit in the stream's payload.
440 (let* ((index (quotient (bytevector-length dump) 4))
526382ff
LC
441 (byte (bytevector-u8-ref dump index)))
442 (bytevector-u8-set! dump index (logxor #xff byte)))
443
444 (and (not (file-exists? file))
445 (guard (c ((nix-protocol-error? c)
446 (pk 'c c)
447 (and (not (zero? (nix-protocol-error-status c)))
448 (string-contains (nix-protocol-error-message c)
449 "corrupt"))))
450 (let* ((source (open-bytevector-input-port dump))
451 (imported (import-paths %store source)))
452 (pk 'corrupt-imported imported)
453 #f)))))
454
6bfec3ed
LC
455(test-assert "register-path"
456 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
457 "-fake")))
458 (when (valid-path? %store file)
459 (delete-paths %store (list file)))
460 (false-if-exception (delete-file file))
461
462 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
463 (drv (string-append file ".drv")))
464 (call-with-output-file file
465 (cut display "This is a fake store item.\n" <>))
466 (register-path file
467 #:references (list ref)
468 #:deriver drv)
469
470 (and (valid-path? %store file)
471 (equal? (references %store file) (list ref))
472 (null? (valid-derivers %store file))
473 (null? (referrers %store file))))))
474
3259877d
LC
475(test-end "store")
476
477\f
478(exit (= (test-runner-fail-count (test-runner-current)) 0))