projects/guix·blob version-0.14.0 · refs

293 lines · 9.4 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Oleg Pykhalov <[email protected]>
3;;; Copyright © 2017 Ludovic Courtès <[email protected]>
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
20(define-module (gnu tests version-control)
21 #:use-module (gnu tests)
22 #:use-module (gnu system)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services version-control)
28 #:use-module (gnu services web)
29 #:use-module (gnu services networking)
30 #:use-module (gnu packages version-control)
31 #:use-module (guix gexp)
32 #:use-module (guix store)
33 #:use-module (guix modules)
34 #:export (%test-cgit
35 %test-git-http))
36
37(define README-contents
38 "Hello! This is what goes inside the 'README' file.")
39
40(define %make-git-repository
41 ;; Create Git repository in /srv/git/test.
42 (with-imported-modules (source-module-closure
43 '((guix build utils)))
44 #~(begin
45 (use-modules (guix build utils))
46
47 (let ((git (string-append #$git "/bin/git")))
48 (mkdir-p "/tmp/test-repo")
49 (with-directory-excursion "/tmp/test-repo"
50 (call-with-output-file "/tmp/test-repo/README"
51 (lambda (port)
52 (display #$README-contents port)))
53 (invoke git "config" "--global" "user.email" "[email protected]")
54 (invoke git "config" "--global" "user.name" "A U Thor")
55 (invoke git "init")
56 (invoke git "add" ".")
57 (invoke git "commit" "-m" "That's a commit."))
58
59 (mkdir-p "/srv/git")
60 (rename-file "/tmp/test-repo/.git" "/srv/git/test")))))
61
62(define %test-repository-service
63 ;; Service that creates /srv/git/test.
64 (simple-service 'make-git-repository activation-service-type
65 %make-git-repository))
66
67(define %cgit-configuration-nginx
68 (list
69 (nginx-server-configuration
70 (root cgit)
71 (locations
72 (list
73 (nginx-location-configuration
74 (uri "@cgit")
75 (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
76 "fastcgi_param PATH_INFO $uri;"
77 "fastcgi_param QUERY_STRING $args;"
78 "fastcgi_param HTTP_HOST $server_name;"
79 "fastcgi_pass 127.0.0.1:9000;")))))
80 (try-files (list "$uri" "@cgit"))
81 (http-port 19418)
82 (https-port #f)
83 (ssl-certificate #f)
84 (ssl-certificate-key #f))))
85
86(define %cgit-os
87 ;; Operating system under test.
88 (let ((base-os
89 (simple-operating-system
90 (dhcp-client-service)
91 (service nginx-service-type)
92 (service fcgiwrap-service-type)
93 (service cgit-service-type
94 (cgit-configuration
95 (nginx %cgit-configuration-nginx)))
96 %test-repository-service)))
97 (operating-system
98 (inherit base-os)
99 (packages (cons* git
100 (operating-system-packages base-os))))))
101
102(define* (run-cgit-test #:optional (http-port 19418))
103 "Run tests in %CGIT-OS, which has nginx running and listening on
104HTTP-PORT."
105 (define os
106 (marionette-operating-system
107 %cgit-os
108 #:imported-modules '((gnu services herd)
109 (guix combinators))))
110
111 (define vm
112 (virtual-machine
113 (operating-system os)
114 (port-forwardings `((8080 . ,http-port)))))
115
116 (define test
117 (with-imported-modules '((gnu build marionette))
118 #~(begin
119 (use-modules (srfi srfi-11) (srfi srfi-64)
120 (gnu build marionette)
121 (web uri)
122 (web client)
123 (web response))
124
125 (define marionette
126 (make-marionette (list #$vm)))
127
128 (mkdir #$output)
129 (chdir #$output)
130
131 (test-begin "cgit")
132
133 ;; Wait for nginx to be up and running.
134 (test-eq "service running"
135 'running!
136 (marionette-eval
137 '(begin
138 (use-modules (gnu services herd))
139 (start-service 'nginx)
140 'running!)
141 marionette))
142
143 ;; Wait for fcgiwrap to be up and running.
144 (test-eq "service running"
145 'running!
146 (marionette-eval
147 '(begin
148 (use-modules (gnu services herd))
149 (start-service 'fcgiwrap)
150 'running!)
151 marionette))
152
153 ;; Make sure the PID file is created.
154 (test-assert "PID file"
155 (marionette-eval
156 '(file-exists? "/var/run/nginx/pid")
157 marionette))
158
159 ;; Make sure the configuration file is created.
160 (test-assert "configuration file"
161 (marionette-eval
162 '(file-exists? "/etc/cgitrc")
163 marionette))
164
165 ;; Make sure Git test repository is created.
166 (test-assert "Git test repository"
167 (marionette-eval
168 '(file-exists? "/srv/git/test")
169 marionette))
170
171 ;; Make sure we can access pages that correspond to our repository.
172 (letrec-syntax ((test-url
173 (syntax-rules ()
174 ((_ path code)
175 (test-equal (string-append "GET " path)
176 code
177 (let-values (((response body)
178 (http-get (string-append
179 "http://localhost:8080"
180 path))))
181 (response-code response))))
182 ((_ path)
183 (test-url path 200)))))
184 (test-url "/")
185 (test-url "/test")
186 (test-url "/test/log")
187 (test-url "/test/tree")
188 (test-url "/test/tree/README")
189 (test-url "/test/does-not-exist" 404)
190 (test-url "/test/tree/does-not-exist" 404)
191 (test-url "/does-not-exist" 404))
192
193 (test-end)
194 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
195
196 (gexp->derivation "cgit-test" test))
197
198(define %test-cgit
199 (system-test
200 (name "cgit")
201 (description "Connect to a running Cgit server.")
202 (value (run-cgit-test))))
203
204
205;;;
206;;; Git server.
207;;;
208
209(define %git-nginx-configuration
210 (nginx-configuration
211 (server-blocks
212 (list
213 (nginx-server-configuration
214 (http-port 19418)
215 (https-port #f)
216 (ssl-certificate #f)
217 (ssl-certificate-key #f)
218 (locations
219 (list (git-http-nginx-location-configuration
220 (git-http-configuration (export-all? #t)
221 (uri-path "/git"))))))))))
222
223(define %git-http-os
224 (simple-operating-system
225 (dhcp-client-service)
226 (service fcgiwrap-service-type)
227 (service nginx-service-type %git-nginx-configuration)
228 %test-repository-service))
229
230(define* (run-git-http-test #:optional (http-port 19418))
231 (define os
232 (marionette-operating-system
233 %git-http-os
234 #:imported-modules '((gnu services herd)
235 (guix combinators))))
236
237 (define vm
238 (virtual-machine
239 (operating-system os)
240 (port-forwardings `((8080 . ,http-port)))))
241
242 (define test
243 (with-imported-modules '((gnu build marionette)
244 (guix build utils))
245 #~(begin
246 (use-modules (srfi srfi-64)
247 (rnrs io ports)
248 (gnu build marionette)
249 (guix build utils))
250
251 (define marionette
252 (make-marionette (list #$vm)))
253
254 (mkdir #$output)
255 (chdir #$output)
256
257 (test-begin "git-http")
258
259 ;; Wait for nginx to be up and running.
260 (test-eq "nginx running"
261 'running!
262 (marionette-eval
263 '(begin
264 (use-modules (gnu services herd))
265 (start-service 'nginx)
266 'running!)
267 marionette))
268
269 ;; Make sure Git test repository is created.
270 (test-assert "Git test repository"
271 (marionette-eval
272 '(file-exists? "/srv/git/test")
273 marionette))
274
275 ;; Make sure we can clone the repo from the host.
276 (test-equal "clone"
277 '#$README-contents
278 (begin
279 (invoke #$(file-append git "/bin/git") "clone" "-v"
280 "http://localhost:8080/git/test" "/tmp/clone")
281 (call-with-input-file "/tmp/clone/README"
282 get-string-all)))
283
284 (test-end)
285 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
286
287 (gexp->derivation "git-http" test))
288
289(define %test-git-http
290 (system-test
291 (name "git-http")
292 (description "Connect to a running Git HTTP server.")
293 (value (run-git-http-test))))