projects/guix·blob bootstrap-team · refs

1046 lines · 33.3 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017, 2020-2021, 2023 Ludovic Courtès <[email protected]>
3;;; Copyright © 2017, 2019 Christopher Baines <[email protected]>
4;;; Copyright © 2017, 2018 Clément Lassieur <[email protected]>
5;;; Copyright © 2018 Pierre-Antoine Rouby <[email protected]>
6;;; Copyright © 2018 Marius Bakke <[email protected]>
7;;; Copyright © 2024 Maxim Cournoyer <[email protected]>
8;;; Copyright © 2025 Rodion Goritskov <[email protected]>
9;;;
10;;; This file is part of GNU Guix.
11;;;
12;;; GNU Guix is free software; you can redistribute it and/or modify it
13;;; under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 3 of the License, or (at
15;;; your option) any later version.
16;;;
17;;; GNU Guix is distributed in the hope that it will be useful, but
18;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25(define-module (gnu tests web)
26 #:use-module (gnu tests)
27 #:use-module (gnu system)
28 #:use-module (gnu system file-systems)
29 #:use-module (gnu system shadow)
30 #:use-module (gnu system vm)
31 #:use-module (gnu services)
32 #:use-module (gnu services web)
33 #:use-module (gnu services databases)
34 #:use-module (gnu services getmail)
35 #:use-module (gnu services networking)
36 #:use-module (gnu services shepherd)
37 #:use-module (gnu services mail)
38 #:use-module (gnu packages base)
39 #:use-module (gnu packages databases)
40 #:use-module (gnu packages guile-xyz)
41 #:use-module (gnu packages gnupg)
42 #:use-module (gnu packages patchutils)
43 #:use-module (gnu packages python)
44 #:use-module (gnu packages tls)
45 #:use-module (gnu packages web)
46 #:use-module (guix packages)
47 #:use-module (guix modules)
48 #:use-module (guix records)
49 #:use-module (guix gexp)
50 #:use-module (guix store)
51 #:use-module (guix utils)
52 #:use-module (ice-9 match)
53 #:export (%test-httpd
54 %test-nginx
55 %test-nginx+anonip
56 %test-varnish
57 %test-php-fpm
58 %test-hpcguix-web
59 %test-anonip
60 %test-patchwork
61 %test-agate
62 %test-miniflux-admin-string
63 %test-miniflux-admin-file
64 %test-miniflux-socket))
65
66(define %index.html-contents
67 ;; Contents of the /index.html file.
68 "Hello, guix!")
69
70(define %make-http-root
71 ;; Create our server root in /srv.
72 #~(begin
73 (mkdir "/srv")
74 (mkdir "/srv/http")
75 (call-with-output-file "/srv/http/index.html"
76 (lambda (port)
77 (display #$%index.html-contents port)))))
78
79(define retry-on-error
80 #~(lambda* (f #:key times delay)
81 (let loop ((attempt 1))
82 (match (catch
83 #t
84 (lambda ()
85 (cons #t
86 (f)))
87 (lambda args
88 (cons #f
89 args)))
90 ((#t . return-value)
91 return-value)
92 ((#f . error-args)
93 (if (>= attempt times)
94 (apply throw error-args)
95 (begin
96 (sleep delay)
97 (loop (+ 1 attempt)))))))))
98
99(define* (run-webserver-test name test-os #:key (log-file #f) (http-port 8080)
100 extra-tests)
101 "Run tests in %NGINX-OS, which has nginx running and listening on HTTP-PORT.
102EXTRA-TESTS should be a sexp of gexp containing extra code to run as part of
103the tests."
104 (define os
105 (marionette-operating-system
106 test-os
107 #:imported-modules '((gnu services herd)
108 (guix combinators))))
109
110 (define forwarded-port 8080)
111
112 (define vm
113 (virtual-machine
114 (operating-system os)
115 (port-forwardings `((,http-port . ,forwarded-port)))))
116
117 (define test
118 (with-imported-modules '((gnu build marionette))
119 #~(begin
120 (use-modules (srfi srfi-11) (srfi srfi-64)
121 (gnu build marionette)
122 (web uri)
123 (web client)
124 (web response))
125
126 (define marionette
127 (make-marionette (list #$vm)))
128
129 (test-runner-current (system-test-runner #$output))
130 (test-begin #$name)
131
132 (test-assert #$(string-append name " service running")
133 (marionette-eval
134 '(begin
135 (use-modules (gnu services herd))
136 (match (start-service '#$(string->symbol name))
137 (#f #f)
138 (('service response-parts ...)
139 (match (assq-ref response-parts 'running)
140 ((#t) #t)
141 ((pid) pid)))))
142 marionette))
143
144 (test-assert "HTTP port ready"
145 (wait-for-tcp-port #$forwarded-port marionette))
146
147 ;; Retrieve the index.html file we put in /srv.
148 (test-equal "http-get"
149 '(200 #$%index.html-contents)
150 (let-values
151 (((response text)
152 (http-get #$(simple-format
153 #f "http://localhost:~A/index.html" forwarded-port)
154 #:decode-body? #t)))
155 (list (response-code response) text)))
156
157 #$@(if log-file
158 `((test-assert ,(string-append "log file exists " log-file)
159 (marionette-eval
160 '(file-exists? ,log-file)
161 marionette)))
162 '())
163
164 #$extra-tests
165
166 (test-end))))
167
168 (gexp->derivation (string-append name "-test") test))
169
170
171;;;
172;;; HTTPD
173;;;
174
175(define %httpd-os
176 (simple-operating-system
177 (service dhcpcd-service-type)
178 (service httpd-service-type
179 (httpd-configuration
180 (config
181 (httpd-config-file
182 (listen '("8080"))))))
183 (simple-service 'make-http-root activation-service-type
184 %make-http-root)))
185
186(define %test-httpd
187 (system-test
188 (name "httpd")
189 (description "Connect to a running HTTPD server.")
190 (value (run-webserver-test name %httpd-os
191 #:log-file "/var/log/httpd/error_log"))))
192
193
194;;;
195;;; NGINX
196;;;
197
198(define %nginx-servers
199 ;; Server blocks.
200 (list (nginx-server-configuration
201 (listen '("8080")))))
202
203(define %nginx-os
204 ;; Operating system under test.
205 (simple-operating-system
206 (service dhcpcd-service-type)
207 (service nginx-service-type
208 (nginx-configuration
209 (log-directory "/var/log/nginx")
210 (server-blocks %nginx-servers)))
211 (simple-service 'make-http-root activation-service-type
212 %make-http-root)))
213
214(define %test-nginx
215 (system-test
216 (name "nginx")
217 (description "Connect to a running NGINX server.")
218 (value (run-webserver-test name %nginx-os
219 #:log-file "/var/log/nginx/access.log"))))
220
221(define %nginx+anonip-os
222 (simple-operating-system
223 (service dhcpcd-service-type)
224 (service anonip-service-type
225 (anonip-configuration
226 (input "/var/run/anonip/access.log")
227 (output "/var/log/anonip/access.log")
228 (debug? #t)))
229 (service nginx-service-type
230 (nginx-configuration
231 (log-directory "/var/run/anonip/")
232 (server-blocks %nginx-servers)
233 (shepherd-requirement '(anonip-/var/log/anonip/access.log))))
234 (simple-service 'make-http-root activation-service-type
235 %make-http-root)))
236
237(define nginx-anonip-tests
238 #~(test-assert "anonip service is running"
239 (marionette-eval
240 '(begin
241 (use-modules (gnu services herd))
242 (wait-for-service 'anonip-/var/log/anonip/access.log))
243 marionette)))
244
245(define %test-nginx+anonip
246 (system-test
247 (name "nginx+anonip")
248 (description "Run a NGINX server with logs anonymized by Anonip")
249 (value (run-webserver-test "nginx" %nginx+anonip-os
250 #:log-file "/var/log/anonip/access.log"
251 #:extra-tests nginx-anonip-tests))))
252
253
254;;;
255;;; Varnish
256;;;
257
258(define %varnish-vcl
259 (mixed-text-file
260 "varnish-test.vcl"
261 "vcl 4.0;
262backend dummy { .host = \"127.1.1.1\"; }
263sub vcl_recv { return(synth(200, \"OK\")); }
264sub vcl_synth {
265 synthetic(\"" %index.html-contents "\");
266 set resp.http.Content-Type = \"text/plain\";
267 return(deliver);
268}"))
269
270(define %varnish-os
271 (simple-operating-system
272 (service dhcpcd-service-type)
273 ;; Pretend to be a web server that serves %index.html-contents.
274 (service varnish-service-type
275 (varnish-configuration
276 (name "/tmp/server")
277 ;; Use a small VSL buffer to fit in the test VM.
278 (parameters '(("vsl_space" . "4M")))
279 (vcl %varnish-vcl)))
280 ;; Proxy the "server" using the builtin configuration.
281 (service varnish-service-type
282 (varnish-configuration
283 (parameters '(("vsl_space" . "4M")))
284 (backend "localhost:80")
285 (listen '(":8080"))))))
286
287(define %test-varnish
288 (system-test
289 (name "varnish")
290 (description "Test the Varnish Cache server.")
291 (value (run-webserver-test "varnish-default" %varnish-os))))
292
293
294;;;
295;;; PHP-FPM
296;;;
297
298(define %make-php-fpm-http-root
299 ;; Create our server root in /srv.
300 #~(begin
301 (mkdir "/srv")
302 (call-with-output-file "/srv/index.php"
303 (lambda (port)
304 (display "<?php
305phpinfo();
306echo(\"Computed by php:\".((string)(2+3)));
307?>\n" port)))))
308
309(define %php-fpm-nginx-server-blocks
310 (list (nginx-server-configuration
311 (root "/srv")
312 (locations
313 (list (nginx-php-location)))
314 (listen '("8042"))
315 (ssl-certificate #f)
316 (ssl-certificate-key #f))))
317
318(define %php-fpm-os
319 ;; Operating system under test.
320 (simple-operating-system
321 (service dhcpcd-service-type)
322 (service php-fpm-service-type)
323 (service nginx-service-type
324 (nginx-configuration
325 (server-blocks %php-fpm-nginx-server-blocks)))
326 (simple-service 'make-http-root activation-service-type
327 %make-php-fpm-http-root)))
328
329(define* (run-php-fpm-test #:optional (http-port 8042))
330 "Run tests in %PHP-FPM-OS, which has nginx running and listening on
331HTTP-PORT, along with php-fpm."
332 (define os
333 (marionette-operating-system
334 %php-fpm-os
335 #:imported-modules '((gnu services herd)
336 (guix combinators))))
337
338 (define vm
339 (virtual-machine
340 (operating-system os)
341 (port-forwardings `((8080 . ,http-port)))))
342
343 (define test
344 (with-imported-modules '((gnu build marionette)
345 (guix build utils))
346 #~(begin
347 (use-modules (srfi srfi-11) (srfi srfi-64)
348 (gnu build marionette)
349 (web uri)
350 (web client)
351 (web response))
352
353 (define marionette
354 (make-marionette (list #$vm)))
355
356 (test-runner-current (system-test-runner #$output))
357 (test-begin "php-fpm")
358
359 (test-assert "php-fpm running"
360 (marionette-eval
361 '(begin
362 (use-modules (gnu services herd))
363 (match (start-service 'php-fpm)
364 (#f #f)
365 (('service response-parts ...)
366 (match (assq-ref response-parts 'running)
367 ((pid) pid)))))
368 marionette))
369
370 (test-assert "nginx running"
371 (marionette-eval
372 '(begin
373 (use-modules (gnu services herd))
374 (start-service 'nginx))
375 marionette))
376
377 (test-equal "http-get"
378 200
379 (let-values (((response text)
380 (http-get "http://localhost:8080/index.php"
381 #:decode-body? #t)))
382 (response-code response)))
383
384 (test-equal "php computed result is sent"
385 "Computed by php:5"
386 (let-values (((response text)
387 (http-get "http://localhost:8080/index.php"
388 #:decode-body? #t)))
389 (begin
390 (use-modules (ice-9 regex))
391 (let ((matches (string-match "Computed by php:5" text)))
392 (and matches
393 (match:substring matches 0))))))
394
395 (test-end))))
396
397 (gexp->derivation "php-fpm-test" test))
398
399(define %test-php-fpm
400 (system-test
401 (name "php-fpm")
402 (description "Test PHP-FPM through nginx.")
403 (value (run-php-fpm-test))))
404
405
406;;;
407;;; hpcguix-web
408;;;
409
410(define* (run-hpcguix-web-server-test name test-os)
411 "Run tests in %HPCGUIX-WEB-OS, which has hpcguix-web running."
412 (define os
413 (marionette-operating-system
414 test-os
415 #:imported-modules '((gnu services herd)
416 (guix combinators))))
417
418 (define vm
419 (virtual-machine
420 (operating-system os)
421 (port-forwardings '((8080 . 5000)))
422 (memory-size 1024)))
423
424 (define test
425 (with-imported-modules '((gnu build marionette))
426 #~(begin
427 (use-modules (srfi srfi-11) (srfi srfi-64)
428 (ice-9 match)
429 (gnu build marionette)
430 (web uri)
431 (web client)
432 (web response))
433
434 (define marionette
435 (make-marionette (list #$vm)))
436
437 (test-runner-current (system-test-runner #$output))
438 (test-begin #$name)
439
440 (test-assert "hpcguix-web running"
441 (marionette-eval
442 '(begin
443 (use-modules (gnu services herd))
444 (match (start-service 'hpcguix-web)
445 (#f #f)
446 (('service response-parts ...)
447 (match (assq-ref response-parts 'running)
448 ((pid) pid)))))
449 marionette))
450
451 (test-equal "http-get"
452 200
453 (begin
454 (wait-for-tcp-port 5000 marionette)
455 (#$retry-on-error
456 (lambda ()
457 (let-values (((response text)
458 (http-get "http://localhost:8080")))
459 (response-code response)))
460 #:times 10
461 #:delay 5)))
462
463 (test-end))))
464
465 (gexp->derivation (string-append name "-test") test))
466
467(define %hpcguix-web-specs
468 ;; Server config gexp.
469 #~(hpcweb-configuration
470 (title-prefix "[TEST] HPCGUIX-WEB")))
471
472(define %hpcguix-web-os
473 (simple-operating-system
474 (service dhcpcd-service-type)
475 (service hpcguix-web-service-type
476 (hpcguix-web-configuration
477 (specs %hpcguix-web-specs)
478 (address "0.0.0.0")))))
479
480(define %test-hpcguix-web
481 (system-test
482 (name "hpcguix-web")
483 (description "Connect to a running hpcguix-web server.")
484 (value (run-hpcguix-web-server-test name %hpcguix-web-os))))
485
486
487;;;
488;;; Anonip
489;;;
490(define %anonip-os
491 ;; Operating system under test.
492 (simple-operating-system
493 (service anonip-service-type
494 (anonip-configuration
495 (input "/var/run/anonip/access.log")
496 (output "/var/log/anonip/access.log")
497 (debug? #t)))))
498
499(define (run-anonip-test)
500 (define os
501 (marionette-operating-system
502 %anonip-os
503 #:imported-modules '((gnu services herd)
504 (guix combinators))))
505
506 (define vm
507 (virtual-machine
508 (operating-system os)
509 ;; We are interested in verifying if anonip still launches following a
510 ;; reboot; thus make the base image writable.
511 (volatile? #f)))
512
513 (define test
514 (with-imported-modules '((gnu build marionette))
515 #~(begin
516 (use-modules (ice-9 match)
517 (srfi srfi-64)
518 (gnu build marionette))
519
520 (define marionette
521 (make-marionette (list #$vm)))
522
523 (test-runner-current (system-test-runner #$output))
524 (test-begin "anonip")
525
526 (test-assert "service is running"
527 (marionette-eval
528 '(begin
529 (use-modules (gnu services herd))
530 (wait-for-service 'anonip-/var/log/anonip/access.log))
531 marionette))
532
533 (test-assert "service can be restarted"
534 (marionette-eval
535 '(begin
536 (use-modules (gnu services herd))
537 (restart-service 'anonip-/var/log/anonip/access.log)
538 (wait-for-service 'anonip-/var/log/anonip/access.log))
539 marionette))
540
541 (test-assert "ip addresses are anonymized"
542 (marionette-eval
543 '(begin
544 (use-modules (ice-9 textual-ports))
545 (call-with-output-file "/var/run/anonip/access.log"
546 (lambda (port)
547 (display "192.168.100.200 - - \
548[30/Oct/2024:14:57:44 +0100] GET /xxx.narinfo HTTP/1.1\" 200 1065 \
549\"-\" \"GNU Guile\"\n" port)
550 (display "2001:0db8:85a3:0000:0000:8a2e:0370:7334 - - \
551[30/Oct/2024:14:57:44 +0100] \"GET /xxx.narinfo HTTP/1.1\" 200 1065 \
552\"-\" \"GNU Guile\"\n" port)))
553 (#$retry-on-error
554 (lambda ()
555 (call-with-input-file "/var/log/anonip/access.log"
556 (lambda (port)
557 (let ((content (get-string-all port)))
558 ;; The expected values are taken from anonip's test
559 ;; suite (see its test_module.py file).
560 (or (and (string-contains content "192.168.96.0")
561 (string-contains content "2001:db8:85a0::"))
562 (error "could not find expected anonymized IPs"
563 content))))))
564 #:times 20
565 #:delay 1))
566 marionette))
567
568 (test-assert "service is running after reboot"
569 (begin
570 (marionette-eval
571 '(begin
572 (use-modules (gnu services herd))
573 (eval-there '(begin
574 (use-modules (shepherd system))
575 (sync) ;ensure the log is fully written
576 (reboot))))
577 marionette)
578 ;; Note: a distinct marionette-eval call is needed here; if
579 ;; included in the previous one issuing the reboot,
580 ;; 'wait-for-service' would apparently run before the system had
581 ;; rebooted (and succeed), which would defeat the test.
582 (marionette-eval
583 '(begin
584 (use-modules (gnu services herd))
585 (wait-for-service 'anonip-/var/log/anonip/access.log))
586 marionette)))
587
588 (test-assert "service can be stopped"
589 (marionette-eval
590 '(begin
591 (use-modules (gnu services herd))
592 (stop-service 'anonip-/var/log/anonip/access.log))
593 marionette))
594
595 (test-end))))
596
597 (gexp->derivation "anonip-test" test))
598
599(define %test-anonip
600 (system-test
601 (name "anonip")
602 (description "Anonymize logs via Anonip")
603 (value (run-anonip-test))))
604
605
606;;;
607;;; Patchwork
608;;;
609
610(define (patchwork-initial-database-setup-service configuration)
611 (define start-gexp
612 #~(primitive-exit
613 (if (and
614 (zero?
615 (system* #$(file-append postgresql "/bin/createuser")
616 #$(patchwork-database-configuration-user
617 configuration)))
618 (zero?
619 (system* #$(file-append postgresql "/bin/createdb")
620 "-O"
621 #$(patchwork-database-configuration-user
622 configuration)
623 #$(patchwork-database-configuration-name
624 configuration))))
625 0
626 1)))
627
628 (shepherd-service
629 (requirement '(postgres))
630 (provision '(patchwork-postgresql-user-and-database))
631 (start #~(lambda _
632 (zero? (spawn-command
633 '(#$(program-file "patchwork-initial-database-setup"
634 start-gexp))
635 #:user "postgres"
636 #:group "postgres"))))
637 (stop #~(const #f))
638 (documentation "Setup patchwork database.")))
639
640(define (patchwork-os patchwork)
641 (simple-operating-system
642 (service dhcpcd-service-type)
643 (service httpd-service-type
644 (httpd-configuration
645 (config
646 (httpd-config-file
647 (listen '("8080"))))))
648 (service postgresql-service-type
649 (postgresql-configuration
650 (postgresql postgresql)))
651 (service patchwork-service-type
652 (patchwork-configuration
653 (patchwork patchwork)
654 (domain "localhost")
655 (settings-module
656 (patchwork-settings-module
657 (allowed-hosts (list domain))
658 (default-from-email "")))
659 (getmail-retriever-config
660 (getmail-retriever-configuration
661 (type "SimpleIMAPSSLRetriever")
662 (server "imap.example.com")
663 (port 993)
664 (username "username")
665 (password "password")
666 (extra-parameters
667 '((mailboxes . ("INBOX"))))))))
668 (simple-service 'patchwork-database-setup
669 shepherd-root-service-type
670 (list
671 (patchwork-initial-database-setup-service
672 (patchwork-database-configuration))))))
673
674(define (run-patchwork-test patchwork)
675 "Run tests in %NGINX-OS, which has nginx running and listening on
676HTTP-PORT."
677 (define os
678 (marionette-operating-system
679 (patchwork-os patchwork)
680 #:imported-modules '((gnu services herd)
681 (guix combinators))))
682
683 (define forwarded-port 8080)
684
685 (define vm
686 (virtual-machine
687 (operating-system os)
688 (port-forwardings `((8080 . ,forwarded-port)))
689 (memory-size 1024)))
690
691 (define test
692 (with-imported-modules '((gnu build marionette))
693 #~(begin
694 (use-modules (srfi srfi-11) (srfi srfi-64)
695 (ice-9 match)
696 (gnu build marionette)
697 (web uri)
698 (web client)
699 (web response))
700
701 (define marionette
702 (make-marionette (list #$vm)))
703
704 (test-runner-current (system-test-runner #$output))
705 (test-begin "patchwork")
706
707 (test-assert "patchwork-postgresql-user-and-service started"
708 (marionette-eval
709 '(begin
710 (use-modules (gnu services herd))
711 (match (start-service 'patchwork-postgresql-user-and-database)
712 (#f #f)
713 (('service response-parts ...)
714 (match (assq-ref response-parts 'running)
715 ((#t) #t)
716 ((pid) pid)))))
717 marionette))
718
719 (test-assert "patchwork-setup started"
720 (marionette-eval
721 '(begin
722 (use-modules (gnu services herd))
723 (match (start-service 'patchwork-setup)
724 (#f #f)
725 (('service response-parts ...)
726 (match (assq-ref response-parts 'running)
727 ((#t) #t)
728 ((pid) pid)))))
729 marionette))
730
731 (test-assert "httpd running"
732 (marionette-eval
733 '(begin
734 (use-modules (gnu services herd))
735 (start-service 'httpd))
736 marionette))
737
738 (test-equal "http-get"
739 200
740 (#$retry-on-error
741 (lambda ()
742 (let-values
743 (((response text)
744 (http-get #$(simple-format
745 #f "http://localhost:~A/" forwarded-port)
746 #:decode-body? #t)))
747 (response-code response)))
748 #:times 10
749 #:delay 5))
750
751 (test-end))))
752
753 (gexp->derivation "patchwork-test" test))
754
755(define %test-patchwork
756 (system-test
757 (name "patchwork")
758 (description "Connect to a running Patchwork service.")
759 (value (run-patchwork-test patchwork))))
760
761
762;;;
763;;; Agate
764;;;
765
766(define %index.gmi-contents
767 ;; Contents of the /index.gmi file.
768 "Hello, guix!")
769
770(define %make-agate-root
771 ;; Create our server root in /srv.
772 #~(begin
773 (mkdir "/srv")
774 (mkdir "/srv/gemini")
775 (mkdir "/srv/gemini-certs")
776 ;; Directory should be writable for Agate user to generate certificates
777 (let ((user (getpw "agate")))
778 (chown "/srv/gemini-certs" (passwd:uid user) (passwd:gid user)))
779 (call-with-output-file (string-append "/srv/gemini/index.gmi")
780 (lambda (port)
781 (display #$%index.gmi-contents port)))))
782
783(define %agate-os
784 (simple-operating-system
785 (service dhcpcd-service-type)
786 (simple-service 'make-agate-root activation-service-type
787 %make-agate-root)
788 (service agate-service-type
789 (agate-configuration
790 (hostnames '("localhost"))))))
791
792(define* (run-agate-test name test-os expected-content)
793 (define os
794 (marionette-operating-system
795 test-os
796 #:imported-modules '((gnu services herd)
797 (guix combinators))
798 #:extensions (list guile-gemini guile-gnutls)))
799
800 (define forwarded-port 1965)
801
802 (define vm
803 (virtual-machine
804 (operating-system os)
805 (port-forwardings `((1965 . ,forwarded-port)))))
806
807 (define test
808 (with-imported-modules '((gnu build marionette))
809 #~(begin
810 (use-modules (srfi srfi-64)
811 (gnu build marionette))
812
813 (define marionette
814 (make-marionette (list #$vm)))
815
816 (test-runner-current (system-test-runner #$output))
817 (test-begin #$name)
818
819 (test-assert #$(string-append name " service running")
820 (marionette-eval
821 '(begin
822 (use-modules (gnu services herd))
823 (match (start-service '#$(string->symbol name))
824 (#f #f)
825 (('service response-parts ...)
826 (match (assq-ref response-parts 'running)
827 ((#t) #t)
828 ((pid) pid)))))
829 marionette))
830
831 (test-assert "Agate TCP port ready, IPv4"
832 (wait-for-tcp-port #$forwarded-port marionette))
833
834 (test-assert "Agate TCP port ready, IPv6"
835 (wait-for-tcp-port #$forwarded-port marionette
836 #:address
837 '(make-socket-address
838 AF_INET6 (inet-pton AF_INET6 "::1") #$forwarded-port)))
839
840 (test-equal "Agate responses with the specified index.gmi"
841 #$expected-content
842 (marionette-eval '(begin
843 (use-modules (ice-9 iconv)
844 (gemini client)
845 (gemini request)
846 (gemini response))
847 (bytevector->string (gemini-response-body-bytes
848 (send-gemini-request
849 (build-gemini-request #:host "localhost" #:port #$forwarded-port)))
850 "utf8")) marionette))
851
852 (test-end))))
853 (gexp->derivation "agate-test" test))
854
855(define %test-agate
856 (system-test
857 (name "agate")
858 (description "Connect to a running Agate service.")
859 (value (run-agate-test name %agate-os %index.gmi-contents))))
860
861
862;;;
863;;; Miniflux
864;;;
865
866(define %miniflux-create-admin-credentials
867 #~(begin
868 (mkdir "/var/miniflux")
869 (call-with-output-file "/var/miniflux/admin-username"
870 (lambda (port)
871 (display "test" port)))
872 (call-with-output-file "/var/miniflux/admin-password"
873 (lambda (port)
874 (display "testpassword" port)))))
875
876(define miniflux-base-system
877 (lambda (miniflux-config)
878 (simple-operating-system
879 (simple-service 'create-admin-credentials
880 activation-service-type
881 %miniflux-create-admin-credentials)
882 (service dhcpcd-service-type)
883 (service postgresql-service-type
884 (postgresql-configuration
885 (postgresql postgresql-13)))
886 (service miniflux-service-type
887 miniflux-config))))
888
889(define %miniflux-with-admin-as-string
890 (miniflux-base-system
891 (miniflux-configuration
892 (listen-address "0.0.0.0:8080")
893 (create-administrator-account? #t)
894 (administrator-account-name "test")
895 (administrator-account-password "testpassword"))))
896
897(define %miniflux-with-admin-as-file
898 (miniflux-base-system
899 (miniflux-configuration
900 (listen-address "0.0.0.0:8080")
901 (create-administrator-account? #t)
902 (administrator-account-name "/var/miniflux/admin-username")
903 (administrator-account-password "/var/miniflux/admin-password"))))
904
905(define %miniflux-with-socket
906 (miniflux-base-system
907 (miniflux-configuration
908 (listen-address "/var/run/miniflux/miniflux.sock"))))
909
910(define* (run-miniflux-test name test-os)
911 (define os
912 (marionette-operating-system
913 test-os
914 #:imported-modules '((gnu services herd)
915 (guix combinators))))
916
917 (define forwarded-port 8080)
918
919 (define vm
920 (virtual-machine
921 (operating-system os)
922 (memory-size 512)
923 (port-forwardings `((8080 . ,forwarded-port)))))
924
925 (define test
926 (with-extensions (list guile-gcrypt)
927 (with-imported-modules '((gnu build marionette))
928 #~(begin
929 (use-modules (srfi srfi-64)
930 (srfi srfi-11)
931 (gnu build marionette)
932 (web client)
933 (web uri)
934 (web response)
935 (ice-9 match)
936 (ice-9 iconv)
937 (gcrypt base64))
938
939 (define marionette
940 (make-marionette (list #$vm)))
941
942 (test-runner-current (system-test-runner #$output))
943 (test-begin #$name)
944
945 (test-assert "Check Miniflux service is running"
946 (begin
947 (#$retry-on-error
948 (lambda ()
949 (marionette-eval
950 '(begin
951 (use-modules (gnu services herd))
952 (match (start-service '#$(string->symbol "miniflux"))
953 (#f #f)
954 (('service response-parts ...)
955 (match (assq-ref response-parts 'running)
956 (#f #f)
957 ((running) #t)))))
958 marionette))
959 #:delay 1
960 #:times 10)))
961
962 (test-assert "Miniflux TCP port ready, IPv4"
963 (wait-for-tcp-port #$forwarded-port marionette))
964
965 (test-assert "Miniflux login page is opened"
966 (begin
967 (wait-for-tcp-port #$forwarded-port marionette)
968 (#$retry-on-error
969 (lambda ()
970 (let-values (((_ text)
971 (http-get
972 #$(format #f "http://localhost:~A/" forwarded-port)
973 #:decode-body? #t)))
974 (string-contains text "<title>Sign In - Miniflux</title>")))
975 #:times 10
976 #:delay 2)))
977
978 (define authorization-header
979 (let ((encoded (base64-encode (string->bytevector "test:testpassword" "utf-8"))))
980 `(authorization . (basic . ,encoded))))
981
982 (test-equal "Miniflux initial admin API call is successful"
983 200
984 (begin
985 (wait-for-tcp-port #$forwarded-port marionette)
986 (#$retry-on-error
987 (lambda ()
988 (let-values (((response _)
989 (http-get #$(format #f "http://localhost:~A/v1/me" forwarded-port)
990 #:headers (list authorization-header)
991 #:decode-body? #t)))
992
993 (response-code response)))
994 #:times 10
995 #:delay 2)))
996
997 (test-end)))))
998 (gexp->derivation (string-append name "-test") test))
999
1000(define* (run-miniflux-socket-test name test-os)
1001 (define os
1002 (marionette-operating-system
1003 test-os
1004 #:imported-modules '((gnu services herd)
1005 (guix combinators))))
1006
1007 (define vm
1008 (virtual-machine
1009 (operating-system os)
1010 (memory-size 512)))
1011
1012 (define test
1013 (with-imported-modules '((gnu build marionette))
1014 #~(begin
1015 (use-modules (srfi srfi-64)
1016 (gnu build marionette))
1017
1018 (define marionette
1019 (make-marionette (list #$vm)))
1020
1021 (test-runner-current (system-test-runner #$output))
1022 (test-begin #$name)
1023
1024 (test-assert "Check socket file is created"
1025 (wait-for-unix-socket "/var/run/miniflux/miniflux.sock" marionette))
1026
1027 (test-end))))
1028 (gexp->derivation (string-append name "-test") test))
1029
1030(define %test-miniflux-admin-string
1031 (system-test
1032 (name "miniflux-admin-string")
1033 (description "Run Miniflux with initial admin credentials as string.")
1034 (value (run-miniflux-test name %miniflux-with-admin-as-string))))
1035
1036(define %test-miniflux-admin-file
1037 (system-test
1038 (name "miniflux-admin-file")
1039 (description "Run Miniflux with initial admin credentials as file.")
1040 (value (run-miniflux-test name %miniflux-with-admin-as-file))))
1041
1042(define %test-miniflux-socket
1043 (system-test
1044 (name "miniflux-socket")
1045 (description "Run Miniflux on unix socket.")
1046 (value (run-miniflux-socket-test name %miniflux-with-socket))))