substitute-binary: Defer narinfo authentication and authorization checks.
[jackhill/guix/guix.git] / tests / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
6bfec3ed 2;;; Copyright © 2012, 2013, 2014 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
LC
18
19
20(define-module (test-store)
21 #:use-module (guix store)
22 #:use-module (guix utils)
72626a71 23 #:use-module (guix hash)
3259877d 24 #:use-module (guix base32)
0f3d2504
LC
25 #:use-module (guix packages)
26 #:use-module (guix derivations)
fe0cff14 27 #:use-module (guix nar)
fae31edc 28 #:use-module (gnu packages)
1ffa7090 29 #:use-module (gnu packages bootstrap)
3259877d 30 #:use-module (ice-9 match)
526382ff 31 #:use-module (rnrs bytevectors)
fe0cff14 32 #:use-module (rnrs io ports)
f65cf81a 33 #:use-module (web uri)
3259877d
LC
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
526382ff 36 #:use-module (srfi srfi-26)
c3eb878f 37 #:use-module (srfi srfi-34)
3259877d
LC
38 #:use-module (srfi srfi-64))
39
40;; Test the (guix store) module.
41
42(define %store
43 (false-if-exception (open-connection)))
44
45(when %store
46 ;; Make sure we build everything by ourselves.
47 (set-build-options %store #:use-substitutes? #f))
48
49(define %seed
50 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
51
52(define (random-text)
53 (number->string (random (expt 2 256) %seed) 16))
54
55\f
56(test-begin "store")
57
2c6ab6cc
LC
58(test-equal "store-path-hash-part"
59 "283gqy39v3g9dxjy26rynl0zls82fmcg"
60 (store-path-hash-part
61 (string-append (%store-prefix)
62 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
63
64(test-equal "store-path-hash-part #f"
65 #f
66 (store-path-hash-part
67 (string-append (%store-prefix)
68 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
69
c61a5b4a
LC
70(test-equal "store-path-package-name"
71 "guile-2.0.7"
72 (store-path-package-name
73 (string-append (%store-prefix)
74 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
75
76(test-equal "store-path-package-name #f"
77 #f
78 (store-path-package-name
79 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
80
9336e5b5
LC
81(test-assert "direct-store-path?"
82 (and (direct-store-path?
83 (string-append (%store-prefix)
84 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
85 (not (direct-store-path?
86 (string-append
87 (%store-prefix)
88 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))))
89
e297d8fc
LC
90(test-skip (if %store 0 13))
91
92(test-assert "valid-path? live"
93 (let ((p (add-text-to-store %store "hello" "hello, world")))
94 (valid-path? %store p)))
95
96(test-assert "valid-path? false"
97 (not (valid-path? %store
98 (string-append (%store-prefix) "/"
99 (make-string 32 #\e) "-foobar"))))
100
101(test-assert "valid-path? error"
102 (with-store s
103 (guard (c ((nix-protocol-error? c) #t))
104 (valid-path? s "foo")
105 #f)))
106
107(test-assert "valid-path? recovery"
108 ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
109 ;; close the connection after receiving a 'valid-path?' RPC with a non-store
110 ;; file name. See
111 ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
112 ;; details.
113 (with-store s
114 (let-syntax ((true-if-error (syntax-rules ()
115 ((_ exp)
116 (guard (c ((nix-protocol-error? c) #t))
117 exp #f)))))
118 (and (true-if-error (valid-path? s "foo"))
119 (true-if-error (valid-path? s "bar"))
120 (true-if-error (valid-path? s "baz"))
121 (true-if-error (valid-path? s "chbouib"))
122 (valid-path? s (add-text-to-store s "valid" "yeah"))))))
11e7a6cf
LC
123
124(test-assert "hash-part->path"
125 (let ((p (add-text-to-store %store "hello" "hello, world")))
126 (equal? (hash-part->path %store (store-path-hash-part p))
127 p)))
3259877d
LC
128
129(test-assert "dead-paths"
cfbf9160 130 (let ((p (add-text-to-store %store "random-text" (random-text))))
3259877d
LC
131 (member p (dead-paths %store))))
132
133;; FIXME: Find a test for `live-paths'.
134;;
135;; (test-assert "temporary root is in live-paths"
136;; (let* ((p1 (add-text-to-store %store "random-text"
137;; (random-text) '()))
138;; (b (add-text-to-store %store "link-builder"
139;; (format #f "echo ~a > $out" p1)
140;; '()))
a987d2c0
LC
141;; (d1 (derivation %store "link"
142;; "/bin/sh" `("-e" ,b)
143;; #:inputs `((,b) (,p1))))
59688fc4 144;; (p2 (derivation->output-path d1)))
3259877d
LC
145;; (and (add-temp-root %store p2)
146;; (build-derivations %store (list d1))
147;; (valid-path? %store p1)
148;; (member (pk p2) (live-paths %store)))))
149
150(test-assert "dead path can be explicitly collected"
151 (let ((p (add-text-to-store %store "random-text"
152 (random-text) '())))
153 (let-values (((paths freed) (delete-paths %store (list p))))
154 (and (equal? paths (list p))
155 (> freed 0)
156 (not (file-exists? p))))))
157
fae31edc
LC
158(test-assert "references"
159 (let* ((t1 (add-text-to-store %store "random1"
cfbf9160 160 (random-text)))
fae31edc
LC
161 (t2 (add-text-to-store %store "random2"
162 (random-text) (list t1))))
163 (and (equal? (list t1) (references %store t2))
164 (equal? (list t2) (referrers %store t1))
165 (null? (references %store t1))
166 (null? (referrers %store t2)))))
167
3f1e6939
LC
168(test-assert "requisites"
169 (let* ((t1 (add-text-to-store %store "random1"
170 (random-text) '()))
171 (t2 (add-text-to-store %store "random2"
172 (random-text) (list t1)))
173 (t3 (add-text-to-store %store "random3"
174 (random-text) (list t2)))
175 (t4 (add-text-to-store %store "random4"
176 (random-text) (list t1 t3))))
177 (define (same? x y)
178 (and (= (length x) (length y))
179 (lset= equal? x y)))
180
181 (and (same? (requisites %store t1) (list t1))
182 (same? (requisites %store t2) (list t1 t2))
183 (same? (requisites %store t3) (list t1 t2 t3))
184 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
185
fae31edc
LC
186(test-assert "derivers"
187 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
188 (s (add-to-store %store "bash" #t "sha256"
189 (search-bootstrap-binary "bash"
190 (%current-system))))
a987d2c0
LC
191 (d (derivation %store "the-thing"
192 s `("-e" ,b)
193 #:env-vars `(("foo" . ,(random-text)))
194 #:inputs `((,b) (,s))))
59688fc4 195 (o (derivation->output-path d)))
fae31edc 196 (and (build-derivations %store (list d))
59688fc4 197 (equal? (query-derivation-outputs %store (derivation-file-name d))
fae31edc
LC
198 (list o))
199 (equal? (valid-derivers %store o)
59688fc4 200 (list (derivation-file-name d))))))
fae31edc 201
50add477
LC
202(test-assert "topologically-sorted, one item"
203 (let* ((a (add-text-to-store %store "a" "a"))
204 (b (add-text-to-store %store "b" "b" (list a)))
205 (c (add-text-to-store %store "c" "c" (list b)))
206 (d (add-text-to-store %store "d" "d" (list c)))
207 (s (topologically-sorted %store (list d))))
208 (equal? s (list a b c d))))
209
210(test-assert "topologically-sorted, several items"
211 (let* ((a (add-text-to-store %store "a" "a"))
212 (b (add-text-to-store %store "b" "b" (list a)))
213 (c (add-text-to-store %store "c" "c" (list b)))
214 (d (add-text-to-store %store "d" "d" (list c)))
215 (s1 (topologically-sorted %store (list d a c b)))
216 (s2 (topologically-sorted %store (list b d c a b d))))
217 (equal? s1 s2 (list a b c d))))
218
219(test-assert "topologically-sorted, more difficult"
220 (let* ((a (add-text-to-store %store "a" "a"))
221 (b (add-text-to-store %store "b" "b" (list a)))
222 (c (add-text-to-store %store "c" "c" (list b)))
223 (d (add-text-to-store %store "d" "d" (list c)))
224 (w (add-text-to-store %store "w" "w"))
225 (x (add-text-to-store %store "x" "x" (list w)))
226 (y (add-text-to-store %store "y" "y" (list x d)))
227 (s1 (topologically-sorted %store (list y)))
228 (s2 (topologically-sorted %store (list c y)))
229 (s3 (topologically-sorted %store (cons y (references %store y)))))
58cbbe4b
LC
230 ;; The order in which 'references' returns the references of Y is
231 ;; unspecified, so accommodate.
232 (let* ((x-then-d? (equal? (references %store y) (list x d))))
233 (and (equal? s1
234 (if x-then-d?
235 (list w x a b c d y)
236 (list a b c d w x y)))
237 (equal? s2
238 (if x-then-d?
239 (list a b c w x d y)
240 (list a b c d w x y)))
241 (lset= string=? s1 s3)))))
50add477 242
eddd4077
LC
243(test-assert "log-file, derivation"
244 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
245 (s (add-to-store %store "bash" #t "sha256"
246 (search-bootstrap-binary "bash"
247 (%current-system))))
248 (d (derivation %store "the-thing"
249 s `("-e" ,b)
250 #:env-vars `(("foo" . ,(random-text)))
251 #:inputs `((,b) (,s)))))
252 (and (build-derivations %store (list d))
253 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
254
255(test-assert "log-file, output file name"
256 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
257 (s (add-to-store %store "bash" #t "sha256"
258 (search-bootstrap-binary "bash"
259 (%current-system))))
260 (d (derivation %store "the-thing"
261 s `("-e" ,b)
262 #:env-vars `(("foo" . ,(random-text)))
263 #:inputs `((,b) (,s))))
264 (o (derivation->output-path d)))
265 (and (build-derivations %store (list d))
266 (file-exists? (pk (log-file %store o)))
267 (string=? (log-file %store (derivation-file-name d))
268 (log-file %store o)))))
269
0f3d2504
LC
270(test-assert "no substitutes"
271 (let* ((s (open-connection))
272 (d1 (package-derivation s %bootstrap-guile (%current-system)))
273 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
59688fc4 274 (o (map derivation->output-path (list d1 d2))))
0f3d2504 275 (set-build-options s #:use-substitutes? #f)
59688fc4
LC
276 (and (not (has-substitutes? s (derivation-file-name d1)))
277 (not (has-substitutes? s (derivation-file-name d2)))
0f3d2504
LC
278 (null? (substitutable-paths s o))
279 (null? (substitutable-path-info s o)))))
280
f65cf81a
LC
281(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
282
283(test-assert "substitute query"
284 (let* ((s (open-connection))
285 (d (package-derivation s %bootstrap-guile (%current-system)))
59688fc4 286 (o (derivation->output-path d))
f65cf81a
LC
287 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
288 (compose uri-path string->uri))))
289 ;; Create fake substituter data, to be read by `substitute-binary'.
290 (call-with-output-file (string-append dir "/nix-cache-info")
291 (lambda (p)
292 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
fe0cff14 293 (%store-prefix))))
f65cf81a
LC
294 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
295 ".narinfo")
296 (lambda (p)
297 (format p "StorePath: ~a
298URL: ~a
299Compression: none
300NarSize: 1234
301References:
302System: ~a
303Deriver: ~a~%"
304 o ; StorePath
305 (string-append dir "/example.nar") ; URL
306 (%current-system) ; System
59688fc4
LC
307 (basename
308 (derivation-file-name d))))) ; Deriver
f65cf81a 309
eba783b7
LC
310 ;; Remove entry from the local cache.
311 (false-if-exception
312 (delete-file (string-append (getenv "XDG_CACHE_HOME")
313 "/guix/substitute-binary/"
314 (store-path-hash-part o))))
315
f65cf81a
LC
316 ;; Make sure `substitute-binary' correctly communicates the above data.
317 (set-build-options s #:use-substitutes? #t)
318 (and (has-substitutes? s o)
319 (equal? (list o) (substitutable-paths s (list o)))
320 (match (pk 'spi (substitutable-path-info s (list o)))
321 (((? substitutable? s))
59688fc4 322 (and (string=? (substitutable-deriver s) (derivation-file-name d))
f65cf81a
LC
323 (null? (substitutable-references s))
324 (equal? (substitutable-nar-size s) 1234)))))))
325
fe0cff14
LC
326(test-assert "substitute"
327 (let* ((s (open-connection))
328 (c (random-text)) ; contents of the output
329 (d (build-expression->derivation
dd1a5a15 330 s "substitute-me"
fe0cff14
LC
331 `(call-with-output-file %output
332 (lambda (p)
333 (exit 1) ; would actually fail
334 (display ,c p)))
fe0cff14
LC
335 #:guile-for-build
336 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 337 (o (derivation->output-path d))
fe0cff14
LC
338 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
339 (compose uri-path string->uri))))
340 ;; Create fake substituter data, to be read by `substitute-binary'.
341 (call-with-output-file (string-append dir "/nix-cache-info")
342 (lambda (p)
343 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
344 (%store-prefix))))
345 (call-with-output-file (string-append dir "/example.out")
346 (lambda (p)
347 (display c p)))
348 (call-with-output-file (string-append dir "/example.nar")
349 (lambda (p)
350 (write-file (string-append dir "/example.out") p)))
351 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
352 ".narinfo")
353 (lambda (p)
354 (format p "StorePath: ~a
355URL: ~a
356Compression: none
357NarSize: 1234
358NarHash: sha256:~a
359References:
360System: ~a
361Deriver: ~a~%"
362 o ; StorePath
363 "example.nar" ; relative URL
364 (call-with-input-file (string-append dir "/example.nar")
365 (compose bytevector->nix-base32-string sha256
366 get-bytevector-all))
367 (%current-system) ; System
59688fc4
LC
368 (basename
369 (derivation-file-name d))))) ; Deriver
fe0cff14
LC
370
371 ;; Make sure we use `substitute-binary'.
372 (set-build-options s #:use-substitutes? #t)
373 (and (has-substitutes? s o)
374 (build-derivations s (list d))
375 (equal? c (call-with-input-file o get-string-all)))))
376
c3eb878f
LC
377(test-assert "substitute --fallback"
378 (let* ((s (open-connection))
379 (t (random-text)) ; contents of the output
380 (d (build-expression->derivation
dd1a5a15 381 s "substitute-me-not"
c3eb878f
LC
382 `(call-with-output-file %output
383 (lambda (p)
384 (display ,t p)))
c3eb878f
LC
385 #:guile-for-build
386 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 387 (o (derivation->output-path d))
c3eb878f
LC
388 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
389 (compose uri-path string->uri))))
390 ;; Create fake substituter data, to be read by `substitute-binary'.
391 (call-with-output-file (string-append dir "/nix-cache-info")
392 (lambda (p)
393 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
394 (%store-prefix))))
395 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
396 ".narinfo")
397 (lambda (p)
398 (format p "StorePath: ~a
399URL: ~a
400Compression: none
401NarSize: 1234
402NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
403References:
404System: ~a
405Deriver: ~a~%"
406 o ; StorePath
407 "does-not-exist.nar" ; relative URL
408 (%current-system) ; System
59688fc4
LC
409 (basename
410 (derivation-file-name d))))) ; Deriver
c3eb878f
LC
411
412 ;; Make sure we use `substitute-binary'.
413 (set-build-options s #:use-substitutes? #t)
414 (and (has-substitutes? s o)
415 (guard (c ((nix-protocol-error? c)
416 ;; The substituter failed as expected. Now make sure that
417 ;; #:fallback? #t works correctly.
418 (set-build-options s
419 #:use-substitutes? #t
420 #:fallback? #t)
421 (and (build-derivations s (list d))
422 (equal? t (call-with-input-file o get-string-all)))))
423 ;; Should fail.
424 (build-derivations s (list d))
425 #f))))
426
526382ff
LC
427(test-assert "export/import several paths"
428 (let* ((texts (unfold (cut >= <> 10)
429 (lambda _ (random-text))
430 1+
431 0))
432 (files (map (cut add-text-to-store %store "text" <>) texts))
433 (dump (call-with-bytevector-output-port
434 (cut export-paths %store files <>))))
435 (delete-paths %store files)
436 (and (every (negate file-exists?) files)
437 (let* ((source (open-bytevector-input-port dump))
438 (imported (import-paths %store source)))
439 (and (equal? imported files)
440 (every file-exists? files)
441 (equal? texts
442 (map (lambda (file)
443 (call-with-input-file file
444 get-string-all))
445 files)))))))
446
99fbddf9 447(test-assert "export/import paths, ensure topological order"
cafb92d8
LC
448 (let* ((file0 (add-text-to-store %store "baz" (random-text)))
449 (file1 (add-text-to-store %store "foo" (random-text)
450 (list file0)))
99fbddf9
LC
451 (file2 (add-text-to-store %store "bar" (random-text)
452 (list file1)))
453 (files (list file1 file2))
454 (dump1 (call-with-bytevector-output-port
455 (cute export-paths %store (list file1 file2) <>)))
456 (dump2 (call-with-bytevector-output-port
457 (cute export-paths %store (list file2 file1) <>))))
458 (delete-paths %store files)
459 (and (every (negate file-exists?) files)
460 (bytevector=? dump1 dump2)
461 (let* ((source (open-bytevector-input-port dump1))
462 (imported (import-paths %store source)))
cafb92d8 463 ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
99fbddf9
LC
464 (and (equal? imported (list file1 file2))
465 (every file-exists? files)
cafb92d8 466 (equal? (list file0) (references %store file1))
99fbddf9
LC
467 (equal? (list file1) (references %store file2)))))))
468
526382ff
LC
469(test-assert "import corrupt path"
470 (let* ((text (random-text))
471 (file (add-text-to-store %store "text" text))
472 (dump (call-with-bytevector-output-port
473 (cut export-paths %store (list file) <>))))
474 (delete-paths %store (list file))
475
6df1fb89
LC
476 ;; Flip a bit in the stream's payload.
477 (let* ((index (quotient (bytevector-length dump) 4))
526382ff
LC
478 (byte (bytevector-u8-ref dump index)))
479 (bytevector-u8-set! dump index (logxor #xff byte)))
480
481 (and (not (file-exists? file))
482 (guard (c ((nix-protocol-error? c)
483 (pk 'c c)
484 (and (not (zero? (nix-protocol-error-status c)))
485 (string-contains (nix-protocol-error-message c)
486 "corrupt"))))
487 (let* ((source (open-bytevector-input-port dump))
488 (imported (import-paths %store source)))
489 (pk 'corrupt-imported imported)
490 #f)))))
491
6bfec3ed
LC
492(test-assert "register-path"
493 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
494 "-fake")))
495 (when (valid-path? %store file)
496 (delete-paths %store (list file)))
497 (false-if-exception (delete-file file))
498
499 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
500 (drv (string-append file ".drv")))
501 (call-with-output-file file
502 (cut display "This is a fake store item.\n" <>))
503 (register-path file
504 #:references (list ref)
505 #:deriver drv)
506
507 (and (valid-path? %store file)
508 (equal? (references %store file) (list ref))
509 (null? (valid-derivers %store file))
510 (null? (referrers %store file))))))
511
3259877d
LC
512(test-end "store")
513
514\f
515(exit (= (test-runner-fail-count (test-runner-current)) 0))