1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 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 (web uri)
31 #:export (open-connection-for-tests
38 with-derivation-narinfo
39 with-derivation-substitute
44 ;;; This module provide shared infrastructure for the test suite. For
45 ;;; internal use only.
49 (define (open-connection-for-tests)
50 "Open a connection to the build daemon for tests purposes and return it."
51 (guard (c ((nix-error? c)
52 (format (current-error-port)
53 "warning: build daemon error: ~s~%" c)
55 (let ((store (open-connection)))
56 ;; Make sure we build everything by ourselves.
57 (set-build-options store #:use-substitutes? #f)
59 ;; Use the bootstrap Guile when running tests, so we don't end up
60 ;; building everything in the temporary test store.
61 (%guile-for-build (package-derivation store %bootstrap-guile))
66 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
69 "Return the hexadecimal representation of a random number."
70 (number->string (random (expt 2 256) %seed) 16))
72 (define (random-bytevector n)
73 "Return a random bytevector of N bytes."
74 (let ((bv (make-bytevector n)))
78 (bytevector-u8-set! bv i (random 256 %seed))
82 (define (network-reachable?)
83 "Return true if we can reach the Internet."
84 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
86 (define-syntax-rule (mock (module proc replacement) body ...)
87 "Within BODY, replace the definition of PROC from MODULE with the definition
88 given by REPLACEMENT."
89 (let* ((m (resolve-module 'module))
90 (original (module-ref m 'proc)))
92 (lambda () (module-set! m 'proc replacement))
94 (lambda () (module-set! m 'proc original)))))
98 ;;; Narinfo files, as used by the substituter.
101 (define* (derivation-narinfo drv #:key (nar "example.nar")
102 (sha256 (make-bytevector 32 0)))
103 "Return the contents of the narinfo corresponding to DRV; NAR should be the
104 file name of the archive containing the substitute for DRV, and SHA256 is the
106 (format #f "StorePath: ~a
114 (derivation->output-path drv) ; StorePath
116 (bytevector->nix-base32-string sha256) ; NarHash
117 (derivation-system drv) ; System
119 (derivation-file-name drv)))) ; Deriver
121 (define %substitute-directory
123 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
124 (compose uri-path string->uri))))
126 (define* (call-with-derivation-narinfo drv thunk
127 #:key (sha256 (make-bytevector 32 0)))
128 "Call THUNK in a context where fake substituter data, as read by 'guix
129 substitute-binary', has been installed for DRV. SHA256 is the hash of the
130 expected output of DRV."
131 (let* ((output (derivation->output-path drv))
132 (dir (%substitute-directory))
133 (info (string-append dir "/nix-cache-info"))
134 (narinfo (string-append dir "/" (store-path-hash-part output)
138 (call-with-output-file info
140 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
142 (call-with-output-file narinfo
144 (display (derivation-narinfo drv #:sha256 sha256) p))))
147 (delete-file narinfo)
148 (delete-file info)))))
150 (define-syntax with-derivation-narinfo
151 (syntax-rules (sha256 =>)
152 "Evaluate BODY in a context where DRV looks substitutable from the
153 substituter's viewpoint."
154 ((_ drv (sha256 => hash) body ...)
155 (call-with-derivation-narinfo drv
159 (call-with-derivation-narinfo drv
163 (define* (call-with-derivation-substitute drv contents thunk
165 "Call THUNK in a context where a substitute for DRV has been installed,
166 using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
167 expected hash of the substitute; otherwise use the hash of the nar containing
169 (define dir (%substitute-directory))
172 (call-with-output-file (string-append dir "/example.out")
174 (display contents port)))
175 (call-with-output-file (string-append dir "/example.nar")
177 (write-file (string-append dir "/example.out") p))))
179 (let ((hash (call-with-input-file (string-append dir "/example.nar")
181 ;; Create fake substituter data, to be read by `substitute-binary'.
182 (call-with-derivation-narinfo drv
184 #:sha256 (or sha256 hash))))
186 (delete-file (string-append dir "/example.out"))
187 (delete-file (string-append dir "/example.nar")))))
189 (define (shebang-too-long?)
190 "Return true if the typical shebang in the current store would exceed
191 Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
194 (string-append "#!" (%store-prefix) "/"
196 "-bootstrap-binaries-0/bin/bash\0"))
198 (> (string-length shebang) 128))
200 (define-syntax with-derivation-substitute
201 (syntax-rules (sha256 =>)
202 "Evaluate BODY in a context where DRV is substitutable with the given
204 ((_ drv contents (sha256 => hash) body ...)
205 (call-with-derivation-substitute drv contents
208 ((_ drv contents body ...)
209 (call-with-derivation-substitute drv contents
213 (define-syntax-rule (dummy-package name* extra-fields ...)
214 "Return a \"dummy\" package called NAME*, with all its compulsory fields
215 initialized with default values, and with EXTRA-FIELDS set as specified."
216 (package extra-fields ...
217 (name name*) (version "0") (source #f)
218 (build-system gnu-build-system)
219 (synopsis #f) (description #f)
220 (home-page #f) (license #f)))
223 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
224 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
227 ;;; tests.scm ends here