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