store: Rename '&nix-error' to '&store-error'.
[jackhill/guix/guix.git] / tests / store.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 (define-module (test-store)
20 #:use-module (guix tests)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix monads)
24 #:use-module (gcrypt hash)
25 #:use-module (guix base32)
26 #:use-module (guix packages)
27 #:use-module (guix derivations)
28 #:use-module (guix serialization)
29 #:use-module (guix build utils)
30 #:use-module (guix gexp)
31 #:use-module (gnu packages)
32 #:use-module (gnu packages bootstrap)
33 #:use-module (ice-9 match)
34 #:use-module (ice-9 regex)
35 #:use-module (rnrs bytevectors)
36 #:use-module (rnrs io ports)
37 #:use-module (web uri)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-11)
40 #:use-module (srfi srfi-26)
41 #:use-module (srfi srfi-34)
42 #:use-module (srfi srfi-64))
43
44 ;; Test the (guix store) module.
45
46 (define %store
47 (open-connection-for-tests))
48
49 (define %shell
50 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
51
52 \f
53 (test-begin "store")
54
55 (test-assert "open-connection with file:// URI"
56 (let ((store (open-connection (string-append "file://"
57 (%daemon-socket-uri)))))
58 (and (add-text-to-store store "foo" "bar")
59 (begin
60 (close-connection store)
61 #t))))
62
63 (test-equal "connection handshake error"
64 EPROTO
65 (let ((port (%make-void-port "rw")))
66 (guard (c ((store-connection-error? c)
67 (and (eq? port (store-connection-error-file c))
68 (store-connection-error-code c))))
69 (open-connection #f #:port port)
70 'broken)))
71
72 (test-equal "store-path-hash-part"
73 "283gqy39v3g9dxjy26rynl0zls82fmcg"
74 (store-path-hash-part
75 (string-append (%store-prefix)
76 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
77
78 (test-equal "store-path-hash-part #f"
79 #f
80 (store-path-hash-part
81 (string-append (%store-prefix)
82 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
83
84 (test-equal "store-path-package-name"
85 "guile-2.0.7"
86 (store-path-package-name
87 (string-append (%store-prefix)
88 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
89
90 (test-equal "store-path-package-name #f"
91 #f
92 (store-path-package-name
93 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
94
95 (test-assert "direct-store-path?"
96 (and (direct-store-path?
97 (string-append (%store-prefix)
98 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
99 (not (direct-store-path?
100 (string-append
101 (%store-prefix)
102 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
103 (not (direct-store-path? (%store-prefix)))))
104
105 (test-skip (if %store 0 13))
106
107 (test-equal "add-data-to-store"
108 #vu8(1 2 3 4 5)
109 (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
110 get-bytevector-all))
111
112 (test-assert "valid-path? live"
113 (let ((p (add-text-to-store %store "hello" "hello, world")))
114 (valid-path? %store p)))
115
116 (test-assert "valid-path? false"
117 (not (valid-path? %store
118 (string-append (%store-prefix) "/"
119 (make-string 32 #\e) "-foobar"))))
120
121 (test-assert "valid-path? error"
122 (with-store s
123 (guard (c ((store-protocol-error? c) #t))
124 (valid-path? s "foo")
125 #f)))
126
127 (test-assert "valid-path? recovery"
128 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
129 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
130 ;; file name. See
131 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
132 ;; details.
133 (with-store s
134 (let-syntax ((true-if-error (syntax-rules ()
135 ((_ exp)
136 (guard (c ((store-protocol-error? c) #t))
137 exp #f)))))
138 (and (true-if-error (valid-path? s "foo"))
139 (true-if-error (valid-path? s "bar"))
140 (true-if-error (valid-path? s "baz"))
141 (true-if-error (valid-path? s "chbouib"))
142 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
143
144 (test-assert "hash-part->path"
145 (let ((p (add-text-to-store %store "hello" "hello, world")))
146 (equal? (hash-part->path %store (store-path-hash-part p))
147 p)))
148
149 (test-assert "dead-paths"
150 (let ((p (add-text-to-store %store "random-text" (random-text))))
151 (->bool (member p (dead-paths %store)))))
152
153 ;; FIXME: Find a test for `live-paths'.
154 ;;
155 ;; (test-assert "temporary root is in live-paths"
156 ;; (let* ((p1 (add-text-to-store %store "random-text"
157 ;; (random-text) '()))
158 ;; (b (add-text-to-store %store "link-builder"
159 ;; (format #f "echo ~a > $out" p1)
160 ;; '()))
161 ;; (d1 (derivation %store "link"
162 ;; "/bin/sh" `("-e" ,b)
163 ;; #:inputs `((,b) (,p1))))
164 ;; (p2 (derivation->output-path d1)))
165 ;; (and (add-temp-root %store p2)
166 ;; (build-derivations %store (list d1))
167 ;; (valid-path? %store p1)
168 ;; (member (pk p2) (live-paths %store)))))
169
170 (test-assert "permanent root"
171 (let* ((p (with-store store
172 (let ((p (add-text-to-store store "random-text"
173 (random-text))))
174 (add-permanent-root p)
175 (add-permanent-root p) ; should not throw
176 p))))
177 (and (member p (live-paths %store))
178 (begin
179 (remove-permanent-root p)
180 (->bool (member p (dead-paths %store)))))))
181
182 (test-assert "dead path can be explicitly collected"
183 (let ((p (add-text-to-store %store "random-text"
184 (random-text) '())))
185 (let-values (((paths freed) (delete-paths %store (list p))))
186 (and (equal? paths (list p))
187 ;; XXX: On some file systems (notably Btrfs), freed
188 ;; may return 0. See <https://bugs.gnu.org/29363>.
189 ;;(> freed 0)
190 (not (file-exists? p))))))
191
192 (test-assert "add-text-to-store vs. delete-paths"
193 ;; Before, 'add-text-to-store' would return PATH2 without noticing that it
194 ;; is no longer valid.
195 (with-store store
196 (let* ((text (random-text))
197 (path (add-text-to-store store "delete-me" text))
198 (deleted (delete-paths store (list path)))
199 (path2 (add-text-to-store store "delete-me" text)))
200 (and (string=? path path2)
201 (equal? deleted (list path))
202 (valid-path? store path)
203 (file-exists? path)))))
204
205 (test-assert "add-to-store vs. delete-paths"
206 ;; Same as above.
207 (with-store store
208 (let* ((file (search-path %load-path "guix.scm"))
209 (path (add-to-store store "delete-me" #t "sha256" file))
210 (deleted (delete-paths store (list path)))
211 (path2 (add-to-store store "delete-me" #t "sha256" file)))
212 (and (string=? path path2)
213 (equal? deleted (list path))
214 (valid-path? store path)
215 (file-exists? path)))))
216
217 (test-equal "add-file-tree-to-store"
218 `(42
219 ("." directory #t)
220 ("./bar" directory #t)
221 ("./foo" directory #t)
222 ("./foo/a" regular "file a")
223 ("./foo/b" symlink "a")
224 ("./foo/c" directory #t)
225 ("./foo/c/p" regular "file p")
226 ("./foo/c/q" directory #t)
227 ("./foo/c/q/x" regular
228 ,(string-append "#!" %shell "\nexit 42"))
229 ("./foo/c/q/y" symlink "..")
230 ("./foo/c/q/z" directory #t))
231 (let* ((tree `("file-tree" directory
232 ("foo" directory
233 ("a" regular (data "file a"))
234 ("b" symlink "a")
235 ("c" directory
236 ("p" regular (data ,(string->utf8 "file p")))
237 ("q" directory
238 ("x" executable
239 (data ,(string-append "#!" %shell "\nexit 42")))
240 ("y" symlink "..")
241 ("z" directory))))
242 ("bar" directory)))
243 (result (add-file-tree-to-store %store tree)))
244 (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
245 (with-directory-excursion result
246 (map (lambda (file)
247 (let ((type (stat:type (lstat file))))
248 `(,file ,type
249 ,(match type
250 ((or 'regular 'executable)
251 (call-with-input-file file
252 get-string-all))
253 ('symlink (readlink file))
254 ('directory #t)))))
255 (find-files "." #:directories? #t))))))
256
257 (test-equal "add-file-tree-to-store, flat"
258 "Hello, world!"
259 (let* ((tree `("flat-file" regular (data "Hello, world!")))
260 (result (add-file-tree-to-store %store tree)))
261 (and (file-exists? result)
262 (call-with-input-file result get-string-all))))
263
264 (test-assert "references"
265 (let* ((t1 (add-text-to-store %store "random1"
266 (random-text)))
267 (t2 (add-text-to-store %store "random2"
268 (random-text) (list t1))))
269 (and (equal? (list t1) (references %store t2))
270 (equal? (list t2) (referrers %store t1))
271 (null? (references %store t1))
272 (null? (referrers %store t2)))))
273
274 (test-assert "references/substitutes missing reference info"
275 (with-store s
276 (set-build-options s #:use-substitutes? #f)
277 (guard (c ((store-protocol-error? c) #t))
278 (let* ((b (add-to-store s "bash" #t "sha256"
279 (search-bootstrap-binary "bash"
280 (%current-system))))
281 (d (derivation s "the-thing" b '("--help")
282 #:inputs `((,b)))))
283 (references/substitutes s (list (derivation->output-path d) b))
284 #f))))
285
286 (test-assert "references/substitutes with substitute info"
287 (with-store s
288 (set-build-options s #:use-substitutes? #t)
289 (let* ((t1 (add-text-to-store s "random1" (random-text)))
290 (t2 (add-text-to-store s "random2" (random-text)
291 (list t1)))
292 (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
293 (b (add-to-store s "bash" #t "sha256"
294 (search-bootstrap-binary "bash"
295 (%current-system))))
296 (d (derivation s "the-thing" b `("-e" ,t3)
297 #:inputs `((,b) (,t3) (,t2))
298 #:env-vars `(("t2" . ,t2))))
299 (o (derivation->output-path d)))
300 (with-derivation-narinfo d
301 (sha256 => (sha256 (string->utf8 t2)))
302 (references => (list t2))
303
304 (equal? (references/substitutes s (list o t3 t2 t1))
305 `((,t2) ;refs of O
306 () ;refs of T3
307 (,t1) ;refs of T2
308 ())))))) ;refs of T1
309
310 (test-equal "substitutable-path-info when substitutes are turned off"
311 '()
312 (with-store s
313 (set-build-options s #:use-substitutes? #f)
314 (let* ((b (add-to-store s "bash" #t "sha256"
315 (search-bootstrap-binary "bash"
316 (%current-system))))
317 (d (derivation s "the-thing" b '("--version")
318 #:inputs `((,b))))
319 (o (derivation->output-path d)))
320 (with-derivation-narinfo d
321 (substitutable-path-info s (list o))))))
322
323 (test-equal "substitutable-paths when substitutes are turned off"
324 '()
325 (with-store s
326 (set-build-options s #:use-substitutes? #f)
327 (let* ((b (add-to-store s "bash" #t "sha256"
328 (search-bootstrap-binary "bash"
329 (%current-system))))
330 (d (derivation s "the-thing" b '("--version")
331 #:inputs `((,b))))
332 (o (derivation->output-path d)))
333 (with-derivation-narinfo d
334 (substitutable-paths s (list o))))))
335
336 (test-assert "requisites"
337 (let* ((t1 (add-text-to-store %store "random1"
338 (random-text) '()))
339 (t2 (add-text-to-store %store "random2"
340 (random-text) (list t1)))
341 (t3 (add-text-to-store %store "random3"
342 (random-text) (list t2)))
343 (t4 (add-text-to-store %store "random4"
344 (random-text) (list t1 t3))))
345 (define (same? x y)
346 (and (= (length x) (length y))
347 (lset= equal? x y)))
348
349 (and (same? (requisites %store (list t1)) (list t1))
350 (same? (requisites %store (list t2)) (list t1 t2))
351 (same? (requisites %store (list t3)) (list t1 t2 t3))
352 (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
353 (same? (requisites %store (list t1 t2 t3 t4))
354 (list t1 t2 t3 t4)))))
355
356 (test-assert "derivers"
357 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
358 (s (add-to-store %store "bash" #t "sha256"
359 (search-bootstrap-binary "bash"
360 (%current-system))))
361 (d (derivation %store "the-thing"
362 s `("-e" ,b)
363 #:env-vars `(("foo" . ,(random-text)))
364 #:inputs `((,b) (,s))))
365 (o (derivation->output-path d)))
366 (and (build-derivations %store (list d))
367 (equal? (query-derivation-outputs %store (derivation-file-name d))
368 (list o))
369 (equal? (valid-derivers %store o)
370 (list (derivation-file-name d))))))
371
372 (test-assert "topologically-sorted, one item"
373 (let* ((a (add-text-to-store %store "a" "a"))
374 (b (add-text-to-store %store "b" "b" (list a)))
375 (c (add-text-to-store %store "c" "c" (list b)))
376 (d (add-text-to-store %store "d" "d" (list c)))
377 (s (topologically-sorted %store (list d))))
378 (equal? s (list a b c d))))
379
380 (test-assert "topologically-sorted, several items"
381 (let* ((a (add-text-to-store %store "a" "a"))
382 (b (add-text-to-store %store "b" "b" (list a)))
383 (c (add-text-to-store %store "c" "c" (list b)))
384 (d (add-text-to-store %store "d" "d" (list c)))
385 (s1 (topologically-sorted %store (list d a c b)))
386 (s2 (topologically-sorted %store (list b d c a b d))))
387 (equal? s1 s2 (list a b c d))))
388
389 (test-assert "topologically-sorted, more difficult"
390 (let* ((a (add-text-to-store %store "a" "a"))
391 (b (add-text-to-store %store "b" "b" (list a)))
392 (c (add-text-to-store %store "c" "c" (list b)))
393 (d (add-text-to-store %store "d" "d" (list c)))
394 (w (add-text-to-store %store "w" "w"))
395 (x (add-text-to-store %store "x" "x" (list w)))
396 (y (add-text-to-store %store "y" "y" (list x d)))
397 (s1 (topologically-sorted %store (list y)))
398 (s2 (topologically-sorted %store (list c y)))
399 (s3 (topologically-sorted %store (cons y (references %store y)))))
400 ;; The order in which 'references' returns the references of Y is
401 ;; unspecified, so accommodate.
402 (let* ((x-then-d? (equal? (references %store y) (list x d))))
403 (and (equal? s1
404 (if x-then-d?
405 (list w x a b c d y)
406 (list a b c d w x y)))
407 (equal? s2
408 (if x-then-d?
409 (list a b c w x d y)
410 (list a b c d w x y)))
411 (lset= string=? s1 s3)))))
412
413 (test-assert "current-build-output-port, UTF-8"
414 ;; Are UTF-8 strings in the build log properly interpreted?
415 (string-contains
416 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
417 (call-with-output-string
418 (lambda (port)
419 (parameterize ((current-build-output-port port))
420 (let* ((s "Here’s a Greek letter: λ.")
421 (d (build-expression->derivation
422 %store "foo" `(display ,s)
423 #:guile-for-build
424 (package-derivation s %bootstrap-guile (%current-system)))))
425 (guard (c ((store-protocol-error? c) #t))
426 (build-derivations %store (list d))))))))
427 "Here’s a Greek letter: λ."))
428
429 (test-assert "current-build-output-port, UTF-8 + garbage"
430 ;; What about a mixture of UTF-8 + garbage?
431 (string-contains
432 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
433 (call-with-output-string
434 (lambda (port)
435 (parameterize ((current-build-output-port port))
436 (let ((d (build-expression->derivation
437 %store "foo"
438 `(begin
439 (use-modules (rnrs io ports))
440 (display "garbage: ")
441 (put-bytevector (current-output-port) #vu8(128))
442 (display "lambda: λ\n"))
443 #:guile-for-build
444 (package-derivation %store %bootstrap-guile))))
445 (guard (c ((store-protocol-error? c) #t))
446 (build-derivations %store (list d))))))))
447 "garbage: �lambda: λ"))
448
449 (test-assert "log-file, derivation"
450 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
451 (s (add-to-store %store "bash" #t "sha256"
452 (search-bootstrap-binary "bash"
453 (%current-system))))
454 (d (derivation %store "the-thing"
455 s `("-e" ,b)
456 #:env-vars `(("foo" . ,(random-text)))
457 #:inputs `((,b) (,s)))))
458 (and (build-derivations %store (list d))
459 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
460
461 (test-assert "log-file, output file name"
462 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
463 (s (add-to-store %store "bash" #t "sha256"
464 (search-bootstrap-binary "bash"
465 (%current-system))))
466 (d (derivation %store "the-thing"
467 s `("-e" ,b)
468 #:env-vars `(("foo" . ,(random-text)))
469 #:inputs `((,b) (,s))))
470 (o (derivation->output-path d)))
471 (and (build-derivations %store (list d))
472 (file-exists? (pk (log-file %store o)))
473 (string=? (log-file %store (derivation-file-name d))
474 (log-file %store o)))))
475
476 (test-assert "no substitutes"
477 (with-store s
478 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
479 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
480 (o (map derivation->output-path (list d1 d2))))
481 (set-build-options s #:use-substitutes? #f)
482 (and (not (has-substitutes? s (derivation-file-name d1)))
483 (not (has-substitutes? s (derivation-file-name d2)))
484 (null? (substitutable-paths s o))
485 (null? (substitutable-path-info s o))))))
486
487 (test-assert "build-things with output path"
488 (with-store s
489 (let* ((c (random-text)) ;contents of the output
490 (d (build-expression->derivation
491 s "substitute-me"
492 `(call-with-output-file %output
493 (lambda (p)
494 (display ,c p)))
495 #:guile-for-build
496 (package-derivation s %bootstrap-guile (%current-system))))
497 (o (derivation->output-path d)))
498 (set-build-options s #:use-substitutes? #f)
499
500 ;; Pass 'build-things' the output file name, O. However, since there
501 ;; are no substitutes for O, it will just do nothing.
502 (build-things s (list o))
503 (not (valid-path? s o)))))
504
505 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
506
507 (test-assert "substitute query"
508 (with-store s
509 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
510 (o (derivation->output-path d)))
511 ;; Create fake substituter data, to be read by 'guix substitute'.
512 (with-derivation-narinfo d
513 ;; Remove entry from the local cache.
514 (false-if-exception
515 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
516 "/guix/substitute")))
517
518 ;; Make sure 'guix substitute' correctly communicates the above
519 ;; data.
520 (set-build-options s #:use-substitutes? #t
521 #:substitute-urls (%test-substitute-urls))
522 (and (has-substitutes? s o)
523 (equal? (list o) (substitutable-paths s (list o)))
524 (match (pk 'spi (substitutable-path-info s (list o)))
525 (((? substitutable? s))
526 (and (string=? (substitutable-deriver s)
527 (derivation-file-name d))
528 (null? (substitutable-references s))
529 (equal? (substitutable-nar-size s) 1234)))))))))
530
531 (test-assert "substitute query, alternating URLs"
532 (let* ((d (with-store s
533 (package-derivation s %bootstrap-guile (%current-system))))
534 (o (derivation->output-path d)))
535 (with-derivation-narinfo d
536 ;; Remove entry from the local cache.
537 (false-if-exception
538 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
539 "/guix/substitute")))
540
541 ;; Note: We reconnect to the daemon to force a new instance of 'guix
542 ;; substitute' to be used; otherwise the #:substitute-urls of
543 ;; 'set-build-options' would have no effect.
544
545 (and (with-store s ;the right substitute URL
546 (set-build-options s #:use-substitutes? #t
547 #:substitute-urls (%test-substitute-urls))
548 (has-substitutes? s o))
549 (with-store s ;the wrong one
550 (set-build-options s #:use-substitutes? #t
551 #:substitute-urls (list
552 "http://does-not-exist"))
553 (not (has-substitutes? s o)))
554 (with-store s ;the right one again
555 (set-build-options s #:use-substitutes? #t
556 #:substitute-urls (%test-substitute-urls))
557 (has-substitutes? s o))
558 (with-store s ;empty list of URLs
559 (set-build-options s #:use-substitutes? #t
560 #:substitute-urls '())
561 (not (has-substitutes? s o)))))))
562
563 (test-assert "substitute"
564 (with-store s
565 (let* ((c (random-text)) ; contents of the output
566 (d (build-expression->derivation
567 s "substitute-me"
568 `(call-with-output-file %output
569 (lambda (p)
570 (exit 1) ; would actually fail
571 (display ,c p)))
572 #:guile-for-build
573 (package-derivation s %bootstrap-guile (%current-system))))
574 (o (derivation->output-path d)))
575 (with-derivation-substitute d c
576 (set-build-options s #:use-substitutes? #t
577 #:substitute-urls (%test-substitute-urls))
578 (and (has-substitutes? s o)
579 (build-derivations s (list d))
580 (equal? c (call-with-input-file o get-string-all)))))))
581
582 (test-assert "substitute + build-things with output path"
583 (with-store s
584 (let* ((c (random-text)) ;contents of the output
585 (d (build-expression->derivation
586 s "substitute-me"
587 `(call-with-output-file %output
588 (lambda (p)
589 (exit 1) ;would actually fail
590 (display ,c p)))
591 #:guile-for-build
592 (package-derivation s %bootstrap-guile (%current-system))))
593 (o (derivation->output-path d)))
594 (with-derivation-substitute d c
595 (set-build-options s #:use-substitutes? #t
596 #:substitute-urls (%test-substitute-urls))
597 (and (has-substitutes? s o)
598 (build-things s (list o)) ;give the output path
599 (valid-path? s o)
600 (equal? c (call-with-input-file o get-string-all)))))))
601
602 (test-assert "substitute, corrupt output hash"
603 ;; Tweak the substituter into installing a substitute whose hash doesn't
604 ;; match the one announced in the narinfo. The daemon must notice this and
605 ;; raise an error.
606 (with-store s
607 (let* ((c "hello, world") ; contents of the output
608 (d (build-expression->derivation
609 s "corrupt-substitute"
610 `(mkdir %output)
611 #:guile-for-build
612 (package-derivation s %bootstrap-guile (%current-system))))
613 (o (derivation->output-path d)))
614 (with-derivation-substitute d c
615 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
616
617 ;; Make sure we use 'guix substitute'.
618 (set-build-options s
619 #:use-substitutes? #t
620 #:fallback? #f
621 #:substitute-urls (%test-substitute-urls))
622 (and (has-substitutes? s o)
623 (guard (c ((store-protocol-error? c)
624 ;; XXX: the daemon writes "hash mismatch in downloaded
625 ;; path", but the actual error returned to the client
626 ;; doesn't mention that.
627 (pk 'corrupt c)
628 (not (zero? (store-protocol-error-status c)))))
629 (build-derivations s (list d))
630 #f))))))
631
632 (test-assert "substitute --fallback"
633 (with-store s
634 (let* ((t (random-text)) ; contents of the output
635 (d (build-expression->derivation
636 s "substitute-me-not"
637 `(call-with-output-file %output
638 (lambda (p)
639 (display ,t p)))
640 #:guile-for-build
641 (package-derivation s %bootstrap-guile (%current-system))))
642 (o (derivation->output-path d)))
643 ;; Create fake substituter data, to be read by 'guix substitute'.
644 (with-derivation-narinfo d
645 ;; Make sure we use 'guix substitute'.
646 (set-build-options s #:use-substitutes? #t
647 #:substitute-urls (%test-substitute-urls))
648 (and (has-substitutes? s o)
649 (guard (c ((store-protocol-error? c)
650 ;; The substituter failed as expected. Now make
651 ;; sure that #:fallback? #t works correctly.
652 (set-build-options s
653 #:use-substitutes? #t
654 #:substitute-urls
655 (%test-substitute-urls)
656 #:fallback? #t)
657 (and (build-derivations s (list d))
658 (equal? t (call-with-input-file o
659 get-string-all)))))
660 ;; Should fail.
661 (build-derivations s (list d))
662 #f))))))
663
664 (test-assert "export/import several paths"
665 (let* ((texts (unfold (cut >= <> 10)
666 (lambda _ (random-text))
667 1+
668 0))
669 (files (map (cut add-text-to-store %store "text" <>) texts))
670 (dump (call-with-bytevector-output-port
671 (cut export-paths %store files <>))))
672 (delete-paths %store files)
673 (and (every (negate file-exists?) files)
674 (let* ((source (open-bytevector-input-port dump))
675 (imported (import-paths %store source)))
676 (and (equal? imported files)
677 (every file-exists? files)
678 (equal? texts
679 (map (lambda (file)
680 (call-with-input-file file
681 get-string-all))
682 files)))))))
683
684 (test-assert "export/import paths, ensure topological order"
685 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
686 (file1 (add-text-to-store %store "foo" (random-text)
687 (list file0)))
688 (file2 (add-text-to-store %store "bar" (random-text)
689 (list file1)))
690 (files (list file1 file2))
691 (dump1 (call-with-bytevector-output-port
692 (cute export-paths %store (list file1 file2) <>)))
693 (dump2 (call-with-bytevector-output-port
694 (cute export-paths %store (list file2 file1) <>))))
695 (delete-paths %store files)
696 (and (every (negate file-exists?) files)
697 (bytevector=? dump1 dump2)
698 (let* ((source (open-bytevector-input-port dump1))
699 (imported (import-paths %store source)))
700 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
701 (and (equal? imported (list file1 file2))
702 (every file-exists? files)
703 (equal? (list file0) (references %store file1))
704 (equal? (list file1) (references %store file2)))))))
705
706 (test-assert "export/import incomplete"
707 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
708 (file1 (add-text-to-store %store "foo" (random-text)
709 (list file0)))
710 (file2 (add-text-to-store %store "bar" (random-text)
711 (list file1)))
712 (dump (call-with-bytevector-output-port
713 (cute export-paths %store (list file2) <>))))
714 (delete-paths %store (list file0 file1 file2))
715 (guard (c ((store-protocol-error? c)
716 (and (not (zero? (store-protocol-error-status c)))
717 (string-contains (store-protocol-error-message c)
718 "not valid"))))
719 ;; Here we get an exception because DUMP does not include FILE0 and
720 ;; FILE1, which are dependencies of FILE2.
721 (import-paths %store (open-bytevector-input-port dump)))))
722
723 (test-assert "export/import recursive"
724 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
725 (file1 (add-text-to-store %store "foo" (random-text)
726 (list file0)))
727 (file2 (add-text-to-store %store "bar" (random-text)
728 (list file1)))
729 (dump (call-with-bytevector-output-port
730 (cute export-paths %store (list file2) <>
731 #:recursive? #t))))
732 (delete-paths %store (list file0 file1 file2))
733 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
734 (and (equal? imported (list file0 file1 file2))
735 (every file-exists? (list file0 file1 file2))
736 (equal? (list file0) (references %store file1))
737 (equal? (list file1) (references %store file2))))))
738
739 (test-assert "write-file & export-path yield the same result"
740 ;; Here we compare 'write-file' and the daemon's own implementation.
741 ;; 'write-file' is the reference because we know it sorts file
742 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
743 ;; currently happen to be sorted as a side-effect of some unrelated
744 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
745 ;; changes there.
746 (run-with-store %store
747 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
748 (out1 -> (derivation->output-path drv1))
749 (data -> (unfold (cut >= <> 26)
750 (lambda (i)
751 (random-bytevector 128))
752 1+ 0))
753 (build
754 -> #~(begin
755 (use-modules (rnrs io ports) (srfi srfi-1))
756 (let ()
757 (define letters
758 (map (lambda (i)
759 (string
760 (integer->char
761 (+ i (char->integer #\a)))))
762 (iota 26)))
763 (define (touch file data)
764 (call-with-output-file file
765 (lambda (port)
766 (put-bytevector port data))))
767
768 (mkdir #$output)
769 (chdir #$output)
770
771 ;; The files must be different so they have
772 ;; different inode numbers, and the inode
773 ;; order must differ from the lexicographic
774 ;; order.
775 (for-each touch
776 (append (drop letters 10)
777 (take letters 10))
778 (list #$@data))
779 #t)))
780 (drv2 (gexp->derivation "bunch" build))
781 (out2 -> (derivation->output-path drv2))
782 (item-info -> (store-lift query-path-info)))
783 (mbegin %store-monad
784 (built-derivations (list drv1 drv2))
785 (foldm %store-monad
786 (lambda (item result)
787 (define ref-hash
788 (let-values (((port get) (open-sha256-port)))
789 (write-file item port)
790 (close-port port)
791 (get)))
792
793 ;; 'query-path-info' returns a hash produced by using the
794 ;; daemon's C++ 'dump' function, which is the implementation
795 ;; under test.
796 (>>= (item-info item)
797 (lambda (info)
798 (return
799 (and result
800 (bytevector=? (path-info-hash info) ref-hash))))))
801 #t
802 (list out1 out2))))
803 #:guile-for-build (%guile-for-build)))
804
805 (test-assert "import corrupt path"
806 (let* ((text (random-text))
807 (file (add-text-to-store %store "text" text))
808 (dump (call-with-bytevector-output-port
809 (cut export-paths %store (list file) <>))))
810 (delete-paths %store (list file))
811
812 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
813 ;; the file contents in DUMP, regardless of the store prefix.
814 (let* ((index #x70)
815 (byte (bytevector-u8-ref dump index)))
816 (bytevector-u8-set! dump index (logxor #xff byte)))
817
818 (and (not (file-exists? file))
819 (guard (c ((store-protocol-error? c)
820 (pk 'c c)
821 (and (not (zero? (store-protocol-error-status c)))
822 (string-contains (store-protocol-error-message c)
823 "corrupt"))))
824 (let* ((source (open-bytevector-input-port dump))
825 (imported (import-paths %store source)))
826 (pk 'corrupt-imported imported)
827 #f)))))
828
829 (test-assert "verify-store"
830 (let* ((text (random-text))
831 (file1 (add-text-to-store %store "foo" text))
832 (file2 (add-text-to-store %store "bar" (random-text)
833 (list file1))))
834 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
835 (begin
836 (delete-file file1)
837 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
838 (begin
839 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
840 ;; without actually creating the file. ;
841 (call-with-output-file file1
842 (lambda (port)
843 (display text port)))
844 (pk 'verify3 (verify-store %store)))))) ;OK again
845
846 (test-assert "verify-store + check-contents"
847 ;; XXX: This test is I/O intensive.
848 (with-store s
849 (let* ((text (random-text))
850 (drv (build-expression->derivation
851 s "corrupt"
852 `(let ((out (assoc-ref %outputs "out")))
853 (call-with-output-file out
854 (lambda (port)
855 (display ,text port)))
856 #t)
857 #:guile-for-build
858 (package-derivation s %bootstrap-guile (%current-system))))
859 (file (derivation->output-path drv)))
860 (with-derivation-substitute drv text
861 (and (build-derivations s (list drv))
862 (verify-store s #:check-contents? #t) ;should be OK
863 (begin
864 (chmod file #o644)
865 (call-with-output-file file
866 (lambda (port)
867 (display "corrupt!" port)))
868 #t)
869
870 ;; Make sure the corruption is detected. We don't test repairing
871 ;; because only "trusted" users are allowed to do it, but we
872 ;; don't expose that notion of trusted users that nix-daemon
873 ;; supports because it seems dubious and redundant with what the
874 ;; OS provides (in Nix "trusted" users have additional
875 ;; privileges, such as overriding the set of substitute URLs, but
876 ;; we instead want to allow anyone to modify them, provided
877 ;; substitutes are signed by a root-approved key.)
878 (not (verify-store s #:check-contents? #t))
879
880 ;; Delete the corrupt item to leave the store in a clean state.
881 (delete-paths s (list file)))))))
882
883 (test-assert "build-things, check mode"
884 (with-store store
885 (call-with-temporary-output-file
886 (lambda (entropy entropy-port)
887 (write (random-text) entropy-port)
888 (force-output entropy-port)
889 (let* ((drv (build-expression->derivation
890 store "non-deterministic"
891 `(begin
892 (use-modules (rnrs io ports))
893 (let ((out (assoc-ref %outputs "out")))
894 (call-with-output-file out
895 (lambda (port)
896 ;; Rely on the fact that tests do not use the
897 ;; chroot, and thus ENTROPY is readable.
898 (display (call-with-input-file ,entropy
899 get-string-all)
900 port)))
901 #t))
902 #:guile-for-build
903 (package-derivation store %bootstrap-guile (%current-system))))
904 (file (derivation->output-path drv)))
905 (and (build-things store (list (derivation-file-name drv)))
906 (begin
907 (write (random-text) entropy-port)
908 (force-output entropy-port)
909 (guard (c ((store-protocol-error? c)
910 (pk 'determinism-exception c)
911 (and (not (zero? (store-protocol-error-status c)))
912 (string-contains (store-protocol-error-message c)
913 "deterministic"))))
914 ;; This one will produce a different result. Since we're in
915 ;; 'check' mode, this must fail.
916 (build-things store (list (derivation-file-name drv))
917 (build-mode check))
918 #f))))))))
919
920 (test-assert "build multiple times"
921 (with-store store
922 ;; Ask to build twice.
923 (set-build-options store #:rounds 2 #:use-substitutes? #f)
924
925 (call-with-temporary-output-file
926 (lambda (entropy entropy-port)
927 (write (random-text) entropy-port)
928 (force-output entropy-port)
929 (let* ((drv (build-expression->derivation
930 store "non-deterministic"
931 `(begin
932 (use-modules (rnrs io ports))
933 (let ((out (assoc-ref %outputs "out")))
934 (call-with-output-file out
935 (lambda (port)
936 ;; Rely on the fact that tests do not use the
937 ;; chroot, and thus ENTROPY is accessible.
938 (display (call-with-input-file ,entropy
939 get-string-all)
940 port)
941 (call-with-output-file ,entropy
942 (lambda (port)
943 (write 'foobar port)))))
944 #t))
945 #:guile-for-build
946 (package-derivation store %bootstrap-guile (%current-system))))
947 (file (derivation->output-path drv)))
948 (guard (c ((store-protocol-error? c)
949 (pk 'multiple-build c)
950 (and (not (zero? (store-protocol-error-status c)))
951 (string-contains (store-protocol-error-message c)
952 "deterministic"))))
953 ;; This one will produce a different result on the second run.
954 (current-build-output-port (current-error-port))
955 (build-things store (list (derivation-file-name drv)))
956 #f))))))
957
958 (test-equal "store-lower"
959 "Lowered."
960 (let* ((add (store-lower text-file))
961 (file (add %store "foo" "Lowered.")))
962 (call-with-input-file file get-string-all)))
963
964 (test-equal "current-system"
965 "bar"
966 (parameterize ((%current-system "frob"))
967 (run-with-store %store
968 (mbegin %store-monad
969 (set-current-system "bar")
970 (current-system))
971 #:system "foo")))
972
973 (test-assert "query-path-info"
974 (let* ((ref (add-text-to-store %store "ref" "foo"))
975 (item (add-text-to-store %store "item" "bar" (list ref)))
976 (info (query-path-info %store item)))
977 (and (equal? (path-info-references info) (list ref))
978 (equal? (path-info-hash info)
979 (sha256
980 (string->utf8
981 (call-with-output-string (cut write-file item <>))))))))
982
983 (test-assert "path-info-deriver"
984 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
985 (s (add-to-store %store "bash" #t "sha256"
986 (search-bootstrap-binary "bash"
987 (%current-system))))
988 (d (derivation %store "the-thing"
989 s `("-e" ,b)
990 #:env-vars `(("foo" . ,(random-text)))
991 #:inputs `((,b) (,s))))
992 (o (derivation->output-path d)))
993 (and (build-derivations %store (list d))
994 (not (path-info-deriver (query-path-info %store b)))
995 (string=? (derivation-file-name d)
996 (path-info-deriver (query-path-info %store o))))))
997
998 (test-equal "build-cores"
999 (list 0 42)
1000 (with-store store
1001 (let* ((build (add-text-to-store store "build.sh"
1002 "echo $NIX_BUILD_CORES > $out"))
1003 (bash (add-to-store store "bash" #t "sha256"
1004 (search-bootstrap-binary "bash"
1005 (%current-system))))
1006 (drv1 (derivation store "the-thing" bash
1007 `("-e" ,build)
1008 #:inputs `((,bash) (,build))
1009 #:env-vars `(("x" . ,(random-text)))))
1010 (drv2 (derivation store "the-thing" bash
1011 `("-e" ,build)
1012 #:inputs `((,bash) (,build))
1013 #:env-vars `(("x" . ,(random-text))))))
1014 (and (build-derivations store (list drv1))
1015 (begin
1016 (set-build-options store #:build-cores 42)
1017 (build-derivations store (list drv2)))
1018 (list (call-with-input-file (derivation->output-path drv1)
1019 read)
1020 (call-with-input-file (derivation->output-path drv2)
1021 read))))))
1022
1023 (test-equal "multiplexed-build-output"
1024 '("Hello from first." "Hello from second.")
1025 (with-store store
1026 (let* ((build (add-text-to-store store "build.sh"
1027 "echo Hello from $NAME.; echo > $out"))
1028 (bash (add-to-store store "bash" #t "sha256"
1029 (search-bootstrap-binary "bash"
1030 (%current-system))))
1031 (drv1 (derivation store "one" bash
1032 `("-e" ,build)
1033 #:inputs `((,bash) (,build))
1034 #:env-vars `(("NAME" . "first")
1035 ("x" . ,(random-text)))))
1036 (drv2 (derivation store "two" bash
1037 `("-e" ,build)
1038 #:inputs `((,bash) (,build))
1039 #:env-vars `(("NAME" . "second")
1040 ("x" . ,(random-text))))))
1041 (set-build-options store
1042 #:print-build-trace #t
1043 #:multiplexed-build-output? #t
1044 #:max-build-jobs 10)
1045 (let ((port (open-output-string)))
1046 ;; Send the build log to PORT.
1047 (parameterize ((current-build-output-port port))
1048 (build-derivations store (list drv1 drv2)))
1049
1050 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1051 ;; traces that allow us to retrieve each builder's output (we assume
1052 ;; there's exactly one "build-output" trace for each builder, which is
1053 ;; reasonable.)
1054 (let* ((log (get-output-string port))
1055 (started (fold-matches
1056 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1057 log '() cons))
1058 (done (fold-matches
1059 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1060 log '() cons))
1061 (output (fold-matches
1062 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1063 log '() cons))
1064 (drv-pid (lambda (name)
1065 (lambda (m)
1066 (let ((drv (match:substring m 1))
1067 (pid (string->number
1068 (match:substring m 4))))
1069 (and (string-suffix? name drv) pid)))))
1070 (pid-log (lambda (pid)
1071 (lambda (m)
1072 (let ((n (string->number
1073 (match:substring m 1)))
1074 (len (string->number
1075 (match:substring m 2)))
1076 (str (match:substring m 3)))
1077 (and (= pid n)
1078 (= (string-length str) (- len 1))
1079 str)))))
1080 (pid1 (any (drv-pid "one.drv") started))
1081 (pid2 (any (drv-pid "two.drv") started)))
1082 (list (any (pid-log pid1) output)
1083 (any (pid-log pid2) output)))))))
1084
1085 (test-end "store")