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
37 with-derivation-narinfo
38 with-derivation-substitute
43 ;;; This module provide shared infrastructure for the test suite. For
44 ;;; internal use only.
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)
54 (let ((store (open-connection)))
55 ;; Make sure we build everything by ourselves.
56 (set-build-options store #:use-substitutes? #f)
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))
65 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
68 "Return the hexadecimal representation of a random number."
69 (number->string (random (expt 2 256) %seed) 16))
71 (define (random-bytevector n)
72 "Return a random bytevector of N bytes."
73 (let ((bv (make-bytevector n)))
77 (bytevector-u8-set! bv i (random 256 %seed))
81 (define (network-reachable?)
82 "Return true if we can reach the Internet."
83 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
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)))
91 (lambda () (module-set! m 'proc replacement))
93 (lambda () (module-set! m 'proc original)))))
97 ;;; Narinfo files, as used by the substituter.
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
105 (format #f "StorePath: ~a
113 (derivation->output-path drv) ; StorePath
115 (bytevector->nix-base32-string sha256) ; NarHash
116 (derivation-system drv) ; System
118 (derivation-file-name drv)))) ; Deriver
120 (define %substitute-directory
122 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
123 (compose uri-path string->uri))))
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)
137 (call-with-output-file info
139 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
141 (call-with-output-file narinfo
143 (display (derivation-narinfo drv #:sha256 sha256) p))))
146 (delete-file narinfo)
147 (delete-file info)))))
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
158 (call-with-derivation-narinfo drv
162 (define* (call-with-derivation-substitute drv contents thunk
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
168 (define dir (%substitute-directory))
171 (call-with-output-file (string-append dir "/example.out")
173 (display contents port)))
174 (call-with-output-file (string-append dir "/example.nar")
176 (write-file (string-append dir "/example.out") p))))
178 (let ((hash (call-with-input-file (string-append dir "/example.nar")
180 ;; Create fake substituter data, to be read by `substitute-binary'.
181 (call-with-derivation-narinfo drv
183 #:sha256 (or sha256 hash))))
185 (delete-file (string-append dir "/example.out"))
186 (delete-file (string-append dir "/example.nar")))))
188 (define-syntax with-derivation-substitute
189 (syntax-rules (sha256 =>)
190 "Evaluate BODY in a context where DRV is substitutable with the given
192 ((_ drv contents (sha256 => hash) body ...)
193 (call-with-derivation-substitute drv contents
196 ((_ drv contents body ...)
197 (call-with-derivation-substitute drv contents
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)))
211 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
212 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
215 ;;; tests.scm ends here