tests: Remove unused import (gnu system grub).
[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 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
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))
32
33 (define %index.html-contents
34 ;; Contents of the /index.html file served by nginx.
35 "Hello, nginx!")
36
37 (define %make-http-root
38 ;; Create our server root in /srv.
39 #~(begin
40 (mkdir "/srv")
41 (call-with-output-file "/srv/index.html"
42 (lambda (port)
43 (display #$%index.html-contents port)))))
44
45 (define %nginx-servers
46 ;; Server blocks.
47 (list (nginx-server-configuration
48 (root "/srv")
49 (http-port 8042)
50 (https-port #f)
51 (ssl-certificate #f)
52 (ssl-certificate-key #f))))
53
54 (define %nginx-os
55 ;; Operating system under test.
56 (simple-operating-system
57 (dhcp-client-service)
58 (service nginx-service-type
59 (nginx-configuration
60 (log-directory "/var/log/nginx")
61 (server-blocks %nginx-servers)))
62 (simple-service 'make-http-root activation-service-type
63 %make-http-root)))
64
65 (define* (run-nginx-test #:optional (http-port 8042))
66 "Run tests in %NGINX-OS, which has nginx running and listening on
67 HTTP-PORT."
68 (mlet* %store-monad ((os -> (marionette-operating-system
69 %nginx-os
70 #:imported-modules '((gnu services herd)
71 (guix combinators))))
72 (command (system-qemu-image/shared-store-script
73 os #:graphic? #f)))
74 (define test
75 (with-imported-modules '((gnu build marionette))
76 #~(begin
77 (use-modules (srfi srfi-11) (srfi srfi-64)
78 (gnu build marionette)
79 (web uri)
80 (web client)
81 (web response))
82
83 (define 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"
87 (string-append
88 "user,hostfwd=tcp::8080-:"
89 #$(number->string http-port)))))
90
91 (mkdir #$output)
92 (chdir #$output)
93
94 (test-begin "nginx")
95
96 ;; Wait for nginx to be up and running.
97 (test-eq "service running"
98 'running!
99 (marionette-eval
100 '(begin
101 (use-modules (gnu services herd))
102 (start-service 'nginx)
103 'running!)
104 marionette))
105
106 ;; Make sure the PID file is created.
107 (test-assert "PID file"
108 (marionette-eval
109 '(file-exists? "/var/run/nginx/pid")
110 marionette))
111
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"
117 #:decode-body? #t)))
118 (list (response-code response) text)))
119
120 ;; There should be a log file in here.
121 (test-assert "log file"
122 (marionette-eval
123 '(file-exists? "/var/log/nginx/access.log")
124 marionette))
125
126 (test-end)
127 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
128
129 (gexp->derivation "nginx-test" test)))
130
131 (define %test-nginx
132 (system-test
133 (name "nginx")
134 (description "Connect to a running NGINX server.")
135 (value (run-nginx-test))))