publish.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. ;; Avoid interference.
  19. (unsetenv "http_proxy")
  20. (define-module (test-publish)
  21. #:use-module (guix scripts publish)
  22. #:use-module (guix tests)
  23. #:use-module (guix config)
  24. #:use-module (guix utils)
  25. #:use-module (guix hash)
  26. #:use-module (guix store)
  27. #:use-module (guix derivations)
  28. #:use-module (guix gexp)
  29. #:use-module (guix base32)
  30. #:use-module (guix base64)
  31. #:use-module ((guix records) #:select (recutils->alist))
  32. #:use-module ((guix serialization) #:select (restore-file))
  33. #:use-module (guix pk-crypto)
  34. #:use-module ((guix pki) #:select (%public-key-file %private-key-file))
  35. #:use-module (guix zlib)
  36. #:use-module (web uri)
  37. #:use-module (web client)
  38. #:use-module (web response)
  39. #:use-module (rnrs bytevectors)
  40. #:use-module (ice-9 binary-ports)
  41. #:use-module (srfi srfi-1)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-64)
  44. #:use-module (ice-9 format)
  45. #:use-module (ice-9 match)
  46. #:use-module (ice-9 rdelim))
  47. (define %store
  48. (open-connection-for-tests))
  49. (define %reference (add-text-to-store %store "ref" "foo"))
  50. (define %item (add-text-to-store %store "item" "bar" (list %reference)))
  51. (define (http-get-body uri)
  52. (call-with-values (lambda () (http-get uri))
  53. (lambda (response body) body)))
  54. (define (http-get-port uri)
  55. (let ((socket (open-socket-for-uri uri)))
  56. ;; Make sure to use an unbuffered port so that we can then peek at the
  57. ;; underlying file descriptor via 'call-with-gzip-input-port'.
  58. (setvbuf socket _IONBF)
  59. (call-with-values
  60. (lambda ()
  61. (http-get uri #:port socket #:streaming? #t))
  62. (lambda (response port)
  63. ;; Don't (setvbuf port _IONBF) because of <http://bugs.gnu.org/19610>
  64. ;; (PORT might be a custom binary input port).
  65. port))))
  66. (define (publish-uri route)
  67. (string-append "http://localhost:6789" route))
  68. (define-syntax-rule (with-separate-output-ports exp ...)
  69. ;; Since ports aren't thread-safe in Guile 2.0, duplicate the output and
  70. ;; error ports to make sure the two threads don't end up stepping on each
  71. ;; other's toes.
  72. (with-output-to-port (duplicate-port (current-output-port) "w")
  73. (lambda ()
  74. (with-error-to-port (duplicate-port (current-error-port) "w")
  75. (lambda ()
  76. exp ...)))))
  77. ;; Run a local publishing server in a separate thread.
  78. (with-separate-output-ports
  79. (call-with-new-thread
  80. (lambda ()
  81. (guix-publish "--port=6789" "-C0")))) ;attempt to avoid port collision
  82. (define (wait-until-ready port)
  83. ;; Wait until the server is accepting connections.
  84. (let ((conn (socket PF_INET SOCK_STREAM 0)))
  85. (let loop ()
  86. (unless (false-if-exception
  87. (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
  88. (loop)))))
  89. (define (wait-for-file file)
  90. ;; Wait until FILE shows up.
  91. (let loop ((i 20))
  92. (cond ((file-exists? file)
  93. #t)
  94. ((zero? i)
  95. (error "file didn't show up" file))
  96. (else
  97. (pk 'wait-for-file file)
  98. (sleep 1)
  99. (loop (- i 1))))))
  100. ;; Wait until the two servers are ready.
  101. (wait-until-ready 6789)
  102. ;; Initialize the public/private key SRFI-39 parameters.
  103. (%public-key (read-file-sexp %public-key-file))
  104. (%private-key (read-file-sexp %private-key-file))
  105. (test-begin "publish")
  106. (test-equal "/nix-cache-info"
  107. (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
  108. %store-directory)
  109. (http-get-body (publish-uri "/nix-cache-info")))
  110. (test-equal "/*.narinfo"
  111. (let* ((info (query-path-info %store %item))
  112. (unsigned-info
  113. (format #f
  114. "StorePath: ~a
  115. URL: nar/~a
  116. Compression: none
  117. NarHash: sha256:~a
  118. NarSize: ~d
  119. References: ~a
  120. FileSize: ~a~%"
  121. %item
  122. (basename %item)
  123. (bytevector->nix-base32-string
  124. (path-info-hash info))
  125. (path-info-nar-size info)
  126. (basename (first (path-info-references info)))
  127. (path-info-nar-size info)))
  128. (signature (base64-encode
  129. (string->utf8
  130. (canonical-sexp->string
  131. ((@@ (guix scripts publish) signed-string)
  132. unsigned-info))))))
  133. (format #f "~aSignature: 1;~a;~a~%"
  134. unsigned-info (gethostname) signature))
  135. (utf8->string
  136. (http-get-body
  137. (publish-uri
  138. (string-append "/" (store-path-hash-part %item) ".narinfo")))))
  139. (test-equal "/*.narinfo with properly encoded '+' sign"
  140. ;; See <http://bugs.gnu.org/21888>.
  141. (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
  142. (info (query-path-info %store item))
  143. (unsigned-info
  144. (format #f
  145. "StorePath: ~a
  146. URL: nar/~a
  147. Compression: none
  148. NarHash: sha256:~a
  149. NarSize: ~d
  150. References: ~%\
  151. FileSize: ~a~%"
  152. item
  153. (uri-encode (basename item))
  154. (bytevector->nix-base32-string
  155. (path-info-hash info))
  156. (path-info-nar-size info)
  157. (path-info-nar-size info)))
  158. (signature (base64-encode
  159. (string->utf8
  160. (canonical-sexp->string
  161. ((@@ (guix scripts publish) signed-string)
  162. unsigned-info))))))
  163. (format #f "~aSignature: 1;~a;~a~%"
  164. unsigned-info (gethostname) signature))
  165. (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
  166. (utf8->string
  167. (http-get-body
  168. (publish-uri
  169. (string-append "/" (store-path-hash-part item) ".narinfo"))))))
  170. (test-equal "/nar/*"
  171. "bar"
  172. (call-with-temporary-output-file
  173. (lambda (temp port)
  174. (let ((nar (utf8->string
  175. (http-get-body
  176. (publish-uri
  177. (string-append "/nar/" (basename %item)))))))
  178. (call-with-input-string nar (cut restore-file <> temp)))
  179. (call-with-input-file temp read-string))))
  180. (unless (zlib-available?)
  181. (test-skip 1))
  182. (test-equal "/nar/gzip/*"
  183. "bar"
  184. (call-with-temporary-output-file
  185. (lambda (temp port)
  186. (let ((nar (http-get-port
  187. (publish-uri
  188. (string-append "/nar/gzip/" (basename %item))))))
  189. (call-with-gzip-input-port nar
  190. (cut restore-file <> temp)))
  191. (call-with-input-file temp read-string))))
  192. (unless (zlib-available?)
  193. (test-skip 1))
  194. (test-equal "/*.narinfo with compression"
  195. `(("StorePath" . ,%item)
  196. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  197. ("Compression" . "gzip"))
  198. (let ((thread (with-separate-output-ports
  199. (call-with-new-thread
  200. (lambda ()
  201. (guix-publish "--port=6799" "-C5"))))))
  202. (wait-until-ready 6799)
  203. (let* ((url (string-append "http://localhost:6799/"
  204. (store-path-hash-part %item) ".narinfo"))
  205. (body (http-get-port url)))
  206. (filter (lambda (item)
  207. (match item
  208. (("Compression" . _) #t)
  209. (("StorePath" . _) #t)
  210. (("URL" . _) #t)
  211. (_ #f)))
  212. (recutils->alist body)))))
  213. (unless (zlib-available?)
  214. (test-skip 1))
  215. (test-equal "/*.narinfo for a compressed file"
  216. '("none" "nar") ;compression-less nar
  217. ;; Assume 'guix publish -C' is already running on port 6799.
  218. (let* ((item (add-text-to-store %store "fake.tar.gz"
  219. "This is a fake compressed file."))
  220. (url (string-append "http://localhost:6799/"
  221. (store-path-hash-part item) ".narinfo"))
  222. (body (http-get-port url))
  223. (info (recutils->alist body)))
  224. (list (assoc-ref info "Compression")
  225. (dirname (assoc-ref info "URL")))))
  226. (test-equal "custom nar path"
  227. ;; Serve nars at /foo/bar/chbouib instead of /nar.
  228. (list `(("StorePath" . ,%item)
  229. ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
  230. ("Compression" . "none"))
  231. 200
  232. 404)
  233. (let ((thread (with-separate-output-ports
  234. (call-with-new-thread
  235. (lambda ()
  236. (guix-publish "--port=6798" "-C0"
  237. "--nar-path=///foo/bar//chbouib/"))))))
  238. (wait-until-ready 6798)
  239. (let* ((base "http://localhost:6798/")
  240. (part (store-path-hash-part %item))
  241. (url (string-append base part ".narinfo"))
  242. (nar-url (string-append base "foo/bar/chbouib/"
  243. (basename %item)))
  244. (body (http-get-port url)))
  245. (list (filter (lambda (item)
  246. (match item
  247. (("Compression" . _) #t)
  248. (("StorePath" . _) #t)
  249. (("URL" . _) #t)
  250. (_ #f)))
  251. (recutils->alist body))
  252. (response-code (http-get nar-url))
  253. (response-code
  254. (http-get (string-append base "nar/" (basename %item))))))))
  255. (test-equal "/nar/ with properly encoded '+' sign"
  256. "Congrats!"
  257. (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
  258. (call-with-temporary-output-file
  259. (lambda (temp port)
  260. (let ((nar (utf8->string
  261. (http-get-body
  262. (publish-uri
  263. (string-append "/nar/" (uri-encode (basename item))))))))
  264. (call-with-input-string nar (cut restore-file <> temp)))
  265. (call-with-input-file temp read-string)))))
  266. (test-equal "/nar/invalid"
  267. 404
  268. (begin
  269. (call-with-output-file (string-append (%store-prefix) "/invalid")
  270. (lambda (port)
  271. (display "This file is not a valid store item." port)))
  272. (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
  273. (test-equal "/file/NAME/sha256/HASH"
  274. "Hello, Guix world!"
  275. (let* ((data "Hello, Guix world!")
  276. (hash (call-with-input-string data port-sha256))
  277. (drv (run-with-store %store
  278. (gexp->derivation "the-file.txt"
  279. #~(call-with-output-file #$output
  280. (lambda (port)
  281. (display #$data port)))
  282. #:hash-algo 'sha256
  283. #:hash hash)))
  284. (out (build-derivations %store (list drv))))
  285. (utf8->string
  286. (http-get-body
  287. (publish-uri
  288. (string-append "/file/the-file.txt/sha256/"
  289. (bytevector->nix-base32-string hash)))))))
  290. (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
  291. 404
  292. (let ((uri (publish-uri
  293. "/file/the-file.txt/sha256/not-a-nix-base32-string")))
  294. (response-code (http-get uri))))
  295. (test-equal "/file/NAME/sha256/INVALID-HASH"
  296. 404
  297. (let ((uri (publish-uri
  298. (string-append "/file/the-file.txt/sha256/"
  299. (bytevector->nix-base32-string
  300. (call-with-input-string "" port-sha256))))))
  301. (response-code (http-get uri))))
  302. (unless (zlib-available?)
  303. (test-skip 1))
  304. (test-equal "with cache"
  305. (list #t
  306. `(("StorePath" . ,%item)
  307. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  308. ("Compression" . "gzip"))
  309. 200 ;nar/gzip/…
  310. #t ;Content-Length
  311. #t ;FileSize
  312. 404) ;nar/…
  313. (call-with-temporary-directory
  314. (lambda (cache)
  315. (let ((thread (with-separate-output-ports
  316. (call-with-new-thread
  317. (lambda ()
  318. (guix-publish "--port=6797" "-C2"
  319. (string-append "--cache=" cache)))))))
  320. (wait-until-ready 6797)
  321. (let* ((base "http://localhost:6797/")
  322. (part (store-path-hash-part %item))
  323. (url (string-append base part ".narinfo"))
  324. (nar-url (string-append base "nar/gzip/" (basename %item)))
  325. (cached (string-append cache "/gzip/" (basename %item)
  326. ".narinfo"))
  327. (nar (string-append cache "/gzip/"
  328. (basename %item) ".nar"))
  329. (response (http-get url)))
  330. (and (= 404 (response-code response))
  331. ;; We should get an explicitly short TTL for 404 in this case
  332. ;; because it's going to become 200 shortly.
  333. (match (assq-ref (response-headers response) 'cache-control)
  334. ((('max-age . ttl))
  335. (< ttl 3600)))
  336. (wait-for-file cached)
  337. (let* ((body (http-get-port url))
  338. (compressed (http-get nar-url))
  339. (uncompressed (http-get (string-append base "nar/"
  340. (basename %item))))
  341. (narinfo (recutils->alist body)))
  342. (list (file-exists? nar)
  343. (filter (lambda (item)
  344. (match item
  345. (("Compression" . _) #t)
  346. (("StorePath" . _) #t)
  347. (("URL" . _) #t)
  348. (_ #f)))
  349. narinfo)
  350. (response-code compressed)
  351. (= (response-content-length compressed)
  352. (stat:size (stat nar)))
  353. (= (string->number
  354. (assoc-ref narinfo "FileSize"))
  355. (stat:size (stat nar)))
  356. (response-code uncompressed)))))))))
  357. (unless (zlib-available?)
  358. (test-skip 1))
  359. (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
  360. (random-text))))
  361. (test-equal "with cache, uncompressed"
  362. (list #t
  363. `(("StorePath" . ,item)
  364. ("URL" . ,(string-append "nar/" (basename item)))
  365. ("Compression" . "none"))
  366. 200 ;nar/…
  367. (path-info-nar-size
  368. (query-path-info %store item)) ;FileSize
  369. 404) ;nar/gzip/…
  370. (call-with-temporary-directory
  371. (lambda (cache)
  372. (let ((thread (with-separate-output-ports
  373. (call-with-new-thread
  374. (lambda ()
  375. (guix-publish "--port=6796" "-C2"
  376. (string-append "--cache=" cache)))))))
  377. (wait-until-ready 6796)
  378. (let* ((base "http://localhost:6796/")
  379. (part (store-path-hash-part item))
  380. (url (string-append base part ".narinfo"))
  381. (cached (string-append cache "/none/"
  382. (basename item) ".narinfo"))
  383. (nar (string-append cache "/none/"
  384. (basename item) ".nar"))
  385. (response (http-get url)))
  386. (and (= 404 (response-code response))
  387. (wait-for-file cached)
  388. (let* ((body (http-get-port url))
  389. (compressed (http-get (string-append base "nar/gzip/"
  390. (basename item))))
  391. (uncompressed (http-get (string-append base "nar/"
  392. (basename item))))
  393. (narinfo (recutils->alist body)))
  394. (list (file-exists? nar)
  395. (filter (lambda (item)
  396. (match item
  397. (("Compression" . _) #t)
  398. (("StorePath" . _) #t)
  399. (("URL" . _) #t)
  400. (_ #f)))
  401. narinfo)
  402. (response-code uncompressed)
  403. (string->number
  404. (assoc-ref narinfo "FileSize"))
  405. (response-code compressed))))))))))
  406. (test-end "publish")