doc: Fix typo in "Locales".
[jackhill/guix/guix.git] / tests / publish.scm
CommitLineData
aff8ce7c
DT
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(define-module (test-publish)
20 #:use-module (guix scripts publish)
21 #:use-module (guix tests)
22 #:use-module (guix config)
23 #:use-module (guix utils)
24 #:use-module (guix hash)
25 #:use-module (guix store)
26 #:use-module (guix base32)
27 #:use-module (guix base64)
28 #:use-module ((guix serialization) #:select (restore-file))
29 #:use-module (guix pk-crypto)
30 #:use-module (web client)
31 #:use-module (web response)
32 #:use-module (rnrs bytevectors)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-64)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 rdelim))
38
39(define %store
40 (open-connection-for-tests))
41
42(define %reference (add-text-to-store %store "ref" "foo"))
43
44(define %item (add-text-to-store %store "item" "bar" (list %reference)))
45
46(define (http-get-body uri)
47 (call-with-values (lambda () (http-get uri))
48 (lambda (response body) body)))
49
50(define (publish-uri route)
51 (string-append "http://localhost:6789" route))
52
53;; Run a local publishing server in a separate thread.
54(call-with-new-thread
55 (lambda ()
56 (guix-publish "--port=6789"))) ; attempt to avoid port collision
57
58;; Wait until the server is accepting connections.
59(let ((conn (socket PF_INET SOCK_STREAM 0)))
60 (let loop ()
61 (unless (false-if-exception
62 (connect conn AF_INET (inet-pton AF_INET "127.0.0.1") 6789))
63 (loop))))
64
65(test-begin "publish")
66
67(test-equal "/nix-cache-info"
68 (format #f "StoreDir: ~a\nWantMassQuery: 0\nPriority: 100\n"
69 %store-directory)
70 (http-get-body (publish-uri "/nix-cache-info")))
71
72(test-equal "/*.narinfo"
73 (let* ((info (query-path-info %store %item))
74 (unsigned-info
75 (format #f
76 "StorePath: ~a
77URL: nar/~a
78Compression: none
79NarHash: sha256:~a
80NarSize: ~d
81References: ~a~%"
82 %item
83 (basename %item)
9cced526 84 (bytevector->nix-base32-string
aff8ce7c
DT
85 (path-info-hash info))
86 (path-info-nar-size info)
87 (basename (first (path-info-references info)))))
88 (signature (base64-encode
89 (string->utf8
90 (canonical-sexp->string
91 ((@@ (guix scripts publish) signed-string)
92 unsigned-info))))))
93 (format #f "~aSignature: 1;~a;~a~%"
94 unsigned-info (gethostname) signature))
95 (utf8->string
96 (http-get-body
97 (publish-uri
98 (string-append "/" (store-path-hash-part %item) ".narinfo")))))
99
100(test-equal "/nar/*"
101 "bar"
102 (call-with-temporary-output-file
103 (lambda (temp port)
104 (let ((nar (utf8->string
105 (http-get-body
106 (publish-uri
107 (string-append "/nar/" (basename %item)))))))
108 (call-with-input-string nar (cut restore-file <> temp)))
109 (call-with-input-file temp read-string))))
110
111(test-end "publish")
112
113\f
114(exit (= (test-runner-fail-count (test-runner-current)) 0))