tests: Skip tests that would fail due to the shebang length.
[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 network-reachable?
35 shebang-too-long?
36 mock
37 %substitute-directory
38 with-derivation-narinfo
39 with-derivation-substitute
40 dummy-package))
41
42 ;;; Commentary:
43 ;;;
44 ;;; This module provide shared infrastructure for the test suite. For
45 ;;; internal use only.
46 ;;;
47 ;;; Code:
48
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)
54 #f))
55 (let ((store (open-connection)))
56 ;; Make sure we build everything by ourselves.
57 (set-build-options store #:use-substitutes? #f)
58
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))
62
63 store)))
64
65 (define %seed
66 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
67
68 (define (random-text)
69 "Return the hexadecimal representation of a random number."
70 (number->string (random (expt 2 256) %seed) 16))
71
72 (define (random-bytevector n)
73 "Return a random bytevector of N bytes."
74 (let ((bv (make-bytevector n)))
75 (let loop ((i 0))
76 (if (< i n)
77 (begin
78 (bytevector-u8-set! bv i (random 256 %seed))
79 (loop (1+ i)))
80 bv))))
81
82 (define (network-reachable?)
83 "Return true if we can reach the Internet."
84 (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
85
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)))
91 (dynamic-wind
92 (lambda () (module-set! m 'proc replacement))
93 (lambda () body ...)
94 (lambda () (module-set! m 'proc original)))))
95
96 \f
97 ;;;
98 ;;; Narinfo files, as used by the substituter.
99 ;;;
100
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
105 expected hash."
106 (format #f "StorePath: ~a
107 URL: ~a
108 Compression: none
109 NarSize: 1234
110 NarHash: sha256:~a
111 References:
112 System: ~a
113 Deriver: ~a~%"
114 (derivation->output-path drv) ; StorePath
115 nar ; URL
116 (bytevector->nix-base32-string sha256) ; NarHash
117 (derivation-system drv) ; System
118 (basename
119 (derivation-file-name drv)))) ; Deriver
120
121 (define %substitute-directory
122 (make-parameter
123 (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
124 (compose uri-path string->uri))))
125
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)
135 ".narinfo")))
136 (dynamic-wind
137 (lambda ()
138 (call-with-output-file info
139 (lambda (p)
140 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
141 (%store-prefix))))
142 (call-with-output-file narinfo
143 (lambda (p)
144 (display (derivation-narinfo drv #:sha256 sha256) p))))
145 thunk
146 (lambda ()
147 (delete-file narinfo)
148 (delete-file info)))))
149
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
156 (lambda () body ...)
157 #:sha256 hash))
158 ((_ drv body ...)
159 (call-with-derivation-narinfo drv
160 (lambda ()
161 body ...)))))
162
163 (define* (call-with-derivation-substitute drv contents thunk
164 #:key sha256)
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
168 CONTENTS."
169 (define dir (%substitute-directory))
170 (dynamic-wind
171 (lambda ()
172 (call-with-output-file (string-append dir "/example.out")
173 (lambda (port)
174 (display contents port)))
175 (call-with-output-file (string-append dir "/example.nar")
176 (lambda (p)
177 (write-file (string-append dir "/example.out") p))))
178 (lambda ()
179 (let ((hash (call-with-input-file (string-append dir "/example.nar")
180 port-sha256)))
181 ;; Create fake substituter data, to be read by `substitute-binary'.
182 (call-with-derivation-narinfo drv
183 thunk
184 #:sha256 (or sha256 hash))))
185 (lambda ()
186 (delete-file (string-append dir "/example.out"))
187 (delete-file (string-append dir "/example.nar")))))
188
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
192 all included."
193 (define shebang
194 (string-append "#!" (%store-prefix) "/"
195 (make-string 32 #\a)
196 "-bootstrap-binaries-0/bin/bash\0"))
197
198 (> (string-length shebang) 128))
199
200 (define-syntax with-derivation-substitute
201 (syntax-rules (sha256 =>)
202 "Evaluate BODY in a context where DRV is substitutable with the given
203 CONTENTS."
204 ((_ drv contents (sha256 => hash) body ...)
205 (call-with-derivation-substitute drv contents
206 (lambda () body ...)
207 #:sha256 hash))
208 ((_ drv contents body ...)
209 (call-with-derivation-substitute drv contents
210 (lambda ()
211 body ...)))))
212
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)))
221
222 ;; Local Variables:
223 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
224 ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
225 ;; End:
226
227 ;;; tests.scm ends here