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