Debian Codegraph @

tests/publish.scm

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <[email protected]>
3 ;;; Copyright © 2020 by Amar M. Singh <[email protected]>
4 ;;; Copyright © 2016-2022 Ludovic Courtès <[email protected]>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 ;; Avoid interference.
22 (unsetenv "http_proxy")
23
24 (define-module (test-publish)
25 #:use-module (guix scripts publish)
26 #:use-module (guix tests)
27 #:use-module (guix config)
28 #:use-module (guix utils)
29 #:use-module (gcrypt hash)
30 #:use-module (guix store)
31 #:use-module (guix derivations)
32 #:use-module (guix gexp)
33 #:use-module (guix base32)
34 #:use-module (guix base64)
35 #:use-module ((guix records) #:select (recutils->alist))
36 #:use-module ((guix serialization) #:select (restore-file))
37 #:use-module (gcrypt pk-crypto)
38 #:use-module ((guix pki) #:select (%public-key-file %private-key-file))
39 #:use-module (zlib)
40 #:use-module (lzlib)
41 #:autoload (zstd) (call-with-zstd-input-port)
42 #:use-module (web uri)
43 #:use-module (web client)
44 #:use-module (web request)
45 #:use-module (web response)
46 #:use-module ((guix http-client) #:select (http-multiple-get))
47 #:use-module (rnrs bytevectors)
48 #:use-module (ice-9 binary-ports)
49 #:use-module (srfi srfi-1)
50 #:use-module (srfi srfi-26)
51 #:use-module (srfi srfi-64)
52 #:use-module (srfi srfi-71)
53 #:use-module (ice-9 threads)
54 #:use-module (ice-9 format)
55 #:use-module (ice-9 match)
56 #:use-module (ice-9 rdelim))
57
58 (define %store
59 (open-connection-for-tests))
60
61 (define (zstd-supported?)
62 (resolve-module '(zstd) #t #f #:ensure #f))
63
64 (define %reference (add-text-to-store %store "ref" "foo"))
65
66 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
67
68 (define (http-get-body uri)
69 (call-with-values (lambda () (http-get uri))
70 (lambda (response body) body)))
71
72 (define (http-get-port uri)
73 (let ((socket (open-socket-for-uri uri)))
74 ;; Make sure to use an unbuffered port so that we can then peek at the
75 ;; underlying file descriptor via 'call-with-gzip-input-port'.
76 (setvbuf socket 'none)
77 (call-with-values
78 (lambda ()
79 (http-get uri #:port socket #:streaming? #t))
80 (lambda (response port)
81 ;; Don't (setvbuf port 'none) because of <http://bugs.gnu.org/19610>
82 ;; (PORT might be a custom binary input port).
83 port))))
84
85 (define (publish-uri route)
86 (string-append "http://localhost:6789" route))
87
88 (define-syntax-rule (with-separate-output-ports exp ...)
89 ;; Since ports aren't thread-safe in Guile 2.0, duplicate the output and
90 ;; error ports to make sure the two threads don't end up stepping on each
91 ;; other's toes.
92 (with-output-to-port (duplicate-port (current-output-port) "w")
93 (lambda ()
94 (with-error-to-port (duplicate-port (current-error-port) "w")
95 (lambda ()
96 exp ...)))))
97
98 ;; Run a local publishing server in a separate thread.
99 (with-separate-output-ports
100 (call-with-new-thread
101 (lambda ()
102 (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
103
104 (define (wait-until-ready port)
105 ;; Wait until the server is accepting connections.
106 (let ((conn (socket PF_INET SOCK_STREAM 0)))
107 (let loop ()
108 (unless (false-if-exception
109 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
110 (loop)))))
111
112 (define (wait-for-file file)
113 ;; Wait until FILE shows up.
114 (let loop ((i 20))
115 (cond ((file-exists? file)
116 #t)
117 ((zero? i)
118 (error "file didn't show up" file))
119 (else
120 (pk 'wait-for-file file)
121 (sleep 1)
122 (loop (- i 1))))))
123
124 (define %gzip-magic-bytes
125 ;; Magic bytes of gzip file.
126 #vu8(#x1f #x8b))
127
128 ;; Wait until the two servers are ready.
129 (wait-until-ready 6789)
130
131 ;; Initialize the public/private key SRFI-39 parameters.
132 (%public-key (read-file-sexp %public-key-file))
133 (%private-key (read-file-sexp %private-key-file))
134
135
136 (test-begin "publish")
137
138 (test-equal "/nix-cache-info"
139 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
140 %store-directory)
141 (http-get-body (publish-uri "/nix-cache-info")))
142
143 (test-equal "/*.narinfo"
144 (let* ((info (query-path-info %store %item))
145 (unsigned-info
146 (format #f
147 "StorePath: ~a
148 NarHash: sha256:~a
149 NarSize: ~d
150 References: ~a~%"
151 %item
152 (bytevector->nix-base32-string
153 (path-info-hash info))
154 (path-info-nar-size info)
155 (basename (first (path-info-references info)))))
156 (signature (base64-encode
157 (string->utf8
158 (canonical-sexp->string
159 (signed-string unsigned-info))))))
160 (format #f "~aSignature: 1;~a;~a
161 URL: nar/~a
162 Compression: none
163 FileSize: ~a\n"
164 unsigned-info (gethostname) signature
165 (basename %item)
166 (path-info-nar-size info)))
167 (utf8->string
168 (http-get-body
169 (publish-uri
170 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
171
172 (test-equal "/*.narinfo pipeline"
173 (make-list 500 200)
174 ;; Make sure clients can pipeline requests and correct responses, in the
175 ;; right order. See <https://issues.guix.gnu.org/54723>.
176 (let* ((uri (string->uri (publish-uri
177 (string-append "/"
178 (store-path-hash-part %item)
179 ".narinfo"))))
180 (_ expected (http-get uri #:streaming? #f #:decode-body? #f)))
181 (http-multiple-get (string->uri (publish-uri ""))
182 (lambda (request response port result)
183 (and (bytevector=? expected
184 (get-bytevector-n port
185 (response-content-length
186 response)))
187 (cons (response-code response) result)))
188 '()
189 (make-list 500 (build-request uri))
190 #:batch-size 77)))
191
192 (test-equal "/*.narinfo with properly encoded '+' sign"
193 ;; See <http://bugs.gnu.org/21888>.
194 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
195 (info (query-path-info %store item))
196 (unsigned-info
197 (format #f
198 "StorePath: ~a
199 NarHash: sha256:~a
200 NarSize: ~d
201 References: ~%"
202 item
203 (bytevector->nix-base32-string
204 (path-info-hash info))
205 (path-info-nar-size info)))
206 (signature (base64-encode
207 (string->utf8
208 (canonical-sexp->string
209 (signed-string unsigned-info))))))
210 (format #f "~aSignature: 1;~a;~a
211 URL: nar/~a
212 Compression: none
213 FileSize: ~a~%"
214 unsigned-info (gethostname) signature
215 (uri-encode (basename item))
216 (path-info-nar-size info)))
217
218 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
219 (utf8->string
220 (http-get-body
221 (publish-uri
222 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
223
224 (test-equal "/nar/*"
225 "bar"
226 (call-with-temporary-output-file
227 (lambda (temp port)
228 (let ((nar (utf8->string
229 (http-get-body
230 (publish-uri
231 (string-append "/nar/" (basename %item)))))))
232 (call-with-input-string nar (cut restore-file <> temp)))
233 (call-with-input-file temp read-string))))
234
235 (test-equal "/nar/gzip/*"
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/gzip/" (basename %item))))))
242 (call-with-gzip-input-port nar
243 (cut restore-file <> temp)))
244 (call-with-input-file temp read-string))))
245
246 (test-equal "/nar/gzip/* is really gzip"
247 %gzip-magic-bytes
248 ;; Since 'gzdopen' (aka. 'call-with-gzip-input-port') transparently reads
249 ;; uncompressed gzip, the test above doesn't check whether it's actually
250 ;; gzip. This is what this test does. See <https://bugs.gnu.org/30184>.
251 (let ((nar (http-get-port
252 (publish-uri
253 (string-append "/nar/gzip/" (basename %item))))))
254 (get-bytevector-n nar (bytevector-length %gzip-magic-bytes))))
255
256 (test-equal "/nar/lzip/*"
257 "bar"
258 (call-with-temporary-output-file
259 (lambda (temp port)
260 (let ((nar (http-get-port
261 (publish-uri
262 (string-append "/nar/lzip/" (basename %item))))))
263 (call-with-lzip-input-port nar
264 (cut restore-file <> temp)))
265 (call-with-input-file temp read-string))))
266
267 (unless (zstd-supported?) (test-skip 1))
268 (test-equal "/nar/zstd/*"
269 "bar"
270 (call-with-temporary-output-file
271 (lambda (temp port)
272 (let ((nar (http-get-port
273 (publish-uri
274 (string-append "/nar/zstd/" (basename %item))))))
275 (call-with-zstd-input-port nar
276 (cut restore-file <> temp)))
277 (call-with-input-file temp read-string))))
278
279 (test-equal "/*.narinfo with compression"
280 `(("StorePath" . ,%item)
281 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
282 ("Compression" . "gzip"))
283 (let ((thread (with-separate-output-ports
284 (call-with-new-thread
285 (lambda ()
286 (guix-publish "--port=6799" "-C5"))))))
287 (wait-until-ready 6799)
288 (let* ((url (string-append "http://localhost:6799/"
289 (store-path-hash-part %item) ".narinfo"))
290 (body (http-get-port url)))
291 (filter (lambda (item)
292 (match item
293 (("Compression" . _) #t)
294 (("StorePath" . _) #t)
295 (("URL" . _) #t)
296 (_ #f)))
297 (recutils->alist body)))))
298
299 (test-equal "/*.narinfo with lzip compression"
300 `(("StorePath" . ,%item)
301 ("URL" . ,(string-append "nar/lzip/" (basename %item)))
302 ("Compression" . "lzip"))
303 (let ((thread (with-separate-output-ports
304 (call-with-new-thread
305 (lambda ()
306 (guix-publish "--port=6790" "-Clzip"))))))
307 (wait-until-ready 6790)
308 (let* ((url (string-append "http://localhost:6790/"
309 (store-path-hash-part %item) ".narinfo"))
310 (body (http-get-port url)))
311 (filter (lambda (item)
312 (match item
313 (("Compression" . _) #t)
314 (("StorePath" . _) #t)
315 (("URL" . _) #t)
316 (_ #f)))
317 (recutils->alist body)))))
318
319 (test-equal "/*.narinfo for a compressed file"
320 '("none" "nar") ;compression-less nar
321 ;; Assume 'guix publish -C' is already running on port 6799.
322 (let* ((item (add-text-to-store %store "fake.tar.gz"
323 "This is a fake compressed file."))
324 (url (string-append "http://localhost:6799/"
325 (store-path-hash-part item) ".narinfo"))
326 (body (http-get-port url))
327 (info (recutils->alist body)))
328 (list (assoc-ref info "Compression")
329 (dirname (assoc-ref info "URL")))))
330
331 (test-equal "/*.narinfo with lzip + gzip"
332 `((("StorePath" . ,%item)
333 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
334 ("Compression" . "gzip")
335 ("URL" . ,(string-append "nar/lzip/" (basename %item)))
336 ("Compression" . "lzip"))
337 200
338 200)
339 (call-with-temporary-directory
340 (lambda (cache)
341 (let ((thread (with-separate-output-ports
342 (call-with-new-thread
343 (lambda ()
344 (guix-publish "--port=6793" "-Cgzip:2" "-Clzip:2"))))))
345 (wait-until-ready 6793)
346 (let* ((base "http://localhost:6793/")
347 (part (store-path-hash-part %item))
348 (url (string-append base part ".narinfo"))
349 (body (http-get-port url)))
350 (list (filter (match-lambda
351 (("StorePath" . _) #t)
352 (("URL" . _) #t)
353 (("Compression" . _) #t)
354 (_ #f))
355 (recutils->alist body))
356 (response-code
357 (http-get (string-append base "nar/gzip/"
358 (basename %item))))
359 (response-code
360 (http-get (string-append base "nar/lzip/"
361 (basename %item))))))))))
362
363 (test-equal "custom nar path"
364 ;; Serve nars at /foo/bar/chbouib instead of /nar.
365 (list `(("StorePath" . ,%item)
366 ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
367 ("Compression" . "none"))
368 200
369 404)
370 (let ((thread (with-separate-output-ports
371 (call-with-new-thread
372 (lambda ()
373 (guix-publish "--port=6798" "-C0"
374 "--nar-path=///foo/bar//chbouib/"))))))
375 (wait-until-ready 6798)
376 (let* ((base "http://localhost:6798/")
377 (part (store-path-hash-part %item))
378 (url (string-append base part ".narinfo"))
379 (nar-url (string-append base "foo/bar/chbouib/"
380 (basename %item)))
381 (body (http-get-port url)))
382 (list (filter (lambda (item)
383 (match item
384 (("Compression" . _) #t)
385 (("StorePath" . _) #t)
386 (("URL" . _) #t)
387 (_ #f)))
388 (recutils->alist body))
389 (response-code (http-get nar-url))
390 (response-code
391 (http-get (string-append base "nar/" (basename %item))))))))
392
393 (test-equal "/nar/ with properly encoded '+' sign"
394 "Congrats!"
395 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
396 (call-with-temporary-output-file
397 (lambda (temp port)
398 (let ((nar (utf8->string
399 (http-get-body
400 (publish-uri
401 (string-append "/nar/" (uri-encode (basename item))))))))
402 (call-with-input-string nar (cut restore-file <> temp)))
403 (call-with-input-file temp read-string)))))
404
405 (test-equal "/nar/invalid"
406 404
407 (begin
408 (call-with-output-file (string-append (%store-prefix) "/invalid")
409 (lambda (port)
410 (display "This file is not a valid store item." port)))
411 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
412
413 (unless (network-reachable?) (test-skip 1))
414 (test-equal "/file/NAME/sha256/HASH"
415 "Hello, Guix world!"
416 (let* ((data "Hello, Guix world!")
417 (hash (call-with-input-string data port-sha256))
418 (drv (run-with-store %store
419 (gexp->derivation "the-file.txt"
420 #~(call-with-output-file #$output
421 (lambda (port)
422 (display #$data port)))
423 #:hash-algo 'sha256
424 #:hash hash)))
425 (out (build-derivations %store (list drv))))
426 (utf8->string
427 (http-get-body
428 (publish-uri
429 (string-append "/file/the-file.txt/sha256/"
430 (bytevector->nix-base32-string hash)))))))
431
432 (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
433 404
434 (let ((uri (publish-uri
435 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
436 (response-code (http-get uri))))
437
438 (test-equal "/file/NAME/sha256/INVALID-HASH"
439 404
440 (let ((uri (publish-uri
441 (string-append "/file/the-file.txt/sha256/"
442 (bytevector->nix-base32-string
443 (call-with-input-string "" port-sha256))))))
444 (response-code (http-get uri))))
445
446 (test-equal "with cache"
447 (list #t
448 `(("StorePath" . ,%item)
449 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
450 ("Compression" . "gzip"))
451 200 ;nar/gzip/…
452 #t ;Content-Length
453 #t ;FileSize
454 404) ;nar/…
455 (call-with-temporary-directory
456 (lambda (cache)
457 (let ((thread (with-separate-output-ports
458 (call-with-new-thread
459 (lambda ()
460 (guix-publish "--port=6797" "-C2"
461 (string-append "--cache=" cache)
462 "--cache-bypass-threshold=0"))))))
463 (wait-until-ready 6797)
464 (let* ((base "http://localhost:6797/")
465 (part (store-path-hash-part %item))
466 (url (string-append base part ".narinfo"))
467 (nar-url (string-append base "nar/gzip/" (basename %item)))
468 (cached (string-append cache "/gzip/" (basename %item)
469 ".narinfo"))
470 (nar (string-append cache "/gzip/"
471 (basename %item) ".nar"))
472 (response (http-get url)))
473 (and (= 404 (response-code response))
474
475 ;; We should get an explicitly short TTL for 404 in this case
476 ;; because it's going to become 200 shortly.
477 (match (assq-ref (response-headers response) 'cache-control)
478 ((('max-age . ttl))
479 (< ttl 3600)))
480
481 (wait-for-file cached)
482
483 ;; Both the narinfo and nar should be world-readable.
484 (= #o444 (logand #o444 (stat:perms (lstat cached))))
485 (= #o444 (logand #o444 (stat:perms (lstat nar))))
486
487 (let* ((body (http-get-port url))
488 (compressed (http-get nar-url))
489 (uncompressed (http-get (string-append base "nar/"
490 (basename %item))))
491 (narinfo (recutils->alist body)))
492 (list (file-exists? nar)
493 (filter (lambda (item)
494 (match item
495 (("Compression" . _) #t)
496 (("StorePath" . _) #t)
497 (("URL" . _) #t)
498 (_ #f)))
499 narinfo)
500 (response-code compressed)
501 (= (response-content-length compressed)
502 (stat:size (stat nar)))
503 (= (string->number
504 (assoc-ref narinfo "FileSize"))
505 (stat:size (stat nar)))
506 (response-code uncompressed)))))))))
507
508 (test-equal "with cache, lzip + gzip"
509 '(200 200 404)
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=6794" "-Cgzip:2" "-Clzip:2"
516 (string-append "--cache=" cache)
517 "--cache-bypass-threshold=0"))))))
518 (wait-until-ready 6794)
519 (let* ((base "http://localhost:6794/")
520 (part (store-path-hash-part %item))
521 (url (string-append base part ".narinfo"))
522 (nar-url (cute string-append "nar/" <> "/"
523 (basename %item)))
524 (cached (cute string-append cache "/" <> "/"
525 (basename %item) ".narinfo"))
526 (nar (cute string-append cache "/" <> "/"
527 (basename %item) ".nar"))
528 (response (http-get url)))
529 (wait-for-file (cached "gzip"))
530 (let* ((body (http-get-port url))
531 (narinfo (recutils->alist body))
532 (uncompressed (string-append base "nar/"
533 (basename %item))))
534 (and (file-exists? (nar "gzip"))
535 (file-exists? (nar "lzip"))
536 (match (pk 'narinfo/gzip+lzip narinfo)
537 ((("StorePath" . path)
538 _ ...
539 ("Signature" . _)
540 ("URL" . gzip-url)
541 ("Compression" . "gzip")
542 ("FileSize" . (= string->number gzip-size))
543 ("URL" . lzip-url)
544 ("Compression" . "lzip")
545 ("FileSize" . (= string->number lzip-size)))
546 (and (string=? gzip-url (nar-url "gzip"))
547 (string=? lzip-url (nar-url "lzip"))
548 (= gzip-size
549 (stat:size (stat (nar "gzip"))))
550 (= lzip-size
551 (stat:size (stat (nar "lzip")))))))
552 (list (response-code
553 (http-get (string-append base (nar-url "gzip"))))
554 (response-code
555 (http-get (string-append base (nar-url "lzip"))))
556 (response-code
557 (http-get uncompressed))))))))))
558
559 (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
560 (random-text))))
561 (test-equal "with cache, uncompressed"
562 (list #t
563 (* 42 3600) ;TTL on narinfo
564 `(("StorePath" . ,item)
565 ("URL" . ,(string-append "nar/" (basename item)))
566 ("Compression" . "none"))
567 200 ;nar/…
568 (* 42 3600) ;TTL on nar/…
569 (path-info-nar-size
570 (query-path-info %store item)) ;FileSize
571 404) ;nar/gzip/…
572 (call-with-temporary-directory
573 (lambda (cache)
574 (let ((thread (with-separate-output-ports
575 (call-with-new-thread
576 (lambda ()
577 (guix-publish "--port=6796" "-C2" "--ttl=42h"
578 (string-append "--cache=" cache)
579 "--cache-bypass-threshold=0"))))))
580 (wait-until-ready 6796)
581 (let* ((base "http://localhost:6796/")
582 (part (store-path-hash-part item))
583 (url (string-append base part ".narinfo"))
584 (cached (string-append cache "/none/"
585 (basename item) ".narinfo"))
586 (nar (string-append cache "/none/"
587 (basename item) ".nar"))
588 (response (http-get url)))
589 (and (= 404 (response-code response))
590
591 (wait-for-file cached)
592 (let* ((response (http-get url))
593 (body (http-get-port url))
594 (compressed (http-get (string-append base "nar/gzip/"
595 (basename item))))
596 (uncompressed (http-get (string-append base "nar/"
597 (basename item))))
598 (narinfo (recutils->alist body)))
599 (list (file-exists? nar)
600 (match (assq-ref (response-headers response)
601 'cache-control)
602 ((('max-age . ttl)) ttl)
603 (_ #f))
604
605 (filter (lambda (item)
606 (match item
607 (("Compression" . _) #t)
608 (("StorePath" . _) #t)
609 (("URL" . _) #t)
610 (_ #f)))
611 narinfo)
612 (response-code uncompressed)
613 (match (assq-ref (response-headers uncompressed)
614 'cache-control)
615 ((('max-age . ttl)) ttl)
616 (_ #f))
617
618 (string->number
619 (assoc-ref narinfo "FileSize"))
620 (response-code compressed))))))))))
621
622 (test-equal "with cache, vanishing item" ;<https://bugs.gnu.org/33897>
623 200
624 (call-with-temporary-directory
625 (lambda (cache)
626 (let ((thread (with-separate-output-ports
627 (call-with-new-thread
628 (lambda ()
629 (guix-publish "--port=6795"
630 (string-append "--cache=" cache)))))))
631 (wait-until-ready 6795)
632
633 ;; Make sure that, even if ITEM disappears, we're still able to fetch
634 ;; it.
635 (let* ((base "http://localhost:6795/")
636 (item (add-text-to-store %store "random" (random-text)))
637 (part (store-path-hash-part item))
638 (url (string-append base part ".narinfo"))
639 (cached (string-append cache "/gzip/"
640 (basename item)
641 ".narinfo"))
642 (response (http-get url)))
643 (and (= 200 (response-code response)) ;we're below the threshold
644 (wait-for-file cached)
645 (begin
646 (delete-paths %store (list item))
647 (response-code (pk 'response (http-get url))))))))))
648
649 (test-equal "with cache, cache bypass"
650 200
651 (call-with-temporary-directory
652 (lambda (cache)
653 (let ((thread (with-separate-output-ports
654 (call-with-new-thread
655 (lambda ()
656 (guix-publish "--port=6788" "-C" "gzip"
657 (string-append "--cache=" cache)))))))
658 (wait-until-ready 6788)
659
660 (let* ((base "http://localhost:6788/")
661 (item (add-text-to-store %store "random" (random-text)))
662 (part (store-path-hash-part item))
663 (narinfo (string-append base part ".narinfo"))
664 (nar (string-append base "nar/gzip/" (basename item)))
665 (cached (string-append cache "/gzip/" (basename item)
666 ".narinfo")))
667 ;; We're below the default cache bypass threshold, so NAR and NARINFO
668 ;; should immediately return 200. The NARINFO request should trigger
669 ;; caching, and the next request to NAR should return 200 as well.
670 (and (let ((response (pk 'r1 (http-get nar))))
671 (and (= 200 (response-code response))
672 (not (response-content-length response)))) ;not known
673 (= 200 (response-code (http-get narinfo)))
674 (begin
675 (wait-for-file cached)
676 (let ((response (pk 'r2 (http-get nar))))
677 (and (> (response-content-length response)
678 (stat:size (stat item)))
679 (response-code response))))))))))
680
681 (test-equal "with cache, cache bypass, unmapped hash part"
682 200
683
684 ;; This test reproduces the bug described in <https://bugs.gnu.org/44442>:
685 ;; the daemon connection would be closed as a side effect of a nar request
686 ;; for a non-existing file name.
687 (call-with-temporary-directory
688 (lambda (cache)
689 (let ((thread (with-separate-output-ports
690 (call-with-new-thread
691 (lambda ()
692 (guix-publish "--port=6787" "-C" "gzip"
693 (string-append "--cache=" cache)))))))
694 (wait-until-ready 6787)
695
696 (let* ((base "http://localhost:6787/")
697 (item (add-text-to-store %store "random" (random-text)))
698 (part (store-path-hash-part item))
699 (narinfo (string-append base part ".narinfo"))
700 (nar (string-append base "nar/gzip/" (basename item)))
701 (cached (string-append cache "/gzip/" (basename item)
702 ".narinfo")))
703 ;; The first response used to be 500 and to terminate the daemon
704 ;; connection as a side effect.
705 (and (= (response-code
706 (http-get (string-append base "nar/gzip/"
707 (make-string 32 #\e)
708 "-does-not-exist")))
709 404)
710 (= 200 (response-code (http-get nar)))
711 (= 200 (response-code (http-get narinfo)))
712 (begin
713 (wait-for-file cached)
714 (response-code (http-get nar)))))))))
715
716 ;; Disable, further investigation needed.
717 (unless (network-reachable?) (test-skip 1))
718 (test-equal "/log/NAME"
719 `(200 #t text/plain (gzip))
720 (let ((drv (run-with-store %store
721 (gexp->derivation "with-log"
722 #~(call-with-output-file #$output
723 (lambda (port)
724 (display "Hello, build log!"
725 (current-error-port))
726 (display #$(random-text) port)))))))
727 (build-derivations %store (list drv))
728 (let* ((response (http-get
729 (publish-uri (string-append "/log/"
730 (basename (derivation->output-path drv))))
731 #:decode-body? #f))
732 (base (basename (derivation-file-name drv)))
733 (log (string-append (dirname %state-directory)
734 "/log/guix/drvs/" (string-take base 2)
735 "/" (string-drop base 2) ".gz")))
736 (list (response-code response)
737 (= (response-content-length response) (stat:size (stat log)))
738 (first (response-content-type response))
739 (response-content-encoding response)))))
740
741 (test-equal "negative TTL"
742 `(404 42)
743
744 (call-with-temporary-directory
745 (lambda (cache)
746 (let ((thread (with-separate-output-ports
747 (call-with-new-thread
748 (lambda ()
749 (guix-publish "--port=6786" "-C0"
750 "--negative-ttl=42s"))))))
751 (wait-until-ready 6786)
752
753 (let* ((base "http://localhost:6786/")
754 (url (string-append base (make-string 32 #\z)
755 ".narinfo"))
756 (response (http-get url)))
757 (list (response-code response)
758 (match (assq-ref (response-headers response) 'cache-control)
759 ((('max-age . ttl)) ttl)
760 (_ #f))))))))
761
762 (test-equal "no negative TTL"
763 `(404 #f)
764 (let* ((uri (publish-uri
765 (string-append "/" (make-string 32 #\z)
766 ".narinfo")))
767 (response (http-get uri)))
768 (list (response-code response)
769 (assq-ref (response-headers response) 'cache-control))))
770
771 (test-equal "/log/NAME not found"
772 404
773 (let ((uri (publish-uri "/log/does-not-exist")))
774 (response-code (http-get uri))))
775
776 (test-equal "/signing-key.pub"
777 200
778 (response-code (http-get (publish-uri "/signing-key.pub"))))
779
780 (test-equal "non-GET query"
781 '(200 404)
782 (let ((path (string-append "/" (store-path-hash-part %item)
783 ".narinfo")))
784 (map response-code
785 (list (http-get (publish-uri path))
786 (http-post (publish-uri path))))))
787
788 (test-end "publish")