pk-crypto: Add a few sexp utility procedures.
[jackhill/guix/guix.git] / tests / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
0f3d2504 2;;; Copyright © 2012, 2013 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)
fe0cff14 31 #:use-module (rnrs io ports)
f65cf81a 32 #:use-module (web uri)
3259877d
LC
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-11)
c3eb878f 35 #:use-module (srfi srfi-34)
3259877d
LC
36 #:use-module (srfi srfi-64))
37
38;; Test the (guix store) module.
39
40(define %store
41 (false-if-exception (open-connection)))
42
43(when %store
44 ;; Make sure we build everything by ourselves.
45 (set-build-options %store #:use-substitutes? #f))
46
47(define %seed
48 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
49
50(define (random-text)
51 (number->string (random (expt 2 256) %seed) 16))
52
53\f
54(test-begin "store")
55
2c6ab6cc
LC
56(test-equal "store-path-hash-part"
57 "283gqy39v3g9dxjy26rynl0zls82fmcg"
58 (store-path-hash-part
59 (string-append (%store-prefix)
60 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
61
62(test-equal "store-path-hash-part #f"
63 #f
64 (store-path-hash-part
65 (string-append (%store-prefix)
66 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
67
c61a5b4a
LC
68(test-equal "store-path-package-name"
69 "guile-2.0.7"
70 (store-path-package-name
71 (string-append (%store-prefix)
72 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
73
74(test-equal "store-path-package-name #f"
75 #f
76 (store-path-package-name
77 "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
78
9336e5b5
LC
79(test-assert "direct-store-path?"
80 (and (direct-store-path?
81 (string-append (%store-prefix)
82 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
83 (not (direct-store-path?
84 (string-append
85 (%store-prefix)
86 "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))))
87
3259877d
LC
88(test-skip (if %store 0 10))
89
90(test-assert "dead-paths"
cfbf9160 91 (let ((p (add-text-to-store %store "random-text" (random-text))))
3259877d
LC
92 (member p (dead-paths %store))))
93
94;; FIXME: Find a test for `live-paths'.
95;;
96;; (test-assert "temporary root is in live-paths"
97;; (let* ((p1 (add-text-to-store %store "random-text"
98;; (random-text) '()))
99;; (b (add-text-to-store %store "link-builder"
100;; (format #f "echo ~a > $out" p1)
101;; '()))
a987d2c0
LC
102;; (d1 (derivation %store "link"
103;; "/bin/sh" `("-e" ,b)
104;; #:inputs `((,b) (,p1))))
59688fc4 105;; (p2 (derivation->output-path d1)))
3259877d
LC
106;; (and (add-temp-root %store p2)
107;; (build-derivations %store (list d1))
108;; (valid-path? %store p1)
109;; (member (pk p2) (live-paths %store)))))
110
111(test-assert "dead path can be explicitly collected"
112 (let ((p (add-text-to-store %store "random-text"
113 (random-text) '())))
114 (let-values (((paths freed) (delete-paths %store (list p))))
115 (and (equal? paths (list p))
116 (> freed 0)
117 (not (file-exists? p))))))
118
fae31edc
LC
119(test-assert "references"
120 (let* ((t1 (add-text-to-store %store "random1"
cfbf9160 121 (random-text)))
fae31edc
LC
122 (t2 (add-text-to-store %store "random2"
123 (random-text) (list t1))))
124 (and (equal? (list t1) (references %store t2))
125 (equal? (list t2) (referrers %store t1))
126 (null? (references %store t1))
127 (null? (referrers %store t2)))))
128
3f1e6939
LC
129(test-assert "requisites"
130 (let* ((t1 (add-text-to-store %store "random1"
131 (random-text) '()))
132 (t2 (add-text-to-store %store "random2"
133 (random-text) (list t1)))
134 (t3 (add-text-to-store %store "random3"
135 (random-text) (list t2)))
136 (t4 (add-text-to-store %store "random4"
137 (random-text) (list t1 t3))))
138 (define (same? x y)
139 (and (= (length x) (length y))
140 (lset= equal? x y)))
141
142 (and (same? (requisites %store t1) (list t1))
143 (same? (requisites %store t2) (list t1 t2))
144 (same? (requisites %store t3) (list t1 t2 t3))
145 (same? (requisites %store t4) (list t1 t2 t3 t4)))))
146
fae31edc
LC
147(test-assert "derivers"
148 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
149 (s (add-to-store %store "bash" #t "sha256"
150 (search-bootstrap-binary "bash"
151 (%current-system))))
a987d2c0
LC
152 (d (derivation %store "the-thing"
153 s `("-e" ,b)
154 #:env-vars `(("foo" . ,(random-text)))
155 #:inputs `((,b) (,s))))
59688fc4 156 (o (derivation->output-path d)))
fae31edc 157 (and (build-derivations %store (list d))
59688fc4 158 (equal? (query-derivation-outputs %store (derivation-file-name d))
fae31edc
LC
159 (list o))
160 (equal? (valid-derivers %store o)
59688fc4 161 (list (derivation-file-name d))))))
fae31edc 162
eddd4077
LC
163(test-assert "log-file, derivation"
164 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
165 (s (add-to-store %store "bash" #t "sha256"
166 (search-bootstrap-binary "bash"
167 (%current-system))))
168 (d (derivation %store "the-thing"
169 s `("-e" ,b)
170 #:env-vars `(("foo" . ,(random-text)))
171 #:inputs `((,b) (,s)))))
172 (and (build-derivations %store (list d))
173 (file-exists? (pk (log-file %store (derivation-file-name d)))))))
174
175(test-assert "log-file, output file name"
176 (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
177 (s (add-to-store %store "bash" #t "sha256"
178 (search-bootstrap-binary "bash"
179 (%current-system))))
180 (d (derivation %store "the-thing"
181 s `("-e" ,b)
182 #:env-vars `(("foo" . ,(random-text)))
183 #:inputs `((,b) (,s))))
184 (o (derivation->output-path d)))
185 (and (build-derivations %store (list d))
186 (file-exists? (pk (log-file %store o)))
187 (string=? (log-file %store (derivation-file-name d))
188 (log-file %store o)))))
189
0f3d2504
LC
190(test-assert "no substitutes"
191 (let* ((s (open-connection))
192 (d1 (package-derivation s %bootstrap-guile (%current-system)))
193 (d2 (package-derivation s %bootstrap-glibc (%current-system)))
59688fc4 194 (o (map derivation->output-path (list d1 d2))))
0f3d2504 195 (set-build-options s #:use-substitutes? #f)
59688fc4
LC
196 (and (not (has-substitutes? s (derivation-file-name d1)))
197 (not (has-substitutes? s (derivation-file-name d2)))
0f3d2504
LC
198 (null? (substitutable-paths s o))
199 (null? (substitutable-path-info s o)))))
200
f65cf81a
LC
201(test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
202
203(test-assert "substitute query"
204 (let* ((s (open-connection))
205 (d (package-derivation s %bootstrap-guile (%current-system)))
59688fc4 206 (o (derivation->output-path d))
f65cf81a
LC
207 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
208 (compose uri-path string->uri))))
209 ;; Create fake substituter data, to be read by `substitute-binary'.
210 (call-with-output-file (string-append dir "/nix-cache-info")
211 (lambda (p)
212 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
fe0cff14 213 (%store-prefix))))
f65cf81a
LC
214 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
215 ".narinfo")
216 (lambda (p)
217 (format p "StorePath: ~a
218URL: ~a
219Compression: none
220NarSize: 1234
221References:
222System: ~a
223Deriver: ~a~%"
224 o ; StorePath
225 (string-append dir "/example.nar") ; URL
226 (%current-system) ; System
59688fc4
LC
227 (basename
228 (derivation-file-name d))))) ; Deriver
f65cf81a 229
eba783b7
LC
230 ;; Remove entry from the local cache.
231 (false-if-exception
232 (delete-file (string-append (getenv "XDG_CACHE_HOME")
233 "/guix/substitute-binary/"
234 (store-path-hash-part o))))
235
f65cf81a
LC
236 ;; Make sure `substitute-binary' correctly communicates the above data.
237 (set-build-options s #:use-substitutes? #t)
238 (and (has-substitutes? s o)
239 (equal? (list o) (substitutable-paths s (list o)))
240 (match (pk 'spi (substitutable-path-info s (list o)))
241 (((? substitutable? s))
59688fc4 242 (and (string=? (substitutable-deriver s) (derivation-file-name d))
f65cf81a
LC
243 (null? (substitutable-references s))
244 (equal? (substitutable-nar-size s) 1234)))))))
245
fe0cff14
LC
246(test-assert "substitute"
247 (let* ((s (open-connection))
248 (c (random-text)) ; contents of the output
249 (d (build-expression->derivation
dd1a5a15 250 s "substitute-me"
fe0cff14
LC
251 `(call-with-output-file %output
252 (lambda (p)
253 (exit 1) ; would actually fail
254 (display ,c p)))
fe0cff14
LC
255 #:guile-for-build
256 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 257 (o (derivation->output-path d))
fe0cff14
LC
258 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
259 (compose uri-path string->uri))))
260 ;; Create fake substituter data, to be read by `substitute-binary'.
261 (call-with-output-file (string-append dir "/nix-cache-info")
262 (lambda (p)
263 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
264 (%store-prefix))))
265 (call-with-output-file (string-append dir "/example.out")
266 (lambda (p)
267 (display c p)))
268 (call-with-output-file (string-append dir "/example.nar")
269 (lambda (p)
270 (write-file (string-append dir "/example.out") p)))
271 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
272 ".narinfo")
273 (lambda (p)
274 (format p "StorePath: ~a
275URL: ~a
276Compression: none
277NarSize: 1234
278NarHash: sha256:~a
279References:
280System: ~a
281Deriver: ~a~%"
282 o ; StorePath
283 "example.nar" ; relative URL
284 (call-with-input-file (string-append dir "/example.nar")
285 (compose bytevector->nix-base32-string sha256
286 get-bytevector-all))
287 (%current-system) ; System
59688fc4
LC
288 (basename
289 (derivation-file-name d))))) ; Deriver
fe0cff14
LC
290
291 ;; Make sure we use `substitute-binary'.
292 (set-build-options s #:use-substitutes? #t)
293 (and (has-substitutes? s o)
294 (build-derivations s (list d))
295 (equal? c (call-with-input-file o get-string-all)))))
296
c3eb878f
LC
297(test-assert "substitute --fallback"
298 (let* ((s (open-connection))
299 (t (random-text)) ; contents of the output
300 (d (build-expression->derivation
dd1a5a15 301 s "substitute-me-not"
c3eb878f
LC
302 `(call-with-output-file %output
303 (lambda (p)
304 (display ,t p)))
c3eb878f
LC
305 #:guile-for-build
306 (package-derivation s %bootstrap-guile (%current-system))))
59688fc4 307 (o (derivation->output-path d))
c3eb878f
LC
308 (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
309 (compose uri-path string->uri))))
310 ;; Create fake substituter data, to be read by `substitute-binary'.
311 (call-with-output-file (string-append dir "/nix-cache-info")
312 (lambda (p)
313 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
314 (%store-prefix))))
315 (call-with-output-file (string-append dir "/" (store-path-hash-part o)
316 ".narinfo")
317 (lambda (p)
318 (format p "StorePath: ~a
319URL: ~a
320Compression: none
321NarSize: 1234
322NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
323References:
324System: ~a
325Deriver: ~a~%"
326 o ; StorePath
327 "does-not-exist.nar" ; relative URL
328 (%current-system) ; System
59688fc4
LC
329 (basename
330 (derivation-file-name d))))) ; Deriver
c3eb878f
LC
331
332 ;; Make sure we use `substitute-binary'.
333 (set-build-options s #:use-substitutes? #t)
334 (and (has-substitutes? s o)
335 (guard (c ((nix-protocol-error? c)
336 ;; The substituter failed as expected. Now make sure that
337 ;; #:fallback? #t works correctly.
338 (set-build-options s
339 #:use-substitutes? #t
340 #:fallback? #t)
341 (and (build-derivations s (list d))
342 (equal? t (call-with-input-file o get-string-all)))))
343 ;; Should fail.
344 (build-derivations s (list d))
345 #f))))
346
3259877d
LC
347(test-end "store")
348
349\f
350(exit (= (test-runner-fail-count (test-runner-current)) 0))