Commit | Line | Data |
---|---|---|
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 | 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)))) |
3259877d LC |
122 | (member p (dead-paths %store)))) |
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 | ||
fae31edc LC |
161 | (test-assert "references" |
162 | (let* ((t1 (add-text-to-store %store "random1" | |
cfbf9160 | 163 | (random-text))) |
fae31edc LC |
164 | (t2 (add-text-to-store %store "random2" |
165 | (random-text) (list t1)))) | |
166 | (and (equal? (list t1) (references %store t2)) | |
167 | (equal? (list t2) (referrers %store t1)) | |
168 | (null? (references %store t1)) | |
169 | (null? (referrers %store t2))))) | |
170 | ||
3f1e6939 LC |
171 | (test-assert "requisites" |
172 | (let* ((t1 (add-text-to-store %store "random1" | |
173 | (random-text) '())) | |
174 | (t2 (add-text-to-store %store "random2" | |
175 | (random-text) (list t1))) | |
176 | (t3 (add-text-to-store %store "random3" | |
177 | (random-text) (list t2))) | |
178 | (t4 (add-text-to-store %store "random4" | |
179 | (random-text) (list t1 t3)))) | |
180 | (define (same? x y) | |
181 | (and (= (length x) (length y)) | |
182 | (lset= equal? x y))) | |
183 | ||
184 | (and (same? (requisites %store t1) (list t1)) | |
185 | (same? (requisites %store t2) (list t1 t2)) | |
186 | (same? (requisites %store t3) (list t1 t2 t3)) | |
187 | (same? (requisites %store t4) (list t1 t2 t3 t4))))) | |
188 | ||
fae31edc LC |
189 | (test-assert "derivers" |
190 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
191 | (s (add-to-store %store "bash" #t "sha256" | |
192 | (search-bootstrap-binary "bash" | |
193 | (%current-system)))) | |
a987d2c0 LC |
194 | (d (derivation %store "the-thing" |
195 | s `("-e" ,b) | |
196 | #:env-vars `(("foo" . ,(random-text))) | |
197 | #:inputs `((,b) (,s)))) | |
59688fc4 | 198 | (o (derivation->output-path d))) |
fae31edc | 199 | (and (build-derivations %store (list d)) |
59688fc4 | 200 | (equal? (query-derivation-outputs %store (derivation-file-name d)) |
fae31edc LC |
201 | (list o)) |
202 | (equal? (valid-derivers %store o) | |
59688fc4 | 203 | (list (derivation-file-name d)))))) |
fae31edc | 204 | |
50add477 LC |
205 | (test-assert "topologically-sorted, one item" |
206 | (let* ((a (add-text-to-store %store "a" "a")) | |
207 | (b (add-text-to-store %store "b" "b" (list a))) | |
208 | (c (add-text-to-store %store "c" "c" (list b))) | |
209 | (d (add-text-to-store %store "d" "d" (list c))) | |
210 | (s (topologically-sorted %store (list d)))) | |
211 | (equal? s (list a b c d)))) | |
212 | ||
213 | (test-assert "topologically-sorted, several items" | |
214 | (let* ((a (add-text-to-store %store "a" "a")) | |
215 | (b (add-text-to-store %store "b" "b" (list a))) | |
216 | (c (add-text-to-store %store "c" "c" (list b))) | |
217 | (d (add-text-to-store %store "d" "d" (list c))) | |
218 | (s1 (topologically-sorted %store (list d a c b))) | |
219 | (s2 (topologically-sorted %store (list b d c a b d)))) | |
220 | (equal? s1 s2 (list a b c d)))) | |
221 | ||
222 | (test-assert "topologically-sorted, more difficult" | |
223 | (let* ((a (add-text-to-store %store "a" "a")) | |
224 | (b (add-text-to-store %store "b" "b" (list a))) | |
225 | (c (add-text-to-store %store "c" "c" (list b))) | |
226 | (d (add-text-to-store %store "d" "d" (list c))) | |
227 | (w (add-text-to-store %store "w" "w")) | |
228 | (x (add-text-to-store %store "x" "x" (list w))) | |
229 | (y (add-text-to-store %store "y" "y" (list x d))) | |
230 | (s1 (topologically-sorted %store (list y))) | |
231 | (s2 (topologically-sorted %store (list c y))) | |
232 | (s3 (topologically-sorted %store (cons y (references %store y))))) | |
58cbbe4b LC |
233 | ;; The order in which 'references' returns the references of Y is |
234 | ;; unspecified, so accommodate. | |
235 | (let* ((x-then-d? (equal? (references %store y) (list x d)))) | |
236 | (and (equal? s1 | |
237 | (if x-then-d? | |
238 | (list w x a b c d y) | |
239 | (list a b c d w x y))) | |
240 | (equal? s2 | |
241 | (if x-then-d? | |
242 | (list a b c w x d y) | |
243 | (list a b c d w x y))) | |
244 | (lset= string=? s1 s3))))) | |
50add477 | 245 | |
eddd4077 LC |
246 | (test-assert "log-file, derivation" |
247 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
248 | (s (add-to-store %store "bash" #t "sha256" | |
249 | (search-bootstrap-binary "bash" | |
250 | (%current-system)))) | |
251 | (d (derivation %store "the-thing" | |
252 | s `("-e" ,b) | |
253 | #:env-vars `(("foo" . ,(random-text))) | |
254 | #:inputs `((,b) (,s))))) | |
255 | (and (build-derivations %store (list d)) | |
256 | (file-exists? (pk (log-file %store (derivation-file-name d))))))) | |
257 | ||
258 | (test-assert "log-file, output file name" | |
259 | (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '())) | |
260 | (s (add-to-store %store "bash" #t "sha256" | |
261 | (search-bootstrap-binary "bash" | |
262 | (%current-system)))) | |
263 | (d (derivation %store "the-thing" | |
264 | s `("-e" ,b) | |
265 | #:env-vars `(("foo" . ,(random-text))) | |
266 | #:inputs `((,b) (,s)))) | |
267 | (o (derivation->output-path d))) | |
268 | (and (build-derivations %store (list d)) | |
269 | (file-exists? (pk (log-file %store o))) | |
270 | (string=? (log-file %store (derivation-file-name d)) | |
271 | (log-file %store o))))) | |
272 | ||
0f3d2504 LC |
273 | (test-assert "no substitutes" |
274 | (let* ((s (open-connection)) | |
275 | (d1 (package-derivation s %bootstrap-guile (%current-system))) | |
276 | (d2 (package-derivation s %bootstrap-glibc (%current-system))) | |
59688fc4 | 277 | (o (map derivation->output-path (list d1 d2)))) |
0f3d2504 | 278 | (set-build-options s #:use-substitutes? #f) |
59688fc4 LC |
279 | (and (not (has-substitutes? s (derivation-file-name d1))) |
280 | (not (has-substitutes? s (derivation-file-name d2))) | |
0f3d2504 LC |
281 | (null? (substitutable-paths s o)) |
282 | (null? (substitutable-path-info s o))))) | |
283 | ||
f65cf81a LC |
284 | (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1)) |
285 | ||
286 | (test-assert "substitute query" | |
287 | (let* ((s (open-connection)) | |
288 | (d (package-derivation s %bootstrap-guile (%current-system))) | |
59688fc4 | 289 | (o (derivation->output-path d)) |
f65cf81a LC |
290 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
291 | (compose uri-path string->uri)))) | |
292 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
293 | (call-with-output-file (string-append dir "/nix-cache-info") | |
294 | (lambda (p) | |
295 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
fe0cff14 | 296 | (%store-prefix)))) |
f65cf81a LC |
297 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) |
298 | ".narinfo") | |
299 | (lambda (p) | |
300 | (format p "StorePath: ~a | |
301 | URL: ~a | |
302 | Compression: none | |
303 | NarSize: 1234 | |
304 | References: | |
305 | System: ~a | |
306 | Deriver: ~a~%" | |
307 | o ; StorePath | |
308 | (string-append dir "/example.nar") ; URL | |
309 | (%current-system) ; System | |
59688fc4 LC |
310 | (basename |
311 | (derivation-file-name d))))) ; Deriver | |
f65cf81a | 312 | |
eba783b7 LC |
313 | ;; Remove entry from the local cache. |
314 | (false-if-exception | |
315 | (delete-file (string-append (getenv "XDG_CACHE_HOME") | |
316 | "/guix/substitute-binary/" | |
317 | (store-path-hash-part o)))) | |
318 | ||
f65cf81a LC |
319 | ;; Make sure `substitute-binary' correctly communicates the above data. |
320 | (set-build-options s #:use-substitutes? #t) | |
321 | (and (has-substitutes? s o) | |
322 | (equal? (list o) (substitutable-paths s (list o))) | |
323 | (match (pk 'spi (substitutable-path-info s (list o))) | |
324 | (((? substitutable? s)) | |
59688fc4 | 325 | (and (string=? (substitutable-deriver s) (derivation-file-name d)) |
f65cf81a LC |
326 | (null? (substitutable-references s)) |
327 | (equal? (substitutable-nar-size s) 1234))))))) | |
328 | ||
fe0cff14 LC |
329 | (test-assert "substitute" |
330 | (let* ((s (open-connection)) | |
331 | (c (random-text)) ; contents of the output | |
332 | (d (build-expression->derivation | |
dd1a5a15 | 333 | s "substitute-me" |
fe0cff14 LC |
334 | `(call-with-output-file %output |
335 | (lambda (p) | |
336 | (exit 1) ; would actually fail | |
337 | (display ,c p))) | |
fe0cff14 LC |
338 | #:guile-for-build |
339 | (package-derivation s %bootstrap-guile (%current-system)))) | |
59688fc4 | 340 | (o (derivation->output-path d)) |
fe0cff14 LC |
341 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
342 | (compose uri-path string->uri)))) | |
343 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
344 | (call-with-output-file (string-append dir "/nix-cache-info") | |
345 | (lambda (p) | |
346 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
347 | (%store-prefix)))) | |
348 | (call-with-output-file (string-append dir "/example.out") | |
349 | (lambda (p) | |
350 | (display c p))) | |
351 | (call-with-output-file (string-append dir "/example.nar") | |
352 | (lambda (p) | |
353 | (write-file (string-append dir "/example.out") p))) | |
354 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) | |
355 | ".narinfo") | |
356 | (lambda (p) | |
357 | (format p "StorePath: ~a | |
358 | URL: ~a | |
359 | Compression: none | |
360 | NarSize: 1234 | |
361 | NarHash: sha256:~a | |
362 | References: | |
363 | System: ~a | |
364 | Deriver: ~a~%" | |
365 | o ; StorePath | |
366 | "example.nar" ; relative URL | |
367 | (call-with-input-file (string-append dir "/example.nar") | |
368 | (compose bytevector->nix-base32-string sha256 | |
369 | get-bytevector-all)) | |
370 | (%current-system) ; System | |
59688fc4 LC |
371 | (basename |
372 | (derivation-file-name d))))) ; Deriver | |
fe0cff14 LC |
373 | |
374 | ;; Make sure we use `substitute-binary'. | |
375 | (set-build-options s #:use-substitutes? #t) | |
376 | (and (has-substitutes? s o) | |
377 | (build-derivations s (list d)) | |
378 | (equal? c (call-with-input-file o get-string-all))))) | |
379 | ||
491e6de7 LC |
380 | (test-assert "substitute, corrupt output hash" |
381 | ;; Tweak the substituter into installing a substitute whose hash doesn't | |
382 | ;; match the one announced in the narinfo. The daemon must notice this and | |
383 | ;; raise an error. | |
384 | (let* ((s (open-connection)) | |
385 | (c "hello, world") ; contents of the output | |
386 | (d (build-expression->derivation | |
387 | s "corrupt-substitute" | |
388 | `(mkdir %output) | |
389 | #:guile-for-build | |
390 | (package-derivation s %bootstrap-guile (%current-system)))) | |
391 | (o (derivation->output-path d)) | |
392 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") | |
393 | (compose uri-path string->uri)))) | |
394 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
395 | (call-with-output-file (string-append dir "/nix-cache-info") | |
396 | (lambda (p) | |
397 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
398 | (%store-prefix)))) | |
399 | (call-with-output-file (string-append dir "/example.out") | |
400 | (lambda (p) | |
401 | (display "The contents here do not match C." p))) | |
402 | (call-with-output-file (string-append dir "/example.nar") | |
403 | (lambda (p) | |
404 | (write-file (string-append dir "/example.out") p))) | |
405 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) | |
406 | ".narinfo") | |
407 | (lambda (p) | |
408 | (format p "StorePath: ~a | |
409 | URL: ~a | |
410 | Compression: none | |
411 | NarSize: 1234 | |
412 | NarHash: sha256:~a | |
413 | References: | |
414 | System: ~a | |
415 | Deriver: ~a~%" | |
416 | o ; StorePath | |
417 | "example.nar" ; relative URL | |
418 | (bytevector->nix-base32-string | |
419 | (sha256 (string->utf8 c))) | |
420 | (%current-system) ; System | |
421 | (basename | |
422 | (derivation-file-name d))))) ; Deriver | |
423 | ||
424 | ;; Make sure we use `substitute-binary'. | |
425 | (set-build-options s | |
426 | #:use-substitutes? #t | |
427 | #:fallback? #f) | |
428 | (and (has-substitutes? s o) | |
429 | (guard (c ((nix-protocol-error? c) | |
430 | ;; XXX: the daemon writes "hash mismatch in downloaded | |
431 | ;; path", but the actual error returned to the client | |
432 | ;; doesn't mention that. | |
433 | (pk 'corrupt c) | |
434 | (not (zero? (nix-protocol-error-status c))))) | |
435 | (build-derivations s (list d)) | |
436 | #f)))) | |
437 | ||
c3eb878f LC |
438 | (test-assert "substitute --fallback" |
439 | (let* ((s (open-connection)) | |
440 | (t (random-text)) ; contents of the output | |
441 | (d (build-expression->derivation | |
dd1a5a15 | 442 | s "substitute-me-not" |
c3eb878f LC |
443 | `(call-with-output-file %output |
444 | (lambda (p) | |
445 | (display ,t p))) | |
c3eb878f LC |
446 | #:guile-for-build |
447 | (package-derivation s %bootstrap-guile (%current-system)))) | |
59688fc4 | 448 | (o (derivation->output-path d)) |
c3eb878f LC |
449 | (dir (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") |
450 | (compose uri-path string->uri)))) | |
451 | ;; Create fake substituter data, to be read by `substitute-binary'. | |
452 | (call-with-output-file (string-append dir "/nix-cache-info") | |
453 | (lambda (p) | |
454 | (format p "StoreDir: ~a\nWantMassQuery: 0\n" | |
455 | (%store-prefix)))) | |
456 | (call-with-output-file (string-append dir "/" (store-path-hash-part o) | |
457 | ".narinfo") | |
458 | (lambda (p) | |
459 | (format p "StorePath: ~a | |
460 | URL: ~a | |
461 | Compression: none | |
462 | NarSize: 1234 | |
463 | NarHash: sha256:0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73 | |
464 | References: | |
465 | System: ~a | |
466 | Deriver: ~a~%" | |
467 | o ; StorePath | |
468 | "does-not-exist.nar" ; relative URL | |
469 | (%current-system) ; System | |
59688fc4 LC |
470 | (basename |
471 | (derivation-file-name d))))) ; Deriver | |
c3eb878f LC |
472 | |
473 | ;; Make sure we use `substitute-binary'. | |
474 | (set-build-options s #:use-substitutes? #t) | |
475 | (and (has-substitutes? s o) | |
476 | (guard (c ((nix-protocol-error? c) | |
477 | ;; The substituter failed as expected. Now make sure that | |
478 | ;; #:fallback? #t works correctly. | |
479 | (set-build-options s | |
480 | #:use-substitutes? #t | |
481 | #:fallback? #t) | |
482 | (and (build-derivations s (list d)) | |
483 | (equal? t (call-with-input-file o get-string-all))))) | |
484 | ;; Should fail. | |
485 | (build-derivations s (list d)) | |
486 | #f)))) | |
487 | ||
526382ff LC |
488 | (test-assert "export/import several paths" |
489 | (let* ((texts (unfold (cut >= <> 10) | |
490 | (lambda _ (random-text)) | |
491 | 1+ | |
492 | 0)) | |
493 | (files (map (cut add-text-to-store %store "text" <>) texts)) | |
494 | (dump (call-with-bytevector-output-port | |
495 | (cut export-paths %store files <>)))) | |
496 | (delete-paths %store files) | |
497 | (and (every (negate file-exists?) files) | |
498 | (let* ((source (open-bytevector-input-port dump)) | |
499 | (imported (import-paths %store source))) | |
500 | (and (equal? imported files) | |
501 | (every file-exists? files) | |
502 | (equal? texts | |
503 | (map (lambda (file) | |
504 | (call-with-input-file file | |
505 | get-string-all)) | |
506 | files))))))) | |
507 | ||
99fbddf9 | 508 | (test-assert "export/import paths, ensure topological order" |
cafb92d8 LC |
509 | (let* ((file0 (add-text-to-store %store "baz" (random-text))) |
510 | (file1 (add-text-to-store %store "foo" (random-text) | |
511 | (list file0))) | |
99fbddf9 LC |
512 | (file2 (add-text-to-store %store "bar" (random-text) |
513 | (list file1))) | |
514 | (files (list file1 file2)) | |
515 | (dump1 (call-with-bytevector-output-port | |
516 | (cute export-paths %store (list file1 file2) <>))) | |
517 | (dump2 (call-with-bytevector-output-port | |
518 | (cute export-paths %store (list file2 file1) <>)))) | |
519 | (delete-paths %store files) | |
520 | (and (every (negate file-exists?) files) | |
521 | (bytevector=? dump1 dump2) | |
522 | (let* ((source (open-bytevector-input-port dump1)) | |
523 | (imported (import-paths %store source))) | |
cafb92d8 | 524 | ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0. |
99fbddf9 LC |
525 | (and (equal? imported (list file1 file2)) |
526 | (every file-exists? files) | |
cafb92d8 | 527 | (equal? (list file0) (references %store file1)) |
99fbddf9 LC |
528 | (equal? (list file1) (references %store file2))))))) |
529 | ||
526382ff LC |
530 | (test-assert "import corrupt path" |
531 | (let* ((text (random-text)) | |
532 | (file (add-text-to-store %store "text" text)) | |
533 | (dump (call-with-bytevector-output-port | |
534 | (cut export-paths %store (list file) <>)))) | |
535 | (delete-paths %store (list file)) | |
536 | ||
6df1fb89 LC |
537 | ;; Flip a bit in the stream's payload. |
538 | (let* ((index (quotient (bytevector-length dump) 4)) | |
526382ff LC |
539 | (byte (bytevector-u8-ref dump index))) |
540 | (bytevector-u8-set! dump index (logxor #xff byte))) | |
541 | ||
542 | (and (not (file-exists? file)) | |
543 | (guard (c ((nix-protocol-error? c) | |
544 | (pk 'c c) | |
545 | (and (not (zero? (nix-protocol-error-status c))) | |
546 | (string-contains (nix-protocol-error-message c) | |
547 | "corrupt")))) | |
548 | (let* ((source (open-bytevector-input-port dump)) | |
549 | (imported (import-paths %store source))) | |
550 | (pk 'corrupt-imported imported) | |
551 | #f))))) | |
552 | ||
6bfec3ed LC |
553 | (test-assert "register-path" |
554 | (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f) | |
555 | "-fake"))) | |
556 | (when (valid-path? %store file) | |
557 | (delete-paths %store (list file))) | |
558 | (false-if-exception (delete-file file)) | |
559 | ||
560 | (let ((ref (add-text-to-store %store "ref-of-fake" (random-text))) | |
561 | (drv (string-append file ".drv"))) | |
562 | (call-with-output-file file | |
563 | (cut display "This is a fake store item.\n" <>)) | |
564 | (register-path file | |
565 | #:references (list ref) | |
566 | #:deriver drv) | |
567 | ||
568 | (and (valid-path? %store file) | |
569 | (equal? (references %store file) (list ref)) | |
570 | (null? (valid-derivers %store file)) | |
571 | (null? (referrers %store file)))))) | |
572 | ||
3259877d LC |
573 | (test-end "store") |
574 | ||
575 | \f | |
576 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |