1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
4 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
5 ;;; Copyright © 2018 Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
22 (define-module (gnu tests web)
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 web)
30 #:use-module (gnu services networking)
31 #:use-module (guix gexp)
32 #:use-module (guix store)
38 (define %index.html-contents
39 ;; Contents of the /index.html file.
42 (define %make-http-root
43 ;; Create our server root in /srv.
47 (call-with-output-file "/srv/http/index.html"
49 (display #$%index.html-contents port)))))
51 (define* (run-webserver-test name test-os #:key (log-file #f) (http-port 8080))
52 "Run tests in %NGINX-OS, which has nginx running and listening on
55 (marionette-operating-system
57 #:imported-modules '((gnu services herd)
60 (define forwarded-port 8080)
65 (port-forwardings `((,http-port . ,forwarded-port)))))
68 (with-imported-modules '((gnu build marionette))
70 (use-modules (srfi srfi-11) (srfi srfi-64)
71 (gnu build marionette)
77 (make-marionette (list #$vm)))
84 (test-assert #$(string-append name " service running")
87 (use-modules (gnu services herd))
88 (match (start-service '#$(string->symbol name))
90 (('service response-parts ...)
91 (match (assq-ref response-parts 'running)
93 ((pid) (number? pid))))))
96 ;; Retrieve the index.html file we put in /srv.
97 (test-equal "http-get"
98 '(200 #$%index.html-contents)
101 (http-get #$(simple-format
102 #f "http://localhost:~A/index.html" forwarded-port)
104 (list (response-code response) text)))
107 `((test-assert ,(string-append "log file exists " log-file)
109 '(file-exists? ,log-file)
114 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
116 (gexp->derivation (string-append name "-test") test))
124 (simple-operating-system
125 (dhcp-client-service)
126 (service httpd-service-type
130 (listen '("8080"))))))
131 (simple-service 'make-http-root activation-service-type
137 (description "Connect to a running HTTPD server.")
138 (value (run-webserver-test name %httpd-os
139 #:log-file "/var/log/httpd/error_log"))))
146 (define %nginx-servers
148 (list (nginx-server-configuration
149 (listen '("8080")))))
152 ;; Operating system under test.
153 (simple-operating-system
154 (dhcp-client-service)
155 (service nginx-service-type
157 (log-directory "/var/log/nginx")
158 (server-blocks %nginx-servers)))
159 (simple-service 'make-http-root activation-service-type
165 (description "Connect to a running NGINX server.")
166 (value (run-webserver-test name %nginx-os
167 #:log-file "/var/log/nginx/access.log"))))
174 (define %make-php-fpm-http-root
175 ;; Create our server root in /srv.
178 (call-with-output-file "/srv/index.php"
182 echo(\"Computed by php:\".((string)(2+3)));
185 (define %php-fpm-nginx-server-blocks
186 (list (nginx-server-configuration
189 (list (nginx-php-location)))
192 (ssl-certificate-key #f))))
195 ;; Operating system under test.
196 (simple-operating-system
197 (dhcp-client-service)
198 (service php-fpm-service-type)
199 (service nginx-service-type
201 (server-blocks %php-fpm-nginx-server-blocks)))
202 (simple-service 'make-http-root activation-service-type
203 %make-php-fpm-http-root)))
205 (define* (run-php-fpm-test #:optional (http-port 8042))
206 "Run tests in %PHP-FPM-OS, which has nginx running and listening on
207 HTTP-PORT, along with php-fpm."
209 (marionette-operating-system
211 #:imported-modules '((gnu services herd)
212 (guix combinators))))
216 (operating-system os)
217 (port-forwardings `((8080 . ,http-port)))))
220 (with-imported-modules '((gnu build marionette)
223 (use-modules (srfi srfi-11) (srfi srfi-64)
224 (gnu build marionette)
230 (make-marionette (list #$vm)))
235 (test-begin "php-fpm")
237 (test-assert "php-fpm running"
240 (use-modules (gnu services herd))
241 (match (start-service 'php-fpm)
243 (('service response-parts ...)
244 (match (assq-ref response-parts 'running)
245 ((pid) (number? pid))))))
248 (test-eq "nginx running"
252 (use-modules (gnu services herd))
253 (start-service 'nginx)
257 (test-equal "http-get"
259 (let-values (((response text)
260 (http-get "http://localhost:8080/index.php"
262 (response-code response)))
264 (test-equal "php computed result is sent"
266 (let-values (((response text)
267 (http-get "http://localhost:8080/index.php"
270 (use-modules (ice-9 regex))
271 (let ((matches (string-match "Computed by php:5" text)))
273 (match:substring matches 0))))))
277 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
279 (gexp->derivation "php-fpm-test" test))
281 (define %test-php-fpm
284 (description "Test PHP-FPM through nginx.")
285 (value (run-php-fpm-test))))
292 (define* (run-hpcguix-web-server-test name test-os)
293 "Run tests in %HPCGUIX-WEB-OS, which has hpcguix-web running."
295 (marionette-operating-system
297 #:imported-modules '((gnu services herd)
298 (guix combinators))))
302 (operating-system os)
303 (port-forwardings '((8080 . 5000)))))
306 (with-imported-modules '((gnu build marionette))
308 (use-modules (srfi srfi-11) (srfi srfi-64)
309 (gnu build marionette)
315 (make-marionette (list #$vm)))
322 (test-assert "hpcguix-web running"
325 (use-modules (gnu services herd))
326 (match (start-service 'hpcguix-web)
328 (('service response-parts ...)
329 (match (assq-ref response-parts 'running)
330 ((pid) (number? pid))))))
333 (test-equal "http-get"
336 (wait-for-tcp-port 5000 marionette)
337 (let-values (((response text)
338 (http-get "http://localhost:8080")))
339 (response-code response))))
342 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
344 (gexp->derivation (string-append name "-test") test))
346 (define %hpcguix-web-specs
347 ;; Server config gexp.
348 #~(define site-config
349 (hpcweb-configuration
350 (title-prefix "[TEST] HPCGUIX-WEB"))))
352 (define %hpcguix-web-os
353 (simple-operating-system
354 (dhcp-client-service)
355 (service hpcguix-web-service-type
356 (hpcguix-web-configuration
357 (specs %hpcguix-web-specs)))))
359 (define %test-hpcguix-web
362 (description "Connect to a running hpcguix-web server.")
363 (value (run-hpcguix-web-server-test name %hpcguix-web-os))))