publish.scm 32 KB

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