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