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