publish.scm 25 KB

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