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