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