gnu: Add emacs-haskell-snippets.
[jackhill/guix/guix.git] / tests / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
041b340d 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3259877d 3;;;
233e7676 4;;; This file is part of GNU Guix.
3259877d 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
3259877d
LC
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
3259877d
LC
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
3259877d 18
3259877d 19(define-module (test-store)
c1bc358f 20 #:use-module (guix tests)
81c580c8 21 #:use-module (guix config)
3259877d
LC
22 #:use-module (guix store)
23 #:use-module (guix utils)
320ca999 24 #:use-module (guix monads)
ca719424 25 #:use-module (gcrypt hash)
3259877d 26 #:use-module (guix base32)
0f3d2504
LC
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
0363991a 29 #:use-module (guix serialization)
895d1eda 30 #:use-module (guix build utils)
ce72c780 31 #:use-module (guix gexp)
fae31edc 32 #:use-module (gnu packages)
1ffa7090 33 #:use-module (gnu packages bootstrap)
3259877d 34 #:use-module (ice-9 match)
6ef61cc4 35 #:use-module (ice-9 regex)
526382ff 36 #:use-module (rnrs bytevectors)
fe0cff14 37 #:use-module (rnrs io ports)
f65cf81a 38 #:use-module (web uri)
3259877d
LC
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
526382ff 41 #:use-module (srfi srfi-26)
c3eb878f 42 #:use-module (srfi srfi-34)
3259877d
LC
43 #:use-module (srfi srfi-64))
44
45;; Test the (guix store) module.
46
47(define %store
c1bc358f 48 (open-connection-for-tests))
3259877d 49
3d430170
LC
50(define %shell
51 (or (getenv "SHELL") (getenv "CONFIG_SHELL")))
52
3259877d
LC
53\f
54(test-begin "store")
55
1397b422
LC
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
13d5e8da
LC
64(test-equal "connection handshake error"
65 EPROTO
66 (let ((port (%make-void-port "rw")))
f9e8a123
LC
67 (guard (c ((store-connection-error? c)
68 (and (eq? port (store-connection-error-file c))
69 (store-connection-error-code c))))
13d5e8da
LC
70 (open-connection #f #:port port)
71 'broken)))
72
2c6ab6cc
LC
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
c61a5b4a
LC
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
9336e5b5
LC
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)
eee21271
LC
103 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
104 (not (direct-store-path? (%store-prefix)))))
9336e5b5 105
81c580c8
LC
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))))
e297d8fc 117
0d268c5d
LC
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
e297d8fc
LC
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
f9e8a123 134 (guard (c ((store-protocol-error? c) #t))
e297d8fc
LC
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)
f9e8a123 147 (guard (c ((store-protocol-error? c) #t))
e297d8fc
LC
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"))))))
11e7a6cf
LC
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)))
3259877d
LC
159
160(test-assert "dead-paths"
cfbf9160 161 (let ((p (add-text-to-store %store "random-text" (random-text))))
5f1f10c5 162 (->bool (member p (dead-paths %store)))))
3259877d
LC
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;; '()))
a987d2c0
LC
172;; (d1 (derivation %store "link"
173;; "/bin/sh" `("-e" ,b)
174;; #:inputs `((,b) (,p1))))
59688fc4 175;; (p2 (derivation->output-path d1)))
3259877d
LC
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
a9d2a105
LC
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
3259877d
LC
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))
953c2de7
MB
198 ;; XXX: On some file systems (notably Btrfs), freed
199 ;; may return 0. See <https://bugs.gnu.org/29363>.
200 ;;(> freed 0)
3259877d
LC
201 (not (file-exists? p))))))
202
000c59b6
LC
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
7f11efba
LC
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)
3d430170
LC
238 ("./foo/c/q/x" regular
239 ,(string-append "#!" %shell "\nexit 42"))
7f11efba
LC
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
3d430170 250 (data ,(string-append "#!" %shell "\nexit 42")))
7f11efba
LC
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
fae31edc
LC
275(test-assert "references"
276 (let* ((t1 (add-text-to-store %store "random1"
cfbf9160 277 (random-text)))
fae31edc
LC
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
6581ec9a
LC
285(test-assert "references/substitutes missing reference info"
286 (with-store s
287 (set-build-options s #:use-substitutes? #f)
f9e8a123 288 (guard (c ((store-protocol-error? c) #t))
6581ec9a
LC
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)))))
151afd84
LC
294 (references/substitutes s (list (derivation->output-path d) b))
295 #f))))
6581ec9a
LC
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
151afd84
LC
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
3f1e6939
LC
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
f6fee16e
LC
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)))))
3f1e6939 366
fae31edc
LC
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))))
a987d2c0
LC
372 (d (derivation %store "the-thing"
373 s `("-e" ,b)
374 #:env-vars `(("foo" . ,(random-text)))
375 #:inputs `((,b) (,s))))
59688fc4 376 (o (derivation->output-path d)))
fae31edc 377 (and (build-derivations %store (list d))
59688fc4 378 (equal? (query-derivation-outputs %store (derivation-file-name d))
fae31edc
LC
379 (list o))
380 (equal? (valid-derivers %store o)
59688fc4 381 (list (derivation-file-name d))))))
fae31edc 382
041b340d
LC
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
c40bf581
LC
415(test-assert "map/accumulate-builds"
416 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
417 (s (add-to-store %store "bash" #t "sha256"
418 (search-bootstrap-binary "bash"
419 (%current-system))))
420 (d1 (derivation %store "the-thing"
421 s `("-e" ,b)
422 #:env-vars `(("foo" . ,(random-text)))
423 #:sources (list b s)))
424 (d2 (derivation %store "the-thing"
425 s `("-e" ,b)
426 #:env-vars `(("foo" . ,(random-text))
427 ("bar" . "baz"))
428 #:sources (list b s))))
429 (with-build-handler (lambda (continue store things mode)
430 (equal? (map derivation-file-name (list d1 d2))
431 things))
432 (map/accumulate-builds %store
433 (lambda (drv)
434 (build-derivations %store (list drv))
435 (add-to-store %store "content-addressed"
436 #t "sha256"
437 (derivation->output-path drv)))
438 (list d1 d2)))))
439
440(test-assert "mapm/accumulate-builds"
441 (let* ((d1 (run-with-store %store
442 (gexp->derivation "foo" #~(mkdir #$output))))
443 (d2 (run-with-store %store
444 (gexp->derivation "bar" #~(mkdir #$output)))))
445 (with-build-handler (lambda (continue store things mode)
446 (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
447 (pk 'XX things)))
448 (run-with-store %store
449 (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
450
50add477
LC
451(test-assert "topologically-sorted, one item"
452 (let* ((a (add-text-to-store %store "a" "a"))
453 (b (add-text-to-store %store "b" "b" (list a)))
454 (c (add-text-to-store %store "c" "c" (list b)))
455 (d (add-text-to-store %store "d" "d" (list c)))
456 (s (topologically-sorted %store (list d))))
457 (equal? s (list a b c d))))
458
459(test-assert "topologically-sorted, several items"
460 (let* ((a (add-text-to-store %store "a" "a"))
461 (b (add-text-to-store %store "b" "b" (list a)))
462 (c (add-text-to-store %store "c" "c" (list b)))
463 (d (add-text-to-store %store "d" "d" (list c)))
464 (s1 (topologically-sorted %store (list d a c b)))
465 (s2 (topologically-sorted %store (list b d c a b d))))
466 (equal? s1 s2 (list a b c d))))
467
468(test-assert "topologically-sorted, more difficult"
469 (let* ((a (add-text-to-store %store "a" "a"))
470 (b (add-text-to-store %store "b" "b" (list a)))
471 (c (add-text-to-store %store "c" "c" (list b)))
472 (d (add-text-to-store %store "d" "d" (list c)))
473 (w (add-text-to-store %store "w" "w"))
474 (x (add-text-to-store %store "x" "x" (list w)))
475 (y (add-text-to-store %store "y" "y" (list x d)))
476 (s1 (topologically-sorted %store (list y)))
477 (s2 (topologically-sorted %store (list c y)))
478 (s3 (topologically-sorted %store (cons y (references %store y)))))
58cbbe4b
LC
479 ;; The order in which 'references' returns the references of Y is
480 ;; unspecified, so accommodate.
481 (let* ((x-then-d? (equal? (references %store y) (list x d))))
482 (and (equal? s1
483 (if x-then-d?
484 (list w x a b c d y)
485 (list a b c d w x y)))
486 (equal? s2
487 (if x-then-d?
488 (list a b c w x d y)
489 (list a b c d w x y)))
490 (lset= string=? s1 s3)))))
50add477 491
ce72c780
LC
492(test-assert "current-build-output-port, UTF-8"
493 ;; Are UTF-8 strings in the build log properly interpreted?
494 (string-contains
495 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
496 (call-with-output-string
497 (lambda (port)
498 (parameterize ((current-build-output-port port))
499 (let* ((s "Here’s a Greek letter: λ.")
500 (d (build-expression->derivation
501 %store "foo" `(display ,s)
502 #:guile-for-build
503 (package-derivation s %bootstrap-guile (%current-system)))))
f9e8a123 504 (guard (c ((store-protocol-error? c) #t))
ce72c780
LC
505 (build-derivations %store (list d))))))))
506 "Here’s a Greek letter: λ."))
507
508(test-assert "current-build-output-port, UTF-8 + garbage"
509 ;; What about a mixture of UTF-8 + garbage?
510 (string-contains
511 (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
512 (call-with-output-string
513 (lambda (port)
514 (parameterize ((current-build-output-port port))
515 (let ((d (build-expression->derivation
516 %store "foo"
517 `(begin
518 (use-modules (rnrs io ports))
519 (display "garbage: ")
520 (put-bytevector (current-output-port) #vu8(128))
521 (display "lambda: λ\n"))
522 #:guile-for-build
523 (package-derivation %store %bootstrap-guile))))
f9e8a123 524 (guard (c ((store-protocol-error? c) #t))
ce72c780 525 (build-derivations %store (list d))))))))
a65177a6 526 "garbage: �lambda: λ"))
ce72c780 527
eddd4077
LC
528(test-assert "log-file, derivation"
529 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
530 (s (add-to-store %store "bash" #t "sha256"
531 (search-bootstrap-binary "bash"
532 (%current-system))))
533 (d (derivation %store "the-thing"
534 s `("-e" ,b)
535 #:env-vars `(("foo" . ,(random-text)))
536 #:inputs `((,b) (,s)))))
537 (and (build-derivations %store (list d))
538 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
539
540(test-assert "log-file, output file name"
541 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
542 (s (add-to-store %store "bash" #t "sha256"
543 (search-bootstrap-binary "bash"
544 (%current-system))))
545 (d (derivation %store "the-thing"
546 s `("-e" ,b)
547 #:env-vars `(("foo" . ,(random-text)))
548 #:inputs `((,b) (,s))))
549 (o (derivation->output-path d)))
550 (and (build-derivations %store (list d))
551 (file-exists? (pk (log-file %store o)))
552 (string=? (log-file %store (derivation-file-name d))
553 (log-file %store o)))))
554
0f3d2504 555(test-assert "no substitutes"
2d53df66
LC
556 (with-store s
557 (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
558 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
559 (o (map derivation->output-path (list d1 d2))))
560 (set-build-options s #:use-substitutes? #f)
561 (and (not (has-substitutes? s (derivation-file-name d1)))
562 (not (has-substitutes? s (derivation-file-name d2)))
563 (null? (substitutable-paths s o))
564 (null? (substitutable-path-info s o))))))
0f3d2504 565
abac874b
LC
566(test-assert "build-things with output path"
567 (with-store s
568 (let* ((c (random-text)) ;contents of the output
569 (d (build-expression->derivation
570 s "substitute-me"
571 `(call-with-output-file %output
572 (lambda (p)
573 (display ,c p)))
574 #:guile-for-build
575 (package-derivation s %bootstrap-guile (%current-system))))
576 (o (derivation->output-path d)))
577 (set-build-options s #:use-substitutes? #f)
578
579 ;; Pass 'build-things' the output file name, O. However, since there
580 ;; are no substitutes for O, it will just do nothing.
581 (build-things s (list o))
582 (not (valid-path? s o)))))
583
f65cf81a
LC
584(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
585
586(test-assert "substitute query"
2d53df66 587 (with-store s
6eebbab5
LC
588 (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
589 (o (derivation->output-path d)))
2c74fde0 590 ;; Create fake substituter data, to be read by 'guix substitute'.
6eebbab5
LC
591 (with-derivation-narinfo d
592 ;; Remove entry from the local cache.
593 (false-if-exception
895d1eda
LC
594 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
595 "/guix/substitute")))
6eebbab5 596
2c74fde0 597 ;; Make sure 'guix substitute' correctly communicates the above
6eebbab5 598 ;; data.
24f5aaaf
LC
599 (set-build-options s #:use-substitutes? #t
600 #:substitute-urls (%test-substitute-urls))
6eebbab5
LC
601 (and (has-substitutes? s o)
602 (equal? (list o) (substitutable-paths s (list o)))
603 (match (pk 'spi (substitutable-path-info s (list o)))
604 (((? substitutable? s))
605 (and (string=? (substitutable-deriver s)
606 (derivation-file-name d))
607 (null? (substitutable-references s))
608 (equal? (substitutable-nar-size s) 1234)))))))))
f65cf81a 609
24f5aaaf
LC
610(test-assert "substitute query, alternating URLs"
611 (let* ((d (with-store s
612 (package-derivation s %bootstrap-guile (%current-system))))
613 (o (derivation->output-path d)))
614 (with-derivation-narinfo d
615 ;; Remove entry from the local cache.
616 (false-if-exception
617 (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
618 "/guix/substitute")))
619
620 ;; Note: We reconnect to the daemon to force a new instance of 'guix
621 ;; substitute' to be used; otherwise the #:substitute-urls of
622 ;; 'set-build-options' would have no effect.
623
624 (and (with-store s ;the right substitute URL
625 (set-build-options s #:use-substitutes? #t
626 #:substitute-urls (%test-substitute-urls))
627 (has-substitutes? s o))
628 (with-store s ;the wrong one
629 (set-build-options s #:use-substitutes? #t
630 #:substitute-urls (list
631 "http://does-not-exist"))
632 (not (has-substitutes? s o)))
633 (with-store s ;the right one again
634 (set-build-options s #:use-substitutes? #t
635 #:substitute-urls (%test-substitute-urls))
71e2065a
LC
636 (has-substitutes? s o))
637 (with-store s ;empty list of URLs
638 (set-build-options s #:use-substitutes? #t
639 #:substitute-urls '())
640 (not (has-substitutes? s o)))))))
24f5aaaf 641
fe0cff14 642(test-assert "substitute"
2d53df66
LC
643 (with-store s
644 (let* ((c (random-text)) ; contents of the output
645 (d (build-expression->derivation
646 s "substitute-me"
647 `(call-with-output-file %output
648 (lambda (p)
649 (exit 1) ; would actually fail
650 (display ,c p)))
651 #:guile-for-build
652 (package-derivation s %bootstrap-guile (%current-system))))
e6c8839c
LC
653 (o (derivation->output-path d)))
654 (with-derivation-substitute d c
24f5aaaf
LC
655 (set-build-options s #:use-substitutes? #t
656 #:substitute-urls (%test-substitute-urls))
e6c8839c
LC
657 (and (has-substitutes? s o)
658 (build-derivations s (list d))
659 (equal? c (call-with-input-file o get-string-all)))))))
fe0cff14 660
abac874b
LC
661(test-assert "substitute + build-things with output path"
662 (with-store s
663 (let* ((c (random-text)) ;contents of the output
664 (d (build-expression->derivation
665 s "substitute-me"
666 `(call-with-output-file %output
667 (lambda (p)
668 (exit 1) ;would actually fail
669 (display ,c p)))
670 #:guile-for-build
671 (package-derivation s %bootstrap-guile (%current-system))))
672 (o (derivation->output-path d)))
673 (with-derivation-substitute d c
24f5aaaf
LC
674 (set-build-options s #:use-substitutes? #t
675 #:substitute-urls (%test-substitute-urls))
abac874b
LC
676 (and (has-substitutes? s o)
677 (build-things s (list o)) ;give the output path
678 (valid-path? s o)
679 (equal? c (call-with-input-file o get-string-all)))))))
680
f8a9f99c
LC
681(test-assert "substitute + build-things with specific output"
682 (with-store s
683 (let* ((c (random-text)) ;contents of the output
684 (d (build-expression->derivation
685 s "substitute-me" `(begin ,c (exit 1)) ;would fail
686 #:outputs '("out" "one" "two")
687 #:guile-for-build
688 (package-derivation s %bootstrap-guile (%current-system))))
689 (o (derivation->output-path d)))
690 (with-derivation-substitute d c
691 (set-build-options s #:use-substitutes? #t
692 #:substitute-urls (%test-substitute-urls))
693 (and (has-substitutes? s o)
694
695 ;; Ask for nothing but the "out" output of D.
696 (build-things s `((,(derivation-file-name d) . "out")))
697
698 (valid-path? s o)
699 (equal? c (call-with-input-file o get-string-all)))))))
700
491e6de7
LC
701(test-assert "substitute, corrupt output hash"
702 ;; Tweak the substituter into installing a substitute whose hash doesn't
703 ;; match the one announced in the narinfo. The daemon must notice this and
704 ;; raise an error.
2d53df66
LC
705 (with-store s
706 (let* ((c "hello, world") ; contents of the output
707 (d (build-expression->derivation
708 s "corrupt-substitute"
709 `(mkdir %output)
710 #:guile-for-build
711 (package-derivation s %bootstrap-guile (%current-system))))
e6c8839c
LC
712 (o (derivation->output-path d)))
713 (with-derivation-substitute d c
714 (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
715
2c74fde0 716 ;; Make sure we use 'guix substitute'.
e6c8839c
LC
717 (set-build-options s
718 #:use-substitutes? #t
24f5aaaf
LC
719 #:fallback? #f
720 #:substitute-urls (%test-substitute-urls))
e6c8839c 721 (and (has-substitutes? s o)
f9e8a123 722 (guard (c ((store-protocol-error? c)
e6c8839c
LC
723 ;; XXX: the daemon writes "hash mismatch in downloaded
724 ;; path", but the actual error returned to the client
725 ;; doesn't mention that.
726 (pk 'corrupt c)
f9e8a123 727 (not (zero? (store-protocol-error-status c)))))
e6c8839c
LC
728 (build-derivations s (list d))
729 #f))))))
491e6de7 730
c3eb878f 731(test-assert "substitute --fallback"
2d53df66 732 (with-store s
6eebbab5 733 (let* ((t (random-text)) ; contents of the output
2d53df66
LC
734 (d (build-expression->derivation
735 s "substitute-me-not"
736 `(call-with-output-file %output
737 (lambda (p)
738 (display ,t p)))
739 #:guile-for-build
740 (package-derivation s %bootstrap-guile (%current-system))))
6eebbab5 741 (o (derivation->output-path d)))
2c74fde0 742 ;; Create fake substituter data, to be read by 'guix substitute'.
6eebbab5 743 (with-derivation-narinfo d
2c74fde0 744 ;; Make sure we use 'guix substitute'.
24f5aaaf
LC
745 (set-build-options s #:use-substitutes? #t
746 #:substitute-urls (%test-substitute-urls))
6eebbab5 747 (and (has-substitutes? s o)
f9e8a123 748 (guard (c ((store-protocol-error? c)
6eebbab5
LC
749 ;; The substituter failed as expected. Now make
750 ;; sure that #:fallback? #t works correctly.
751 (set-build-options s
752 #:use-substitutes? #t
24f5aaaf
LC
753 #:substitute-urls
754 (%test-substitute-urls)
6eebbab5
LC
755 #:fallback? #t)
756 (and (build-derivations s (list d))
757 (equal? t (call-with-input-file o
758 get-string-all)))))
759 ;; Should fail.
760 (build-derivations s (list d))
761 #f))))))
c3eb878f 762
526382ff
LC
763(test-assert "export/import several paths"
764 (let* ((texts (unfold (cut >= <> 10)
765 (lambda _ (random-text))
766 1+
767 0))
768 (files (map (cut add-text-to-store %store "text" <>) texts))
769 (dump (call-with-bytevector-output-port
770 (cut export-paths %store files <>))))
771 (delete-paths %store files)
772 (and (every (negate file-exists?) files)
773 (let* ((source (open-bytevector-input-port dump))
774 (imported (import-paths %store source)))
775 (and (equal? imported files)
776 (every file-exists? files)
777 (equal? texts
778 (map (lambda (file)
779 (call-with-input-file file
780 get-string-all))
781 files)))))))
782
99fbddf9 783(test-assert "export/import paths, ensure topological order"
cafb92d8
LC
784 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
785 (file1 (add-text-to-store %store "foo" (random-text)
786 (list file0)))
99fbddf9
LC
787 (file2 (add-text-to-store %store "bar" (random-text)
788 (list file1)))
789 (files (list file1 file2))
790 (dump1 (call-with-bytevector-output-port
791 (cute export-paths %store (list file1 file2) <>)))
792 (dump2 (call-with-bytevector-output-port
793 (cute export-paths %store (list file2 file1) <>))))
794 (delete-paths %store files)
795 (and (every (negate file-exists?) files)
796 (bytevector=? dump1 dump2)
797 (let* ((source (open-bytevector-input-port dump1))
798 (imported (import-paths %store source)))
cafb92d8 799 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
99fbddf9
LC
800 (and (equal? imported (list file1 file2))
801 (every file-exists? files)
cafb92d8 802 (equal? (list file0) (references %store file1))
99fbddf9
LC
803 (equal? (list file1) (references %store file2)))))))
804
5b3d863f
LC
805(test-assert "export/import incomplete"
806 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
807 (file1 (add-text-to-store %store "foo" (random-text)
808 (list file0)))
809 (file2 (add-text-to-store %store "bar" (random-text)
810 (list file1)))
811 (dump (call-with-bytevector-output-port
812 (cute export-paths %store (list file2) <>))))
813 (delete-paths %store (list file0 file1 file2))
f9e8a123
LC
814 (guard (c ((store-protocol-error? c)
815 (and (not (zero? (store-protocol-error-status c)))
816 (string-contains (store-protocol-error-message c)
5b3d863f
LC
817 "not valid"))))
818 ;; Here we get an exception because DUMP does not include FILE0 and
819 ;; FILE1, which are dependencies of FILE2.
820 (import-paths %store (open-bytevector-input-port dump)))))
821
822(test-assert "export/import recursive"
823 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
824 (file1 (add-text-to-store %store "foo" (random-text)
825 (list file0)))
826 (file2 (add-text-to-store %store "bar" (random-text)
827 (list file1)))
828 (dump (call-with-bytevector-output-port
829 (cute export-paths %store (list file2) <>
830 #:recursive? #t))))
831 (delete-paths %store (list file0 file1 file2))
832 (let ((imported (import-paths %store (open-bytevector-input-port dump))))
833 (and (equal? imported (list file0 file1 file2))
834 (every file-exists? (list file0 file1 file2))
835 (equal? (list file0) (references %store file1))
836 (equal? (list file1) (references %store file2))))))
837
320ca999
LC
838(test-assert "write-file & export-path yield the same result"
839 ;; Here we compare 'write-file' and the daemon's own implementation.
840 ;; 'write-file' is the reference because we know it sorts file
841 ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
842 ;; currently happen to be sorted as a side-effect of some unrelated
843 ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
844 ;; changes there.
845 (run-with-store %store
846 (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
847 (out1 -> (derivation->output-path drv1))
848 (data -> (unfold (cut >= <> 26)
849 (lambda (i)
850 (random-bytevector 128))
851 1+ 0))
852 (build
853 -> #~(begin
854 (use-modules (rnrs io ports) (srfi srfi-1))
855 (let ()
856 (define letters
857 (map (lambda (i)
858 (string
859 (integer->char
860 (+ i (char->integer #\a)))))
861 (iota 26)))
862 (define (touch file data)
863 (call-with-output-file file
864 (lambda (port)
865 (put-bytevector port data))))
866
867 (mkdir #$output)
868 (chdir #$output)
869
870 ;; The files must be different so they have
871 ;; different inode numbers, and the inode
872 ;; order must differ from the lexicographic
873 ;; order.
874 (for-each touch
875 (append (drop letters 10)
876 (take letters 10))
877 (list #$@data))
878 #t)))
879 (drv2 (gexp->derivation "bunch" build))
880 (out2 -> (derivation->output-path drv2))
881 (item-info -> (store-lift query-path-info)))
882 (mbegin %store-monad
883 (built-derivations (list drv1 drv2))
884 (foldm %store-monad
885 (lambda (item result)
886 (define ref-hash
887 (let-values (((port get) (open-sha256-port)))
888 (write-file item port)
889 (close-port port)
890 (get)))
891
892 ;; 'query-path-info' returns a hash produced by using the
893 ;; daemon's C++ 'dump' function, which is the implementation
894 ;; under test.
895 (>>= (item-info item)
896 (lambda (info)
897 (return
898 (and result
899 (bytevector=? (path-info-hash info) ref-hash))))))
900 #t
901 (list out1 out2))))
902 #:guile-for-build (%guile-for-build)))
903
526382ff
LC
904(test-assert "import corrupt path"
905 (let* ((text (random-text))
906 (file (add-text-to-store %store "text" text))
907 (dump (call-with-bytevector-output-port
908 (cut export-paths %store (list file) <>))))
909 (delete-paths %store (list file))
910
1a0adad8
LC
911 ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
912 ;; the file contents in DUMP, regardless of the store prefix.
913 (let* ((index #x70)
526382ff
LC
914 (byte (bytevector-u8-ref dump index)))
915 (bytevector-u8-set! dump index (logxor #xff byte)))
916
917 (and (not (file-exists? file))
f9e8a123 918 (guard (c ((store-protocol-error? c)
526382ff 919 (pk 'c c)
f9e8a123
LC
920 (and (not (zero? (store-protocol-error-status c)))
921 (string-contains (store-protocol-error-message c)
526382ff
LC
922 "corrupt"))))
923 (let* ((source (open-bytevector-input-port dump))
924 (imported (import-paths %store source)))
925 (pk 'corrupt-imported imported)
926 #f)))))
927
c63d9403
LC
928(test-assert "verify-store"
929 (let* ((text (random-text))
930 (file1 (add-text-to-store %store "foo" text))
931 (file2 (add-text-to-store %store "bar" (random-text)
932 (list file1))))
933 (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
934 (begin
935 (delete-file file1)
936 (not (pk 'verify2 (verify-store %store)))) ;bad! ;
937 (begin
938 ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
939 ;; without actually creating the file. ;
940 (call-with-output-file file1
941 (lambda (port)
942 (display text port)))
943 (pk 'verify3 (verify-store %store)))))) ;OK again
944
945(test-assert "verify-store + check-contents"
946 ;; XXX: This test is I/O intensive.
947 (with-store s
948 (let* ((text (random-text))
949 (drv (build-expression->derivation
950 s "corrupt"
951 `(let ((out (assoc-ref %outputs "out")))
952 (call-with-output-file out
953 (lambda (port)
954 (display ,text port)))
955 #t)
956 #:guile-for-build
957 (package-derivation s %bootstrap-guile (%current-system))))
958 (file (derivation->output-path drv)))
959 (with-derivation-substitute drv text
960 (and (build-derivations s (list drv))
961 (verify-store s #:check-contents? #t) ;should be OK
962 (begin
963 (chmod file #o644)
964 (call-with-output-file file
965 (lambda (port)
966 (display "corrupt!" port)))
967 #t)
968
969 ;; Make sure the corruption is detected. We don't test repairing
970 ;; because only "trusted" users are allowed to do it, but we
971 ;; don't expose that notion of trusted users that nix-daemon
972 ;; supports because it seems dubious and redundant with what the
973 ;; OS provides (in Nix "trusted" users have additional
974 ;; privileges, such as overriding the set of substitute URLs, but
975 ;; we instead want to allow anyone to modify them, provided
976 ;; substitutes are signed by a root-approved key.)
977 (not (verify-store s #:check-contents? #t))
978
979 ;; Delete the corrupt item to leave the store in a clean state.
980 (delete-paths s (list file)))))))
981
07e70f48
LC
982(test-assert "build-things, check mode"
983 (with-store store
984 (call-with-temporary-output-file
985 (lambda (entropy entropy-port)
986 (write (random-text) entropy-port)
987 (force-output entropy-port)
988 (let* ((drv (build-expression->derivation
989 store "non-deterministic"
990 `(begin
991 (use-modules (rnrs io ports))
992 (let ((out (assoc-ref %outputs "out")))
993 (call-with-output-file out
994 (lambda (port)
2fba87ac
LC
995 ;; Rely on the fact that tests do not use the
996 ;; chroot, and thus ENTROPY is readable.
07e70f48
LC
997 (display (call-with-input-file ,entropy
998 get-string-all)
999 port)))
1000 #t))
1001 #:guile-for-build
1002 (package-derivation store %bootstrap-guile (%current-system))))
1003 (file (derivation->output-path drv)))
1004 (and (build-things store (list (derivation-file-name drv)))
1005 (begin
1006 (write (random-text) entropy-port)
1007 (force-output entropy-port)
f9e8a123 1008 (guard (c ((store-protocol-error? c)
07e70f48 1009 (pk 'determinism-exception c)
f9e8a123
LC
1010 (and (not (zero? (store-protocol-error-status c)))
1011 (string-contains (store-protocol-error-message c)
07e70f48
LC
1012 "deterministic"))))
1013 ;; This one will produce a different result. Since we're in
1014 ;; 'check' mode, this must fail.
1015 (build-things store (list (derivation-file-name drv))
1016 (build-mode check))
1017 #f))))))))
1018
d5912428
LC
1019(test-assert "build-succeeded trace in check mode"
1020 (string-contains
1021 (call-with-output-string
1022 (lambda (port)
1023 (let ((d (build-expression->derivation
1024 %store "foo" '(mkdir (assoc-ref %outputs "out"))
1025 #:guile-for-build
1026 (package-derivation %store %bootstrap-guile))))
1027 (build-derivations %store (list d))
1028 (parameterize ((current-build-output-port port))
1029 (build-derivations %store (list d) (build-mode check))))))
1030 "@ build-succeeded"))
1031
2fba87ac
LC
1032(test-assert "build multiple times"
1033 (with-store store
1034 ;; Ask to build twice.
1035 (set-build-options store #:rounds 2 #:use-substitutes? #f)
1036
1037 (call-with-temporary-output-file
1038 (lambda (entropy entropy-port)
1039 (write (random-text) entropy-port)
1040 (force-output entropy-port)
1041 (let* ((drv (build-expression->derivation
1042 store "non-deterministic"
1043 `(begin
1044 (use-modules (rnrs io ports))
1045 (let ((out (assoc-ref %outputs "out")))
1046 (call-with-output-file out
1047 (lambda (port)
1048 ;; Rely on the fact that tests do not use the
1049 ;; chroot, and thus ENTROPY is accessible.
1050 (display (call-with-input-file ,entropy
1051 get-string-all)
1052 port)
1053 (call-with-output-file ,entropy
1054 (lambda (port)
1055 (write 'foobar port)))))
1056 #t))
1057 #:guile-for-build
1058 (package-derivation store %bootstrap-guile (%current-system))))
1059 (file (derivation->output-path drv)))
f9e8a123 1060 (guard (c ((store-protocol-error? c)
2fba87ac 1061 (pk 'multiple-build c)
f9e8a123
LC
1062 (and (not (zero? (store-protocol-error-status c)))
1063 (string-contains (store-protocol-error-message c)
2fba87ac
LC
1064 "deterministic"))))
1065 ;; This one will produce a different result on the second run.
1066 (current-build-output-port (current-error-port))
1067 (build-things store (list (derivation-file-name drv)))
1068 #f))))))
1069
023d9892
LC
1070(test-equal "store-lower"
1071 "Lowered."
1072 (let* ((add (store-lower text-file))
1073 (file (add %store "foo" "Lowered.")))
1074 (call-with-input-file file get-string-all)))
1075
98a7b528
LC
1076(test-equal "current-system"
1077 "bar"
1078 (parameterize ((%current-system "frob"))
1079 (run-with-store %store
1080 (mbegin %store-monad
1081 (set-current-system "bar")
1082 (current-system))
1083 #:system "foo")))
1084
533d1768
DT
1085(test-assert "query-path-info"
1086 (let* ((ref (add-text-to-store %store "ref" "foo"))
1087 (item (add-text-to-store %store "item" "bar" (list ref)))
1088 (info (query-path-info %store item)))
1089 (and (equal? (path-info-references info) (list ref))
1090 (equal? (path-info-hash info)
1091 (sha256
1092 (string->utf8
1093 (call-with-output-string (cut write-file item <>))))))))
1094
22572d56
LC
1095(test-assert "path-info-deriver"
1096 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
1097 (s (add-to-store %store "bash" #t "sha256"
1098 (search-bootstrap-binary "bash"
1099 (%current-system))))
1100 (d (derivation %store "the-thing"
1101 s `("-e" ,b)
1102 #:env-vars `(("foo" . ,(random-text)))
1103 #:inputs `((,b) (,s))))
1104 (o (derivation->output-path d)))
1105 (and (build-derivations %store (list d))
1106 (not (path-info-deriver (query-path-info %store b)))
1107 (string=? (derivation-file-name d)
1108 (path-info-deriver (query-path-info %store o))))))
1109
deac976d
LC
1110(test-equal "build-cores"
1111 (list 0 42)
1112 (with-store store
1113 (let* ((build (add-text-to-store store "build.sh"
1114 "echo $NIX_BUILD_CORES > $out"))
1115 (bash (add-to-store store "bash" #t "sha256"
1116 (search-bootstrap-binary "bash"
1117 (%current-system))))
1118 (drv1 (derivation store "the-thing" bash
1119 `("-e" ,build)
1120 #:inputs `((,bash) (,build))
1121 #:env-vars `(("x" . ,(random-text)))))
1122 (drv2 (derivation store "the-thing" bash
1123 `("-e" ,build)
1124 #:inputs `((,bash) (,build))
1125 #:env-vars `(("x" . ,(random-text))))))
1126 (and (build-derivations store (list drv1))
1127 (begin
1128 (set-build-options store #:build-cores 42)
1129 (build-derivations store (list drv2)))
1130 (list (call-with-input-file (derivation->output-path drv1)
1131 read)
1132 (call-with-input-file (derivation->output-path drv2)
1133 read))))))
1134
6ef61cc4
LC
1135(test-equal "multiplexed-build-output"
1136 '("Hello from first." "Hello from second.")
1137 (with-store store
1138 (let* ((build (add-text-to-store store "build.sh"
1139 "echo Hello from $NAME.; echo > $out"))
1140 (bash (add-to-store store "bash" #t "sha256"
1141 (search-bootstrap-binary "bash"
1142 (%current-system))))
1143 (drv1 (derivation store "one" bash
1144 `("-e" ,build)
1145 #:inputs `((,bash) (,build))
1146 #:env-vars `(("NAME" . "first")
1147 ("x" . ,(random-text)))))
1148 (drv2 (derivation store "two" bash
1149 `("-e" ,build)
1150 #:inputs `((,bash) (,build))
1151 #:env-vars `(("NAME" . "second")
1152 ("x" . ,(random-text))))))
1153 (set-build-options store
1154 #:print-build-trace #t
1155 #:multiplexed-build-output? #t
1156 #:max-build-jobs 10)
1157 (let ((port (open-output-string)))
1158 ;; Send the build log to PORT.
1159 (parameterize ((current-build-output-port port))
1160 (build-derivations store (list drv1 drv2)))
1161
1162 ;; Retrieve the build log; make sure it contains valid "@ build-log"
1163 ;; traces that allow us to retrieve each builder's output (we assume
1164 ;; there's exactly one "build-output" trace for each builder, which is
1165 ;; reasonable.)
1166 (let* ((log (get-output-string port))
1167 (started (fold-matches
1168 (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
1169 log '() cons))
1170 (done (fold-matches
1171 (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
1172 log '() cons))
1173 (output (fold-matches
1174 (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
1175 log '() cons))
1176 (drv-pid (lambda (name)
1177 (lambda (m)
1178 (let ((drv (match:substring m 1))
1179 (pid (string->number
1180 (match:substring m 4))))
1181 (and (string-suffix? name drv) pid)))))
1182 (pid-log (lambda (pid)
1183 (lambda (m)
1184 (let ((n (string->number
1185 (match:substring m 1)))
1186 (len (string->number
1187 (match:substring m 2)))
1188 (str (match:substring m 3)))
1189 (and (= pid n)
1190 (= (string-length str) (- len 1))
1191 str)))))
1192 (pid1 (any (drv-pid "one.drv") started))
1193 (pid2 (any (drv-pid "two.drv") started)))
1194 (list (any (pid-log pid1) output)
1195 (any (pid-log pid2) output)))))))
1196
3259877d 1197(test-end "store")