projects/guix·blob wip-full-source-bootstrap · refs

541 lines · 17.7 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017, 2018 Oleg Pykhalov <[email protected]>
3;;; Copyright © 2017, 2018, 2020, 2021 Ludovic Courtès <[email protected]>
4;;; Copyright © 2017, 2018 Clément Lassieur <[email protected]>
5;;; Copyright © 2018 Christopher Baines <[email protected]>
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (gnu tests version-control)
23 #:use-module (gnu tests)
24 #:use-module (gnu system)
25 #:use-module (gnu system file-systems)
26 #:use-module (gnu system shadow)
27 #:use-module (gnu system vm)
28 #:use-module (gnu services)
29 #:use-module (gnu services version-control)
30 #:use-module (gnu services cgit)
31 #:use-module (gnu services ssh)
32 #:use-module (gnu services web)
33 #:use-module (gnu services networking)
34 #:use-module (gnu packages version-control)
35 #:use-module (gnu packages ssh)
36 #:use-module (guix gexp)
37 #:use-module (guix store)
38 #:use-module (guix modules)
39 #:export (%test-cgit
40 %test-git-http
41 %test-gitolite
42 %test-gitile))
43
44(define README-contents
45 "Hello! This is what goes inside the 'README' file.")
46
47(define %make-git-repository
48 ;; Create Git repository in /srv/git/test.
49 (with-imported-modules (source-module-closure
50 '((guix build utils)))
51 #~(begin
52 (use-modules (guix build utils))
53
54 (let ((git (string-append #$git "/bin/git")))
55 (mkdir-p "/tmp/test-repo")
56 (with-directory-excursion "/tmp/test-repo"
57 (call-with-output-file "/tmp/test-repo/README"
58 (lambda (port)
59 (display #$README-contents port)))
60 (invoke git "config" "--global" "user.email" "[email protected]")
61 (invoke git "config" "--global" "user.name" "A U Thor")
62 (invoke git "init")
63 (invoke git "add" ".")
64 (invoke git "commit" "-m" "That's a commit."))
65
66 (mkdir-p "/srv/git")
67 (rename-file "/tmp/test-repo/.git" "/srv/git/test")
68 (with-output-to-file "/srv/git/test/git-daemon-export-ok"
69 (lambda _
70 (display "")))))))
71
72(define %test-repository-service
73 ;; Service that creates /srv/git/test.
74 (simple-service 'make-git-repository activation-service-type
75 %make-git-repository))
76
77(define %cgit-configuration-nginx
78 (list
79 (nginx-server-configuration
80 (root cgit)
81 (locations
82 (list
83 (nginx-location-configuration
84 (uri "@cgit")
85 (body '("fastcgi_param SCRIPT_FILENAME $document_root/lib/cgit/cgit.cgi;"
86 "fastcgi_param PATH_INFO $uri;"
87 "fastcgi_param QUERY_STRING $args;"
88 "fastcgi_param HTTP_HOST $server_name;"
89 "fastcgi_pass 127.0.0.1:9000;")))))
90 (try-files (list "$uri" "@cgit"))
91 (listen '("19418"))
92 (ssl-certificate #f)
93 (ssl-certificate-key #f))))
94
95(define %cgit-os
96 ;; Operating system under test.
97 (let ((base-os
98 (simple-operating-system
99 (service dhcp-client-service-type)
100 (service cgit-service-type
101 (cgit-configuration
102 (nginx %cgit-configuration-nginx)))
103 %test-repository-service)))
104 (operating-system
105 (inherit base-os)
106 (packages (cons* git
107 (operating-system-packages base-os))))))
108
109(define* (run-cgit-test #:optional (http-port 19418))
110 "Run tests in %CGIT-OS, which has nginx running and listening on
111HTTP-PORT."
112 (define os
113 (marionette-operating-system
114 %cgit-os
115 #:imported-modules '((gnu services herd)
116 (guix combinators))))
117
118 (define vm
119 (virtual-machine
120 (operating-system os)
121 (port-forwardings `((8080 . ,http-port)))))
122
123 (define test
124 (with-imported-modules '((gnu build marionette))
125 #~(begin
126 (use-modules (srfi srfi-11) (srfi srfi-64)
127 (gnu build marionette)
128 (web uri)
129 (web client)
130 (web response))
131
132 (define marionette
133 (make-marionette (list #$vm)))
134
135 (test-runner-current (system-test-runner #$output))
136 (test-begin "cgit")
137
138 ;; XXX: Shepherd reads the config file *before* binding its control
139 ;; socket, so /var/run/shepherd/socket might not exist yet when the
140 ;; 'marionette' service is started.
141 (test-assert "shepherd socket ready"
142 (marionette-eval
143 `(begin
144 (use-modules (gnu services herd))
145 (let loop ((i 10))
146 (cond ((file-exists? (%shepherd-socket-file))
147 #t)
148 ((> i 0)
149 (sleep 1)
150 (loop (- i 1)))
151 (else
152 'failure))))
153 marionette))
154
155 ;; Wait for nginx to be up and running.
156 (test-assert "nginx running"
157 (marionette-eval
158 '(begin
159 (use-modules (gnu services herd))
160 (start-service 'nginx))
161 marionette))
162
163 ;; Wait for fcgiwrap to be up and running.
164 (test-assert "fcgiwrap running"
165 (marionette-eval
166 '(begin
167 (use-modules (gnu services herd))
168 (start-service 'fcgiwrap))
169 marionette))
170
171 ;; Make sure the PID file is created.
172 (test-assert "PID file"
173 (marionette-eval
174 '(file-exists? "/var/run/nginx/pid")
175 marionette))
176
177 ;; Make sure the configuration file is created.
178 (test-assert "configuration file"
179 (marionette-eval
180 '(file-exists? "/etc/cgitrc")
181 marionette))
182
183 ;; Make sure Git test repository is created.
184 (test-assert "Git test repository"
185 (marionette-eval
186 '(file-exists? "/srv/git/test")
187 marionette))
188
189 ;; Make sure we can access pages that correspond to our repository.
190 (letrec-syntax ((test-url
191 (syntax-rules ()
192 ((_ path code)
193 (test-equal (string-append "GET " path)
194 code
195 (let-values (((response body)
196 (http-get (string-append
197 "http://localhost:8080"
198 path))))
199 (response-code response))))
200 ((_ path)
201 (test-url path 200)))))
202 (test-url "/")
203 (test-url "/test")
204 (test-url "/test/log")
205 (test-url "/test/tree")
206 (test-url "/test/tree/README")
207 (test-url "/test/does-not-exist" 404)
208 (test-url "/test/tree/does-not-exist" 404)
209 (test-url "/does-not-exist" 404))
210
211 (test-end))))
212
213 (gexp->derivation "cgit-test" test))
214
215(define %test-cgit
216 (system-test
217 (name "cgit")
218 (description "Connect to a running Cgit server.")
219 (value (run-cgit-test))))
220
221
222;;;
223;;; Git server.
224;;;
225
226(define %git-nginx-configuration
227 (nginx-configuration
228 (server-blocks
229 (list
230 (nginx-server-configuration
231 (listen '("19418"))
232 (ssl-certificate #f)
233 (ssl-certificate-key #f)
234 (locations
235 (list (git-http-nginx-location-configuration
236 (git-http-configuration (export-all? #t)
237 (uri-path "/git"))))))))))
238
239(define %git-http-os
240 (simple-operating-system
241 (service dhcp-client-service-type)
242 (service fcgiwrap-service-type)
243 (service nginx-service-type %git-nginx-configuration)
244 %test-repository-service))
245
246(define* (run-git-http-test #:optional (http-port 19418))
247 (define os
248 (marionette-operating-system
249 %git-http-os
250 #:imported-modules '((gnu services herd)
251 (guix combinators))))
252
253 (define vm
254 (virtual-machine
255 (operating-system os)
256 (port-forwardings `((8080 . ,http-port)))))
257
258 (define test
259 (with-imported-modules '((gnu build marionette)
260 (guix build utils))
261 #~(begin
262 (use-modules (srfi srfi-64)
263 (rnrs io ports)
264 (gnu build marionette)
265 (guix build utils))
266
267 (define marionette
268 (make-marionette (list #$vm)))
269
270 (test-runner-current (system-test-runner #$output))
271 (test-begin "git-http")
272
273 ;; Wait for nginx to be up and running.
274 (test-assert "nginx running"
275 (marionette-eval
276 '(begin
277 (use-modules (gnu services herd))
278 (start-service 'nginx))
279 marionette))
280
281 ;; Make sure Git test repository is created.
282 (test-assert "Git test repository"
283 (marionette-eval
284 '(file-exists? "/srv/git/test")
285 marionette))
286
287 (test-assert "fcgiwrap listens"
288 ;; Wait for fcgiwrap to be ready before cloning.
289 (wait-for-tcp-port 9000 marionette))
290
291 ;; Make sure we can clone the repo from the host.
292 (test-equal "clone"
293 '#$README-contents
294 (begin
295 (invoke #$(file-append git "/bin/git") "clone" "-v"
296 "http://localhost:8080/git/test" "/tmp/clone")
297 (call-with-input-file "/tmp/clone/README"
298 get-string-all)))
299
300 (test-end))))
301
302 (gexp->derivation "git-http" test))
303
304(define %test-git-http
305 (system-test
306 (name "git-http")
307 (description "Connect to a running Git HTTP server.")
308 (value (run-git-http-test))))
309
310
311;;;
312;;; Gitolite.
313;;;
314
315(define %gitolite-test-admin-keypair
316 (computed-file
317 "gitolite-test-admin-keypair"
318 (with-imported-modules (source-module-closure
319 '((guix build utils)))
320 #~(begin
321 (use-modules (ice-9 match) (srfi srfi-26)
322 (guix build utils))
323
324 (mkdir #$output)
325 (invoke #$(file-append openssh "/bin/ssh-keygen")
326 "-f" (string-append #$output "/test-admin")
327 "-t" "rsa"
328 "-q"
329 "-N" "")))))
330
331(define %gitolite-os
332 (simple-operating-system
333 (service dhcp-client-service-type)
334 (service openssh-service-type)
335 (service gitolite-service-type
336 (gitolite-configuration
337 (admin-pubkey
338 (file-append %gitolite-test-admin-keypair "/test-admin.pub"))))))
339
340(define (run-gitolite-test)
341 (define os
342 (marionette-operating-system
343 %gitolite-os
344 #:imported-modules '((gnu services herd)
345 (guix combinators))))
346
347 (define vm
348 (virtual-machine
349 (operating-system os)
350 (port-forwardings `((2222 . 22)))))
351
352 (define test
353 (with-imported-modules '((gnu build marionette)
354 (guix build utils))
355 #~(begin
356 (use-modules (srfi srfi-64)
357 (rnrs io ports)
358 (gnu build marionette)
359 (guix build utils))
360
361 (define marionette
362 (make-marionette (list #$vm)))
363
364 (test-runner-current (system-test-runner #$output))
365 (test-begin "gitolite")
366
367 ;; Wait for sshd to be up and running.
368 (test-assert "service running"
369 (marionette-eval
370 '(begin
371 (use-modules (gnu services herd))
372 (start-service 'ssh-daemon))
373 marionette))
374
375 (display #$%gitolite-test-admin-keypair)
376
377 (setenv "GIT_SSH_VARIANT" "ssh")
378 (setenv "GIT_SSH_COMMAND"
379 (string-join
380 '(#$(file-append openssh "/bin/ssh")
381 "-i" #$(file-append %gitolite-test-admin-keypair
382 "/test-admin")
383 "-o" "UserKnownHostsFile=/dev/null"
384 "-o" "StrictHostKeyChecking=no")))
385
386 (test-assert "cloning the admin repository"
387 (invoke #$(file-append git "/bin/git")
388 "clone" "-v"
389 "ssh://git@localhost:2222/gitolite-admin"
390 "/tmp/clone"))
391
392 (test-assert "admin key exists"
393 (file-exists? "/tmp/clone/keydir/test-admin.pub"))
394
395 (with-directory-excursion "/tmp/clone"
396 (invoke #$(file-append git "/bin/git")
397 "-c" "user.name=Guix" "-c" "user.email=guix"
398 "commit"
399 "-m" "Test commit"
400 "--allow-empty")
401
402 (test-assert "pushing, and the associated hooks"
403 (invoke #$(file-append git "/bin/git") "push")))
404
405 (test-end))))
406
407 (gexp->derivation "gitolite" test))
408
409(define %test-gitolite
410 (system-test
411 (name "gitolite")
412 (description "Clone the Gitolite admin repository.")
413 (value (run-gitolite-test))))
414
415;;;
416;;; Gitile.
417;;;
418
419(define %gitile-configuration-nginx
420 (nginx-server-configuration
421 (root "/does/not/exists")
422 (try-files (list "$uri" "=404"))
423 (listen '("19418"))
424 (ssl-certificate #f)
425 (ssl-certificate-key #f)))
426
427(define %gitile-os
428 ;; Operating system under test.
429 (simple-operating-system
430 (service dhcp-client-service-type)
431 (simple-service 'srv-git activation-service-type
432 #~(mkdir-p "/srv/git"))
433 (service gitile-service-type
434 (gitile-configuration
435 (base-git-url "http://localhost")
436 (repositories "/srv/git")
437 (nginx %gitile-configuration-nginx)))
438 %test-repository-service))
439
440(define* (run-gitile-test #:optional (http-port 19418))
441 "Run tests in %GITOLITE-OS, which has nginx running and listening on
442HTTP-PORT."
443 (define os
444 (marionette-operating-system
445 %gitile-os
446 #:imported-modules '((gnu services herd)
447 (guix combinators))))
448
449 (define vm
450 (virtual-machine
451 (operating-system os)
452 (port-forwardings `((8081 . ,http-port)))
453 (memory-size 1024)))
454
455 (define test
456 (with-imported-modules '((gnu build marionette))
457 #~(begin
458 (use-modules (srfi srfi-11) (srfi srfi-64)
459 (gnu build marionette)
460 (web uri)
461 (web client)
462 (web response))
463
464 (define marionette
465 (make-marionette (list #$vm)))
466
467 (test-runner-current (system-test-runner #$output))
468 (test-begin "gitile")
469
470 ;; XXX: Shepherd reads the config file *before* binding its control
471 ;; socket, so /var/run/shepherd/socket might not exist yet when the
472 ;; 'marionette' service is started.
473 (test-assert "shepherd socket ready"
474 (marionette-eval
475 `(begin
476 (use-modules (gnu services herd))
477 (let loop ((i 10))
478 (cond ((file-exists? (%shepherd-socket-file))
479 #t)
480 ((> i 0)
481 (sleep 1)
482 (loop (- i 1)))
483 (else
484 'failure))))
485 marionette))
486
487 ;; Wait for nginx to be up and running.
488 (test-assert "nginx running"
489 (marionette-eval
490 '(begin
491 (use-modules (gnu services herd))
492 (start-service 'nginx))
493 marionette))
494
495 ;; Make sure the PID file is created.
496 (test-assert "PID file"
497 (marionette-eval
498 '(file-exists? "/var/run/nginx/pid")
499 marionette))
500
501 ;; Make sure Git test repository is created.
502 (test-assert "Git test repository"
503 (marionette-eval
504 '(file-exists? "/srv/git/test")
505 marionette))
506
507 (sleep 2)
508
509 ;; Make sure we can access pages that correspond to our repository.
510 (letrec-syntax ((test-url
511 (syntax-rules ()
512 ((_ path code)
513 (test-equal (string-append "GET " path)
514 code
515 (let-values (((response body)
516 (http-get (string-append
517 "http://localhost:8081"
518 path))))
519 (response-code response))))
520 ((_ path)
521 (test-url path 200)))))
522 (test-url "/")
523 (test-url "/css/gitile.css")
524 (test-url "/test")
525 (test-url "/test/commits")
526 (test-url "/test/tree" 404)
527 (test-url "/test/tree/-")
528 (test-url "/test/tree/-/README")
529 (test-url "/test/does-not-exist" 404)
530 (test-url "/test/tree/-/does-not-exist" 404)
531 (test-url "/does-not-exist" 404))
532
533 (test-end))))
534
535 (gexp->derivation "gitile-test" test))
536
537(define %test-gitile
538 (system-test
539 (name "gitile")
540 (description "Connect to a running Gitile server.")
541 (value (run-gitile-test))))