tests: Add missing import.
[jackhill/guix/guix.git] / guix / tests.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
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
32 random-text
33 random-bytevector
34 mock
35 %substitute-directory
36 with-derivation-narinfo
37 with-derivation-substitute
38 dummy-package))
39
40 ;;; Commentary:
41 ;;;
42 ;;; This module provide shared infrastructure for the test suite. For
43 ;;; internal use only.
44 ;;;
45 ;;; Code:
46
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)
52 #f))
53 (let ((store (open-connection)))
54 ;; Make sure we build everything by ourselves.
55 (set-build-options store #:use-substitutes? #f)
56
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))
60
61 store)))
62
63 (define %seed
64 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
65
66 (define (random-text)
67 "Return the hexadecimal representation of a random number."
68 (number->string (random (expt 2 256) %seed) 16))
69
70 (define (random-bytevector n)
71 "Return a random bytevector of N bytes."
72 (let ((bv (make-bytevector n)))
73 (let loop ((i 0))
74 (if (< i n)
75 (begin
76 (bytevector-u8-set! bv i (random 256 %seed))
77 (loop (1+ i)))
78 bv))))
79
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)))
85 (dynamic-wind
86 (lambda () (module-set! m 'proc replacement))
87 (lambda () body ...)
88 (lambda () (module-set! m 'proc original)))))
89
90 \f
91 ;;;
92 ;;; Narinfo files, as used by the substituter.
93 ;;;
94
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
99 expected hash."
100 (format #f "StorePath: ~a
101 URL: ~a
102 Compression: none
103 NarSize: 1234
104 NarHash: sha256:~a
105 References:
106 System: ~a
107 Deriver: ~a~%"
108 (derivation->output-path drv) ; StorePath
109 nar ; URL
110 (bytevector->nix-base32-string sha256) ; NarHash
111 (derivation-system drv) ; System
112 (basename
113 (derivation-file-name drv)))) ; Deriver
114
115 (define %substitute-directory
116 (make-parameter
117 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
118 (compose uri-path string->uri))))
119
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)
129 ".narinfo")))
130 (dynamic-wind
131 (lambda ()
132 (call-with-output-file info
133 (lambda (p)
134 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
135 (%store-prefix))))
136 (call-with-output-file narinfo
137 (lambda (p)
138 (display (derivation-narinfo drv #:sha256 sha256) p))))
139 thunk
140 (lambda ()
141 (delete-file narinfo)
142 (delete-file info)))))
143
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
150 (lambda () body ...)
151 #:sha256 hash))
152 ((_ drv body ...)
153 (call-with-derivation-narinfo drv
154 (lambda ()
155 body ...)))))
156
157 (define* (call-with-derivation-substitute drv contents thunk
158 #:key sha256)
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
162 CONTENTS."
163 (define dir (%substitute-directory))
164 (dynamic-wind
165 (lambda ()
166 (call-with-output-file (string-append dir "/example.out")
167 (lambda (port)
168 (display contents port)))
169 (call-with-output-file (string-append dir "/example.nar")
170 (lambda (p)
171 (write-file (string-append dir "/example.out") p))))
172 (lambda ()
173 (let ((hash (call-with-input-file (string-append dir "/example.nar")
174 port-sha256)))
175 ;; Create fake substituter data, to be read by `substitute-binary'.
176 (call-with-derivation-narinfo drv
177 thunk
178 #:sha256 (or sha256 hash))))
179 (lambda ()
180 (delete-file (string-append dir "/example.out"))
181 (delete-file (string-append dir "/example.nar")))))
182
183 (define-syntax with-derivation-substitute
184 (syntax-rules (sha256 =>)
185 "Evaluate BODY in a context where DRV is substitutable with the given
186 CONTENTS."
187 ((_ drv contents (sha256 => hash) body ...)
188 (call-with-derivation-substitute drv contents
189 (lambda () body ...)
190 #:sha256 hash))
191 ((_ drv contents body ...)
192 (call-with-derivation-substitute drv contents
193 (lambda ()
194 body ...)))))
195
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)))
204
205 ;; Local Variables:
206 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
207 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
208 ;; End:
209
210 ;;; tests.scm ends here