tests: Add 'test-assertm' to (guix tests).
[jackhill/guix/guix.git] / guix / tests.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU 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 ;;; GNU 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 GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix tests)
20 #:use-module ((guix config) #:select (%storedir %localstatedir))
21 #:use-module (guix store)
22 #:use-module (guix derivations)
23 #:use-module (guix packages)
24 #:use-module (guix base32)
25 #:use-module (guix serialization)
26 #:use-module (gcrypt hash)
27 #:use-module (guix build-system gnu)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-64)
31 #:use-module (rnrs bytevectors)
32 #:use-module (ice-9 binary-ports)
33 #:use-module (web uri)
34 #:export (open-connection-for-tests
35 with-external-store
36 random-text
37 random-bytevector
38 file=?
39 network-reachable?
40 shebang-too-long?
41 mock
42 %test-substitute-urls
43 test-assertm
44 test-equalm
45 %substitute-directory
46 with-derivation-narinfo
47 with-derivation-substitute
48 dummy-package
49 dummy-origin))
50
51 ;;; Commentary:
52 ;;;
53 ;;; This module provide shared infrastructure for the test suite. For
54 ;;; internal use only.
55 ;;;
56 ;;; Code:
57
58 (define %test-substitute-urls
59 ;; URLs where to look for substitutes during tests.
60 (make-parameter
61 (or (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") list)
62 '())))
63
64 (define* (open-connection-for-tests #:optional (uri (%daemon-socket-uri)))
65 "Open a connection to the build daemon for tests purposes and return it."
66 (guard (c ((nix-error? c)
67 (format (current-error-port)
68 "warning: build daemon error: ~s~%" c)
69 #f))
70 (let ((store (open-connection uri)))
71 ;; Make sure we build everything by ourselves.
72 (set-build-options store
73 #:use-substitutes? #f
74 #:substitute-urls (%test-substitute-urls))
75
76 ;; Use the bootstrap Guile when running tests, so we don't end up
77 ;; building everything in the temporary test store.
78 (%guile-for-build (package-derivation store %bootstrap-guile))
79
80 store)))
81
82 (define (call-with-external-store proc)
83 "Call PROC with an open connection to the external store or #f it there is
84 no external store to talk to."
85 (parameterize ((%daemon-socket-uri
86 (string-append %localstatedir
87 "/guix/daemon-socket/socket"))
88 (%store-prefix %storedir))
89 (define store
90 (catch #t
91 (lambda ()
92 (open-connection))
93 (const #f)))
94
95 (dynamic-wind
96 (const #t)
97 (lambda ()
98 ;; Since we're using a different store we must clear the
99 ;; package-derivation cache.
100 (hash-clear! (@@ (guix packages) %derivation-cache))
101
102 (proc store))
103 (lambda ()
104 (when store
105 (close-connection store))))))
106
107 (define-syntax-rule (with-external-store store exp ...)
108 "Evaluate EXP with STORE bound to the external store rather than the
109 temporary test store, or #f if there is no external store to talk to.
110
111 This is meant to be used for tests that need to build packages that would be
112 too expensive to build entirely in the test store."
113 (call-with-external-store (lambda (store) exp ...)))
114
115 (define (random-seed)
116 (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
117 number->string)
118 (logxor (getpid) (car (gettimeofday)))))
119
120 (define %seed
121 (let ((seed (random-seed)))
122 (format (current-error-port) "random seed for tests: ~a~%"
123 seed)
124 (seed->random-state seed)))
125
126 (define (random-text)
127 "Return the hexadecimal representation of a random number."
128 (number->string (random (expt 2 256) %seed) 16))
129
130 (define (random-bytevector n)
131 "Return a random bytevector of N bytes."
132 (let ((bv (make-bytevector n)))
133 (let loop ((i 0))
134 (if (< i n)
135 (begin
136 (bytevector-u8-set! bv i (random 256 %seed))
137 (loop (1+ i)))
138 bv))))
139
140 (define (file=? a b)
141 "Return true if files A and B have the same type and same content."
142 (and (eq? (stat:type (lstat a)) (stat:type (lstat b)))
143 (case (stat:type (lstat a))
144 ((regular)
145 (equal?
146 (call-with-input-file a get-bytevector-all)
147 (call-with-input-file b get-bytevector-all)))
148 ((symlink)
149 (string=? (readlink a) (readlink b)))
150 (else
151 (error "what?" (lstat a))))))
152
153 (define (network-reachable?)
154 "Return true if we can reach the Internet."
155 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
156
157 (define-syntax-rule (mock (module proc replacement) body ...)
158 "Within BODY, replace the definition of PROC from MODULE with the definition
159 given by REPLACEMENT."
160 (let* ((m (resolve-module 'module))
161 (original (module-ref m 'proc)))
162 (dynamic-wind
163 (lambda () (module-set! m 'proc replacement))
164 (lambda () body ...)
165 (lambda () (module-set! m 'proc original)))))
166
167 (define-syntax-rule (test-assertm name exp)
168 "Like 'test-assert', but EXP is a monadic value. A new connection to the
169 store is opened."
170 (test-assert name
171 (let ((store (open-connection-for-tests)))
172 (dynamic-wind
173 (const #t)
174 (lambda ()
175 (run-with-store store exp
176 #:guile-for-build (%guile-for-build)))
177 (lambda ()
178 (close-connection store))))))
179
180 (define-syntax-rule (test-equalm name value exp)
181 "Like 'test-equal', but EXP is a monadic value. A new connection to the
182 store is opened."
183 (test-equal name
184 value
185 (with-store store
186 (run-with-store store exp
187 #:guile-for-build (%guile-for-build)))))
188
189 \f
190 ;;;
191 ;;; Narinfo files, as used by the substituter.
192 ;;;
193
194 (define* (derivation-narinfo drv #:key (nar "example.nar")
195 (sha256 (make-bytevector 32 0))
196 (references '()))
197 "Return the contents of the narinfo corresponding to DRV, with the specified
198 REFERENCES (a list of store items); NAR should be the file name of the archive
199 containing the substitute for DRV, and SHA256 is the expected hash."
200 (format #f "StorePath: ~a
201 URL: ~a
202 Compression: none
203 NarSize: 1234
204 NarHash: sha256:~a
205 References: ~a
206 System: ~a
207 Deriver: ~a~%"
208 (derivation->output-path drv) ; StorePath
209 nar ; URL
210 (bytevector->nix-base32-string sha256) ; NarHash
211 (string-join (map basename references)) ; References
212 (derivation-system drv) ; System
213 (basename
214 (derivation-file-name drv)))) ; Deriver
215
216 (define %substitute-directory
217 (make-parameter
218 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
219 (compose uri-path string->uri))))
220
221 (define* (call-with-derivation-narinfo drv thunk
222 #:key
223 (sha256 (make-bytevector 32 0))
224 (references '()))
225 "Call THUNK in a context where fake substituter data, as read by 'guix
226 substitute', has been installed for DRV. SHA256 is the hash of the
227 expected output of DRV."
228 (let* ((output (derivation->output-path drv))
229 (dir (%substitute-directory))
230 (info (string-append dir "/nix-cache-info"))
231 (narinfo (string-append dir "/" (store-path-hash-part output)
232 ".narinfo")))
233 (dynamic-wind
234 (lambda ()
235 (call-with-output-file info
236 (lambda (p)
237 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
238 (%store-prefix))))
239 (call-with-output-file narinfo
240 (lambda (p)
241 (display (derivation-narinfo drv #:sha256 sha256
242 #:references references)
243 p))))
244 thunk
245 (lambda ()
246 (delete-file narinfo)
247 (delete-file info)))))
248
249 (define-syntax with-derivation-narinfo
250 (syntax-rules (sha256 references =>)
251 "Evaluate BODY in a context where DRV looks substitutable from the
252 substituter's viewpoint."
253 ((_ drv (sha256 => hash) (references => refs) body ...)
254 (call-with-derivation-narinfo drv
255 (lambda () body ...)
256 #:sha256 hash
257 #:references refs))
258 ((_ drv (sha256 => hash) body ...)
259 (with-derivation-narinfo drv
260 (sha256 => hash) (references => '())
261 body ...))
262 ((_ drv body ...)
263 (call-with-derivation-narinfo drv
264 (lambda ()
265 body ...)))))
266
267 (define* (call-with-derivation-substitute drv contents thunk
268 #:key
269 sha256
270 (references '()))
271 "Call THUNK in a context where a substitute for DRV has been installed,
272 using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
273 expected hash of the substitute; otherwise use the hash of the nar containing
274 CONTENTS."
275 (define dir (%substitute-directory))
276 (dynamic-wind
277 (lambda ()
278 (call-with-output-file (string-append dir "/example.out")
279 (lambda (port)
280 (display contents port)))
281 (call-with-output-file (string-append dir "/example.nar")
282 (lambda (p)
283 (write-file (string-append dir "/example.out") p))))
284 (lambda ()
285 (let ((hash (call-with-input-file (string-append dir "/example.nar")
286 port-sha256)))
287 ;; Create fake substituter data, to be read by 'guix substitute'.
288 (call-with-derivation-narinfo drv
289 thunk
290 #:sha256 (or sha256 hash)
291 #:references references)))
292 (lambda ()
293 (delete-file (string-append dir "/example.out"))
294 (delete-file (string-append dir "/example.nar")))))
295
296 (define (shebang-too-long?)
297 "Return true if the typical shebang in the current store would exceed
298 Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
299 all included."
300 (define shebang
301 (string-append "#!" (%store-prefix) "/"
302 (make-string 32 #\a)
303 "-bootstrap-binaries-0/bin/bash\0"))
304
305 (> (string-length shebang) 128))
306
307 (define-syntax with-derivation-substitute
308 (syntax-rules (sha256 references =>)
309 "Evaluate BODY in a context where DRV is substitutable with the given
310 CONTENTS."
311 ((_ drv contents (sha256 => hash) (references => refs) body ...)
312 (call-with-derivation-substitute drv contents
313 (lambda () body ...)
314 #:sha256 hash
315 #:references refs))
316 ((_ drv contents (sha256 => hash) body ...)
317 (with-derivation-substitute drv contents
318 (sha256 => hash) (references => '())
319 body ...))
320 ((_ drv contents body ...)
321 (call-with-derivation-substitute drv contents
322 (lambda ()
323 body ...)))))
324
325 (define-syntax-rule (dummy-package name* extra-fields ...)
326 "Return a \"dummy\" package called NAME*, with all its compulsory fields
327 initialized with default values, and with EXTRA-FIELDS set as specified."
328 (package extra-fields ...
329 (name name*) (version "0") (source #f)
330 (build-system gnu-build-system)
331 (synopsis #f) (description #f)
332 (home-page #f) (license #f)))
333
334 (define-syntax-rule (dummy-origin extra-fields ...)
335 "Return a \"dummy\" origin, with all its compulsory fields initialized with
336 default values, and with EXTRA-FIELDS set as specified."
337 (origin extra-fields ...
338 (method #f) (uri "http://www.example.com")
339 (sha256 (base32 (make-string 52 #\x)))))
340
341 ;; Local Variables:
342 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
343 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
344 ;; End:
345
346 ;;; tests.scm ends here