Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / tests / web.scm
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 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
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)
32 #:export (%test-nginx
33 %test-php-fpm))
34
35 (define %index.html-contents
36 ;; Contents of the /index.html file served by nginx.
37 "Hello, nginx!")
38
39 (define %make-http-root
40 ;; Create our server root in /srv.
41 #~(begin
42 (mkdir "/srv")
43 (call-with-output-file "/srv/index.html"
44 (lambda (port)
45 (display #$%index.html-contents port)))))
46
47 (define %nginx-servers
48 ;; Server blocks.
49 (list (nginx-server-configuration
50 (root "/srv")
51 (listen '("8042" "443 ssl")))))
52
53 (define %nginx-os
54 ;; Operating system under test.
55 (simple-operating-system
56 (dhcp-client-service)
57 (service nginx-service-type
58 (nginx-configuration
59 (log-directory "/var/log/nginx")
60 (server-blocks %nginx-servers)))
61 (simple-service 'make-http-root activation-service-type
62 %make-http-root)))
63
64 (define* (run-nginx-test #:optional (http-port 8042))
65 "Run tests in %NGINX-OS, which has nginx running and listening on
66 HTTP-PORT."
67 (define os
68 (marionette-operating-system
69 %nginx-os
70 #:imported-modules '((gnu services herd)
71 (guix combinators))))
72
73 (define vm
74 (virtual-machine
75 (operating-system os)
76 (port-forwardings `((8080 . ,http-port)))))
77
78 (define test
79 (with-imported-modules '((gnu build marionette))
80 #~(begin
81 (use-modules (srfi srfi-11) (srfi srfi-64)
82 (gnu build marionette)
83 (web uri)
84 (web client)
85 (web response))
86
87 (define marionette
88 (make-marionette (list #$vm)))
89
90 (mkdir #$output)
91 (chdir #$output)
92
93 (test-begin "nginx")
94
95 ;; Wait for nginx to be up and running.
96 (test-eq "service running"
97 'running!
98 (marionette-eval
99 '(begin
100 (use-modules (gnu services herd))
101 (start-service 'nginx)
102 'running!)
103 marionette))
104
105 ;; Make sure the PID file is created.
106 (test-assert "PID file"
107 (marionette-eval
108 '(file-exists? "/var/run/nginx/pid")
109 marionette))
110
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"
116 #:decode-body? #t)))
117 (list (response-code response) text)))
118
119 ;; There should be a log file in here.
120 (test-assert "log file"
121 (marionette-eval
122 '(file-exists? "/var/log/nginx/access.log")
123 marionette))
124
125 (test-end)
126 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
127
128 (gexp->derivation "nginx-test" test))
129
130 (define %test-nginx
131 (system-test
132 (name "nginx")
133 (description "Connect to a running NGINX server.")
134 (value (run-nginx-test))))
135
136 \f
137 ;;;
138 ;;; PHP-FPM
139 ;;;
140
141 (define %make-php-fpm-http-root
142 ;; Create our server root in /srv.
143 #~(begin
144 (mkdir "/srv")
145 (call-with-output-file "/srv/index.php"
146 (lambda (port)
147 (display "<?php
148 phpinfo();
149 echo(\"Computed by php:\".((string)(2+3)));
150 ?>\n" port)))))
151
152 (define %php-fpm-nginx-server-blocks
153 (list (nginx-server-configuration
154 (root "/srv")
155 (locations
156 (list (nginx-php-location)))
157 (listen '("8042"))
158 (ssl-certificate #f)
159 (ssl-certificate-key #f))))
160
161 (define %php-fpm-os
162 ;; Operating system under test.
163 (simple-operating-system
164 (dhcp-client-service)
165 (service php-fpm-service-type)
166 (service nginx-service-type
167 (nginx-configuration
168 (server-blocks %php-fpm-nginx-server-blocks)))
169 (simple-service 'make-http-root activation-service-type
170 %make-php-fpm-http-root)))
171
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."
175 (define os
176 (marionette-operating-system
177 %php-fpm-os
178 #:imported-modules '((gnu services herd)
179 (guix combinators))))
180
181 (define vm
182 (virtual-machine
183 (operating-system os)
184 (port-forwardings `((8080 . ,http-port)))))
185
186 (define test
187 (with-imported-modules '((gnu build marionette)
188 (guix build utils))
189 #~(begin
190 (use-modules (srfi srfi-11) (srfi srfi-64)
191 (gnu build marionette)
192 (web uri)
193 (web client)
194 (web response))
195
196 (define marionette
197 (make-marionette (list #$vm)))
198
199 (mkdir #$output)
200 (chdir #$output)
201
202 (test-begin "php-fpm")
203
204 (test-assert "php-fpm running"
205 (marionette-eval
206 '(begin
207 (use-modules (gnu services herd))
208 (match (start-service 'php-fpm)
209 (#f #f)
210 (('service response-parts ...)
211 (match (assq-ref response-parts 'running)
212 ((pid) (number? pid))))))
213 marionette))
214
215 (test-eq "nginx running"
216 'running!
217 (marionette-eval
218 '(begin
219 (use-modules (gnu services herd))
220 (start-service 'nginx)
221 'running!)
222 marionette))
223
224 (test-equal "http-get"
225 200
226 (let-values (((response text)
227 (http-get "http://localhost:8080/index.php"
228 #:decode-body? #t)))
229 (response-code response)))
230
231 (test-equal "php computed result is sent"
232 "Computed by php:5"
233 (let-values (((response text)
234 (http-get "http://localhost:8080/index.php"
235 #:decode-body? #t)))
236 (begin
237 (use-modules (ice-9 regex))
238 (let ((matches (string-match "Computed by php:5" text)))
239 (and matches
240 (match:substring matches 0))))))
241
242 (test-end)
243
244 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
245
246 (gexp->derivation "php-fpm-test" test))
247
248 (define %test-php-fpm
249 (system-test
250 (name "php-fpm")
251 (description "Test PHP-FPM through nginx.")
252 (value (run-php-fpm-test))))