publish: Publish build logs.
[jackhill/guix/guix.git] / tests / publish.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 ;; Avoid interference.
21 (unsetenv "http_proxy")
22
23 (define-module (test-publish)
24 #:use-module (guix scripts publish)
25 #:use-module (guix tests)
26 #:use-module (guix config)
27 #:use-module (guix utils)
28 #:use-module (guix hash)
29 #:use-module (guix store)
30 #:use-module (guix derivations)
31 #:use-module (guix gexp)
32 #:use-module (guix base32)
33 #:use-module (guix base64)
34 #:use-module ((guix records) #:select (recutils->alist))
35 #:use-module ((guix serialization) #:select (restore-file))
36 #:use-module (guix pk-crypto)
37 #:use-module ((guix pki) #:select (%public-key-file %private-key-file))
38 #:use-module (guix zlib)
39 #:use-module (web uri)
40 #:use-module (web client)
41 #:use-module (web response)
42 #:use-module (rnrs bytevectors)
43 #:use-module (ice-9 binary-ports)
44 #:use-module (srfi srfi-1)
45 #:use-module (srfi srfi-26)
46 #:use-module (srfi srfi-64)
47 #:use-module (ice-9 format)
48 #:use-module (ice-9 match)
49 #:use-module (ice-9 rdelim))
50
51 (define %store
52 (open-connection-for-tests))
53
54 (define %reference (add-text-to-store %store "ref" "foo"))
55
56 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
57
58 (define (http-get-body uri)
59 (call-with-values (lambda () (http-get uri))
60 (lambda (response body) body)))
61
62 (define (http-get-port uri)
63 (let ((socket (open-socket-for-uri uri)))
64 ;; Make sure to use an unbuffered port so that we can then peek at the
65 ;; underlying file descriptor via 'call-with-gzip-input-port'.
66 (setvbuf socket _IONBF)
67 (call-with-values
68 (lambda ()
69 (http-get uri #:port socket #:streaming? #t))
70 (lambda (response port)
71 ;; Don't (setvbuf port _IONBF) because of <http://bugs.gnu.org/19610>
72 ;; (PORT might be a custom binary input port).
73 port))))
74
75 (define (publish-uri route)
76 (string-append "http://localhost:6789" route))
77
78 (define-syntax-rule (with-separate-output-ports exp ...)
79 ;; Since ports aren't thread-safe in Guile 2.0, duplicate the output and
80 ;; error ports to make sure the two threads don't end up stepping on each
81 ;; other's toes.
82 (with-output-to-port (duplicate-port (current-output-port) "w")
83 (lambda ()
84 (with-error-to-port (duplicate-port (current-error-port) "w")
85 (lambda ()
86 exp ...)))))
87
88 ;; Run a local publishing server in a separate thread.
89 (with-separate-output-ports
90 (call-with-new-thread
91 (lambda ()
92 (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
93
94 (define (wait-until-ready port)
95 ;; Wait until the server is accepting connections.
96 (let ((conn (socket PF_INET SOCK_STREAM 0)))
97 (let loop ()
98 (unless (false-if-exception
99 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
100 (loop)))))
101
102 (define (wait-for-file file)
103 ;; Wait until FILE shows up.
104 (let loop ((i 20))
105 (cond ((file-exists? file)
106 #t)
107 ((zero? i)
108 (error "file didn't show up" file))
109 (else
110 (pk 'wait-for-file file)
111 (sleep 1)
112 (loop (- i 1))))))
113
114 ;; Wait until the two servers are ready.
115 (wait-until-ready 6789)
116
117 ;; Initialize the public/private key SRFI-39 parameters.
118 (%public-key (read-file-sexp %public-key-file))
119 (%private-key (read-file-sexp %private-key-file))
120
121 \f
122 (test-begin "publish")
123
124 (test-equal "/nix-cache-info"
125 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
126 %store-directory)
127 (http-get-body (publish-uri "/nix-cache-info")))
128
129 (test-equal "/*.narinfo"
130 (let* ((info (query-path-info %store %item))
131 (unsigned-info
132 (format #f
133 "StorePath: ~a
134 URL: nar/~a
135 Compression: none
136 NarHash: sha256:~a
137 NarSize: ~d
138 References: ~a
139 FileSize: ~a~%"
140 %item
141 (basename %item)
142 (bytevector->nix-base32-string
143 (path-info-hash info))
144 (path-info-nar-size info)
145 (basename (first (path-info-references info)))
146 (path-info-nar-size info)))
147 (signature (base64-encode
148 (string->utf8
149 (canonical-sexp->string
150 ((@@ (guix scripts publish) signed-string)
151 unsigned-info))))))
152 (format #f "~aSignature: 1;~a;~a~%"
153 unsigned-info (gethostname) signature))
154 (utf8->string
155 (http-get-body
156 (publish-uri
157 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
158
159 (test-equal "/*.narinfo with properly encoded '+' sign"
160 ;; See <http://bugs.gnu.org/21888>.
161 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
162 (info (query-path-info %store item))
163 (unsigned-info
164 (format #f
165 "StorePath: ~a
166 URL: nar/~a
167 Compression: none
168 NarHash: sha256:~a
169 NarSize: ~d
170 References: ~%\
171 FileSize: ~a~%"
172 item
173 (uri-encode (basename item))
174 (bytevector->nix-base32-string
175 (path-info-hash info))
176 (path-info-nar-size info)
177 (path-info-nar-size info)))
178 (signature (base64-encode
179 (string->utf8
180 (canonical-sexp->string
181 ((@@ (guix scripts publish) signed-string)
182 unsigned-info))))))
183 (format #f "~aSignature: 1;~a;~a~%"
184 unsigned-info (gethostname) signature))
185
186 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
187 (utf8->string
188 (http-get-body
189 (publish-uri
190 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
191
192 (test-equal "/nar/*"
193 "bar"
194 (call-with-temporary-output-file
195 (lambda (temp port)
196 (let ((nar (utf8->string
197 (http-get-body
198 (publish-uri
199 (string-append "/nar/" (basename %item)))))))
200 (call-with-input-string nar (cut restore-file <> temp)))
201 (call-with-input-file temp read-string))))
202
203 (unless (zlib-available?)
204 (test-skip 1))
205 (test-equal "/nar/gzip/*"
206 "bar"
207 (call-with-temporary-output-file
208 (lambda (temp port)
209 (let ((nar (http-get-port
210 (publish-uri
211 (string-append "/nar/gzip/" (basename %item))))))
212 (call-with-gzip-input-port nar
213 (cut restore-file <> temp)))
214 (call-with-input-file temp read-string))))
215
216 (unless (zlib-available?)
217 (test-skip 1))
218 (test-equal "/*.narinfo with compression"
219 `(("StorePath" . ,%item)
220 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
221 ("Compression" . "gzip"))
222 (let ((thread (with-separate-output-ports
223 (call-with-new-thread
224 (lambda ()
225 (guix-publish "--port=6799" "-C5"))))))
226 (wait-until-ready 6799)
227 (let* ((url (string-append "http://localhost:6799/"
228 (store-path-hash-part %item) ".narinfo"))
229 (body (http-get-port url)))
230 (filter (lambda (item)
231 (match item
232 (("Compression" . _) #t)
233 (("StorePath" . _) #t)
234 (("URL" . _) #t)
235 (_ #f)))
236 (recutils->alist body)))))
237
238 (unless (zlib-available?)
239 (test-skip 1))
240 (test-equal "/*.narinfo for a compressed file"
241 '("none" "nar") ;compression-less nar
242 ;; Assume 'guix publish -C' is already running on port 6799.
243 (let* ((item (add-text-to-store %store "fake.tar.gz"
244 "This is a fake compressed file."))
245 (url (string-append "http://localhost:6799/"
246 (store-path-hash-part item) ".narinfo"))
247 (body (http-get-port url))
248 (info (recutils->alist body)))
249 (list (assoc-ref info "Compression")
250 (dirname (assoc-ref info "URL")))))
251
252 (test-equal "custom nar path"
253 ;; Serve nars at /foo/bar/chbouib instead of /nar.
254 (list `(("StorePath" . ,%item)
255 ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
256 ("Compression" . "none"))
257 200
258 404)
259 (let ((thread (with-separate-output-ports
260 (call-with-new-thread
261 (lambda ()
262 (guix-publish "--port=6798" "-C0"
263 "--nar-path=///foo/bar//chbouib/"))))))
264 (wait-until-ready 6798)
265 (let* ((base "http://localhost:6798/")
266 (part (store-path-hash-part %item))
267 (url (string-append base part ".narinfo"))
268 (nar-url (string-append base "foo/bar/chbouib/"
269 (basename %item)))
270 (body (http-get-port url)))
271 (list (filter (lambda (item)
272 (match item
273 (("Compression" . _) #t)
274 (("StorePath" . _) #t)
275 (("URL" . _) #t)
276 (_ #f)))
277 (recutils->alist body))
278 (response-code (http-get nar-url))
279 (response-code
280 (http-get (string-append base "nar/" (basename %item))))))))
281
282 (test-equal "/nar/ with properly encoded '+' sign"
283 "Congrats!"
284 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
285 (call-with-temporary-output-file
286 (lambda (temp port)
287 (let ((nar (utf8->string
288 (http-get-body
289 (publish-uri
290 (string-append "/nar/" (uri-encode (basename item))))))))
291 (call-with-input-string nar (cut restore-file <> temp)))
292 (call-with-input-file temp read-string)))))
293
294 (test-equal "/nar/invalid"
295 404
296 (begin
297 (call-with-output-file (string-append (%store-prefix) "/invalid")
298 (lambda (port)
299 (display "This file is not a valid store item." port)))
300 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
301
302 (test-equal "/file/NAME/sha256/HASH"
303 "Hello, Guix world!"
304 (let* ((data "Hello, Guix world!")
305 (hash (call-with-input-string data port-sha256))
306 (drv (run-with-store %store
307 (gexp->derivation "the-file.txt"
308 #~(call-with-output-file #$output
309 (lambda (port)
310 (display #$data port)))
311 #:hash-algo 'sha256
312 #:hash hash)))
313 (out (build-derivations %store (list drv))))
314 (utf8->string
315 (http-get-body
316 (publish-uri
317 (string-append "/file/the-file.txt/sha256/"
318 (bytevector->nix-base32-string hash)))))))
319
320 (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
321 404
322 (let ((uri (publish-uri
323 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
324 (response-code (http-get uri))))
325
326 (test-equal "/file/NAME/sha256/INVALID-HASH"
327 404
328 (let ((uri (publish-uri
329 (string-append "/file/the-file.txt/sha256/"
330 (bytevector->nix-base32-string
331 (call-with-input-string "" port-sha256))))))
332 (response-code (http-get uri))))
333
334 (unless (zlib-available?)
335 (test-skip 1))
336 (test-equal "with cache"
337 (list #t
338 `(("StorePath" . ,%item)
339 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
340 ("Compression" . "gzip"))
341 200 ;nar/gzip/…
342 #t ;Content-Length
343 #t ;FileSize
344 404) ;nar/…
345 (call-with-temporary-directory
346 (lambda (cache)
347 (let ((thread (with-separate-output-ports
348 (call-with-new-thread
349 (lambda ()
350 (guix-publish "--port=6797" "-C2"
351 (string-append "--cache=" cache)))))))
352 (wait-until-ready 6797)
353 (let* ((base "http://localhost:6797/")
354 (part (store-path-hash-part %item))
355 (url (string-append base part ".narinfo"))
356 (nar-url (string-append base "nar/gzip/" (basename %item)))
357 (cached (string-append cache "/gzip/" (basename %item)
358 ".narinfo"))
359 (nar (string-append cache "/gzip/"
360 (basename %item) ".nar"))
361 (response (http-get url)))
362 (and (= 404 (response-code response))
363
364 ;; We should get an explicitly short TTL for 404 in this case
365 ;; because it's going to become 200 shortly.
366 (match (assq-ref (response-headers response) 'cache-control)
367 ((('max-age . ttl))
368 (< ttl 3600)))
369
370 (wait-for-file cached)
371 (let* ((body (http-get-port url))
372 (compressed (http-get nar-url))
373 (uncompressed (http-get (string-append base "nar/"
374 (basename %item))))
375 (narinfo (recutils->alist body)))
376 (list (file-exists? nar)
377 (filter (lambda (item)
378 (match item
379 (("Compression" . _) #t)
380 (("StorePath" . _) #t)
381 (("URL" . _) #t)
382 (_ #f)))
383 narinfo)
384 (response-code compressed)
385 (= (response-content-length compressed)
386 (stat:size (stat nar)))
387 (= (string->number
388 (assoc-ref narinfo "FileSize"))
389 (stat:size (stat nar)))
390 (response-code uncompressed)))))))))
391
392 (unless (zlib-available?)
393 (test-skip 1))
394 (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
395 (random-text))))
396 (test-equal "with cache, uncompressed"
397 (list #t
398 `(("StorePath" . ,item)
399 ("URL" . ,(string-append "nar/" (basename item)))
400 ("Compression" . "none"))
401 200 ;nar/…
402 (path-info-nar-size
403 (query-path-info %store item)) ;FileSize
404 404) ;nar/gzip/…
405 (call-with-temporary-directory
406 (lambda (cache)
407 (let ((thread (with-separate-output-ports
408 (call-with-new-thread
409 (lambda ()
410 (guix-publish "--port=6796" "-C2"
411 (string-append "--cache=" cache)))))))
412 (wait-until-ready 6796)
413 (let* ((base "http://localhost:6796/")
414 (part (store-path-hash-part item))
415 (url (string-append base part ".narinfo"))
416 (cached (string-append cache "/none/"
417 (basename item) ".narinfo"))
418 (nar (string-append cache "/none/"
419 (basename item) ".nar"))
420 (response (http-get url)))
421 (and (= 404 (response-code response))
422
423 (wait-for-file cached)
424 (let* ((body (http-get-port url))
425 (compressed (http-get (string-append base "nar/gzip/"
426 (basename item))))
427 (uncompressed (http-get (string-append base "nar/"
428 (basename item))))
429 (narinfo (recutils->alist body)))
430 (list (file-exists? nar)
431 (filter (lambda (item)
432 (match item
433 (("Compression" . _) #t)
434 (("StorePath" . _) #t)
435 (("URL" . _) #t)
436 (_ #f)))
437 narinfo)
438 (response-code uncompressed)
439 (string->number
440 (assoc-ref narinfo "FileSize"))
441 (response-code compressed))))))))))
442
443 (test-equal "/log/NAME"
444 `(200 #t application/x-bzip2)
445 (let ((drv (run-with-store %store
446 (gexp->derivation "with-log"
447 #~(call-with-output-file #$output
448 (lambda (port)
449 (display "Hello, build log!"
450 (current-error-port))
451 (display "" port)))))))
452 (build-derivations %store (list drv))
453 (let* ((response (http-get
454 (publish-uri (string-append "/log/"
455 (basename (derivation->output-path drv))))
456 #:decode-body? #f))
457 (base (basename (derivation-file-name drv)))
458 (log (string-append (dirname %state-directory)
459 "/log/guix/drvs/" (string-take base 2)
460 "/" (string-drop base 2) ".bz2")))
461 (list (response-code response)
462 (= (response-content-length response) (stat:size (stat log)))
463 (first (response-content-type response))))))
464
465 (test-equal "/log/NAME not found"
466 404
467 (let ((uri (publish-uri "/log/does-not-exist")))
468 (response-code (http-get uri))))
469
470 (test-end "publish")