Commit | Line | Data |
---|---|---|
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) |
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 | ||
3259877d LC |
90 | (test-skip (if %store 0 10)) |
91 | ||
92 | (test-assert "dead-paths" | |
cfbf9160 | 93 | (let ((p (add-text-to-store %store "random-text" (random-text)))) |
3259877d LC |
94 | (member p (dead-paths %store)))) |
95 | ||
96 | ;; FIXME: Find a test for `live-paths'. | |
97 | ;; | |
98 | ;; (test-assert "temporary root is in live-paths" | |
99 | ;; (let* ((p1 (add-text-to-store %store "random-text" | |
100 | ;; (random-text) '())) | |
101 | ;; (b (add-text-to-store %store "link-builder" | |
102 | ;; (format #f "echo ~a > $out" p1) | |
103 | ;; '())) | |
a987d2c0 LC |
104 | ;; (d1 (derivation %store "link" |
105 | ;; "/bin/sh" `("-e" ,b) | |
106 | ;; #:inputs `((,b) (,p1)))) | |
59688fc4 | 107 | ;; (p2 (derivation->output-path d1))) |
3259877d LC |
108 | ;; (and (add-temp-root %store p2) |
109 | ;; (build-derivations %store (list d1)) | |
110 | ;; (valid-path? %store p1) | |
111 | ;; (member (pk p2) (live-paths %store))))) | |
112 | ||
113 | (test-assert "dead path can be explicitly collected" | |
114 | (let ((p (add-text-to-store %store "random-text" | |
115 | (random-text) '()))) | |
116 | (let-values (((paths freed) (delete-paths %store (list p)))) | |
117 | (and (equal? paths (list p)) | |
118 | (> freed 0) | |
119 | (not (file-exists? p)))))) | |
120 | ||
fae31edc LC |
121 | (test-assert "references" |
122 | (let* ((t1 (add-text-to-store %store "random1" | |
cfbf9160 | 123 | (random-text))) |
fae31edc LC |
124 | (t2 (add-text-to-store %store "random2" |
125 | (random-text) (list t1)))) | |
126 | (and (equal? (list t1) (references %store t2)) | |
127 | (equal? (list t2) (referrers %store t1)) | |
128 | (null? (references %store t1)) | |
129 | (null? (referrers %store t2))))) | |
130 | ||
3f1e6939 LC |
131 | (test-assert "requisites" |
132 | (let* ((t1 (add-text-to-store %store "random1" | |
133 | (random-text) '())) | |
134 | (t2 (add-text-to-store %store "random2" | |
135 | (random-text) (list t1))) | |
136 | (t3 (add-text-to-store %store "random3" | |
137 | (random-text) (list t2))) | |
138 | (t4 (add-text-to-store %store "random4" | |
139 | (random-text) (list t1 t3)))) | |
140 | (define (same? x y) | |
141 | (and (= (length x) (length y)) | |
142 | (lset= equal? x y))) | |
143 | ||
144 | (and (same? (requisites %store t1) (list t1)) | |
145 | (same? (requisites %store t2) (list t1 t2)) | |
146 | (same? (requisites %store t3) (list t1 t2 t3)) | |
147 | (same? (requisites %store t4) (list t1 t2 t3 t4))))) | |
148 | ||
fae31edc LC |
149 | (test-assert "derivers" |
150 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
151 | (s (add-to-store %store "bash" #t "sha256" | |
152 | (search-bootstrap-binary "bash" | |
153 | (%current-system)))) | |
a987d2c0 LC |
154 | (d (derivation %store "the-thing" |
155 | s `("-e" ,b) | |
156 | #:env-vars `(("foo" . ,(random-text))) | |
157 | #:inputs `((,b) (,s)))) | |
59688fc4 | 158 | (o (derivation->output-path d))) |
fae31edc | 159 | (and (build-derivations %store (list d)) |
59688fc4 | 160 | (equal? (query-derivation-outputs %store (derivation-file-name d)) |
fae31edc LC |
161 | (list o)) |
162 | (equal? (valid-derivers %store o) | |
59688fc4 | 163 | (list (derivation-file-name d)))))) |
fae31edc | 164 | |
eddd4077 LC |
165 | (test-assert "log-file, derivation" |
166 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
167 | (s (add-to-store %store "bash" #t "sha256" | |
168 | (search-bootstrap-binary "bash" | |
169 | (%current-system)))) | |
170 | (d (derivation %store "the-thing" | |
171 | s `("-e" ,b) | |
172 | #:env-vars `(("foo" . ,(random-text))) | |
173 | #:inputs `((,b) (,s))))) | |
174 | (and (build-derivations %store (list d)) | |
175 | (file-exists? (pk (log-file %store (derivation-file-name d))))))) | |
176 | ||
177 | (test-assert "log-file, output file name" | |
178 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
179 | (s (add-to-store %store "bash" #t "sha256" | |
180 | (search-bootstrap-binary "bash" | |
181 | (%current-system)))) | |
182 | (d (derivation %store "the-thing" | |
183 | s `("-e" ,b) | |
184 | #:env-vars `(("foo" . ,(random-text))) | |
185 | #:inputs `((,b) (,s)))) | |
186 | (o (derivation->output-path d))) | |
187 | (and (build-derivations %store (list d)) | |
188 | (file-exists? (pk (log-file %store o))) | |
189 | (string=? (log-file %store (derivation-file-name d)) | |
190 | (log-file %store o))))) | |
191 | ||
0f3d2504 LC |
192 | (test-assert "no substitutes" |
193 | (let* ((s (open-connection)) | |
194 | (d1 (package-derivation s %bootstrap-guile (%current-system))) | |
195 | (d2 (package-derivation s %bootstrap-glibc (%current-system))) | |
59688fc4 | 196 | (o (map derivation->output-path (list d1 d2)))) |
0f3d2504 | 197 | (set-build-options s #:use-substitutes? #f) |
59688fc4 LC |
198 | (and (not (has-substitutes? s (derivation-file-name d1))) |
199 | (not (has-substitutes? s (derivation-file-name d2))) | |
0f3d2504 LC |
200 | (null? (substitutable-paths s o)) |
201 | (null? (substitutable-path-info s o))))) | |
202 | ||
f65cf81a LC |
203 | (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1)) |
204 | ||
205 | (test-assert "substitute query" | |
206 | (let* ((s (open-connection)) | |
207 | (d (package-derivation s %bootstrap-guile (%current-system))) | |
59688fc4 | 208 | (o (derivation->output-path d)) |
f65cf81a LC |
209 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
210 | (compose uri-path string->uri)))) | |
211 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
212 | (call-with-output-file (string-append dir "/nix-cache-info") | |
213 | (lambda (p) | |
214 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
fe0cff14 | 215 | (%store-prefix)))) |
f65cf81a LC |
216 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) |
217 | ".narinfo") | |
218 | (lambda (p) | |
219 | (format p "StorePath: ~a | |
220 | URL: ~a | |
221 | Compression: none | |
222 | NarSize: 1234 | |
223 | References: | |
224 | System: ~a | |
225 | Deriver: ~a~%" | |
226 | o ; StorePath | |
227 | (string-append dir "/example.nar") ; URL | |
228 | (%current-system) ; System | |
59688fc4 LC |
229 | (basename |
230 | (derivation-file-name d))))) ; Deriver | |
f65cf81a | 231 | |
eba783b7 LC |
232 | ;; Remove entry from the local cache. |
233 | (false-if-exception | |
234 | (delete-file (string-append (getenv "XDG_CACHE_HOME") | |
235 | "/guix/substitute-binary/" | |
236 | (store-path-hash-part o)))) | |
237 | ||
f65cf81a LC |
238 | ;; Make sure `substitute-binary' correctly communicates the above data. |
239 | (set-build-options s #:use-substitutes? #t) | |
240 | (and (has-substitutes? s o) | |
241 | (equal? (list o) (substitutable-paths s (list o))) | |
242 | (match (pk 'spi (substitutable-path-info s (list o))) | |
243 | (((? substitutable? s)) | |
59688fc4 | 244 | (and (string=? (substitutable-deriver s) (derivation-file-name d)) |
f65cf81a LC |
245 | (null? (substitutable-references s)) |
246 | (equal? (substitutable-nar-size s) 1234))))))) | |
247 | ||
fe0cff14 LC |
248 | (test-assert "substitute" |
249 | (let* ((s (open-connection)) | |
250 | (c (random-text)) ; contents of the output | |
251 | (d (build-expression->derivation | |
dd1a5a15 | 252 | s "substitute-me" |
fe0cff14 LC |
253 | `(call-with-output-file %output |
254 | (lambda (p) | |
255 | (exit 1) ; would actually fail | |
256 | (display ,c p))) | |
fe0cff14 LC |
257 | #:guile-for-build |
258 | (package-derivation s %bootstrap-guile (%current-system)))) | |
59688fc4 | 259 | (o (derivation->output-path d)) |
fe0cff14 LC |
260 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
261 | (compose uri-path string->uri)))) | |
262 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
263 | (call-with-output-file (string-append dir "/nix-cache-info") | |
264 | (lambda (p) | |
265 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
266 | (%store-prefix)))) | |
267 | (call-with-output-file (string-append dir "/example.out") | |
268 | (lambda (p) | |
269 | (display c p))) | |
270 | (call-with-output-file (string-append dir "/example.nar") | |
271 | (lambda (p) | |
272 | (write-file (string-append dir "/example.out") p))) | |
273 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) | |
274 | ".narinfo") | |
275 | (lambda (p) | |
276 | (format p "StorePath: ~a | |
277 | URL: ~a | |
278 | Compression: none | |
279 | NarSize: 1234 | |
280 | NarHash: sha256:~a | |
281 | References: | |
282 | System: ~a | |
283 | Deriver: ~a~%" | |
284 | o ; StorePath | |
285 | "example.nar" ; relative URL | |
286 | (call-with-input-file (string-append dir "/example.nar") | |
287 | (compose bytevector->nix-base32-string sha256 | |
288 | get-bytevector-all)) | |
289 | (%current-system) ; System | |
59688fc4 LC |
290 | (basename |
291 | (derivation-file-name d))))) ; Deriver | |
fe0cff14 LC |
292 | |
293 | ;; Make sure we use `substitute-binary'. | |
294 | (set-build-options s #:use-substitutes? #t) | |
295 | (and (has-substitutes? s o) | |
296 | (build-derivations s (list d)) | |
297 | (equal? c (call-with-input-file o get-string-all))))) | |
298 | ||
c3eb878f LC |
299 | (test-assert "substitute --fallback" |
300 | (let* ((s (open-connection)) | |
301 | (t (random-text)) ; contents of the output | |
302 | (d (build-expression->derivation | |
dd1a5a15 | 303 | s "substitute-me-not" |
c3eb878f LC |
304 | `(call-with-output-file %output |
305 | (lambda (p) | |
306 | (display ,t p))) | |
c3eb878f LC |
307 | #:guile-for-build |
308 | (package-derivation s %bootstrap-guile (%current-system)))) | |
59688fc4 | 309 | (o (derivation->output-path d)) |
c3eb878f LC |
310 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
311 | (compose uri-path string->uri)))) | |
312 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
313 | (call-with-output-file (string-append dir "/nix-cache-info") | |
314 | (lambda (p) | |
315 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
316 | (%store-prefix)))) | |
317 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) | |
318 | ".narinfo") | |
319 | (lambda (p) | |
320 | (format p "StorePath: ~a | |
321 | URL: ~a | |
322 | Compression: none | |
323 | NarSize: 1234 | |
324 | NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73 | |
325 | References: | |
326 | System: ~a | |
327 | Deriver: ~a~%" | |
328 | o ; StorePath | |
329 | "does-not-exist.nar" ; relative URL | |
330 | (%current-system) ; System | |
59688fc4 LC |
331 | (basename |
332 | (derivation-file-name d))))) ; Deriver | |
c3eb878f LC |
333 | |
334 | ;; Make sure we use `substitute-binary'. | |
335 | (set-build-options s #:use-substitutes? #t) | |
336 | (and (has-substitutes? s o) | |
337 | (guard (c ((nix-protocol-error? c) | |
338 | ;; The substituter failed as expected. Now make sure that | |
339 | ;; #:fallback? #t works correctly. | |
340 | (set-build-options s | |
341 | #:use-substitutes? #t | |
342 | #:fallback? #t) | |
343 | (and (build-derivations s (list d)) | |
344 | (equal? t (call-with-input-file o get-string-all))))) | |
345 | ;; Should fail. | |
346 | (build-derivations s (list d)) | |
347 | #f)))) | |
348 | ||
526382ff LC |
349 | (test-assert "export/import several paths" |
350 | (let* ((texts (unfold (cut >= <> 10) | |
351 | (lambda _ (random-text)) | |
352 | 1+ | |
353 | 0)) | |
354 | (files (map (cut add-text-to-store %store "text" <>) texts)) | |
355 | (dump (call-with-bytevector-output-port | |
356 | (cut export-paths %store files <>)))) | |
357 | (delete-paths %store files) | |
358 | (and (every (negate file-exists?) files) | |
359 | (let* ((source (open-bytevector-input-port dump)) | |
360 | (imported (import-paths %store source))) | |
361 | (and (equal? imported files) | |
362 | (every file-exists? files) | |
363 | (equal? texts | |
364 | (map (lambda (file) | |
365 | (call-with-input-file file | |
366 | get-string-all)) | |
367 | files))))))) | |
368 | ||
369 | (test-assert "import corrupt path" | |
370 | (let* ((text (random-text)) | |
371 | (file (add-text-to-store %store "text" text)) | |
372 | (dump (call-with-bytevector-output-port | |
373 | (cut export-paths %store (list file) <>)))) | |
374 | (delete-paths %store (list file)) | |
375 | ||
376 | ;; Flip a bit in the middle of the stream. | |
377 | (let* ((index (quotient (bytevector-length dump) 3)) | |
378 | (byte (bytevector-u8-ref dump index))) | |
379 | (bytevector-u8-set! dump index (logxor #xff byte))) | |
380 | ||
381 | (and (not (file-exists? file)) | |
382 | (guard (c ((nix-protocol-error? c) | |
383 | (pk 'c c) | |
384 | (and (not (zero? (nix-protocol-error-status c))) | |
385 | (string-contains (nix-protocol-error-message c) | |
386 | "corrupt")))) | |
387 | (let* ((source (open-bytevector-input-port dump)) | |
388 | (imported (import-paths %store source))) | |
389 | (pk 'corrupt-imported imported) | |
390 | #f))))) | |
391 | ||
3259877d LC |
392 | (test-end "store") |
393 | ||
394 | \f | |
395 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |