1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
41 with-derivation-narinfo
42 with-derivation-substitute
48 ;;; This module provide shared infrastructure for the test suite. For
49 ;;; internal use only.
53 (define %test-substitute-urls
54 ;; URLs where to look for substitutes during tests.
56 (or (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") list)
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)
65 (let ((store (open-connection uri)))
66 ;; Make sure we build everything by ourselves.
67 (set-build-options store
69 #:substitute-urls (%test-substitute-urls))
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))
78 (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
80 (logxor (getpid) (car (gettimeofday)))))
83 (let ((seed (random-seed)))
84 (format (current-error-port) "random seed for tests: ~a~%"
86 (seed->random-state seed)))
89 "Return the hexadecimal representation of a random number."
90 (number->string (random (expt 2 256) %seed) 16))
92 (define (random-bytevector n)
93 "Return a random bytevector of N bytes."
94 (let ((bv (make-bytevector n)))
98 (bytevector-u8-set! bv i (random 256 %seed))
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))
108 (call-with-input-file a get-bytevector-all)
109 (call-with-input-file b get-bytevector-all)))
111 (string=? (readlink a) (readlink b)))
113 (error "what?" (lstat a))))))
115 (define (network-reachable?)
116 "Return true if we can reach the Internet."
117 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
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)))
125 (lambda () (module-set! m 'proc replacement))
127 (lambda () (module-set! m 'proc original)))))
131 ;;; Narinfo files, as used by the substituter.
134 (define* (derivation-narinfo drv #:key (nar "example.nar")
135 (sha256 (make-bytevector 32 0))
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
148 (derivation->output-path drv) ; StorePath
150 (bytevector->nix-base32-string sha256) ; NarHash
151 (string-join (map basename references)) ; References
152 (derivation-system drv) ; System
154 (derivation-file-name drv)))) ; Deriver
156 (define %substitute-directory
158 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
159 (compose uri-path string->uri))))
161 (define* (call-with-derivation-narinfo drv thunk
163 (sha256 (make-bytevector 32 0))
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)
175 (call-with-output-file info
177 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
179 (call-with-output-file narinfo
181 (display (derivation-narinfo drv #:sha256 sha256
182 #:references references)
186 (delete-file narinfo)
187 (delete-file info)))))
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
198 ((_ drv (sha256 => hash) body ...)
199 (with-derivation-narinfo drv
200 (sha256 => hash) (references => '())
203 (call-with-derivation-narinfo drv
207 (define* (call-with-derivation-substitute drv contents thunk
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
215 (define dir (%substitute-directory))
218 (call-with-output-file (string-append dir "/example.out")
220 (display contents port)))
221 (call-with-output-file (string-append dir "/example.nar")
223 (write-file (string-append dir "/example.out") p))))
225 (let ((hash (call-with-input-file (string-append dir "/example.nar")
227 ;; Create fake substituter data, to be read by 'guix substitute'.
228 (call-with-derivation-narinfo drv
230 #:sha256 (or sha256 hash)
231 #:references references)))
233 (delete-file (string-append dir "/example.out"))
234 (delete-file (string-append dir "/example.nar")))))
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
241 (string-append "#!" (%store-prefix) "/"
243 "-bootstrap-binaries-0/bin/bash\0"))
245 (> (string-length shebang) 128))
247 (define-syntax with-derivation-substitute
248 (syntax-rules (sha256 references =>)
249 "Evaluate BODY in a context where DRV is substitutable with the given
251 ((_ drv contents (sha256 => hash) (references => refs) body ...)
252 (call-with-derivation-substitute drv contents
256 ((_ drv contents (sha256 => hash) body ...)
257 (with-derivation-substitute drv contents
258 (sha256 => hash) (references => '())
260 ((_ drv contents body ...)
261 (call-with-derivation-substitute drv contents
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)))
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)))))
282 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
283 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
286 ;;; tests.scm ends here