daemon: Support multiplexed build output.
[jackhill/guix/guix.git] / tests / store.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 ((nix-connection-error? c)
67 (and (eq? port (nix-connection-error-file c))
68 (nix-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 ((nix-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 ((nix-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 ((nix-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 ((nix-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 ((nix-protocol-error? c) #t))
446 (build-derivations %store (list d))))))))
447 (cond-expand
448 (guile-2.2 "garbage: �lambda: λ")
449 (else "garbage: ?lambda: λ"))))
450
451 (test-assert "log-file, derivation"
452 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
453 (s (add-to-store %store "bash" #t "sha256"
454 (search-bootstrap-binary "bash"
455 (%current-system))))
456 (d (derivation %store "the-thing"
457 s `("-e" ,b)
458 #:env-vars `(("foo" . ,(random-text)))
459 #:inputs `((,b) (,s)))))
460 (and (build-derivations %store (list d))
461 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
462
463 (test-assert "log-file, output file name"
464 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
465 (s (add-to-store %store "bash" #t "sha256"
466 (search-bootstrap-binary "bash"
467 (%current-system))))
468 (d (derivation %store "the-thing"
469 s `("-e" ,b)
470 #:env-vars `(("foo" . ,(random-text)))
471 #:inputs `((,b) (,s))))
472 (o (derivation->output-path d)))
473 (and (build-derivations %store (list d))
474 (file-exists? (pk (log-file %store o)))
475 (string=? (log-file %store (derivation-file-name d))
476 (log-file %store o)))))
477
478 (test-assert "no substitutes"
479 (with-store s
480 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
481 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
482 (o (map derivation->output-path (list d1 d2))))
483 (set-build-options s #:use-substitutes? #f)
484 (and (not (has-substitutes? s (derivation-file-name d1)))
485 (not (has-substitutes? s (derivation-file-name d2)))
486 (null? (substitutable-paths s o))
487 (null? (substitutable-path-info s o))))))
488
489 (test-assert "build-things with output path"
490 (with-store s
491 (let* ((c (random-text)) ;contents of the output
492 (d (build-expression->derivation
493 s "substitute-me"
494 `(call-with-output-file %output
495 (lambda (p)
496 (display ,c p)))
497 #:guile-for-build
498 (package-derivation s %bootstrap-guile (%current-system))))
499 (o (derivation->output-path d)))
500 (set-build-options s #:use-substitutes? #f)
501
502 ;; Pass 'build-things' the output file name, O. However, since there
503 ;; are no substitutes for O, it will just do nothing.
504 (build-things s (list o))
505 (not (valid-path? s o)))))
506
507 (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
508
509 (test-assert "substitute query"
510 (with-store s
511 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
512 (o (derivation->output-path d)))
513 ;; Create fake substituter data, to be read by 'guix substitute'.
514 (with-derivation-narinfo d
515 ;; Remove entry from the local cache.
516 (false-if-exception
517 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
518 "/guix/substitute")))
519
520 ;; Make sure 'guix substitute' correctly communicates the above
521 ;; data.
522 (set-build-options s #:use-substitutes? #t
523 #:substitute-urls (%test-substitute-urls))
524 (and (has-substitutes? s o)
525 (equal? (list o) (substitutable-paths s (list o)))
526 (match (pk 'spi (substitutable-path-info s (list o)))
527 (((? substitutable? s))
528 (and (string=? (substitutable-deriver s)
529 (derivation-file-name d))
530 (null? (substitutable-references s))
531 (equal? (substitutable-nar-size s) 1234)))))))))
532
533 (test-assert "substitute query, alternating URLs"
534 (let* ((d (with-store s
535 (package-derivation s %bootstrap-guile (%current-system))))
536 (o (derivation->output-path d)))
537 (with-derivation-narinfo d
538 ;; Remove entry from the local cache.
539 (false-if-exception
540 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
541 "/guix/substitute")))
542
543 ;; Note: We reconnect to the daemon to force a new instance of 'guix
544 ;; substitute' to be used; otherwise the #:substitute-urls of
545 ;; 'set-build-options' would have no effect.
546
547 (and (with-store s ;the right substitute URL
548 (set-build-options s #:use-substitutes? #t
549 #:substitute-urls (%test-substitute-urls))
550 (has-substitutes? s o))
551 (with-store s ;the wrong one
552 (set-build-options s #:use-substitutes? #t
553 #:substitute-urls (list
554 "http://does-not-exist"))
555 (not (has-substitutes? s o)))
556 (with-store s ;the right one again
557 (set-build-options s #:use-substitutes? #t
558 #:substitute-urls (%test-substitute-urls))
559 (has-substitutes? s o))
560 (with-store s ;empty list of URLs
561 (set-build-options s #:use-substitutes? #t
562 #:substitute-urls '())
563 (not (has-substitutes? s o)))))))
564
565 (test-assert "substitute"
566 (with-store s
567 (let* ((c (random-text)) ; contents of the output
568 (d (build-expression->derivation
569 s "substitute-me"
570 `(call-with-output-file %output
571 (lambda (p)
572 (exit 1) ; would actually fail
573 (display ,c p)))
574 #:guile-for-build
575 (package-derivation s %bootstrap-guile (%current-system))))
576 (o (derivation->output-path d)))
577 (with-derivation-substitute d c
578 (set-build-options s #:use-substitutes? #t
579 #:substitute-urls (%test-substitute-urls))
580 (and (has-substitutes? s o)
581 (build-derivations s (list d))
582 (equal? c (call-with-input-file o get-string-all)))))))
583
584 (test-assert "substitute + build-things with output path"
585 (with-store s
586 (let* ((c (random-text)) ;contents of the output
587 (d (build-expression->derivation
588 s "substitute-me"
589 `(call-with-output-file %output
590 (lambda (p)
591 (exit 1) ;would actually fail
592 (display ,c p)))
593 #:guile-for-build
594 (package-derivation s %bootstrap-guile (%current-system))))
595 (o (derivation->output-path d)))
596 (with-derivation-substitute d c
597 (set-build-options s #:use-substitutes? #t
598 #:substitute-urls (%test-substitute-urls))
599 (and (has-substitutes? s o)
600 (build-things s (list o)) ;give the output path
601 (valid-path? s o)
602 (equal? c (call-with-input-file o get-string-all)))))))
603
604 (test-assert "substitute, corrupt output hash"
605 ;; Tweak the substituter into installing a substitute whose hash doesn't
606 ;; match the one announced in the narinfo. The daemon must notice this and
607 ;; raise an error.
608 (with-store s
609 (let* ((c "hello, world") ; contents of the output
610 (d (build-expression->derivation
611 s "corrupt-substitute"
612 `(mkdir %output)
613 #:guile-for-build
614 (package-derivation s %bootstrap-guile (%current-system))))
615 (o (derivation->output-path d)))
616 (with-derivation-substitute d c
617 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
618
619 ;; Make sure we use 'guix substitute'.
620 (set-build-options s
621 #:use-substitutes? #t
622 #:fallback? #f
623 #:substitute-urls (%test-substitute-urls))
624 (and (has-substitutes? s o)
625 (guard (c ((nix-protocol-error? c)
626 ;; XXX: the daemon writes "hash mismatch in downloaded
627 ;; path", but the actual error returned to the client
628 ;; doesn't mention that.
629 (pk 'corrupt c)
630 (not (zero? (nix-protocol-error-status c)))))
631 (build-derivations s (list d))
632 #f))))))
633
634 (test-assert "substitute --fallback"
635 (with-store s
636 (let* ((t (random-text)) ; contents of the output
637 (d (build-expression->derivation
638 s "substitute-me-not"
639 `(call-with-output-file %output
640 (lambda (p)
641 (display ,t p)))
642 #:guile-for-build
643 (package-derivation s %bootstrap-guile (%current-system))))
644 (o (derivation->output-path d)))
645 ;; Create fake substituter data, to be read by 'guix substitute'.
646 (with-derivation-narinfo d
647 ;; Make sure we use 'guix substitute'.
648 (set-build-options s #:use-substitutes? #t
649 #:substitute-urls (%test-substitute-urls))
650 (and (has-substitutes? s o)
651 (guard (c ((nix-protocol-error? c)
652 ;; The substituter failed as expected. Now make
653 ;; sure that #:fallback? #t works correctly.
654 (set-build-options s
655 #:use-substitutes? #t
656 #:substitute-urls
657 (%test-substitute-urls)
658 #:fallback? #t)
659 (and (build-derivations s (list d))
660 (equal? t (call-with-input-file o
661 get-string-all)))))
662 ;; Should fail.
663 (build-derivations s (list d))
664 #f))))))
665
666 (test-assert "export/import several paths"
667 (let* ((texts (unfold (cut >= <> 10)
668 (lambda _ (random-text))
669 1+
670 0))
671 (files (map (cut add-text-to-store %store "text" <>) texts))
672 (dump (call-with-bytevector-output-port
673 (cut export-paths %store files <>))))
674 (delete-paths %store files)
675 (and (every (negate file-exists?) files)
676 (let* ((source (open-bytevector-input-port dump))
677 (imported (import-paths %store source)))
678 (and (equal? imported files)
679 (every file-exists? files)
680 (equal? texts
681 (map (lambda (file)
682 (call-with-input-file file
683 get-string-all))
684 files)))))))
685
686 (test-assert "export/import paths, ensure topological order"
687 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
688 (file1 (add-text-to-store %store "foo" (random-text)
689 (list file0)))
690 (file2 (add-text-to-store %store "bar" (random-text)
691 (list file1)))
692 (files (list file1 file2))
693 (dump1 (call-with-bytevector-output-port
694 (cute export-paths %store (list file1 file2) <>)))
695 (dump2 (call-with-bytevector-output-port
696 (cute export-paths %store (list file2 file1) <>))))
697 (delete-paths %store files)
698 (and (every (negate file-exists?) files)
699 (bytevector=? dump1 dump2)
700 (let* ((source (open-bytevector-input-port dump1))
701 (imported (import-paths %store source)))
702 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
703 (and (equal? imported (list file1 file2))
704 (every file-exists? files)
705 (equal? (list file0) (references %store file1))
706 (equal? (list file1) (references %store file2)))))))
707
708 (test-assert "export/import incomplete"
709 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
710 (file1 (add-text-to-store %store "foo" (random-text)
711 (list file0)))
712 (file2 (add-text-to-store %store "bar" (random-text)
713 (list file1)))
714 (dump (call-with-bytevector-output-port
715 (cute export-paths %store (list file2) <>))))
716 (delete-paths %store (list file0 file1 file2))
717 (guard (c ((nix-protocol-error? c)
718 (and (not (zero? (nix-protocol-error-status c)))
719 (string-contains (nix-protocol-error-message c)
720 "not valid"))))
721 ;; Here we get an exception because DUMP does not include FILE0 and
722 ;; FILE1, which are dependencies of FILE2.
723 (import-paths %store (open-bytevector-input-port dump)))))
724
725 (test-assert "export/import recursive"
726 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
727 (file1 (add-text-to-store %store "foo" (random-text)
728 (list file0)))
729 (file2 (add-text-to-store %store "bar" (random-text)
730 (list file1)))
731 (dump (call-with-bytevector-output-port
732 (cute export-paths %store (list file2) <>
733 #:recursive? #t))))
734 (delete-paths %store (list file0 file1 file2))
735 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
736 (and (equal? imported (list file0 file1 file2))
737 (every file-exists? (list file0 file1 file2))
738 (equal? (list file0) (references %store file1))
739 (equal? (list file1) (references %store file2))))))
740
741 (test-assert "write-file & export-path yield the same result"
742 ;; Here we compare 'write-file' and the daemon's own implementation.
743 ;; 'write-file' is the reference because we know it sorts file
744 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
745 ;; currently happen to be sorted as a side-effect of some unrelated
746 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
747 ;; changes there.
748 (run-with-store %store
749 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
750 (out1 -> (derivation->output-path drv1))
751 (data -> (unfold (cut >= <> 26)
752 (lambda (i)
753 (random-bytevector 128))
754 1+ 0))
755 (build
756 -> #~(begin
757 (use-modules (rnrs io ports) (srfi srfi-1))
758 (let ()
759 (define letters
760 (map (lambda (i)
761 (string
762 (integer->char
763 (+ i (char->integer #\a)))))
764 (iota 26)))
765 (define (touch file data)
766 (call-with-output-file file
767 (lambda (port)
768 (put-bytevector port data))))
769
770 (mkdir #$output)
771 (chdir #$output)
772
773 ;; The files must be different so they have
774 ;; different inode numbers, and the inode
775 ;; order must differ from the lexicographic
776 ;; order.
777 (for-each touch
778 (append (drop letters 10)
779 (take letters 10))
780 (list #$@data))
781 #t)))
782 (drv2 (gexp->derivation "bunch" build))
783 (out2 -> (derivation->output-path drv2))
784 (item-info -> (store-lift query-path-info)))
785 (mbegin %store-monad
786 (built-derivations (list drv1 drv2))
787 (foldm %store-monad
788 (lambda (item result)
789 (define ref-hash
790 (let-values (((port get) (open-sha256-port)))
791 (write-file item port)
792 (close-port port)
793 (get)))
794
795 ;; 'query-path-info' returns a hash produced by using the
796 ;; daemon's C++ 'dump' function, which is the implementation
797 ;; under test.
798 (>>= (item-info item)
799 (lambda (info)
800 (return
801 (and result
802 (bytevector=? (path-info-hash info) ref-hash))))))
803 #t
804 (list out1 out2))))
805 #:guile-for-build (%guile-for-build)))
806
807 (test-assert "import corrupt path"
808 (let* ((text (random-text))
809 (file (add-text-to-store %store "text" text))
810 (dump (call-with-bytevector-output-port
811 (cut export-paths %store (list file) <>))))
812 (delete-paths %store (list file))
813
814 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
815 ;; the file contents in DUMP, regardless of the store prefix.
816 (let* ((index #x70)
817 (byte (bytevector-u8-ref dump index)))
818 (bytevector-u8-set! dump index (logxor #xff byte)))
819
820 (and (not (file-exists? file))
821 (guard (c ((nix-protocol-error? c)
822 (pk 'c c)
823 (and (not (zero? (nix-protocol-error-status c)))
824 (string-contains (nix-protocol-error-message c)
825 "corrupt"))))
826 (let* ((source (open-bytevector-input-port dump))
827 (imported (import-paths %store source)))
828 (pk 'corrupt-imported imported)
829 #f)))))
830
831 (test-assert "verify-store"
832 (let* ((text (random-text))
833 (file1 (add-text-to-store %store "foo" text))
834 (file2 (add-text-to-store %store "bar" (random-text)
835 (list file1))))
836 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
837 (begin
838 (delete-file file1)
839 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
840 (begin
841 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
842 ;; without actually creating the file. ;
843 (call-with-output-file file1
844 (lambda (port)
845 (display text port)))
846 (pk 'verify3 (verify-store %store)))))) ;OK again
847
848 (test-assert "verify-store + check-contents"
849 ;; XXX: This test is I/O intensive.
850 (with-store s
851 (let* ((text (random-text))
852 (drv (build-expression->derivation
853 s "corrupt"
854 `(let ((out (assoc-ref %outputs "out")))
855 (call-with-output-file out
856 (lambda (port)
857 (display ,text port)))
858 #t)
859 #:guile-for-build
860 (package-derivation s %bootstrap-guile (%current-system))))
861 (file (derivation->output-path drv)))
862 (with-derivation-substitute drv text
863 (and (build-derivations s (list drv))
864 (verify-store s #:check-contents? #t) ;should be OK
865 (begin
866 (chmod file #o644)
867 (call-with-output-file file
868 (lambda (port)
869 (display "corrupt!" port)))
870 #t)
871
872 ;; Make sure the corruption is detected. We don't test repairing
873 ;; because only "trusted" users are allowed to do it, but we
874 ;; don't expose that notion of trusted users that nix-daemon
875 ;; supports because it seems dubious and redundant with what the
876 ;; OS provides (in Nix "trusted" users have additional
877 ;; privileges, such as overriding the set of substitute URLs, but
878 ;; we instead want to allow anyone to modify them, provided
879 ;; substitutes are signed by a root-approved key.)
880 (not (verify-store s #:check-contents? #t))
881
882 ;; Delete the corrupt item to leave the store in a clean state.
883 (delete-paths s (list file)))))))
884
885 (test-assert "build-things, check mode"
886 (with-store store
887 (call-with-temporary-output-file
888 (lambda (entropy entropy-port)
889 (write (random-text) entropy-port)
890 (force-output entropy-port)
891 (let* ((drv (build-expression->derivation
892 store "non-deterministic"
893 `(begin
894 (use-modules (rnrs io ports))
895 (let ((out (assoc-ref %outputs "out")))
896 (call-with-output-file out
897 (lambda (port)
898 ;; Rely on the fact that tests do not use the
899 ;; chroot, and thus ENTROPY is readable.
900 (display (call-with-input-file ,entropy
901 get-string-all)
902 port)))
903 #t))
904 #:guile-for-build
905 (package-derivation store %bootstrap-guile (%current-system))))
906 (file (derivation->output-path drv)))
907 (and (build-things store (list (derivation-file-name drv)))
908 (begin
909 (write (random-text) entropy-port)
910 (force-output entropy-port)
911 (guard (c ((nix-protocol-error? c)
912 (pk 'determinism-exception c)
913 (and (not (zero? (nix-protocol-error-status c)))
914 (string-contains (nix-protocol-error-message c)
915 "deterministic"))))
916 ;; This one will produce a different result. Since we're in
917 ;; 'check' mode, this must fail.
918 (build-things store (list (derivation-file-name drv))
919 (build-mode check))
920 #f))))))))
921
922 (test-assert "build multiple times"
923 (with-store store
924 ;; Ask to build twice.
925 (set-build-options store #:rounds 2 #:use-substitutes? #f)
926
927 (call-with-temporary-output-file
928 (lambda (entropy entropy-port)
929 (write (random-text) entropy-port)
930 (force-output entropy-port)
931 (let* ((drv (build-expression->derivation
932 store "non-deterministic"
933 `(begin
934 (use-modules (rnrs io ports))
935 (let ((out (assoc-ref %outputs "out")))
936 (call-with-output-file out
937 (lambda (port)
938 ;; Rely on the fact that tests do not use the
939 ;; chroot, and thus ENTROPY is accessible.
940 (display (call-with-input-file ,entropy
941 get-string-all)
942 port)
943 (call-with-output-file ,entropy
944 (lambda (port)
945 (write 'foobar port)))))
946 #t))
947 #:guile-for-build
948 (package-derivation store %bootstrap-guile (%current-system))))
949 (file (derivation->output-path drv)))
950 (guard (c ((nix-protocol-error? c)
951 (pk 'multiple-build c)
952 (and (not (zero? (nix-protocol-error-status c)))
953 (string-contains (nix-protocol-error-message c)
954 "deterministic"))))
955 ;; This one will produce a different result on the second run.
956 (current-build-output-port (current-error-port))
957 (build-things store (list (derivation-file-name drv)))
958 #f))))))
959
960 (test-equal "store-lower"
961 "Lowered."
962 (let* ((add (store-lower text-file))
963 (file (add %store "foo" "Lowered.")))
964 (call-with-input-file file get-string-all)))
965
966 (test-equal "current-system"
967 "bar"
968 (parameterize ((%current-system "frob"))
969 (run-with-store %store
970 (mbegin %store-monad
971 (set-current-system "bar")
972 (current-system))
973 #:system "foo")))
974
975 (test-assert "query-path-info"
976 (let* ((ref (add-text-to-store %store "ref" "foo"))
977 (item (add-text-to-store %store "item" "bar" (list ref)))
978 (info (query-path-info %store item)))
979 (and (equal? (path-info-references info) (list ref))
980 (equal? (path-info-hash info)
981 (sha256
982 (string->utf8
983 (call-with-output-string (cut write-file item <>))))))))
984
985 (test-assert "path-info-deriver"
986 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
987 (s (add-to-store %store "bash" #t "sha256"
988 (search-bootstrap-binary "bash"
989 (%current-system))))
990 (d (derivation %store "the-thing"
991 s `("-e" ,b)
992 #:env-vars `(("foo" . ,(random-text)))
993 #:inputs `((,b) (,s))))
994 (o (derivation->output-path d)))
995 (and (build-derivations %store (list d))
996 (not (path-info-deriver (query-path-info %store b)))
997 (string=? (derivation-file-name d)
998 (path-info-deriver (query-path-info %store o))))))
999
1000 (test-equal "build-cores"
1001 (list 0 42)
1002 (with-store store
1003 (let* ((build (add-text-to-store store "build.sh"
1004 "echo $NIX_BUILD_CORES > $out"))
1005 (bash (add-to-store store "bash" #t "sha256"
1006 (search-bootstrap-binary "bash"
1007 (%current-system))))
1008 (drv1 (derivation store "the-thing" bash
1009 `("-e" ,build)
1010 #:inputs `((,bash) (,build))
1011 #:env-vars `(("x" . ,(random-text)))))
1012 (drv2 (derivation store "the-thing" bash
1013 `("-e" ,build)
1014 #:inputs `((,bash) (,build))
1015 #:env-vars `(("x" . ,(random-text))))))
1016 (and (build-derivations store (list drv1))
1017 (begin
1018 (set-build-options store #:build-cores 42)
1019 (build-derivations store (list drv2)))
1020 (list (call-with-input-file (derivation->output-path drv1)
1021 read)
1022 (call-with-input-file (derivation->output-path drv2)
1023 read))))))
1024
1025 (test-equal "multiplexed-build-output"
1026 '("Hello from first." "Hello from second.")
1027 (with-store store
1028 (let* ((build (add-text-to-store store "build.sh"
1029 "echo Hello from $NAME.; echo > $out"))
1030 (bash (add-to-store store "bash" #t "sha256"
1031 (search-bootstrap-binary "bash"
1032 (%current-system))))
1033 (drv1 (derivation store "one" bash
1034 `("-e" ,build)
1035 #:inputs `((,bash) (,build))
1036 #:env-vars `(("NAME" . "first")
1037 ("x" . ,(random-text)))))
1038 (drv2 (derivation store "two" bash
1039 `("-e" ,build)
1040 #:inputs `((,bash) (,build))
1041 #:env-vars `(("NAME" . "second")
1042 ("x" . ,(random-text))))))
1043 (set-build-options store
1044 #:print-build-trace #t
1045 #:multiplexed-build-output? #t
1046 #:max-build-jobs 10)
1047 (let ((port (open-output-string)))
1048 ;; Send the build log to PORT.
1049 (parameterize ((current-build-output-port port))
1050 (build-derivations store (list drv1 drv2)))
1051
1052 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1053 ;; traces that allow us to retrieve each builder's output (we assume
1054 ;; there's exactly one "build-output" trace for each builder, which is
1055 ;; reasonable.)
1056 (let* ((log (get-output-string port))
1057 (started (fold-matches
1058 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1059 log '() cons))
1060 (done (fold-matches
1061 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1062 log '() cons))
1063 (output (fold-matches
1064 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1065 log '() cons))
1066 (drv-pid (lambda (name)
1067 (lambda (m)
1068 (let ((drv (match:substring m 1))
1069 (pid (string->number
1070 (match:substring m 4))))
1071 (and (string-suffix? name drv) pid)))))
1072 (pid-log (lambda (pid)
1073 (lambda (m)
1074 (let ((n (string->number
1075 (match:substring m 1)))
1076 (len (string->number
1077 (match:substring m 2)))
1078 (str (match:substring m 3)))
1079 (and (= pid n)
1080 (= (string-length str) (- len 1))
1081 str)))))
1082 (pid1 (any (drv-pid "one.drv") started))
1083 (pid2 (any (drv-pid "two.drv") started)))
1084 (list (any (pid-log pid1) output)
1085 (any (pid-log pid2) output)))))))
1086
1087 (test-end "store")