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