Move base32 code to (guix base32).
[jackhill/guix/guix.git] / tests / derivations.scm
CommitLineData
341c6fdd
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; Guix is free software; you can redistribute it and/or modify it
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;;;
11;;; Guix is distributed in the hope that it will be useful, but
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
17;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
18
19
20(define-module (test-derivations)
21 #:use-module (guix derivations)
26bbbb95 22 #:use-module (guix store)
de4c3f26 23 #:use-module (guix utils)
ddc29a78 24 #:use-module (guix base32)
b272c474 25 #:use-module ((guix packages) #:select (package-derivation))
18633d4f 26 #:use-module (distro packages bootstrap)
b37eb5ed 27 #:use-module (srfi srfi-1)
fb3eec83 28 #:use-module (srfi srfi-11)
341c6fdd 29 #:use-module (srfi srfi-26)
99634e3f 30 #:use-module (srfi srfi-34)
341c6fdd 31 #:use-module (srfi srfi-64)
fb3eec83 32 #:use-module (rnrs io ports)
749c6567 33 #:use-module (rnrs bytevectors)
b37eb5ed 34 #:use-module (ice-9 rdelim)
db393b33 35 #:use-module (ice-9 regex)
99634e3f
LC
36 #:use-module (ice-9 ftw)
37 #:use-module (ice-9 match))
341c6fdd 38
26bbbb95
LC
39(define %store
40 (false-if-exception (open-connection)))
41
b272c474 42(when %store
81dbd783
LC
43 ;; Make sure we build everything by ourselves.
44 (set-build-options %store #:use-substitutes? #f)
45
b272c474
LC
46 ;; By default, use %BOOTSTRAP-GUILE for the current system.
47 (let ((drv (package-derivation %store %bootstrap-guile)))
48 (%guile-for-build drv)))
49
b37eb5ed
LC
50(define (directory-contents dir)
51 "Return an alist representing the contents of DIR."
52 (define prefix-len (string-length dir))
53 (sort (file-system-fold (const #t) ; enter?
54 (lambda (path stat result) ; leaf
55 (alist-cons (string-drop path prefix-len)
56 (call-with-input-file path
57 get-bytevector-all)
58 result))
59 (lambda (path stat result) result) ; down
60 (lambda (path stat result) result) ; up
61 (lambda (path stat result) result) ; skip
62 (lambda (path stat errno result) result) ; error
63 '()
64 dir)
65 (lambda (e1 e2)
66 (string<? (car e1) (car e2)))))
67
341c6fdd
LC
68(test-begin "derivations")
69
70(test-assert "parse & export"
33594aa4
LC
71 (let* ((f (search-path %load-path "tests/test.drv"))
72 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
73 (d1 (read-derivation (open-bytevector-input-port b1)))
74 (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
75 (d2 (read-derivation (open-bytevector-input-port b2))))
76 (and (equal? b1 b2)
77 (equal? d1 d2))))
78
b37eb5ed
LC
79(test-skip (if %store 0 4))
80
d1b1c424
LC
81(test-assert "add-to-store, flat"
82 (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
83 (drv (add-to-store %store "flat-test" #t #f "sha256" file)))
84 (and (eq? 'regular (stat:type (stat drv)))
31ef99a8 85 (valid-path? %store drv)
d1b1c424
LC
86 (equal? (call-with-input-file file get-bytevector-all)
87 (call-with-input-file drv get-bytevector-all)))))
88
b37eb5ed
LC
89(test-assert "add-to-store, recursive"
90 (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
91 (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
92 (and (eq? 'directory (stat:type (stat drv)))
31ef99a8 93 (valid-path? %store drv)
b37eb5ed
LC
94 (equal? (directory-contents dir)
95 (directory-contents drv)))))
26bbbb95
LC
96
97(test-assert "derivation with no inputs"
31ef99a8
LC
98 (let* ((builder (add-text-to-store %store "my-builder.sh"
99 "#!/bin/sh\necho hello, world\n"
100 '()))
101 (drv-path (derivation %store "foo" (%current-system) builder
102 '() '(("HOME" . "/homeless")) '())))
103 (and (store-path? drv-path)
104 (valid-path? %store drv-path))))
26bbbb95 105
fb3eec83
LC
106(test-assert "build derivation with 1 source"
107 (let*-values (((builder)
108 (add-text-to-store %store "my-builder.sh"
de4c3f26 109 "echo hello, world > \"$out\"\n"
fb3eec83
LC
110 '()))
111 ((drv-path drv)
98090557 112 (derivation %store "foo" (%current-system)
fb3eec83 113 "/bin/sh" `(,builder)
af7f9e5f
LC
114 '(("HOME" . "/homeless")
115 ("zzz" . "Z!")
116 ("AAA" . "A!"))
fb3eec83
LC
117 `((,builder))))
118 ((succeeded?)
119 (build-derivations %store (list drv-path))))
120 (and succeeded?
121 (let ((path (derivation-output-path
122 (assoc-ref (derivation-outputs drv) "out"))))
31ef99a8
LC
123 (and (valid-path? %store path)
124 (string=? (call-with-input-file path read-line)
125 "hello, world"))))))
fb3eec83 126
749c6567
LC
127(test-assert "fixed-output derivation"
128 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
129 "echo -n hello > $out" '()))
130 (hash (sha256 (string->utf8 "hello")))
98090557 131 (drv-path (derivation %store "fixed" (%current-system)
749c6567 132 "/bin/sh" `(,builder)
813986ac
LC
133 '()
134 `((,builder)) ; optional
749c6567
LC
135 #:hash hash #:hash-algo 'sha256))
136 (succeeded? (build-derivations %store (list drv-path))))
137 (and succeeded?
138 (let ((p (derivation-path->output-path drv-path)))
82058eff
LC
139 (and (equal? (string->utf8 "hello")
140 (call-with-input-file p get-bytevector-all))
141 (bytevector? (query-path-hash %store p)))))))
749c6567 142
813986ac
LC
143(test-assert "fixed-output derivation: output paths are equal"
144 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
145 "echo -n hello > $out" '()))
146 (builder2 (add-text-to-store %store "fixed-builder2.sh"
147 "echo hey; echo -n hello > $out" '()))
148 (hash (sha256 (string->utf8 "hello")))
149 (drv-path1 (derivation %store "fixed" (%current-system)
150 "/bin/sh" `(,builder1)
151 '() `()
152 #:hash hash #:hash-algo 'sha256))
153 (drv-path2 (derivation %store "fixed" (%current-system)
154 "/bin/sh" `(,builder2)
155 '() `()
156 #:hash hash #:hash-algo 'sha256))
157 (succeeded? (build-derivations %store
158 (list drv-path1 drv-path2))))
159 (and succeeded?
160 (equal? (derivation-path->output-path drv-path1)
161 (derivation-path->output-path drv-path2)))))
162
163(test-assert "derivation with a fixed-output input"
164 ;; A derivation D using a fixed-output derivation F doesn't has the same
165 ;; output path when passed F or F', as long as F and F' have the same output
166 ;; path.
167 (let* ((builder1 (add-text-to-store %store "fixed-builder1.sh"
168 "echo -n hello > $out" '()))
169 (builder2 (add-text-to-store %store "fixed-builder2.sh"
170 "echo hey; echo -n hello > $out" '()))
171 (hash (sha256 (string->utf8 "hello")))
172 (fixed1 (derivation %store "fixed" (%current-system)
173 "/bin/sh" `(,builder1)
174 '() `()
175 #:hash hash #:hash-algo 'sha256))
176 (fixed2 (derivation %store "fixed" (%current-system)
177 "/bin/sh" `(,builder2)
178 '() `()
179 #:hash hash #:hash-algo 'sha256))
180 (fixed-out (derivation-path->output-path fixed1))
181 (builder3 (add-text-to-store
182 %store "final-builder.sh"
183 ;; Use Bash hackery to avoid Coreutils.
184 "echo $in ; (read -u 3 c; echo $c) 3< $in > $out" '()))
185 (final1 (derivation %store "final" (%current-system)
186 "/bin/sh" `(,builder3)
187 `(("in" . ,fixed-out))
188 `((,builder3) (,fixed1))))
189 (final2 (derivation %store "final" (%current-system)
190 "/bin/sh" `(,builder3)
191 `(("in" . ,fixed-out))
192 `((,builder3) (,fixed2))))
193 (succeeded? (build-derivations %store
194 (list final1 final2))))
195 (and succeeded?
196 (equal? (derivation-path->output-path final1)
197 (derivation-path->output-path final2)))))
198
7946c4e7
LC
199(test-assert "multiple-output derivation"
200 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
201 "echo one > $out ; echo two > $second"
202 '()))
98090557 203 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
204 "/bin/sh" `(,builder)
205 '(("HOME" . "/homeless")
206 ("zzz" . "Z!")
207 ("AAA" . "A!"))
208 `((,builder))
209 #:outputs '("out" "second")))
210 (succeeded? (build-derivations %store (list drv-path))))
211 (and succeeded?
212 (let ((one (derivation-path->output-path drv-path "out"))
213 (two (derivation-path->output-path drv-path "second")))
214 (and (eq? 'one (call-with-input-file one read))
215 (eq? 'two (call-with-input-file two read)))))))
216
4b1786aa
LC
217(test-assert "multiple-output derivation, non-alphabetic order"
218 ;; Here, the outputs are not listed in alphabetic order. Yet, the store
219 ;; path computation must reorder them first.
220 (let* ((builder (add-text-to-store %store "my-fixed-builder.sh"
221 "echo one > $out ; echo two > $AAA"
222 '()))
223 (drv-path (derivation %store "fixed" (%current-system)
224 "/bin/sh" `(,builder)
225 '()
226 `((,builder))
227 #:outputs '("out" "AAA")))
228 (succeeded? (build-derivations %store (list drv-path))))
229 (and succeeded?
230 (let ((one (derivation-path->output-path drv-path "out"))
231 (two (derivation-path->output-path drv-path "AAA")))
232 (and (eq? 'one (call-with-input-file one read))
233 (eq? 'two (call-with-input-file two read)))))))
234
d66ac374
LC
235(test-assert "user of multiple-output derivation"
236 ;; Check whether specifying several inputs coming from the same
237 ;; multiple-output derivation works.
238 (let* ((builder1 (add-text-to-store %store "my-mo-builder.sh"
239 "echo one > $out ; echo two > $two"
240 '()))
241 (mdrv (derivation %store "multiple-output" (%current-system)
242 "/bin/sh" `(,builder1)
243 '()
244 `((,builder1))
245 #:outputs '("out" "two")))
246 (builder2 (add-text-to-store %store "my-mo-user-builder.sh"
247 "read x < $one;
248 read y < $two;
249 echo \"($x $y)\" > $out"
250 '()))
251 (udrv (derivation %store "multiple-output-user"
252 (%current-system)
253 "/bin/sh" `(,builder2)
254 `(("one" . ,(derivation-path->output-path
255 mdrv "out"))
256 ("two" . ,(derivation-path->output-path
257 mdrv "two")))
258 `((,builder2)
259 ;; two occurrences of MDRV:
260 (,mdrv)
261 (,mdrv "two")))))
262 (and (build-derivations %store (list (pk 'udrv udrv)))
263 (let ((p (derivation-path->output-path udrv)))
264 (and (valid-path? %store p)
265 (equal? '(one two) (call-with-input-file p read)))))))
266
de4c3f26
LC
267\f
268(define %coreutils
8f3ecbd7 269 (false-if-exception
18633d4f 270 (or (package-derivation %store %bootstrap-coreutils&co)
8f3ecbd7 271 (nixpkgs-derivation "coreutils"))))
de4c3f26
LC
272
273(test-skip (if %coreutils 0 1))
274
275(test-assert "build derivation with coreutils"
276 (let* ((builder
277 (add-text-to-store %store "build-with-coreutils.sh"
278 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
279 '()))
280 (drv-path
98090557 281 (derivation %store "foo" (%current-system)
de4c3f26
LC
282 "/bin/sh" `(,builder)
283 `(("PATH" .
284 ,(string-append
285 (derivation-path->output-path %coreutils)
286 "/bin")))
287 `((,builder)
288 (,%coreutils))))
289 (succeeded?
290 (build-derivations %store (list drv-path))))
291 (and succeeded?
292 (let ((p (derivation-path->output-path drv-path)))
31ef99a8
LC
293 (and (valid-path? %store p)
294 (file-exists? (string-append p "/good")))))))
de4c3f26 295
813986ac 296(test-skip (if (%guile-for-build) 0 7))
9a20830e
LC
297
298(test-assert "build-expression->derivation and derivation-prerequisites"
299 (let-values (((drv-path drv)
300 (build-expression->derivation %store "fail" (%current-system)
301 #f '())))
302 (any (match-lambda
303 (($ <derivation-input> path)
304 (string=? path (%guile-for-build))))
305 (derivation-prerequisites drv))))
d9085c23
LC
306
307(test-assert "build-expression->derivation without inputs"
308 (let* ((builder '(begin
309 (mkdir %output)
310 (call-with-output-file (string-append %output "/test")
311 (lambda (p)
312 (display '(hello guix) p)))))
98090557 313 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
314 builder '()))
315 (succeeded? (build-derivations %store (list drv-path))))
316 (and succeeded?
317 (let ((p (derivation-path->output-path drv-path)))
318 (equal? '(hello guix)
319 (call-with-input-file (string-append p "/test") read))))))
320
9a20830e
LC
321(test-assert "build-expression->derivation and derivation-prerequisites-to-build"
322 (let-values (((drv-path drv)
323 (build-expression->derivation %store "fail" (%current-system)
324 #f '())))
325 ;; The only direct dependency is (%guile-for-build) and it's already
326 ;; built.
327 (null? (derivation-prerequisites-to-build %store drv))))
328
db393b33
LC
329(test-assert "build-expression->derivation with expression returning #f"
330 (let* ((builder '(begin
331 (mkdir %output)
332 #f)) ; fail!
333 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
334 builder '()))
335 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
336 (guard (c ((nix-protocol-error? c)
337 ;; Note that the output path may exist at this point, but it
338 ;; is invalid.
31ef99a8
LC
339 (and (string-match "build .* failed"
340 (nix-protocol-error-message c))
341 (not (valid-path? %store out-path)))))
db393b33
LC
342 (build-derivations %store (list drv-path))
343 #f)))
344
9bc07f4d
LC
345(test-assert "build-expression->derivation with two outputs"
346 (let* ((builder '(begin
347 (call-with-output-file (assoc-ref %outputs "out")
348 (lambda (p)
349 (display '(hello) p)))
350 (call-with-output-file (assoc-ref %outputs "second")
351 (lambda (p)
352 (display '(world) p)))))
353 (drv-path (build-expression->derivation %store "double"
98090557 354 (%current-system)
9bc07f4d
LC
355 builder '()
356 #:outputs '("out"
357 "second")))
358 (succeeded? (build-derivations %store (list drv-path))))
359 (and succeeded?
360 (let ((one (derivation-path->output-path drv-path))
361 (two (derivation-path->output-path drv-path "second")))
362 (and (equal? '(hello) (call-with-input-file one read))
363 (equal? '(world) (call-with-input-file two read)))))))
364
d9085c23
LC
365(test-assert "build-expression->derivation with one input"
366 (let* ((builder '(call-with-output-file %output
367 (lambda (p)
368 (let ((cu (assoc-ref %build-inputs "cu")))
369 (close 1)
370 (dup2 (port->fdes p) 1)
371 (execl (string-append cu "/bin/uname")
372 "uname" "-a")))))
98090557 373 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 374 builder
2acb2cb6 375 `(("cu" ,%coreutils))))
d9085c23
LC
376 (succeeded? (build-derivations %store (list drv-path))))
377 (and succeeded?
378 (let ((p (derivation-path->output-path drv-path)))
379 (string-contains (call-with-input-file p read-line) "GNU")))))
380
99634e3f
LC
381(test-assert "imported-files"
382 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
383 ("a/b/c" . ,(search-path %load-path
384 "guix/derivations.scm"))
224f7ad6
LC
385 ("p/q" . ,(search-path %load-path "guix.scm"))
386 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
387 (drv-path (imported-files %store files)))
388 (and (build-derivations %store (list drv-path))
389 (let ((dir (derivation-path->output-path drv-path)))
390 (every (match-lambda
391 ((path . source)
392 (equal? (call-with-input-file (string-append dir "/" path)
393 get-bytevector-all)
394 (call-with-input-file source
395 get-bytevector-all))))
396 files)))))
397
d9024884
LC
398(test-assert "build-expression->derivation with modules"
399 (let* ((builder `(begin
400 (use-modules (guix build utils))
401 (let ((out (assoc-ref %outputs "out")))
402 (mkdir-p (string-append out "/guile/guix/nix"))
403 #t)))
404 (drv-path (build-expression->derivation %store
405 "test-with-modules"
406 (%current-system)
407 builder '()
408 #:modules
409 '((guix build utils)))))
410 (and (build-derivations %store (list drv-path))
411 (let* ((p (derivation-path->output-path drv-path))
412 (s (stat (string-append p "/guile/guix/nix"))))
413 (eq? (stat:type s) 'directory)))))
414
26b969de
LC
415(test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
416 0
417 1))
418
419(test-assert "build-expression->derivation for fixed-output derivation"
420 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
0e383c76
LC
421 (builder
422 `(begin
423 (use-modules (web client) (web uri)
424 (rnrs io ports) (srfi srfi-11))
425 (let-values (((resp bv)
426 (http-get (string->uri ,url) #:decode-body? #f)))
427 (call-with-output-file %output
428 (lambda (p)
429 (put-bytevector p bv))))))
26b969de 430 (drv-path (build-expression->derivation
98090557 431 %store "hello-2.8.tar.gz" (%current-system) builder '()
26b969de
LC
432 #:hash (nix-base32-string->bytevector
433 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
434 #:hash-algo 'sha256))
435 (succeeded? (build-derivations %store (list drv-path))))
436 (and succeeded?
437 (file-exists? (derivation-path->output-path drv-path)))))
438
813986ac
LC
439(test-assert "build-expression->derivation: same fixed-output path"
440 (let* ((builder1 '(call-with-output-file %output
441 (lambda (p)
442 (write "hello" p))))
443 (builder2 '(call-with-output-file (pk 'difference-here! %output)
444 (lambda (p)
445 (write "hello" p))))
446 (hash (sha256 (string->utf8 "hello")))
447 (input1 (build-expression->derivation %store "fixed"
448 (%current-system)
449 builder1 '()
450 #:hash hash
451 #:hash-algo 'sha256))
452 (input2 (build-expression->derivation %store "fixed"
453 (%current-system)
454 builder2 '()
455 #:hash hash
456 #:hash-algo 'sha256))
457 (succeeded? (build-derivations %store (list input1 input2))))
458 (and succeeded?
459 (not (string=? input1 input2))
460 (string=? (derivation-path->output-path input1)
461 (derivation-path->output-path input2)))))
462
7bdd1f0e
LC
463(test-assert "build-expression->derivation with a fixed-output input"
464 (let* ((builder1 '(call-with-output-file %output
465 (lambda (p)
466 (write "hello" p))))
467 (builder2 '(call-with-output-file (pk 'difference-here! %output)
468 (lambda (p)
469 (write "hello" p))))
470 (hash (sha256 (string->utf8 "hello")))
471 (input1 (build-expression->derivation %store "fixed"
472 (%current-system)
473 builder1 '()
474 #:hash hash
475 #:hash-algo 'sha256))
476 (input2 (build-expression->derivation %store "fixed"
477 (%current-system)
478 builder2 '()
479 #:hash hash
480 #:hash-algo 'sha256))
481 (builder3 '(let ((input (assoc-ref %build-inputs "input")))
482 (call-with-output-file %output
483 (lambda (out)
484 (format #f "My input is ~a.~%" input)))))
485 (final1 (build-expression->derivation %store "final"
486 (%current-system)
487 builder3
488 `(("input" ,input1))))
489 (final2 (build-expression->derivation %store "final"
490 (%current-system)
491 builder3
492 `(("input" ,input2)))))
493 (and (string=? (derivation-path->output-path final1)
494 (derivation-path->output-path final2))
495 (build-derivations %store (list final1 final2)))))
496
341c6fdd
LC
497(test-end)
498
499\f
500(exit (= (test-runner-fail-count (test-runner-current)) 0))
501
502;;; Local Variables:
503;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 504;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 505;;; End: