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
45 ;;; This module provide shared infrastructure for the test suite. For
46 ;;; internal use only.
50 (define (open-connection-for-tests)
51 "Open a connection to the build daemon for tests purposes and return it."
52 (guard (c ((nix-error? c)
53 (format (current-error-port)
54 "warning: build daemon error: ~s~%" c)
56 (let ((store (open-connection)))
57 ;; Make sure we build everything by ourselves.
58 (set-build-options store #:use-substitutes? #f)
60 ;; Use the bootstrap Guile when running tests, so we don't end up
61 ;; building everything in the temporary test store.
62 (%guile-for-build (package-derivation store %bootstrap-guile))
67 (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
69 (logxor (getpid) (car (gettimeofday)))))
72 (let ((seed (random-seed)))
73 (format (current-error-port) "random seed for tests: ~a~%"
75 (seed->random-state seed)))
78 "Return the hexadecimal representation of a random number."
79 (number->string (random (expt 2 256) %seed) 16))
81 (define (random-bytevector n)
82 "Return a random bytevector of N bytes."
83 (let ((bv (make-bytevector n)))
87 (bytevector-u8-set! bv i (random 256 %seed))
91 (define (network-reachable?)
92 "Return true if we can reach the Internet."
93 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
95 (define-syntax-rule (mock (module proc replacement) body ...)
96 "Within BODY, replace the definition of PROC from MODULE with the definition
97 given by REPLACEMENT."
98 (let* ((m (resolve-module 'module))
99 (original (module-ref m 'proc)))
101 (lambda () (module-set! m 'proc replacement))
103 (lambda () (module-set! m 'proc original)))))
107 ;;; Narinfo files, as used by the substituter.
110 (define* (derivation-narinfo drv #:key (nar "example.nar")
111 (sha256 (make-bytevector 32 0)))
112 "Return the contents of the narinfo corresponding to DRV; NAR should be the
113 file name of the archive containing the substitute for DRV, and SHA256 is the
115 (format #f "StorePath: ~a
123 (derivation->output-path drv) ; StorePath
125 (bytevector->nix-base32-string sha256) ; NarHash
126 (derivation-system drv) ; System
128 (derivation-file-name drv)))) ; Deriver
130 (define %substitute-directory
132 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
133 (compose uri-path string->uri))))
135 (define* (call-with-derivation-narinfo drv thunk
136 #:key (sha256 (make-bytevector 32 0)))
137 "Call THUNK in a context where fake substituter data, as read by 'guix
138 substitute', has been installed for DRV. SHA256 is the hash of the
139 expected output of DRV."
140 (let* ((output (derivation->output-path drv))
141 (dir (%substitute-directory))
142 (info (string-append dir "/nix-cache-info"))
143 (narinfo (string-append dir "/" (store-path-hash-part output)
147 (call-with-output-file info
149 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
151 (call-with-output-file narinfo
153 (display (derivation-narinfo drv #:sha256 sha256) p))))
156 (delete-file narinfo)
157 (delete-file info)))))
159 (define-syntax with-derivation-narinfo
160 (syntax-rules (sha256 =>)
161 "Evaluate BODY in a context where DRV looks substitutable from the
162 substituter's viewpoint."
163 ((_ drv (sha256 => hash) body ...)
164 (call-with-derivation-narinfo drv
168 (call-with-derivation-narinfo drv
172 (define* (call-with-derivation-substitute drv contents thunk
174 "Call THUNK in a context where a substitute for DRV has been installed,
175 using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
176 expected hash of the substitute; otherwise use the hash of the nar containing
178 (define dir (%substitute-directory))
181 (call-with-output-file (string-append dir "/example.out")
183 (display contents port)))
184 (call-with-output-file (string-append dir "/example.nar")
186 (write-file (string-append dir "/example.out") p))))
188 (let ((hash (call-with-input-file (string-append dir "/example.nar")
190 ;; Create fake substituter data, to be read by 'guix substitute'.
191 (call-with-derivation-narinfo drv
193 #:sha256 (or sha256 hash))))
195 (delete-file (string-append dir "/example.out"))
196 (delete-file (string-append dir "/example.nar")))))
198 (define (shebang-too-long?)
199 "Return true if the typical shebang in the current store would exceed
200 Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
203 (string-append "#!" (%store-prefix) "/"
205 "-bootstrap-binaries-0/bin/bash\0"))
207 (> (string-length shebang) 128))
209 (define-syntax with-derivation-substitute
210 (syntax-rules (sha256 =>)
211 "Evaluate BODY in a context where DRV is substitutable with the given
213 ((_ drv contents (sha256 => hash) body ...)
214 (call-with-derivation-substitute drv contents
217 ((_ drv contents body ...)
218 (call-with-derivation-substitute drv contents
222 (define-syntax-rule (dummy-package name* extra-fields ...)
223 "Return a \"dummy\" package called NAME*, with all its compulsory fields
224 initialized with default values, and with EXTRA-FIELDS set as specified."
225 (package extra-fields ...
226 (name name*) (version "0") (source #f)
227 (build-system gnu-build-system)
228 (synopsis #f) (description #f)
229 (home-page #f) (license #f)))
231 (define-syntax-rule (dummy-origin extra-fields ...)
232 "Return a \"dummy\" origin, with all its compulsory fields initialized with
233 default values, and with EXTRA-FIELDS set as specified."
234 (origin extra-fields ...
235 (method #f) (uri "http://www.example.com")
236 (sha256 (base32 (make-string 52 #\x)))))
239 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
240 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
243 ;;; tests.scm ends here