1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu tests web)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system file-systems)
23 #:use-module (gnu system shadow)
24 #:use-module (gnu system vm)
25 #:use-module (gnu services)
26 #:use-module (gnu services web)
27 #:use-module (gnu services networking)
28 #:use-module (guix gexp)
29 #:use-module (guix store)
30 #:use-module (guix monads)
31 #:export (%test-nginx))
33 (define %index.html-contents
34 ;; Contents of the /index.html file served by nginx.
37 (define %make-http-root
38 ;; Create our server root in /srv.
41 (call-with-output-file "/srv/index.html"
43 (display #$%index.html-contents port)))))
45 (define %nginx-servers
47 (list (nginx-server-configuration
52 (ssl-certificate-key #f))))
55 ;; Operating system under test.
56 (simple-operating-system
58 (service nginx-service-type
60 (log-directory "/var/log/nginx")
61 (server-blocks %nginx-servers)))
62 (simple-service 'make-http-root activation-service-type
65 (define* (run-nginx-test #:optional (http-port 8042))
66 "Run tests in %NGINX-OS, which has nginx running and listening on
68 (mlet* %store-monad ((os -> (marionette-operating-system
70 #:imported-modules '((gnu services herd)
72 (command (system-qemu-image/shared-store-script
75 (with-imported-modules '((gnu build marionette))
77 (use-modules (srfi srfi-11) (srfi srfi-64)
78 (gnu build marionette)
84 ;; Forward the guest's HTTP-PORT, where nginx is listening, to
85 ;; port 8080 in the host.
86 (make-marionette (list #$command "-net"
88 "user,hostfwd=tcp::8080-:"
89 #$(number->string http-port)))))
96 ;; Wait for nginx to be up and running.
97 (test-eq "service running"
101 (use-modules (gnu services herd))
102 (start-service 'nginx)
106 ;; Make sure the PID file is created.
107 (test-assert "PID file"
109 '(file-exists? "/var/run/nginx/pid")
112 ;; Retrieve the index.html file we put in /srv.
113 (test-equal "http-get"
114 '(200 #$%index.html-contents)
115 (let-values (((response text)
116 (http-get "http://localhost:8080/index.html"
118 (list (response-code response) text)))
120 ;; There should be a log file in here.
121 (test-assert "log file"
123 '(file-exists? "/var/log/nginx/access.log")
127 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
129 (gexp->derivation "nginx-test" test)))
134 (description "Connect to a running NGINX server.")
135 (value (run-nginx-test))))