web.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. (define-module (gnu tests web)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system file-systems)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services web)
  26. #:use-module (gnu services networking)
  27. #:use-module (guix gexp)
  28. #:use-module (guix store)
  29. #:export (%test-nginx))
  30. (define %index.html-contents
  31. ;; Contents of the /index.html file served by nginx.
  32. "Hello, nginx!")
  33. (define %make-http-root
  34. ;; Create our server root in /srv.
  35. #~(begin
  36. (mkdir "/srv")
  37. (call-with-output-file "/srv/index.html"
  38. (lambda (port)
  39. (display #$%index.html-contents port)))))
  40. (define %nginx-servers
  41. ;; Server blocks.
  42. (list (nginx-server-configuration
  43. (root "/srv")
  44. (http-port 8042)
  45. (https-port #f)
  46. (ssl-certificate #f)
  47. (ssl-certificate-key #f))))
  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))))