publish.scm 30 KB

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