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