publish.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2018, 2019 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 (guix zlib)
  37. #:use-module (web uri)
  38. #:use-module (web client)
  39. #:use-module (web response)
  40. #:use-module (rnrs bytevectors)
  41. #:use-module (ice-9 binary-ports)
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-26)
  44. #:use-module (srfi srfi-64)
  45. #:use-module (ice-9 format)
  46. #:use-module (ice-9 match)
  47. #:use-module (ice-9 rdelim))
  48. (define %store
  49. (open-connection-for-tests))
  50. (define %reference (add-text-to-store %store "ref" "foo"))
  51. (define %item (add-text-to-store %store "item" "bar" (list %reference)))
  52. (define (http-get-body uri)
  53. (call-with-values (lambda () (http-get uri))
  54. (lambda (response body) body)))
  55. (define (http-get-port uri)
  56. (let ((socket (open-socket-for-uri uri)))
  57. ;; Make sure to use an unbuffered port so that we can then peek at the
  58. ;; underlying file descriptor via 'call-with-gzip-input-port'.
  59. (setvbuf socket 'none)
  60. (call-with-values
  61. (lambda ()
  62. (http-get uri #:port socket #:streaming? #t))
  63. (lambda (response port)
  64. ;; Don't (setvbuf port 'none) because of <http://bugs.gnu.org/19610>
  65. ;; (PORT might be a custom binary input port).
  66. port))))
  67. (define (publish-uri route)
  68. (string-append "http://localhost:6789" route))
  69. (define-syntax-rule (with-separate-output-ports exp ...)
  70. ;; Since ports aren't thread-safe in Guile 2.0, duplicate the output and
  71. ;; error ports to make sure the two threads don't end up stepping on each
  72. ;; other's toes.
  73. (with-output-to-port (duplicate-port (current-output-port) "w")
  74. (lambda ()
  75. (with-error-to-port (duplicate-port (current-error-port) "w")
  76. (lambda ()
  77. exp ...)))))
  78. ;; Run a local publishing server in a separate thread.
  79. (with-separate-output-ports
  80. (call-with-new-thread
  81. (lambda ()
  82. (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
  83. (define (wait-until-ready port)
  84. ;; Wait until the server is accepting connections.
  85. (let ((conn (socket PF_INET SOCK_STREAM 0)))
  86. (let loop ()
  87. (unless (false-if-exception
  88. (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
  89. (loop)))))
  90. (define (wait-for-file file)
  91. ;; Wait until FILE shows up.
  92. (let loop ((i 20))
  93. (cond ((file-exists? file)
  94. #t)
  95. ((zero? i)
  96. (error "file didn't show up" file))
  97. (else
  98. (pk 'wait-for-file file)
  99. (sleep 1)
  100. (loop (- i 1))))))
  101. (define %gzip-magic-bytes
  102. ;; Magic bytes of gzip file.
  103. #vu8(#x1f #x8b))
  104. ;; Wait until the two servers are ready.
  105. (wait-until-ready 6789)
  106. ;; Initialize the public/private key SRFI-39 parameters.
  107. (%public-key (read-file-sexp %public-key-file))
  108. (%private-key (read-file-sexp %private-key-file))
  109. (test-begin "publish")
  110. (test-equal "/nix-cache-info"
  111. (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
  112. %store-directory)
  113. (http-get-body (publish-uri "/nix-cache-info")))
  114. (test-equal "/*.narinfo"
  115. (let* ((info (query-path-info %store %item))
  116. (unsigned-info
  117. (format #f
  118. "StorePath: ~a
  119. URL: nar/~a
  120. Compression: none
  121. NarHash: sha256:~a
  122. NarSize: ~d
  123. References: ~a
  124. FileSize: ~a~%"
  125. %item
  126. (basename %item)
  127. (bytevector->nix-base32-string
  128. (path-info-hash info))
  129. (path-info-nar-size info)
  130. (basename (first (path-info-references info)))
  131. (path-info-nar-size info)))
  132. (signature (base64-encode
  133. (string->utf8
  134. (canonical-sexp->string
  135. ((@@ (guix scripts publish) signed-string)
  136. unsigned-info))))))
  137. (format #f "~aSignature: 1;~a;~a~%"
  138. unsigned-info (gethostname) signature))
  139. (utf8->string
  140. (http-get-body
  141. (publish-uri
  142. (string-append "/" (store-path-hash-part %item) ".narinfo")))))
  143. (test-equal "/*.narinfo with properly encoded '+' sign"
  144. ;; See <http://bugs.gnu.org/21888>.
  145. (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
  146. (info (query-path-info %store item))
  147. (unsigned-info
  148. (format #f
  149. "StorePath: ~a
  150. URL: nar/~a
  151. Compression: none
  152. NarHash: sha256:~a
  153. NarSize: ~d
  154. References: ~%\
  155. FileSize: ~a~%"
  156. item
  157. (uri-encode (basename item))
  158. (bytevector->nix-base32-string
  159. (path-info-hash info))
  160. (path-info-nar-size info)
  161. (path-info-nar-size info)))
  162. (signature (base64-encode
  163. (string->utf8
  164. (canonical-sexp->string
  165. ((@@ (guix scripts publish) signed-string)
  166. 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. (unless (zlib-available?)
  185. (test-skip 1))
  186. (test-equal "/nar/gzip/*"
  187. "bar"
  188. (call-with-temporary-output-file
  189. (lambda (temp port)
  190. (let ((nar (http-get-port
  191. (publish-uri
  192. (string-append "/nar/gzip/" (basename %item))))))
  193. (call-with-gzip-input-port nar
  194. (cut restore-file <> temp)))
  195. (call-with-input-file temp read-string))))
  196. (unless (zlib-available?)
  197. (test-skip 1))
  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. (unless (zlib-available?)
  208. (test-skip 1))
  209. (test-equal "/*.narinfo with compression"
  210. `(("StorePath" . ,%item)
  211. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  212. ("Compression" . "gzip"))
  213. (let ((thread (with-separate-output-ports
  214. (call-with-new-thread
  215. (lambda ()
  216. (guix-publish "--port=6799" "-C5"))))))
  217. (wait-until-ready 6799)
  218. (let* ((url (string-append "http://localhost:6799/"
  219. (store-path-hash-part %item) ".narinfo"))
  220. (body (http-get-port url)))
  221. (filter (lambda (item)
  222. (match item
  223. (("Compression" . _) #t)
  224. (("StorePath" . _) #t)
  225. (("URL" . _) #t)
  226. (_ #f)))
  227. (recutils->alist body)))))
  228. (unless (zlib-available?)
  229. (test-skip 1))
  230. (test-equal "/*.narinfo for a compressed file"
  231. '("none" "nar") ;compression-less nar
  232. ;; Assume 'guix publish -C' is already running on port 6799.
  233. (let* ((item (add-text-to-store %store "fake.tar.gz"
  234. "This is a fake compressed file."))
  235. (url (string-append "http://localhost:6799/"
  236. (store-path-hash-part item) ".narinfo"))
  237. (body (http-get-port url))
  238. (info (recutils->alist body)))
  239. (list (assoc-ref info "Compression")
  240. (dirname (assoc-ref info "URL")))))
  241. (test-equal "custom nar path"
  242. ;; Serve nars at /foo/bar/chbouib instead of /nar.
  243. (list `(("StorePath" . ,%item)
  244. ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
  245. ("Compression" . "none"))
  246. 200
  247. 404)
  248. (let ((thread (with-separate-output-ports
  249. (call-with-new-thread
  250. (lambda ()
  251. (guix-publish "--port=6798" "-C0"
  252. "--nar-path=///foo/bar//chbouib/"))))))
  253. (wait-until-ready 6798)
  254. (let* ((base "http://localhost:6798/")
  255. (part (store-path-hash-part %item))
  256. (url (string-append base part ".narinfo"))
  257. (nar-url (string-append base "foo/bar/chbouib/"
  258. (basename %item)))
  259. (body (http-get-port url)))
  260. (list (filter (lambda (item)
  261. (match item
  262. (("Compression" . _) #t)
  263. (("StorePath" . _) #t)
  264. (("URL" . _) #t)
  265. (_ #f)))
  266. (recutils->alist body))
  267. (response-code (http-get nar-url))
  268. (response-code
  269. (http-get (string-append base "nar/" (basename %item))))))))
  270. (test-equal "/nar/ with properly encoded '+' sign"
  271. "Congrats!"
  272. (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
  273. (call-with-temporary-output-file
  274. (lambda (temp port)
  275. (let ((nar (utf8->string
  276. (http-get-body
  277. (publish-uri
  278. (string-append "/nar/" (uri-encode (basename item))))))))
  279. (call-with-input-string nar (cut restore-file <> temp)))
  280. (call-with-input-file temp read-string)))))
  281. (test-equal "/nar/invalid"
  282. 404
  283. (begin
  284. (call-with-output-file (string-append (%store-prefix) "/invalid")
  285. (lambda (port)
  286. (display "This file is not a valid store item." port)))
  287. (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
  288. (test-equal "/file/NAME/sha256/HASH"
  289. "Hello, Guix world!"
  290. (let* ((data "Hello, Guix world!")
  291. (hash (call-with-input-string data port-sha256))
  292. (drv (run-with-store %store
  293. (gexp->derivation "the-file.txt"
  294. #~(call-with-output-file #$output
  295. (lambda (port)
  296. (display #$data port)))
  297. #:hash-algo 'sha256
  298. #:hash hash)))
  299. (out (build-derivations %store (list drv))))
  300. (utf8->string
  301. (http-get-body
  302. (publish-uri
  303. (string-append "/file/the-file.txt/sha256/"
  304. (bytevector->nix-base32-string hash)))))))
  305. (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
  306. 404
  307. (let ((uri (publish-uri
  308. "/file/the-file.txt/sha256/not-a-nix-base32-string")))
  309. (response-code (http-get uri))))
  310. (test-equal "/file/NAME/sha256/INVALID-HASH"
  311. 404
  312. (let ((uri (publish-uri
  313. (string-append "/file/the-file.txt/sha256/"
  314. (bytevector->nix-base32-string
  315. (call-with-input-string "" port-sha256))))))
  316. (response-code (http-get uri))))
  317. (unless (zlib-available?)
  318. (test-skip 1))
  319. (test-equal "with cache"
  320. (list #t
  321. `(("StorePath" . ,%item)
  322. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  323. ("Compression" . "gzip"))
  324. 200 ;nar/gzip/…
  325. #t ;Content-Length
  326. #t ;FileSize
  327. 404) ;nar/…
  328. (call-with-temporary-directory
  329. (lambda (cache)
  330. (let ((thread (with-separate-output-ports
  331. (call-with-new-thread
  332. (lambda ()
  333. (guix-publish "--port=6797" "-C2"
  334. (string-append "--cache=" cache)))))))
  335. (wait-until-ready 6797)
  336. (let* ((base "http://localhost:6797/")
  337. (part (store-path-hash-part %item))
  338. (url (string-append base part ".narinfo"))
  339. (nar-url (string-append base "nar/gzip/" (basename %item)))
  340. (cached (string-append cache "/gzip/" (basename %item)
  341. ".narinfo"))
  342. (nar (string-append cache "/gzip/"
  343. (basename %item) ".nar"))
  344. (response (http-get url)))
  345. (and (= 404 (response-code response))
  346. ;; We should get an explicitly short TTL for 404 in this case
  347. ;; because it's going to become 200 shortly.
  348. (match (assq-ref (response-headers response) 'cache-control)
  349. ((('max-age . ttl))
  350. (< ttl 3600)))
  351. (wait-for-file cached)
  352. (let* ((body (http-get-port url))
  353. (compressed (http-get nar-url))
  354. (uncompressed (http-get (string-append base "nar/"
  355. (basename %item))))
  356. (narinfo (recutils->alist body)))
  357. (list (file-exists? nar)
  358. (filter (lambda (item)
  359. (match item
  360. (("Compression" . _) #t)
  361. (("StorePath" . _) #t)
  362. (("URL" . _) #t)
  363. (_ #f)))
  364. narinfo)
  365. (response-code compressed)
  366. (= (response-content-length compressed)
  367. (stat:size (stat nar)))
  368. (= (string->number
  369. (assoc-ref narinfo "FileSize"))
  370. (stat:size (stat nar)))
  371. (response-code uncompressed)))))))))
  372. (unless (zlib-available?)
  373. (test-skip 1))
  374. (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
  375. (random-text))))
  376. (test-equal "with cache, uncompressed"
  377. (list #t
  378. (* 42 3600) ;TTL on narinfo
  379. `(("StorePath" . ,item)
  380. ("URL" . ,(string-append "nar/" (basename item)))
  381. ("Compression" . "none"))
  382. 200 ;nar/…
  383. (* 42 3600) ;TTL on nar/…
  384. (path-info-nar-size
  385. (query-path-info %store item)) ;FileSize
  386. 404) ;nar/gzip/…
  387. (call-with-temporary-directory
  388. (lambda (cache)
  389. (let ((thread (with-separate-output-ports
  390. (call-with-new-thread
  391. (lambda ()
  392. (guix-publish "--port=6796" "-C2" "--ttl=42h"
  393. (string-append "--cache=" cache)))))))
  394. (wait-until-ready 6796)
  395. (let* ((base "http://localhost:6796/")
  396. (part (store-path-hash-part item))
  397. (url (string-append base part ".narinfo"))
  398. (cached (string-append cache "/none/"
  399. (basename item) ".narinfo"))
  400. (nar (string-append cache "/none/"
  401. (basename item) ".nar"))
  402. (response (http-get url)))
  403. (and (= 404 (response-code response))
  404. (wait-for-file cached)
  405. (let* ((response (http-get url))
  406. (body (http-get-port url))
  407. (compressed (http-get (string-append base "nar/gzip/"
  408. (basename item))))
  409. (uncompressed (http-get (string-append base "nar/"
  410. (basename item))))
  411. (narinfo (recutils->alist body)))
  412. (list (file-exists? nar)
  413. (match (assq-ref (response-headers response)
  414. 'cache-control)
  415. ((('max-age . ttl)) ttl)
  416. (_ #f))
  417. (filter (lambda (item)
  418. (match item
  419. (("Compression" . _) #t)
  420. (("StorePath" . _) #t)
  421. (("URL" . _) #t)
  422. (_ #f)))
  423. narinfo)
  424. (response-code uncompressed)
  425. (match (assq-ref (response-headers uncompressed)
  426. 'cache-control)
  427. ((('max-age . ttl)) ttl)
  428. (_ #f))
  429. (string->number
  430. (assoc-ref narinfo "FileSize"))
  431. (response-code compressed))))))))))
  432. (test-equal "/log/NAME"
  433. `(200 #t application/x-bzip2)
  434. (let ((drv (run-with-store %store
  435. (gexp->derivation "with-log"
  436. #~(call-with-output-file #$output
  437. (lambda (port)
  438. (display "Hello, build log!"
  439. (current-error-port))
  440. (display #$(random-text) port)))))))
  441. (build-derivations %store (list drv))
  442. (let* ((response (http-get
  443. (publish-uri (string-append "/log/"
  444. (basename (derivation->output-path drv))))
  445. #:decode-body? #f))
  446. (base (basename (derivation-file-name drv)))
  447. (log (string-append (dirname %state-directory)
  448. "/log/guix/drvs/" (string-take base 2)
  449. "/" (string-drop base 2) ".bz2")))
  450. (list (response-code response)
  451. (= (response-content-length response) (stat:size (stat log)))
  452. (first (response-content-type response))))))
  453. (test-equal "/log/NAME not found"
  454. 404
  455. (let ((uri (publish-uri "/log/does-not-exist")))
  456. (response-code (http-get uri))))
  457. (test-equal "non-GET query"
  458. '(200 404)
  459. (let ((path (string-append "/" (store-path-hash-part %item)
  460. ".narinfo")))
  461. (map response-code
  462. (list (http-get (publish-uri path))
  463. (http-post (publish-uri path))))))
  464. (test-end "publish")