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
36 with-derivation-narinfo
37 with-derivation-substitute
42 ;;; This module provide shared infrastructure for the test suite. For
43 ;;; internal use only.
47 (define (open-connection-for-tests)
48 "Open a connection to the build daemon for tests purposes and return it."
49 (guard (c ((nix-error? c)
50 (format (current-error-port)
51 "warning: build daemon error: ~s~%" c)
53 (let ((store (open-connection)))
54 ;; Make sure we build everything by ourselves.
55 (set-build-options store #:use-substitutes? #f)
57 ;; Use the bootstrap Guile when running tests, so we don't end up
58 ;; building everything in the temporary test store.
59 (%guile-for-build (package-derivation store %bootstrap-guile))
64 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
67 "Return the hexadecimal representation of a random number."
68 (number->string (random (expt 2 256) %seed) 16))
70 (define (random-bytevector n)
71 "Return a random bytevector of N bytes."
72 (let ((bv (make-bytevector n)))
76 (bytevector-u8-set! bv i (random 256 %seed))
80 (define-syntax-rule (mock (module proc replacement) body ...)
81 "Within BODY, replace the definition of PROC from MODULE with the definition
82 given by REPLACEMENT."
83 (let* ((m (resolve-module 'module))
84 (original (module-ref m 'proc)))
86 (lambda () (module-set! m 'proc replacement))
88 (lambda () (module-set! m 'proc original)))))
92 ;;; Narinfo files, as used by the substituter.
95 (define* (derivation-narinfo drv #:key (nar "example.nar")
96 (sha256 (make-bytevector 32 0)))
97 "Return the contents of the narinfo corresponding to DRV; NAR should be the
98 file name of the archive containing the substitute for DRV, and SHA256 is the
100 (format #f "StorePath: ~a
108 (derivation->output-path drv) ; StorePath
110 (bytevector->nix-base32-string sha256) ; NarHash
111 (derivation-system drv) ; System
113 (derivation-file-name drv)))) ; Deriver
115 (define %substitute-directory
117 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
118 (compose uri-path string->uri))))
120 (define* (call-with-derivation-narinfo drv thunk
121 #:key (sha256 (make-bytevector 32 0)))
122 "Call THUNK in a context where fake substituter data, as read by 'guix
123 substitute-binary', has been installed for DRV. SHA256 is the hash of the
124 expected output of DRV."
125 (let* ((output (derivation->output-path drv))
126 (dir (%substitute-directory))
127 (info (string-append dir "/nix-cache-info"))
128 (narinfo (string-append dir "/" (store-path-hash-part output)
132 (call-with-output-file info
134 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
136 (call-with-output-file narinfo
138 (display (derivation-narinfo drv #:sha256 sha256) p))))
141 (delete-file narinfo)
142 (delete-file info)))))
144 (define-syntax with-derivation-narinfo
145 (syntax-rules (sha256 =>)
146 "Evaluate BODY in a context where DRV looks substitutable from the
147 substituter's viewpoint."
148 ((_ drv (sha256 => hash) body ...)
149 (call-with-derivation-narinfo drv
153 (call-with-derivation-narinfo drv
157 (define* (call-with-derivation-substitute drv contents thunk
159 "Call THUNK in a context where a substitute for DRV has been installed,
160 using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
161 expected hash of the substitute; otherwise use the hash of the nar containing
163 (define dir (%substitute-directory))
166 (call-with-output-file (string-append dir "/example.out")
168 (display contents port)))
169 (call-with-output-file (string-append dir "/example.nar")
171 (write-file (string-append dir "/example.out") p))))
173 (let ((hash (call-with-input-file (string-append dir "/example.nar")
175 ;; Create fake substituter data, to be read by `substitute-binary'.
176 (call-with-derivation-narinfo drv
178 #:sha256 (or sha256 hash))))
180 (delete-file (string-append dir "/example.out"))
181 (delete-file (string-append dir "/example.nar")))))
183 (define-syntax with-derivation-substitute
184 (syntax-rules (sha256 =>)
185 "Evaluate BODY in a context where DRV is substitutable with the given
187 ((_ drv contents (sha256 => hash) body ...)
188 (call-with-derivation-substitute drv contents
191 ((_ drv contents body ...)
192 (call-with-derivation-substitute drv contents
196 (define-syntax-rule (dummy-package name* extra-fields ...)
197 "Return a \"dummy\" package called NAME*, with all its compulsory fields
198 initialized with default values, and with EXTRA-FIELDS set as specified."
199 (package extra-fields ...
200 (name name*) (version "0") (source #f)
201 (build-system gnu-build-system)
202 (synopsis #f) (description #f)
203 (home-page #f) (license #f)))
206 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
207 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
210 ;;; tests.scm ends here