Add the `valid-path?' RPC.
[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)
b37eb5ed 24 #:use-module (srfi srfi-1)
fb3eec83 25 #:use-module (srfi srfi-11)
341c6fdd 26 #:use-module (srfi srfi-26)
99634e3f 27 #:use-module (srfi srfi-34)
341c6fdd 28 #:use-module (srfi srfi-64)
fb3eec83 29 #:use-module (rnrs io ports)
749c6567 30 #:use-module (rnrs bytevectors)
b37eb5ed 31 #:use-module (ice-9 rdelim)
db393b33 32 #:use-module (ice-9 regex)
99634e3f
LC
33 #:use-module (ice-9 ftw)
34 #:use-module (ice-9 match))
341c6fdd 35
26bbbb95
LC
36(define %store
37 (false-if-exception (open-connection)))
38
b37eb5ed
LC
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)))))
56
341c6fdd
LC
57(test-begin "derivations")
58
59(test-assert "parse & export"
33594aa4
LC
60 (let* ((f (search-path %load-path "tests/test.drv"))
61 (b1 (call-with-input-file f get-bytevector-all))
341c6fdd
LC
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))))
67
b37eb5ed
LC
68(test-skip (if %store 0 4))
69
d1b1c424
LC
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)))
31ef99a8 74 (valid-path? %store drv)
d1b1c424
LC
75 (equal? (call-with-input-file file get-bytevector-all)
76 (call-with-input-file drv get-bytevector-all)))))
77
b37eb5ed
LC
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)))
31ef99a8 82 (valid-path? %store drv)
b37eb5ed
LC
83 (equal? (directory-contents dir)
84 (directory-contents drv)))))
26bbbb95
LC
85
86(test-assert "derivation with no inputs"
31ef99a8
LC
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))))
26bbbb95 94
fb3eec83
LC
95(test-assert "build derivation with 1 source"
96 (let*-values (((builder)
97 (add-text-to-store %store "my-builder.sh"
de4c3f26 98 "echo hello, world > \"$out\"\n"
fb3eec83
LC
99 '()))
100 ((drv-path drv)
98090557 101 (derivation %store "foo" (%current-system)
fb3eec83 102 "/bin/sh" `(,builder)
af7f9e5f
LC
103 '(("HOME" . "/homeless")
104 ("zzz" . "Z!")
105 ("AAA" . "A!"))
fb3eec83
LC
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"))))
31ef99a8
LC
112 (and (valid-path? %store path)
113 (string=? (call-with-input-file path read-line)
114 "hello, world"))))))
fb3eec83 115
749c6567
LC
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")))
98090557 120 (drv-path (derivation %store "fixed" (%current-system)
749c6567
LC
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))))))
129
7946c4e7
LC
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 '()))
98090557 134 (drv-path (derivation %store "fixed" (%current-system)
7946c4e7
LC
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)))))))
147
de4c3f26
LC
148\f
149(define %coreutils
150 (false-if-exception (nixpkgs-derivation "coreutils")))
151
152(test-skip (if %coreutils 0 1))
153
154(test-assert "build derivation with coreutils"
155 (let* ((builder
156 (add-text-to-store %store "build-with-coreutils.sh"
157 "echo $PATH ; mkdir --version ; mkdir $out ; touch $out/good"
158 '()))
159 (drv-path
98090557 160 (derivation %store "foo" (%current-system)
de4c3f26
LC
161 "/bin/sh" `(,builder)
162 `(("PATH" .
163 ,(string-append
164 (derivation-path->output-path %coreutils)
165 "/bin")))
166 `((,builder)
167 (,%coreutils))))
168 (succeeded?
169 (build-derivations %store (list drv-path))))
170 (and succeeded?
171 (let ((p (derivation-path->output-path drv-path)))
31ef99a8
LC
172 (and (valid-path? %store p)
173 (file-exists? (string-append p "/good")))))))
de4c3f26 174
99634e3f 175(test-skip (if (%guile-for-build) 0 4))
d9085c23
LC
176
177(test-assert "build-expression->derivation without inputs"
178 (let* ((builder '(begin
179 (mkdir %output)
180 (call-with-output-file (string-append %output "/test")
181 (lambda (p)
182 (display '(hello guix) p)))))
98090557 183 (drv-path (build-expression->derivation %store "goo" (%current-system)
d9085c23
LC
184 builder '()))
185 (succeeded? (build-derivations %store (list drv-path))))
186 (and succeeded?
187 (let ((p (derivation-path->output-path drv-path)))
188 (equal? '(hello guix)
189 (call-with-input-file (string-append p "/test") read))))))
190
db393b33
LC
191(test-assert "build-expression->derivation with expression returning #f"
192 (let* ((builder '(begin
193 (mkdir %output)
194 #f)) ; fail!
195 (drv-path (build-expression->derivation %store "fail" (%current-system)
31ef99a8
LC
196 builder '()))
197 (out-path (derivation-path->output-path drv-path)))
db393b33
LC
198 (guard (c ((nix-protocol-error? c)
199 ;; Note that the output path may exist at this point, but it
200 ;; is invalid.
31ef99a8
LC
201 (and (string-match "build .* failed"
202 (nix-protocol-error-message c))
203 (not (valid-path? %store out-path)))))
db393b33
LC
204 (build-derivations %store (list drv-path))
205 #f)))
206
9bc07f4d
LC
207(test-assert "build-expression->derivation with two outputs"
208 (let* ((builder '(begin
209 (call-with-output-file (assoc-ref %outputs "out")
210 (lambda (p)
211 (display '(hello) p)))
212 (call-with-output-file (assoc-ref %outputs "second")
213 (lambda (p)
214 (display '(world) p)))))
215 (drv-path (build-expression->derivation %store "double"
98090557 216 (%current-system)
9bc07f4d
LC
217 builder '()
218 #:outputs '("out"
219 "second")))
220 (succeeded? (build-derivations %store (list drv-path))))
221 (and succeeded?
222 (let ((one (derivation-path->output-path drv-path))
223 (two (derivation-path->output-path drv-path "second")))
224 (and (equal? '(hello) (call-with-input-file one read))
225 (equal? '(world) (call-with-input-file two read)))))))
226
d9085c23
LC
227(test-assert "build-expression->derivation with one input"
228 (let* ((builder '(call-with-output-file %output
229 (lambda (p)
230 (let ((cu (assoc-ref %build-inputs "cu")))
231 (close 1)
232 (dup2 (port->fdes p) 1)
233 (execl (string-append cu "/bin/uname")
234 "uname" "-a")))))
98090557 235 (drv-path (build-expression->derivation %store "uname" (%current-system)
d9085c23 236 builder
2acb2cb6 237 `(("cu" ,%coreutils))))
d9085c23
LC
238 (succeeded? (build-derivations %store (list drv-path))))
239 (and succeeded?
240 (let ((p (derivation-path->output-path drv-path)))
241 (string-contains (call-with-input-file p read-line) "GNU")))))
242
99634e3f
LC
243(test-assert "imported-files"
244 (let* ((files `(("x" . ,(search-path %load-path "ice-9/q.scm"))
245 ("a/b/c" . ,(search-path %load-path
246 "guix/derivations.scm"))
224f7ad6
LC
247 ("p/q" . ,(search-path %load-path "guix.scm"))
248 ("p/z" . ,(search-path %load-path "guix/store.scm"))))
99634e3f
LC
249 (drv-path (imported-files %store files)))
250 (and (build-derivations %store (list drv-path))
251 (let ((dir (derivation-path->output-path drv-path)))
252 (every (match-lambda
253 ((path . source)
254 (equal? (call-with-input-file (string-append dir "/" path)
255 get-bytevector-all)
256 (call-with-input-file source
257 get-bytevector-all))))
258 files)))))
259
26b969de
LC
260(test-skip (if (false-if-exception (getaddrinfo "ftp.gnu.org" "http"))
261 0
262 1))
263
264(test-assert "build-expression->derivation for fixed-output derivation"
265 (let* ((url "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz")
0e383c76
LC
266 (builder
267 `(begin
268 (use-modules (web client) (web uri)
269 (rnrs io ports) (srfi srfi-11))
270 (let-values (((resp bv)
271 (http-get (string->uri ,url) #:decode-body? #f)))
272 (call-with-output-file %output
273 (lambda (p)
274 (put-bytevector p bv))))))
26b969de 275 (drv-path (build-expression->derivation
98090557 276 %store "hello-2.8.tar.gz" (%current-system) builder '()
26b969de
LC
277 #:hash (nix-base32-string->bytevector
278 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6")
279 #:hash-algo 'sha256))
280 (succeeded? (build-derivations %store (list drv-path))))
281 (and succeeded?
282 (file-exists? (derivation-path->output-path drv-path)))))
283
341c6fdd
LC
284(test-end)
285
286\f
287(exit (= (test-runner-fail-count (test-runner-current)) 0))
288
289;;; Local Variables:
290;;; eval: (put 'test-assert 'scheme-indent-function 1)
db393b33 291;;; eval: (put 'guard 'scheme-indent-function 1)
341c6fdd 292;;; End: