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>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 ;; Avoid interference.
21 (unsetenv "http_proxy")
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))
52 (open-connection-for-tests))
54 (define %reference (add-text-to-store %store "ref" "foo"))
56 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
58 (define (http-get-body uri)
59 (call-with-values (lambda () (http-get uri))
60 (lambda (response body) body)))
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)
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).
75 (define (publish-uri route)
76 (string-append "http://localhost:6789" route))
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
82 (with-output-to-port (duplicate-port (current-output-port) "w")
84 (with-error-to-port (duplicate-port (current-error-port) "w")
88 ;; Run a local publishing server in a separate thread.
89 (with-separate-output-ports
92 (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
94 (define (wait-until-ready port)
95 ;; Wait until the server is accepting connections.
96 (let ((conn (socket PF_INET SOCK_STREAM 0)))
98 (unless (false-if-exception
99 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
102 (define (wait-for-file file)
103 ;; Wait until FILE shows up.
105 (cond ((file-exists? file)
108 (error "file didn't show up" file))
110 (pk 'wait-for-file file)
114 (define %gzip-magic-bytes
115 ;; Magic bytes of gzip file.
118 ;; Wait until the two servers are ready.
119 (wait-until-ready 6789)
121 ;; Initialize the public/private key SRFI-39 parameters.
122 (%public-key (read-file-sexp %public-key-file))
123 (%private-key (read-file-sexp %private-key-file))
126 (test-begin "publish")
128 (test-equal "/nix-cache-info"
129 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
131 (http-get-body (publish-uri "/nix-cache-info")))
133 (test-equal "/*.narinfo"
134 (let* ((info (query-path-info %store %item))
146 (bytevector->nix-base32-string
147 (path-info-hash info))
148 (path-info-nar-size info)
149 (basename (first (path-info-references info)))
150 (path-info-nar-size info)))
151 (signature (base64-encode
153 (canonical-sexp->string
154 ((@@ (guix scripts publish) signed-string)
156 (format #f "~aSignature: 1;~a;~a~%"
157 unsigned-info (gethostname) signature))
161 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
163 (test-equal "/*.narinfo with properly encoded '+' sign"
164 ;; See <http://bugs.gnu.org/21888>.
165 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
166 (info (query-path-info %store item))
177 (uri-encode (basename item))
178 (bytevector->nix-base32-string
179 (path-info-hash info))
180 (path-info-nar-size info)
181 (path-info-nar-size info)))
182 (signature (base64-encode
184 (canonical-sexp->string
185 ((@@ (guix scripts publish) signed-string)
187 (format #f "~aSignature: 1;~a;~a~%"
188 unsigned-info (gethostname) signature))
190 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
194 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
198 (call-with-temporary-output-file
200 (let ((nar (utf8->string
203 (string-append "/nar/" (basename %item)))))))
204 (call-with-input-string nar (cut restore-file <> temp)))
205 (call-with-input-file temp read-string))))
207 (unless (zlib-available?)
209 (test-equal "/nar/gzip/*"
211 (call-with-temporary-output-file
213 (let ((nar (http-get-port
215 (string-append "/nar/gzip/" (basename %item))))))
216 (call-with-gzip-input-port nar
217 (cut restore-file <> temp)))
218 (call-with-input-file temp read-string))))
220 (unless (zlib-available?)
222 (test-equal "/nar/gzip/* is really gzip"
224 ;; Since 'gzdopen' (aka. 'call-with-gzip-input-port') transparently reads
225 ;; uncompressed gzip, the test above doesn't check whether it's actually
226 ;; gzip. This is what this test does. See <https://bugs.gnu.org/30184>.
227 (let ((nar (http-get-port
229 (string-append "/nar/gzip/" (basename %item))))))
230 (get-bytevector-n nar (bytevector-length %gzip-magic-bytes))))
232 (unless (zlib-available?)
234 (test-equal "/*.narinfo with compression"
235 `(("StorePath" . ,%item)
236 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
237 ("Compression" . "gzip"))
238 (let ((thread (with-separate-output-ports
239 (call-with-new-thread
241 (guix-publish "--port=6799" "-C5"))))))
242 (wait-until-ready 6799)
243 (let* ((url (string-append "http://localhost:6799/"
244 (store-path-hash-part %item) ".narinfo"))
245 (body (http-get-port url)))
246 (filter (lambda (item)
248 (("Compression" . _) #t)
249 (("StorePath" . _) #t)
252 (recutils->alist body)))))
254 (unless (zlib-available?)
256 (test-equal "/*.narinfo for a compressed file"
257 '("none" "nar") ;compression-less nar
258 ;; Assume 'guix publish -C' is already running on port 6799.
259 (let* ((item (add-text-to-store %store "fake.tar.gz"
260 "This is a fake compressed file."))
261 (url (string-append "http://localhost:6799/"
262 (store-path-hash-part item) ".narinfo"))
263 (body (http-get-port url))
264 (info (recutils->alist body)))
265 (list (assoc-ref info "Compression")
266 (dirname (assoc-ref info "URL")))))
268 (test-equal "custom nar path"
269 ;; Serve nars at /foo/bar/chbouib instead of /nar.
270 (list `(("StorePath" . ,%item)
271 ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
272 ("Compression" . "none"))
275 (let ((thread (with-separate-output-ports
276 (call-with-new-thread
278 (guix-publish "--port=6798" "-C0"
279 "--nar-path=///foo/bar//chbouib/"))))))
280 (wait-until-ready 6798)
281 (let* ((base "http://localhost:6798/")
282 (part (store-path-hash-part %item))
283 (url (string-append base part ".narinfo"))
284 (nar-url (string-append base "foo/bar/chbouib/"
286 (body (http-get-port url)))
287 (list (filter (lambda (item)
289 (("Compression" . _) #t)
290 (("StorePath" . _) #t)
293 (recutils->alist body))
294 (response-code (http-get nar-url))
296 (http-get (string-append base "nar/" (basename %item))))))))
298 (test-equal "/nar/ with properly encoded '+' sign"
300 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
301 (call-with-temporary-output-file
303 (let ((nar (utf8->string
306 (string-append "/nar/" (uri-encode (basename item))))))))
307 (call-with-input-string nar (cut restore-file <> temp)))
308 (call-with-input-file temp read-string)))))
310 (test-equal "/nar/invalid"
313 (call-with-output-file (string-append (%store-prefix) "/invalid")
315 (display "This file is not a valid store item." port)))
316 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
318 (test-equal "/file/NAME/sha256/HASH"
320 (let* ((data "Hello, Guix world!")
321 (hash (call-with-input-string data port-sha256))
322 (drv (run-with-store %store
323 (gexp->derivation "the-file.txt"
324 #~(call-with-output-file #$output
326 (display #$data port)))
329 (out (build-derivations %store (list drv))))
333 (string-append "/file/the-file.txt/sha256/"
334 (bytevector->nix-base32-string hash)))))))
336 (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
338 (let ((uri (publish-uri
339 "/file/the-file.txt/sha256/not-a-nix-base32-string")))
340 (response-code (http-get uri))))
342 (test-equal "/file/NAME/sha256/INVALID-HASH"
344 (let ((uri (publish-uri
345 (string-append "/file/the-file.txt/sha256/"
346 (bytevector->nix-base32-string
347 (call-with-input-string "" port-sha256))))))
348 (response-code (http-get uri))))
350 (unless (zlib-available?)
352 (test-equal "with cache"
354 `(("StorePath" . ,%item)
355 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
356 ("Compression" . "gzip"))
361 (call-with-temporary-directory
363 (let ((thread (with-separate-output-ports
364 (call-with-new-thread
366 (guix-publish "--port=6797" "-C2"
367 (string-append "--cache=" cache)))))))
368 (wait-until-ready 6797)
369 (let* ((base "http://localhost:6797/")
370 (part (store-path-hash-part %item))
371 (url (string-append base part ".narinfo"))
372 (nar-url (string-append base "nar/gzip/" (basename %item)))
373 (cached (string-append cache "/gzip/" (basename %item)
375 (nar (string-append cache "/gzip/"
376 (basename %item) ".nar"))
377 (response (http-get url)))
378 (and (= 404 (response-code response))
380 ;; We should get an explicitly short TTL for 404 in this case
381 ;; because it's going to become 200 shortly.
382 (match (assq-ref (response-headers response) 'cache-control)
386 (wait-for-file cached)
387 (let* ((body (http-get-port url))
388 (compressed (http-get nar-url))
389 (uncompressed (http-get (string-append base "nar/"
391 (narinfo (recutils->alist body)))
392 (list (file-exists? nar)
393 (filter (lambda (item)
395 (("Compression" . _) #t)
396 (("StorePath" . _) #t)
400 (response-code compressed)
401 (= (response-content-length compressed)
402 (stat:size (stat nar)))
404 (assoc-ref narinfo "FileSize"))
405 (stat:size (stat nar)))
406 (response-code uncompressed)))))))))
408 (unless (zlib-available?)
410 (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
412 (test-equal "with cache, uncompressed"
414 `(("StorePath" . ,item)
415 ("URL" . ,(string-append "nar/" (basename item)))
416 ("Compression" . "none"))
419 (query-path-info %store item)) ;FileSize
421 (call-with-temporary-directory
423 (let ((thread (with-separate-output-ports
424 (call-with-new-thread
426 (guix-publish "--port=6796" "-C2"
427 (string-append "--cache=" cache)))))))
428 (wait-until-ready 6796)
429 (let* ((base "http://localhost:6796/")
430 (part (store-path-hash-part item))
431 (url (string-append base part ".narinfo"))
432 (cached (string-append cache "/none/"
433 (basename item) ".narinfo"))
434 (nar (string-append cache "/none/"
435 (basename item) ".nar"))
436 (response (http-get url)))
437 (and (= 404 (response-code response))
439 (wait-for-file cached)
440 (let* ((body (http-get-port url))
441 (compressed (http-get (string-append base "nar/gzip/"
443 (uncompressed (http-get (string-append base "nar/"
445 (narinfo (recutils->alist body)))
446 (list (file-exists? nar)
447 (filter (lambda (item)
449 (("Compression" . _) #t)
450 (("StorePath" . _) #t)
454 (response-code uncompressed)
456 (assoc-ref narinfo "FileSize"))
457 (response-code compressed))))))))))
459 (test-equal "/log/NAME"
460 `(200 #t application/x-bzip2)
461 (let ((drv (run-with-store %store
462 (gexp->derivation "with-log"
463 #~(call-with-output-file #$output
465 (display "Hello, build log!"
466 (current-error-port))
467 (display "" port)))))))
468 (build-derivations %store (list drv))
469 (let* ((response (http-get
470 (publish-uri (string-append "/log/"
471 (basename (derivation->output-path drv))))
473 (base (basename (derivation-file-name drv)))
474 (log (string-append (dirname %state-directory)
475 "/log/guix/drvs/" (string-take base 2)
476 "/" (string-drop base 2) ".bz2")))
477 (list (response-code response)
478 (= (response-content-length response) (stat:size (stat log)))
479 (first (response-content-type response))))))
481 (test-equal "/log/NAME not found"
483 (let ((uri (publish-uri "/log/does-not-exist")))
484 (response-code (http-get uri))))