Unset `LD_LIBRARY_PATH' in `build-expression->derivation'.
[guix.git] / tests / derivations.scm
blob097b9d7d282add5f07d86c65cabc8c4f613550fd
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/>.
20 (define-module (test-derivations)
21   #:use-module (guix derivations)
22   #:use-module (guix store)
23   #:use-module (guix utils)
24   #:use-module (srfi srfi-1)
25   #:use-module (srfi srfi-11)
26   #:use-module (srfi srfi-26)
27   #:use-module (srfi srfi-34)
28   #:use-module (srfi srfi-64)
29   #:use-module (rnrs io ports)
30   #:use-module (rnrs bytevectors)
31   #:use-module (ice-9 rdelim)
32   #:use-module (ice-9 regex)
33   #:use-module (ice-9 ftw)
34   #:use-module (ice-9 match))
36 (define %store
37   (false-if-exception (open-connection)))
39 (define (directory-contents dir)
40   "Return an alist representing the contents of DIR."
41   (define prefix-len (string-length dir))
42   (sort (file-system-fold (const #t)                   ; enter?
43                           (lambda (path stat result)   ; leaf
44                             (alist-cons (string-drop path prefix-len)
45                                         (call-with-input-file path
46                                           get-bytevector-all)
47                                         result))
48                           (lambda (path stat result) result)      ; down
49                           (lambda (path stat result) result)      ; up
50                           (lambda (path stat result) result)      ; skip
51                           (lambda (path stat errno result) result) ; error
52                           '()
53                           dir)
54         (lambda (e1 e2)
55           (string<? (car e1) (car e2)))))
57 (test-begin "derivations")
59 (test-assert "parse & export"
60   (let* ((f  (search-path %load-path "tests/test.drv"))
61          (b1 (call-with-input-file f get-bytevector-all))
62          (d1 (read-derivation (open-bytevector-input-port b1)))
63          (b2 (call-with-bytevector-output-port (cut write-derivation d1 <>)))
64          (d2 (read-derivation (open-bytevector-input-port b2))))
65     (and (equal? b1 b2)
66          (equal? d1 d2))))
68 (test-skip (if %store 0 4))
70 (test-assert "add-to-store, flat"
71   (let* ((file (search-path %load-path "language/tree-il/spec.scm"))
72          (drv  (add-to-store %store "flat-test" #t #f "sha256" file)))
73     (and (eq? 'regular (stat:type (stat drv)))
74          (valid-path? %store drv)
75          (equal? (call-with-input-file file get-bytevector-all)
76                  (call-with-input-file drv get-bytevector-all)))))
78 (test-assert "add-to-store, recursive"
79   (let* ((dir (dirname (search-path %load-path "language/tree-il/spec.scm")))
80          (drv (add-to-store %store "dir-tree-test" #t #t "sha256" dir)))
81     (and (eq? 'directory (stat:type (stat drv)))
82          (valid-path? %store drv)
83          (equal? (directory-contents dir)
84                  (directory-contents drv)))))
86 (test-assert "derivation with no inputs"
87   (let* ((builder  (add-text-to-store %store "my-builder.sh"
88                                       "#!/bin/sh\necho hello, world\n"
89                                       '()))
90          (drv-path (derivation %store "foo" (%current-system) builder
91                                '() '(("HOME" . "/homeless")) '())))
92     (and (store-path? drv-path)
93          (valid-path? %store drv-path))))
95 (test-assert "build derivation with 1 source"
96   (let*-values (((builder)
97                  (add-text-to-store %store "my-builder.sh"
98                                     "echo hello, world > \"$out\"\n"
99                                     '()))
100                 ((drv-path drv)
101                  (derivation %store "foo" (%current-system)
102                              "/bin/sh" `(,builder)
103                              '(("HOME" . "/homeless")
104                                ("zzz"  . "Z!")
105                                ("AAA"  . "A!"))
106                              `((,builder))))
107                 ((succeeded?)
108                  (build-derivations %store (list drv-path))))
109     (and succeeded?
110          (let ((path (derivation-output-path
111                       (assoc-ref (derivation-outputs drv) "out"))))
112            (and (valid-path? %store path)
113                 (string=? (call-with-input-file path read-line)
114                           "hello, world"))))))
116 (test-assert "fixed-output derivation"
117   (let* ((builder    (add-text-to-store %store "my-fixed-builder.sh"
118                                         "echo -n hello > $out" '()))
119          (hash       (sha256 (string->utf8 "hello")))
120          (drv-path   (derivation %store "fixed" (%current-system)
121                                  "/bin/sh" `(,builder)
122                                  '() `((,builder))
123                                  #:hash hash #:hash-algo 'sha256))
124          (succeeded? (build-derivations %store (list drv-path))))
125     (and succeeded?
126          (let ((p (derivation-path->output-path drv-path)))
127            (equal? (string->utf8 "hello")
128                    (call-with-input-file p get-bytevector-all))))))
130 (test-assert "multiple-output derivation"
131   (let* ((builder    (add-text-to-store %store "my-fixed-builder.sh"
132                                         "echo one > $out ; echo two > $second"
133                                         '()))
134          (drv-path   (derivation %store "fixed" (%current-system)
135                                  "/bin/sh" `(,builder)
136                                  '(("HOME" . "/homeless")
137                                    ("zzz"  . "Z!")
138                                    ("AAA"  . "A!"))
139                                  `((,builder))
140                                  #:outputs '("out" "second")))
141          (succeeded? (build-derivations %store (list drv-path))))
142     (and succeeded?
143          (let ((one (derivation-path->output-path drv-path "out"))
144                (two (derivation-path->output-path drv-path "second")))
145            (and (eq? 'one (call-with-input-file one read))
146                 (eq? 'two (call-with-input-file two read)))))))
148 (test-assert "multiple-output derivation, non-alphabetic order"
149   ;; Here, the outputs are not listed in alphabetic order.  Yet, the store
150   ;; path computation must reorder them first.
151   (let* ((builder    (add-text-to-store %store "my-fixed-builder.sh"
152                                         "echo one > $out ; echo two > $AAA"
153                                         '()))
154          (drv-path   (derivation %store "fixed" (%current-system)
155                                  "/bin/sh" `(,builder)
156                                  '()
157                                  `((,builder))
158                                  #:outputs '("out" "AAA")))
159          (succeeded? (build-derivations %store (list drv-path))))
160     (and succeeded?
161          (let ((one (derivation-path->output-path drv-path "out"))
162                (two (derivation-path->output-path drv-path "AAA")))
163            (and (eq? 'one (call-with-input-file one read))
164                 (eq? 'two (call-with-input-file two read)))))))
166 (test-assert "user of multiple-output derivation"
167   ;; Check whether specifying several inputs coming from the same
168   ;; multiple-output derivation works.
169   (let* ((builder1   (add-text-to-store %store "my-mo-builder.sh"
170                                         "echo one > $out ; echo two > $two"
171                                         '()))
172          (mdrv       (derivation %store "multiple-output" (%current-system)
173                                  "/bin/sh" `(,builder1)
174                                  '()
175                                  `((,builder1))
176                                  #:outputs '("out" "two")))
177          (builder2   (add-text-to-store %store "my-mo-user-builder.sh"
178                                         "read x < $one;
179                                          read y < $two;
180                                          echo \"($x $y)\" > $out"
181                                         '()))
182          (udrv       (derivation %store "multiple-output-user"
183                                  (%current-system)
184                                  "/bin/sh" `(,builder2)
185                                  `(("one" . ,(derivation-path->output-path
186                                               mdrv "out"))
187                                    ("two" . ,(derivation-path->output-path
188                                               mdrv "two")))
189                                  `((,builder2)
190                                    ;; two occurrences of MDRV:
191                                    (,mdrv)
192                                    (,mdrv "two")))))
193     (and (build-derivations %store (list (pk 'udrv udrv)))
194          (let ((p (derivation-path->output-path udrv)))
195            (and (valid-path? %store p)
196                 (equal? '(one two) (call-with-input-file p read)))))))
199 (define %coreutils
200   (false-if-exception (nixpkgs-derivation "coreutils")))
202 (test-skip (if %coreutils 0 1))
204 (test-assert "build derivation with coreutils"
205   (let* ((builder
206           (add-text-to-store %store "build-with-coreutils.sh"
207                              "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
208                              '()))
209          (drv-path
210           (derivation %store "foo" (%current-system)
211                       "/bin/sh" `(,builder)
212                       `(("PATH" .
213                          ,(string-append
214                            (derivation-path->output-path %coreutils)
215                            "/bin")))
216                       `((,builder)
217                         (,%coreutils))))
218          (succeeded?
219           (build-derivations %store (list drv-path))))
220     (and succeeded?
221          (let ((p (derivation-path->output-path drv-path)))
222            (and (valid-path? %store p)
223                 (file-exists? (string-append p "/good")))))))
225 (test-skip (if (%guile-for-build) 0 6))
227 (test-assert "build-expression->derivation and derivation-prerequisites"
228   (let-values (((drv-path drv)
229                 (build-expression->derivation %store "fail" (%current-system)
230                                               #f '())))
231     (any (match-lambda
232           (($ <derivation-input> path)
233            (string=? path (%guile-for-build))))
234          (derivation-prerequisites drv))))
236 (test-assert "build-expression->derivation without inputs"
237   (let* ((builder    '(begin
238                         (mkdir %output)
239                         (call-with-output-file (string-append %output "/test")
240                           (lambda (p)
241                             (display '(hello guix) p)))))
242          (drv-path   (build-expression->derivation %store "goo" (%current-system)
243                                                    builder '()))
244          (succeeded? (build-derivations %store (list drv-path))))
245     (and succeeded?
246          (let ((p (derivation-path->output-path drv-path)))
247            (equal? '(hello guix)
248                    (call-with-input-file (string-append p "/test") read))))))
250 (test-assert "build-expression->derivation and derivation-prerequisites-to-build"
251   (let-values (((drv-path drv)
252                 (build-expression->derivation %store "fail" (%current-system)
253                                               #f '())))
254     ;; The only direct dependency is (%guile-for-build) and it's already
255     ;; built.
256     (null? (derivation-prerequisites-to-build %store drv))))
258 (test-assert "build-expression->derivation with expression returning #f"
259   (let* ((builder  '(begin
260                       (mkdir %output)
261                       #f))                        ; fail!
262          (drv-path (build-expression->derivation %store "fail" (%current-system)
263                                                  builder '()))
264          (out-path (derivation-path->output-path drv-path)))
265     (guard (c ((nix-protocol-error? c)
266                ;; Note that the output path may exist at this point, but it
267                ;; is invalid.
268                (and (string-match "build .* failed"
269                                   (nix-protocol-error-message c))
270                     (not (valid-path? %store out-path)))))
271       (build-derivations %store (list drv-path))
272       #f)))
274 (test-assert "build-expression->derivation with two outputs"
275   (let* ((builder    '(begin
276                         (call-with-output-file (assoc-ref %outputs "out")
277                           (lambda (p)
278                             (display '(hello) p)))
279                         (call-with-output-file (assoc-ref %outputs "second")
280                           (lambda (p)
281                             (display '(world) p)))))
282          (drv-path   (build-expression->derivation %store "double"
283                                                    (%current-system)
284                                                    builder '()
285                                                    #:outputs '("out"
286                                                                "second")))
287          (succeeded? (build-derivations %store (list drv-path))))
288     (and succeeded?
289          (let ((one (derivation-path->output-path drv-path))
290                (two (derivation-path->output-path drv-path "second")))
291            (and (equal? '(hello) (call-with-input-file one read))
292                 (equal? '(world) (call-with-input-file two read)))))))
294 (test-assert "build-expression->derivation with one input"
295   (let* ((builder    '(call-with-output-file %output
296                         (lambda (p)
297                           (let ((cu (assoc-ref %build-inputs "cu")))
298                             (close 1)
299                             (dup2 (port->fdes p) 1)
300                             (execl (string-append cu "/bin/uname")
301                                    "uname" "-a")))))
302          (drv-path   (build-expression->derivation %store "uname" (%current-system)
303                                                    builder
304                                                    `(("cu" ,%coreutils))))
305          (succeeded? (build-derivations %store (list drv-path))))
306     (and succeeded?
307          (let ((p (derivation-path->output-path drv-path)))
308            (string-contains (call-with-input-file p read-line) "GNU")))))
310 (test-assert "imported-files"
311   (let* ((files    `(("x"     . ,(search-path %load-path "ice-9/q.scm"))
312                      ("a/b/c" . ,(search-path %load-path
313                                               "guix/derivations.scm"))
314                      ("p/q"   . ,(search-path %load-path "guix.scm"))
315                      ("p/z"   . ,(search-path %load-path "guix/store.scm"))))
316          (drv-path (imported-files %store files)))
317     (and (build-derivations %store (list drv-path))
318          (let ((dir (derivation-path->output-path drv-path)))
319            (every (match-lambda
320                    ((path . source)
321                     (equal? (call-with-input-file (string-append dir "/" path)
322                               get-bytevector-all)
323                             (call-with-input-file source
324                               get-bytevector-all))))
325                   files)))))
327 (test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
328                0
329                1))
331 (test-assert "build-expression->derivation for fixed-output derivation"
332   (let* ((url         "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
333          (builder
334           `(begin
335              (use-modules (web client) (web uri)
336                           (rnrs io ports) (srfi srfi-11))
337              (let-values (((resp bv)
338                            (http-get (string->uri ,url) #:decode-body? #f)))
339                (call-with-output-file %output
340                  (lambda (p)
341                    (put-bytevector p bv))))))
342          (drv-path    (build-expression->derivation
343                        %store "hello-2.8.tar.gz" (%current-system) builder '()
344                        #:hash (nix-base32-string->bytevector
345                                "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
346                        #:hash-algo 'sha256))
347          (succeeded?  (build-derivations %store (list drv-path))))
348     (and succeeded?
349          (file-exists? (derivation-path->output-path drv-path)))))
351 (test-end)
354 (exit (= (test-runner-fail-count (test-runner-current)) 0))
356 ;;; Local Variables:
357 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
358 ;;; eval: (put 'guard 'scheme-indent-function 1)
359 ;;; End: