publish.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2018 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 (guix 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 (guix 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 _IONBF)
  60. (call-with-values
  61. (lambda ()
  62. (http-get uri #:port socket #:streaming? #t))
  63. (lambda (response port)
  64. ;; Don't (setvbuf port _IONBF) 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. ;; Wait until the two servers are ready.
  102. (wait-until-ready 6789)
  103. ;; Initialize the public/private key SRFI-39 parameters.
  104. (%public-key (read-file-sexp %public-key-file))
  105. (%private-key (read-file-sexp %private-key-file))
  106. (test-begin "publish")
  107. (test-equal "/nix-cache-info"
  108. (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
  109. %store-directory)
  110. (http-get-body (publish-uri "/nix-cache-info")))
  111. (test-equal "/*.narinfo"
  112. (let* ((info (query-path-info %store %item))
  113. (unsigned-info
  114. (format #f
  115. "StorePath: ~a
  116. URL: nar/~a
  117. Compression: none
  118. NarHash: sha256:~a
  119. NarSize: ~d
  120. References: ~a
  121. FileSize: ~a~%"
  122. %item
  123. (basename %item)
  124. (bytevector->nix-base32-string
  125. (path-info-hash info))
  126. (path-info-nar-size info)
  127. (basename (first (path-info-references info)))
  128. (path-info-nar-size info)))
  129. (signature (base64-encode
  130. (string->utf8
  131. (canonical-sexp->string
  132. ((@@ (guix scripts publish) signed-string)
  133. unsigned-info))))))
  134. (format #f "~aSignature: 1;~a;~a~%"
  135. unsigned-info (gethostname) signature))
  136. (utf8->string
  137. (http-get-body
  138. (publish-uri
  139. (string-append "/" (store-path-hash-part %item) ".narinfo")))))
  140. (test-equal "/*.narinfo with properly encoded '+' sign"
  141. ;; See <http://bugs.gnu.org/21888>.
  142. (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
  143. (info (query-path-info %store item))
  144. (unsigned-info
  145. (format #f
  146. "StorePath: ~a
  147. URL: nar/~a
  148. Compression: none
  149. NarHash: sha256:~a
  150. NarSize: ~d
  151. References: ~%\
  152. FileSize: ~a~%"
  153. item
  154. (uri-encode (basename item))
  155. (bytevector->nix-base32-string
  156. (path-info-hash info))
  157. (path-info-nar-size info)
  158. (path-info-nar-size info)))
  159. (signature (base64-encode
  160. (string->utf8
  161. (canonical-sexp->string
  162. ((@@ (guix scripts publish) signed-string)
  163. unsigned-info))))))
  164. (format #f "~aSignature: 1;~a;~a~%"
  165. unsigned-info (gethostname) signature))
  166. (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
  167. (utf8->string
  168. (http-get-body
  169. (publish-uri
  170. (string-append "/" (store-path-hash-part item) ".narinfo"))))))
  171. (test-equal "/nar/*"
  172. "bar"
  173. (call-with-temporary-output-file
  174. (lambda (temp port)
  175. (let ((nar (utf8->string
  176. (http-get-body
  177. (publish-uri
  178. (string-append "/nar/" (basename %item)))))))
  179. (call-with-input-string nar (cut restore-file <> temp)))
  180. (call-with-input-file temp read-string))))
  181. (unless (zlib-available?)
  182. (test-skip 1))
  183. (test-equal "/nar/gzip/*"
  184. "bar"
  185. (call-with-temporary-output-file
  186. (lambda (temp port)
  187. (let ((nar (http-get-port
  188. (publish-uri
  189. (string-append "/nar/gzip/" (basename %item))))))
  190. (call-with-gzip-input-port nar
  191. (cut restore-file <> temp)))
  192. (call-with-input-file temp read-string))))
  193. (unless (zlib-available?)
  194. (test-skip 1))
  195. (test-equal "/*.narinfo with compression"
  196. `(("StorePath" . ,%item)
  197. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  198. ("Compression" . "gzip"))
  199. (let ((thread (with-separate-output-ports
  200. (call-with-new-thread
  201. (lambda ()
  202. (guix-publish "--port=6799" "-C5"))))))
  203. (wait-until-ready 6799)
  204. (let* ((url (string-append "http://localhost:6799/"
  205. (store-path-hash-part %item) ".narinfo"))
  206. (body (http-get-port url)))
  207. (filter (lambda (item)
  208. (match item
  209. (("Compression" . _) #t)
  210. (("StorePath" . _) #t)
  211. (("URL" . _) #t)
  212. (_ #f)))
  213. (recutils->alist body)))))
  214. (unless (zlib-available?)
  215. (test-skip 1))
  216. (test-equal "/*.narinfo for a compressed file"
  217. '("none" "nar") ;compression-less nar
  218. ;; Assume 'guix publish -C' is already running on port 6799.
  219. (let* ((item (add-text-to-store %store "fake.tar.gz"
  220. "This is a fake compressed file."))
  221. (url (string-append "http://localhost:6799/"
  222. (store-path-hash-part item) ".narinfo"))
  223. (body (http-get-port url))
  224. (info (recutils->alist body)))
  225. (list (assoc-ref info "Compression")
  226. (dirname (assoc-ref info "URL")))))
  227. (test-equal "custom nar path"
  228. ;; Serve nars at /foo/bar/chbouib instead of /nar.
  229. (list `(("StorePath" . ,%item)
  230. ("URL" . ,(string-append "foo/bar/chbouib/" (basename %item)))
  231. ("Compression" . "none"))
  232. 200
  233. 404)
  234. (let ((thread (with-separate-output-ports
  235. (call-with-new-thread
  236. (lambda ()
  237. (guix-publish "--port=6798" "-C0"
  238. "--nar-path=///foo/bar//chbouib/"))))))
  239. (wait-until-ready 6798)
  240. (let* ((base "http://localhost:6798/")
  241. (part (store-path-hash-part %item))
  242. (url (string-append base part ".narinfo"))
  243. (nar-url (string-append base "foo/bar/chbouib/"
  244. (basename %item)))
  245. (body (http-get-port url)))
  246. (list (filter (lambda (item)
  247. (match item
  248. (("Compression" . _) #t)
  249. (("StorePath" . _) #t)
  250. (("URL" . _) #t)
  251. (_ #f)))
  252. (recutils->alist body))
  253. (response-code (http-get nar-url))
  254. (response-code
  255. (http-get (string-append base "nar/" (basename %item))))))))
  256. (test-equal "/nar/ with properly encoded '+' sign"
  257. "Congrats!"
  258. (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
  259. (call-with-temporary-output-file
  260. (lambda (temp port)
  261. (let ((nar (utf8->string
  262. (http-get-body
  263. (publish-uri
  264. (string-append "/nar/" (uri-encode (basename item))))))))
  265. (call-with-input-string nar (cut restore-file <> temp)))
  266. (call-with-input-file temp read-string)))))
  267. (test-equal "/nar/invalid"
  268. 404
  269. (begin
  270. (call-with-output-file (string-append (%store-prefix) "/invalid")
  271. (lambda (port)
  272. (display "This file is not a valid store item." port)))
  273. (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
  274. (test-equal "/file/NAME/sha256/HASH"
  275. "Hello, Guix world!"
  276. (let* ((data "Hello, Guix world!")
  277. (hash (call-with-input-string data port-sha256))
  278. (drv (run-with-store %store
  279. (gexp->derivation "the-file.txt"
  280. #~(call-with-output-file #$output
  281. (lambda (port)
  282. (display #$data port)))
  283. #:hash-algo 'sha256
  284. #:hash hash)))
  285. (out (build-derivations %store (list drv))))
  286. (utf8->string
  287. (http-get-body
  288. (publish-uri
  289. (string-append "/file/the-file.txt/sha256/"
  290. (bytevector->nix-base32-string hash)))))))
  291. (test-equal "/file/NAME/sha256/INVALID-NIX-BASE32-STRING"
  292. 404
  293. (let ((uri (publish-uri
  294. "/file/the-file.txt/sha256/not-a-nix-base32-string")))
  295. (response-code (http-get uri))))
  296. (test-equal "/file/NAME/sha256/INVALID-HASH"
  297. 404
  298. (let ((uri (publish-uri
  299. (string-append "/file/the-file.txt/sha256/"
  300. (bytevector->nix-base32-string
  301. (call-with-input-string "" port-sha256))))))
  302. (response-code (http-get uri))))
  303. (unless (zlib-available?)
  304. (test-skip 1))
  305. (test-equal "with cache"
  306. (list #t
  307. `(("StorePath" . ,%item)
  308. ("URL" . ,(string-append "nar/gzip/" (basename %item)))
  309. ("Compression" . "gzip"))
  310. 200 ;nar/gzip/…
  311. #t ;Content-Length
  312. #t ;FileSize
  313. 404) ;nar/…
  314. (call-with-temporary-directory
  315. (lambda (cache)
  316. (let ((thread (with-separate-output-ports
  317. (call-with-new-thread
  318. (lambda ()
  319. (guix-publish "--port=6797" "-C2"
  320. (string-append "--cache=" cache)))))))
  321. (wait-until-ready 6797)
  322. (let* ((base "http://localhost:6797/")
  323. (part (store-path-hash-part %item))
  324. (url (string-append base part ".narinfo"))
  325. (nar-url (string-append base "nar/gzip/" (basename %item)))
  326. (cached (string-append cache "/gzip/" (basename %item)
  327. ".narinfo"))
  328. (nar (string-append cache "/gzip/"
  329. (basename %item) ".nar"))
  330. (response (http-get url)))
  331. (and (= 404 (response-code response))
  332. ;; We should get an explicitly short TTL for 404 in this case
  333. ;; because it's going to become 200 shortly.
  334. (match (assq-ref (response-headers response) 'cache-control)
  335. ((('max-age . ttl))
  336. (< ttl 3600)))
  337. (wait-for-file cached)
  338. (let* ((body (http-get-port url))
  339. (compressed (http-get nar-url))
  340. (uncompressed (http-get (string-append base "nar/"
  341. (basename %item))))
  342. (narinfo (recutils->alist body)))
  343. (list (file-exists? nar)
  344. (filter (lambda (item)
  345. (match item
  346. (("Compression" . _) #t)
  347. (("StorePath" . _) #t)
  348. (("URL" . _) #t)
  349. (_ #f)))
  350. narinfo)
  351. (response-code compressed)
  352. (= (response-content-length compressed)
  353. (stat:size (stat nar)))
  354. (= (string->number
  355. (assoc-ref narinfo "FileSize"))
  356. (stat:size (stat nar)))
  357. (response-code uncompressed)))))))))
  358. (unless (zlib-available?)
  359. (test-skip 1))
  360. (let ((item (add-text-to-store %store "fake-compressed-thing.tar.gz"
  361. (random-text))))
  362. (test-equal "with cache, uncompressed"
  363. (list #t
  364. `(("StorePath" . ,item)
  365. ("URL" . ,(string-append "nar/" (basename item)))
  366. ("Compression" . "none"))
  367. 200 ;nar/…
  368. (path-info-nar-size
  369. (query-path-info %store item)) ;FileSize
  370. 404) ;nar/gzip/…
  371. (call-with-temporary-directory
  372. (lambda (cache)
  373. (let ((thread (with-separate-output-ports
  374. (call-with-new-thread
  375. (lambda ()
  376. (guix-publish "--port=6796" "-C2"
  377. (string-append "--cache=" cache)))))))
  378. (wait-until-ready 6796)
  379. (let* ((base "http://localhost:6796/")
  380. (part (store-path-hash-part item))
  381. (url (string-append base part ".narinfo"))
  382. (cached (string-append cache "/none/"
  383. (basename item) ".narinfo"))
  384. (nar (string-append cache "/none/"
  385. (basename item) ".nar"))
  386. (response (http-get url)))
  387. (and (= 404 (response-code response))
  388. (wait-for-file cached)
  389. (let* ((body (http-get-port url))
  390. (compressed (http-get (string-append base "nar/gzip/"
  391. (basename item))))
  392. (uncompressed (http-get (string-append base "nar/"
  393. (basename item))))
  394. (narinfo (recutils->alist body)))
  395. (list (file-exists? nar)
  396. (filter (lambda (item)
  397. (match item
  398. (("Compression" . _) #t)
  399. (("StorePath" . _) #t)
  400. (("URL" . _) #t)
  401. (_ #f)))
  402. narinfo)
  403. (response-code uncompressed)
  404. (string->number
  405. (assoc-ref narinfo "FileSize"))
  406. (response-code compressed))))))))))
  407. (test-equal "/log/NAME"
  408. `(200 #t application/x-bzip2)
  409. (let ((drv (run-with-store %store
  410. (gexp->derivation "with-log"
  411. #~(call-with-output-file #$output
  412. (lambda (port)
  413. (display "Hello, build log!"
  414. (current-error-port))
  415. (display "" port)))))))
  416. (build-derivations %store (list drv))
  417. (let* ((response (http-get
  418. (publish-uri (string-append "/log/"
  419. (basename (derivation->output-path drv))))
  420. #:decode-body? #f))
  421. (base (basename (derivation-file-name drv)))
  422. (log (string-append (dirname %state-directory)
  423. "/log/guix/drvs/" (string-take base 2)
  424. "/" (string-drop base 2) ".bz2")))
  425. (list (response-code response)
  426. (= (response-content-length response) (stat:size (stat log)))
  427. (first (response-content-type response))))))
  428. (test-equal "/log/NAME not found"
  429. 404
  430. (let ((uri (publish-uri "/log/does-not-exist")))
  431. (response-code (http-get uri))))
  432. (test-end "publish")