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