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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu tests web)
22 #:use-module (gnu tests)
23 #:use-module (gnu system)
24 #:use-module (gnu system file-systems)
25 #:use-module (gnu system shadow)
26 #:use-module (gnu system vm)
27 #:use-module (gnu services)
28 #:use-module (gnu services web)
29 #:use-module (gnu services networking)
30 #:use-module (guix gexp)
31 #:use-module (guix store)
35 (define %index.html-contents
36 ;; Contents of the /index.html file served by nginx.
39 (define %make-http-root
40 ;; Create our server root in /srv.
43 (call-with-output-file "/srv/index.html"
45 (display #$%index.html-contents port)))))
47 (define %nginx-servers
49 (list (nginx-server-configuration
51 (listen '("8042" "443 ssl")))))
54 ;; Operating system under test.
55 (simple-operating-system
57 (service nginx-service-type
59 (log-directory "/var/log/nginx")
60 (server-blocks %nginx-servers)))
61 (simple-service 'make-http-root activation-service-type
64 (define* (run-nginx-test #:optional (http-port 8042))
65 "Run tests in %NGINX-OS, which has nginx running and listening on
68 (marionette-operating-system
70 #:imported-modules '((gnu services herd)
76 (port-forwardings `((8080 . ,http-port)))))
79 (with-imported-modules '((gnu build marionette))
81 (use-modules (srfi srfi-11) (srfi srfi-64)
82 (gnu build marionette)
88 (make-marionette (list #$vm)))
95 ;; Wait for nginx to be up and running.
96 (test-eq "service running"
100 (use-modules (gnu services herd))
101 (start-service 'nginx)
105 ;; Make sure the PID file is created.
106 (test-assert "PID file"
108 '(file-exists? "/var/run/nginx/pid")
111 ;; Retrieve the index.html file we put in /srv.
112 (test-equal "http-get"
113 '(200 #$%index.html-contents)
114 (let-values (((response text)
115 (http-get "http://localhost:8080/index.html"
117 (list (response-code response) text)))
119 ;; There should be a log file in here.
120 (test-assert "log file"
122 '(file-exists? "/var/log/nginx/access.log")
126 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
128 (gexp->derivation "nginx-test" test))
133 (description "Connect to a running NGINX server.")
134 (value (run-nginx-test))))
141 (define %make-php-fpm-http-root
142 ;; Create our server root in /srv.
145 (call-with-output-file "/srv/index.php"
149 echo(\"Computed by php:\".((string)(2+3)));
152 (define %php-fpm-nginx-server-blocks
153 (list (nginx-server-configuration
156 (list (nginx-php-location)))
159 (ssl-certificate-key #f))))
162 ;; Operating system under test.
163 (simple-operating-system
164 (dhcp-client-service)
165 (service php-fpm-service-type)
166 (service nginx-service-type
168 (server-blocks %php-fpm-nginx-server-blocks)))
169 (simple-service 'make-http-root activation-service-type
170 %make-php-fpm-http-root)))
172 (define* (run-php-fpm-test #:optional (http-port 8042))
173 "Run tests in %PHP-FPM-OS, which has nginx running and listening on
174 HTTP-PORT, along with php-fpm."
176 (marionette-operating-system
178 #:imported-modules '((gnu services herd)
179 (guix combinators))))
183 (operating-system os)
184 (port-forwardings `((8080 . ,http-port)))))
187 (with-imported-modules '((gnu build marionette)
190 (use-modules (srfi srfi-11) (srfi srfi-64)
191 (gnu build marionette)
197 (make-marionette (list #$vm)))
202 (test-begin "php-fpm")
204 (test-assert "php-fpm running"
207 (use-modules (gnu services herd))
208 (match (start-service 'php-fpm)
210 (('service response-parts ...)
211 (match (assq-ref response-parts 'running)
212 ((pid) (number? pid))))))
215 (test-eq "nginx running"
219 (use-modules (gnu services herd))
220 (start-service 'nginx)
224 (test-equal "http-get"
226 (let-values (((response text)
227 (http-get "http://localhost:8080/index.php"
229 (response-code response)))
231 (test-equal "php computed result is sent"
233 (let-values (((response text)
234 (http-get "http://localhost:8080/index.php"
237 (use-modules (ice-9 regex))
238 (let ((matches (string-match "Computed by php:5" text)))
240 (match:substring matches 0))))))
244 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
246 (gexp->derivation "php-fpm-test" test))
248 (define %test-php-fpm
251 (description "Test PHP-FPM through nginx.")
252 (value (run-php-fpm-test))))