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