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