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