publish: Add '--compression'.
[jackhill/guix/guix.git] / tests / publish.scm
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
19 ;; Avoid interference.
20 (unsetenv "http_proxy")
21
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 (guix hash)
28 #:use-module (guix store)
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 zlib)
35 #:use-module (web uri)
36 #:use-module (web client)
37 #:use-module (web response)
38 #:use-module (rnrs bytevectors)
39 #:use-module (ice-9 binary-ports)
40 #:use-module (srfi srfi-1)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-64)
43 #:use-module (ice-9 format)
44 #:use-module (ice-9 match)
45 #:use-module (ice-9 rdelim))
46
47 (define %store
48 (open-connection-for-tests))
49
50 (define %reference (add-text-to-store %store "ref" "foo"))
51
52 (define %item (add-text-to-store %store "item" "bar" (list %reference)))
53
54 (define (http-get-body uri)
55 (call-with-values (lambda () (http-get uri))
56 (lambda (response body) body)))
57
58 (define (http-get-port uri)
59 (call-with-values (lambda () (http-get uri #:streaming? #t))
60 (lambda (response port) port)))
61
62 (define (publish-uri route)
63 (string-append "http://localhost:6789" route))
64
65 ;; Run a local publishing server in a separate thread.
66 (call-with-new-thread
67 (lambda ()
68 (guix-publish "--port=6789" "-C0"))) ;attempt to avoid port collision
69
70 (define (wait-until-ready port)
71 ;; Wait until the server is accepting connections.
72 (let ((conn (socket PF_INET SOCK_STREAM 0)))
73 (let loop ()
74 (unless (false-if-exception
75 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") port))
76 (loop)))))
77
78 ;; Wait until the two servers are ready.
79 (wait-until-ready 6789)
80
81 \f
82 (test-begin "publish")
83
84 (test-equal "/nix-cache-info"
85 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
86 %store-directory)
87 (http-get-body (publish-uri "/nix-cache-info")))
88
89 (test-equal "/*.narinfo"
90 (let* ((info (query-path-info %store %item))
91 (unsigned-info
92 (format #f
93 "StorePath: ~a
94 URL: nar/~a
95 Compression: none
96 NarHash: sha256:~a
97 NarSize: ~d
98 References: ~a~%"
99 %item
100 (basename %item)
101 (bytevector->nix-base32-string
102 (path-info-hash info))
103 (path-info-nar-size info)
104 (basename (first (path-info-references info)))))
105 (signature (base64-encode
106 (string->utf8
107 (canonical-sexp->string
108 ((@@ (guix scripts publish) signed-string)
109 unsigned-info))))))
110 (format #f "~aSignature: 1;~a;~a~%"
111 unsigned-info (gethostname) signature))
112 (utf8->string
113 (http-get-body
114 (publish-uri
115 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
116
117 (test-equal "/*.narinfo with properly encoded '+' sign"
118 ;; See <http://bugs.gnu.org/21888>.
119 (let* ((item (add-text-to-store %store "fake-gtk+" "Congrats!"))
120 (info (query-path-info %store item))
121 (unsigned-info
122 (format #f
123 "StorePath: ~a
124 URL: nar/~a
125 Compression: none
126 NarHash: sha256:~a
127 NarSize: ~d
128 References: ~%"
129 item
130 (uri-encode (basename item))
131 (bytevector->nix-base32-string
132 (path-info-hash info))
133 (path-info-nar-size info)))
134 (signature (base64-encode
135 (string->utf8
136 (canonical-sexp->string
137 ((@@ (guix scripts publish) signed-string)
138 unsigned-info))))))
139 (format #f "~aSignature: 1;~a;~a~%"
140 unsigned-info (gethostname) signature))
141
142 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
143 (utf8->string
144 (http-get-body
145 (publish-uri
146 (string-append "/" (store-path-hash-part item) ".narinfo"))))))
147
148 (test-equal "/nar/*"
149 "bar"
150 (call-with-temporary-output-file
151 (lambda (temp port)
152 (let ((nar (utf8->string
153 (http-get-body
154 (publish-uri
155 (string-append "/nar/" (basename %item)))))))
156 (call-with-input-string nar (cut restore-file <> temp)))
157 (call-with-input-file temp read-string))))
158
159 (unless (zlib-available?)
160 (test-skip 1))
161 (test-equal "/nar/gzip/*"
162 "bar"
163 (call-with-temporary-output-file
164 (lambda (temp port)
165 (let ((nar (http-get-port
166 (publish-uri
167 (string-append "/nar/gzip/" (basename %item))))))
168 (call-with-gzip-input-port nar
169 (cut restore-file <> temp)))
170 (call-with-input-file temp read-string))))
171
172 (unless (zlib-available?)
173 (test-skip 1))
174 (test-equal "/*.narinfo with compression"
175 `(("StorePath" . ,%item)
176 ("URL" . ,(string-append "nar/gzip/" (basename %item)))
177 ("Compression" . "gzip"))
178 (let ((thread (call-with-new-thread
179 (lambda ()
180 (guix-publish "--port=6799" "-C5")))))
181 (wait-until-ready 6799)
182 (let* ((url (string-append "http://localhost:6799/"
183 (store-path-hash-part %item) ".narinfo"))
184 (body (http-get-port url)))
185 (filter (lambda (item)
186 (match item
187 (("Compression" . _) #t)
188 (("StorePath" . _) #t)
189 (("URL" . _) #t)
190 (_ #f)))
191 (recutils->alist body)))))
192
193 (test-equal "/nar/ with properly encoded '+' sign"
194 "Congrats!"
195 (let ((item (add-text-to-store %store "fake-gtk+" "Congrats!")))
196 (call-with-temporary-output-file
197 (lambda (temp port)
198 (let ((nar (utf8->string
199 (http-get-body
200 (publish-uri
201 (string-append "/nar/" (uri-encode (basename item))))))))
202 (call-with-input-string nar (cut restore-file <> temp)))
203 (call-with-input-file temp read-string)))))
204
205 (test-equal "/nar/invalid"
206 404
207 (begin
208 (call-with-output-file (string-append (%store-prefix) "/invalid")
209 (lambda (port)
210 (display "This file is not a valid store item." port)))
211 (response-code (http-get (publish-uri (string-append "/nar/invalid"))))))
212
213 (test-end "publish")