gnu: Add guile-srfi-159.
[jackhill/guix/guix.git] / tests / publish.scm
CommitLineData
aff8ce7c
DT
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2015 David Thompson <davet@gnu.org>
76832d34 3;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
aff8ce7c
DT
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
c74f0cb2
LC
20;; Avoid interference.
21(unsetenv "http_proxy")
22
aff8ce7c
DT
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)
ca719424 28 #:use-module (gcrypt hash)
aff8ce7c 29 #:use-module (guix store)
ff6638d1
LC
30 #:use-module (guix derivations)
31 #:use-module (guix gexp)
aff8ce7c
DT
32 #:use-module (guix base32)
33 #:use-module (guix base64)
4a1fc562 34 #:use-module ((guix records) #:select (recutils->alist))
aff8ce7c 35 #:use-module ((guix serialization) #:select (restore-file))
ca719424 36 #:use-module (gcrypt pk-crypto)
ab2a74e4 37 #:use-module ((guix pki) #:select (%public-key-file %private-key-file))
4a1fc562 38 #:use-module (guix zlib)
66229b04 39 #:use-module (guix lzlib)
93961f02 40 #:use-module (web uri)
aff8ce7c
DT
41 #:use-module (web client)
42 #:use-module (web response)
43 #:use-module (rnrs bytevectors)
4a1fc562 44 #:use-module (ice-9 binary-ports)
aff8ce7c
DT
45 #:use-module (srfi srfi-1)
46 #:use-module (srfi srfi-26)
47 #:use-module (srfi srfi-64)
93961f02 48 #:use-module (ice-9 format)
aff8ce7c
DT
49 #:use-module (ice-9 match)
50 #:use-module (ice-9 rdelim))
51
52(define %store
53 (open-connection-for-tests))
54
55(define %reference (add-text-to-store %store "ref" "foo"))
56
57(define %item (add-text-to-store %store "item" "bar" (list %reference)))
58
59(define (http-get-body uri)
60 (call-with-values (lambda () (http-get uri))
61 (lambda (response body) body)))
62
4a1fc562 63(define (http-get-port uri)
37402ecb
LC
64 (let ((socket (open-socket-for-uri uri)))
65 ;; Make sure to use an unbuffered port so that we can then peek at the
66 ;; underlying file descriptor via 'call-with-gzip-input-port'.
76832d34 67 (setvbuf socket 'none)
37402ecb
LC
68 (call-with-values
69 (lambda ()
70 (http-get uri #:port socket #:streaming? #t))
71 (lambda (response port)
76832d34 72 ;; Don't (setvbuf port 'none) because of <http://bugs.gnu.org/19610>
2c7b48c2 73 ;; (PORT might be a custom binary input port).
37402ecb 74 port))))
4a1fc562 75
aff8ce7c
DT
76(define (publish-uri route)
77 (string-append "http://localhost:6789" route))
78
a5c37603
LC
79(define-syntax-rule (with-separate-output-ports exp ...)
80 ;; Since ports aren't thread-safe in Guile 2.0, duplicate the output and
81 ;; error ports to make sure the two threads don't end up stepping on each
82 ;; other's toes.
83 (with-output-to-port (duplicate-port (current-output-port) "w")
84 (lambda ()
85 (with-error-to-port (duplicate-port (current-error-port) "w")
86 (lambda ()
87 exp ...)))))
88
aff8ce7c 89;; Run a local publishing server in a separate thread.
a5c37603
LC
90(with-separate-output-ports
91 (call-with-new-thread
92 (lambda ()
93 (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
4a1fc562
LC
94
95(define (wait-until-ready port)
96 ;; Wait until the server is accepting connections.
97 (let ((conn (socket PF_INET SOCK_STREAM 0)))
98 (let loop ()
99 (unless (false-if-exception
100 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
101 (loop)))))
aff8ce7c 102
ffa5e0a6
LC
103(define (wait-for-file file)
104 ;; Wait until FILE shows up.
105 (let loop ((i 20))
106 (cond ((file-exists? file)
107 #t)
108 ((zero? i)
109 (error "file didn't show up" file))
110 (else
111 (pk 'wait-for-file file)
112 (sleep 1)
113 (loop (- i 1))))))
114
33988f9b
LC
115(define %gzip-magic-bytes
116 ;; Magic bytes of gzip file.
117 #vu8(#x1f #x8b))
118
4a1fc562
LC
119;; Wait until the two servers are ready.
120(wait-until-ready 6789)
aff8ce7c 121
ab2a74e4
LC
122;; Initialize the public/private key SRFI-39 parameters.
123(%public-key (read-file-sexp %public-key-file))
124(%private-key (read-file-sexp %private-key-file))
125
c74f0cb2 126\f
aff8ce7c
DT
127(test-begin "publish")
128
129(test-equal "/nix-cache-info"
130 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
131 %store-directory)
132 (http-get-body (publish-uri "/nix-cache-info")))
133
134(test-equal "/*.narinfo"
135 (let* ((info (query-path-info %store %item))
136 (unsigned-info
137 (format #f
138 "StorePath: ~a
139URL: nar/~a
140Compression: none
141NarHash: sha256:~a
142NarSize: ~d
dff3189c
LC
143References: ~a
144FileSize: ~a~%"
aff8ce7c
DT
145 %item
146 (basename %item)
9cced526 147 (bytevector->nix-base32-string
aff8ce7c
DT
148 (path-info-hash info))
149 (path-info-nar-size info)
dff3189c
LC
150 (basename (first (path-info-references info)))
151 (path-info-nar-size info)))
aff8ce7c
DT
152 (signature (base64-encode
153 (string->utf8
154 (canonical-sexp->string
155 ((@@ (guix scripts publish) signed-string)
156 unsigned-info))))))
157 (format #f "~aSignature: 1;~a;~a~%"
158 unsigned-info (gethostname) signature))
159 (utf8->string
160 (http-get-body
161 (publish-uri
162 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
163
93961f02
LC
164(test-equal "/*.narinfo with properly encoded '+' sign"
165 ;; See <http://bugs.gnu.org/21888>.
166 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
167 (info (query-path-info %store item))
168 (unsigned-info
169 (format #f
170 "StorePath: ~a
171URL: nar/~a
172Compression: none
173NarHash: sha256:~a
174NarSize: ~d
dff3189c
LC
175References: ~%\
176FileSize: ~a~%"
93961f02
LC
177 item
178 (uri-encode (basename item))
179 (bytevector->nix-base32-string
180 (path-info-hash info))
dff3189c 181 (path-info-nar-size info)
93961f02
LC
182 (path-info-nar-size info)))
183 (signature (base64-encode
184 (string->utf8
185 (canonical-sexp->string
186 ((@@ (guix scripts publish) signed-string)
187 unsigned-info))))))
188 (format #f "~aSignature: 1;~a;~a~%"
189 unsigned-info (gethostname) signature))
190
191 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
192 (utf8->string
193 (http-get-body
194 (publish-uri
195 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
196
aff8ce7c
DT
197(test-equal "/nar/*"
198 "bar"
199 (call-with-temporary-output-file
200 (lambda (temp port)
201 (let ((nar (utf8->string
202 (http-get-body
203 (publish-uri
204 (string-append "/nar/" (basename %item)))))))
205 (call-with-input-string nar (cut restore-file <> temp)))
206 (call-with-input-file temp read-string))))
207
4a1fc562
LC
208(unless (zlib-available?)
209 (test-skip 1))
210(test-equal "/nar/gzip/*"
211 "bar"
212 (call-with-temporary-output-file
213 (lambda (temp port)
214 (let ((nar (http-get-port
215 (publish-uri
216 (string-append "/nar/gzip/" (basename %item))))))
217 (call-with-gzip-input-port nar
218 (cut restore-file <> temp)))
219 (call-with-input-file temp read-string))))
220
33988f9b
LC
221(unless (zlib-available?)
222 (test-skip 1))
223(test-equal "/nar/gzip/* is really gzip"
224 %gzip-magic-bytes
225 ;; Since 'gzdopen' (aka. 'call-with-gzip-input-port') transparently reads
226 ;; uncompressed gzip, the test above doesn't check whether it's actually
227 ;; gzip. This is what this test does. See <https://bugs.gnu.org/30184>.
228 (let ((nar (http-get-port
229 (publish-uri
230 (string-append "/nar/gzip/" (basename %item))))))
231 (get-bytevector-n nar (bytevector-length %gzip-magic-bytes))))
232
66229b04
LC
233(unless (lzlib-available?)
234 (test-skip 1))
235(test-equal "/nar/lzip/*"
236 "bar"
237 (call-with-temporary-output-file
238 (lambda (temp port)
239 (let ((nar (http-get-port
240 (publish-uri
241 (string-append "/nar/lzip/" (basename %item))))))
242 (call-with-lzip-input-port nar
243 (cut restore-file <> temp)))
244 (call-with-input-file temp read-string))))
245
4a1fc562
LC
246(unless (zlib-available?)
247 (test-skip 1))
248(test-equal "/*.narinfo with compression"
249 `(("StorePath" . ,%item)
250 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
251 ("Compression" . "gzip"))
a5c37603
LC
252 (let ((thread (with-separate-output-ports
253 (call-with-new-thread
254 (lambda ()
255 (guix-publish "--port=6799" "-C5"))))))
4a1fc562
LC
256 (wait-until-ready 6799)
257 (let* ((url (string-append "http://localhost:6799/"
258 (store-path-hash-part %item) ".narinfo"))
259 (body (http-get-port url)))
260 (filter (lambda (item)
261 (match item
262 (("Compression" . _) #t)
263 (("StorePath" . _) #t)
264 (("URL" . _) #t)
265 (_ #f)))
266 (recutils->alist body)))))
267
66229b04
LC
268(unless (lzlib-available?)
269 (test-skip 1))
270(test-equal "/*.narinfo with lzip compression"
271 `(("StorePath" . ,%item)
272 ("URL" . ,(string-append "nar/lzip/" (basename %item)))
273 ("Compression" . "lzip"))
274 (let ((thread (with-separate-output-ports
275 (call-with-new-thread
276 (lambda ()
277 (guix-publish "--port=6790" "-Clzip"))))))
278 (wait-until-ready 6790)
279 (let* ((url (string-append "http://localhost:6790/"
280 (store-path-hash-part %item) ".narinfo"))
281 (body (http-get-port url)))
282 (filter (lambda (item)
283 (match item
284 (("Compression" . _) #t)
285 (("StorePath" . _) #t)
286 (("URL" . _) #t)
287 (_ #f)))
288 (recutils->alist body)))))
289
089b1678
LC
290(unless (zlib-available?)
291 (test-skip 1))
292(test-equal "/*.narinfo for a compressed file"
293 '("none" "nar") ;compression-less nar
294 ;; Assume 'guix publish -C' is already running on port 6799.
295 (let* ((item (add-text-to-store %store "fake.tar.gz"
296 "This is a fake compressed file."))
297 (url (string-append "http://localhost:6799/"
298 (store-path-hash-part item) ".narinfo"))
299 (body (http-get-port url))
300 (info (recutils->alist body)))
301 (list (assoc-ref info "Compression")
302 (dirname (assoc-ref info "URL")))))
303
cdd7a7d2
LC
304(test-equal "custom nar path"
305 ;; Serve nars at /foo/bar/chbouib instead of /nar.
306 (list `(("StorePath" . ,%item)
307 ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
308 ("Compression" . "none"))
309 200
310 404)
311 (let ((thread (with-separate-output-ports
312 (call-with-new-thread
313 (lambda ()
314 (guix-publish "--port=6798" "-C0"
315 "--nar-path=///foo/bar//chbouib/"))))))
316 (wait-until-ready 6798)
317 (let* ((base "http://localhost:6798/")
318 (part (store-path-hash-part %item))
319 (url (string-append base part ".narinfo"))
320 (nar-url (string-append base "foo/bar/chbouib/"
321 (basename %item)))
322 (body (http-get-port url)))
323 (list (filter (lambda (item)
324 (match item
325 (("Compression" . _) #t)
326 (("StorePath" . _) #t)
327 (("URL" . _) #t)
328 (_ #f)))
329 (recutils->alist body))
330 (response-code (http-get nar-url))
331 (response-code
332 (http-get (string-append base "nar/" (basename %item))))))))
333
93961f02
LC
334(test-equal "/nar/ with properly encoded '+' sign"
335 "Congrats!"
336 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
337 (call-with-temporary-output-file
338 (lambda (temp port)
339 (let ((nar (utf8->string
340 (http-get-body
341 (publish-uri
342 (string-append "/nar/" (uri-encode (basename item))))))))
343 (call-with-input-string nar (cut restore-file <> temp)))
344 (call-with-input-file temp read-string)))))
345
00435580
LC
346(test-equal "/nar/invalid"
347 404
348 (begin
349 (call-with-output-file (string-append (%store-prefix) "/invalid")
350 (lambda (port)
351 (display "This file is not a valid store item." port)))
352 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
353
ff6638d1
LC
354(test-equal "/file/NAME/sha256/HASH"
355 "Hello, Guix world!"
356 (let* ((data "Hello, Guix world!")
357 (hash (call-with-input-string data port-sha256))
358 (drv (run-with-store %store
359 (gexp->derivation "the-file.txt"
360 #~(call-with-output-file #$output
361 (lambda (port)
362 (display #$data port)))
363 #:hash-algo 'sha256
364 #:hash hash)))
365 (out (build-derivations %store (list drv))))
366 (utf8->string
367 (http-get-body
368 (publish-uri
369 (string-append "/file/the-file.txt/sha256/"
370 (bytevector->nix-base32-string hash)))))))
371
372(test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
373 404
374 (let ((uri (publish-uri
375 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
376 (response-code (http-get uri))))
377
378(test-equal "/file/NAME/sha256/INVALID-HASH"
379 404
380 (let ((uri (publish-uri
381 (string-append "/file/the-file.txt/sha256/"
382 (bytevector->nix-base32-string
383 (call-with-input-string "" port-sha256))))))
384 (response-code (http-get uri))))
385
00753f70
LC
386(unless (zlib-available?)
387 (test-skip 1))
388(test-equal "with cache"
389 (list #t
390 `(("StorePath" . ,%item)
391 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
392 ("Compression" . "gzip"))
393 200 ;nar/gzip/…
394 #t ;Content-Length
dff3189c 395 #t ;FileSize
e5788ebb 396 404) ;nar/…
00753f70
LC
397 (call-with-temporary-directory
398 (lambda (cache)
00753f70
LC
399 (let ((thread (with-separate-output-ports
400 (call-with-new-thread
401 (lambda ()
402 (guix-publish "--port=6797" "-C2"
403 (string-append "--cache=" cache)))))))
404 (wait-until-ready 6797)
405 (let* ((base "http://localhost:6797/")
406 (part (store-path-hash-part %item))
407 (url (string-append base part ".narinfo"))
2a405034 408 (nar-url (string-append base "nar/gzip/" (basename %item)))
00753f70
LC
409 (cached (string-append cache "/gzip/" (basename %item)
410 ".narinfo"))
411 (nar (string-append cache "/gzip/"
412 (basename %item) ".nar"))
413 (response (http-get url)))
414 (and (= 404 (response-code response))
24b21720
LC
415
416 ;; We should get an explicitly short TTL for 404 in this case
417 ;; because it's going to become 200 shortly.
418 (match (assq-ref (response-headers response) 'cache-control)
419 ((('max-age . ttl))
420 (< ttl 3600)))
421
00753f70 422 (wait-for-file cached)
dff3189c
LC
423 (let* ((body (http-get-port url))
424 (compressed (http-get nar-url))
425 (uncompressed (http-get (string-append base "nar/"
426 (basename %item))))
427 (narinfo (recutils->alist body)))
00753f70
LC
428 (list (file-exists? nar)
429 (filter (lambda (item)
430 (match item
431 (("Compression" . _) #t)
432 (("StorePath" . _) #t)
433 (("URL" . _) #t)
434 (_ #f)))
dff3189c 435 narinfo)
00753f70
LC
436 (response-code compressed)
437 (= (response-content-length compressed)
438 (stat:size (stat nar)))
dff3189c
LC
439 (= (string->number
440 (assoc-ref narinfo "FileSize"))
441 (stat:size (stat nar)))
00753f70
LC
442 (response-code uncompressed)))))))))
443
ffa5e0a6
LC
444(unless (zlib-available?)
445 (test-skip 1))
446(let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
447 (random-text))))
448 (test-equal "with cache, uncompressed"
e5788ebb 449 (list #t
9b9de084 450 (* 42 3600) ;TTL on narinfo
ffa5e0a6
LC
451 `(("StorePath" . ,item)
452 ("URL" . ,(string-append "nar/" (basename item)))
453 ("Compression" . "none"))
454 200 ;nar/…
9b9de084 455 (* 42 3600) ;TTL on nar/…
ffa5e0a6
LC
456 (path-info-nar-size
457 (query-path-info %store item)) ;FileSize
458 404) ;nar/gzip/…
459 (call-with-temporary-directory
460 (lambda (cache)
461 (let ((thread (with-separate-output-ports
462 (call-with-new-thread
463 (lambda ()
9b9de084 464 (guix-publish "--port=6796" "-C2" "--ttl=42h"
ffa5e0a6
LC
465 (string-append "--cache=" cache)))))))
466 (wait-until-ready 6796)
467 (let* ((base "http://localhost:6796/")
468 (part (store-path-hash-part item))
469 (url (string-append base part ".narinfo"))
470 (cached (string-append cache "/none/"
471 (basename item) ".narinfo"))
472 (nar (string-append cache "/none/"
473 (basename item) ".nar"))
474 (response (http-get url)))
475 (and (= 404 (response-code response))
476
477 (wait-for-file cached)
9b9de084
LC
478 (let* ((response (http-get url))
479 (body (http-get-port url))
ffa5e0a6
LC
480 (compressed (http-get (string-append base "nar/gzip/"
481 (basename item))))
482 (uncompressed (http-get (string-append base "nar/"
483 (basename item))))
484 (narinfo (recutils->alist body)))
485 (list (file-exists? nar)
9b9de084
LC
486 (match (assq-ref (response-headers response)
487 'cache-control)
488 ((('max-age . ttl)) ttl)
489 (_ #f))
490
ffa5e0a6
LC
491 (filter (lambda (item)
492 (match item
493 (("Compression" . _) #t)
494 (("StorePath" . _) #t)
495 (("URL" . _) #t)
496 (_ #f)))
497 narinfo)
498 (response-code uncompressed)
9b9de084
LC
499 (match (assq-ref (response-headers uncompressed)
500 'cache-control)
501 ((('max-age . ttl)) ttl)
502 (_ #f))
503
ffa5e0a6
LC
504 (string->number
505 (assoc-ref narinfo "FileSize"))
506 (response-code compressed))))))))))
507
493375cd
LC
508(test-equal "with cache, vanishing item" ;<https://bugs.gnu.org/33897>
509 200
510 (call-with-temporary-directory
511 (lambda (cache)
512 (let ((thread (with-separate-output-ports
513 (call-with-new-thread
514 (lambda ()
515 (guix-publish "--port=6795"
516 (string-append "--cache=" cache)))))))
517 (wait-until-ready 6795)
518
519 ;; Make sure that, even if ITEM disappears, we're still able to fetch
520 ;; it.
521 (let* ((base "http://localhost:6795/")
522 (item (add-text-to-store %store "random" (random-text)))
523 (part (store-path-hash-part item))
524 (url (string-append base part ".narinfo"))
525 (cached (string-append cache
526 (if (zlib-available?)
527 "/gzip/" "/none/")
528 (basename item)
529 ".narinfo"))
530 (response (http-get url)))
531 (and (= 404 (response-code response))
532 (wait-for-file cached)
533 (begin
534 (delete-paths %store (list item))
535 (response-code (pk 'response (http-get url))))))))))
536
c04ffadb
LC
537(test-equal "/log/NAME"
538 `(200 #t application/x-bzip2)
539 (let ((drv (run-with-store %store
540 (gexp->derivation "with-log"
541 #~(call-with-output-file #$output
542 (lambda (port)
543 (display "Hello, build log!"
544 (current-error-port))
04d2a16c 545 (display #$(random-text) port)))))))
c04ffadb
LC
546 (build-derivations %store (list drv))
547 (let* ((response (http-get
548 (publish-uri (string-append "/log/"
549 (basename (derivation->output-path drv))))
550 #:decode-body? #f))
551 (base (basename (derivation-file-name drv)))
552 (log (string-append (dirname %state-directory)
553 "/log/guix/drvs/" (string-take base 2)
554 "/" (string-drop base 2) ".bz2")))
555 (list (response-code response)
556 (= (response-content-length response) (stat:size (stat log)))
557 (first (response-content-type response))))))
558
559(test-equal "/log/NAME not found"
560 404
561 (let ((uri (publish-uri "/log/does-not-exist")))
562 (response-code (http-get uri))))
563
c1cd155a
LC
564(test-equal "non-GET query"
565 '(200 404)
566 (let ((path (string-append "/" (store-path-hash-part %item)
567 ".narinfo")))
568 (map response-code
569 (list (http-get (publish-uri path))
570 (http-post (publish-uri path))))))
571
aff8ce7c 572(test-end "publish")