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