web.scm 7.6 KB

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