gnu: tests: web: Generalise the nginx test.
So that it can also be used for other web servers. * gnu/tests/web.scm (%index.html-contents): Change nginx to guix. (%make-http-root): Move the index.html file from /srv to /srv/http. (%nginx-servers): Remove the setting of root. (run-nginx-test, run-webserver-test): Rename run-nginx-test to run-webserver-test and generalise its behaviour (%test-nginx): Change to use run-webserver-test, rather than run-nginx-test.
This commit is contained in:
parent
9b811f690c
commit
d9b530813f
|
@ -33,47 +33,33 @@
|
||||||
%test-php-fpm))
|
%test-php-fpm))
|
||||||
|
|
||||||
(define %index.html-contents
|
(define %index.html-contents
|
||||||
;; Contents of the /index.html file served by nginx.
|
;; Contents of the /index.html file.
|
||||||
"Hello, nginx!")
|
"Hello, guix!")
|
||||||
|
|
||||||
(define %make-http-root
|
(define %make-http-root
|
||||||
;; Create our server root in /srv.
|
;; Create our server root in /srv.
|
||||||
#~(begin
|
#~(begin
|
||||||
(mkdir "/srv")
|
(mkdir "/srv")
|
||||||
(call-with-output-file "/srv/index.html"
|
(mkdir "/srv/http")
|
||||||
|
(call-with-output-file "/srv/http/index.html"
|
||||||
(lambda (port)
|
(lambda (port)
|
||||||
(display #$%index.html-contents port)))))
|
(display #$%index.html-contents port)))))
|
||||||
|
|
||||||
(define %nginx-servers
|
(define* (run-webserver-test name test-os #:key (log-file #f) (http-port 8080))
|
||||||
;; Server blocks.
|
|
||||||
(list (nginx-server-configuration
|
|
||||||
(root "/srv")
|
|
||||||
(listen '("8042" "443 ssl")))))
|
|
||||||
|
|
||||||
(define %nginx-os
|
|
||||||
;; Operating system under test.
|
|
||||||
(simple-operating-system
|
|
||||||
(dhcp-client-service)
|
|
||||||
(service nginx-service-type
|
|
||||||
(nginx-configuration
|
|
||||||
(log-directory "/var/log/nginx")
|
|
||||||
(server-blocks %nginx-servers)))
|
|
||||||
(simple-service 'make-http-root activation-service-type
|
|
||||||
%make-http-root)))
|
|
||||||
|
|
||||||
(define* (run-nginx-test #:optional (http-port 8042))
|
|
||||||
"Run tests in %NGINX-OS, which has nginx running and listening on
|
"Run tests in %NGINX-OS, which has nginx running and listening on
|
||||||
HTTP-PORT."
|
HTTP-PORT."
|
||||||
(define os
|
(define os
|
||||||
(marionette-operating-system
|
(marionette-operating-system
|
||||||
%nginx-os
|
test-os
|
||||||
#:imported-modules '((gnu services herd)
|
#:imported-modules '((gnu services herd)
|
||||||
(guix combinators))))
|
(guix combinators))))
|
||||||
|
|
||||||
|
(define forwarded-port 8080)
|
||||||
|
|
||||||
(define vm
|
(define vm
|
||||||
(virtual-machine
|
(virtual-machine
|
||||||
(operating-system os)
|
(operating-system os)
|
||||||
(port-forwardings `((8080 . ,http-port)))))
|
(port-forwardings `((,http-port . ,forwarded-port)))))
|
||||||
|
|
||||||
(define test
|
(define test
|
||||||
(with-imported-modules '((gnu build marionette))
|
(with-imported-modules '((gnu build marionette))
|
||||||
|
@ -90,48 +76,69 @@ HTTP-PORT."
|
||||||
(mkdir #$output)
|
(mkdir #$output)
|
||||||
(chdir #$output)
|
(chdir #$output)
|
||||||
|
|
||||||
(test-begin "nginx")
|
(test-begin #$name)
|
||||||
|
|
||||||
;; Wait for nginx to be up and running.
|
(test-assert #$(string-append name " service running")
|
||||||
(test-eq "service running"
|
|
||||||
'running!
|
|
||||||
(marionette-eval
|
(marionette-eval
|
||||||
'(begin
|
'(begin
|
||||||
(use-modules (gnu services herd))
|
(use-modules (gnu services herd))
|
||||||
(start-service 'nginx)
|
(match (start-service '#$(string->symbol name))
|
||||||
'running!)
|
(#f #f)
|
||||||
marionette))
|
(('service response-parts ...)
|
||||||
|
(match (assq-ref response-parts 'running)
|
||||||
;; Make sure the PID file is created.
|
((#t) #t)
|
||||||
(test-assert "PID file"
|
((pid) (number? pid))))))
|
||||||
(marionette-eval
|
|
||||||
'(file-exists? "/var/run/nginx/pid")
|
|
||||||
marionette))
|
marionette))
|
||||||
|
|
||||||
;; Retrieve the index.html file we put in /srv.
|
;; Retrieve the index.html file we put in /srv.
|
||||||
(test-equal "http-get"
|
(test-equal "http-get"
|
||||||
'(200 #$%index.html-contents)
|
'(200 #$%index.html-contents)
|
||||||
(let-values (((response text)
|
(let-values
|
||||||
(http-get "http://localhost:8080/index.html"
|
(((response text)
|
||||||
#:decode-body? #t)))
|
(http-get #$(simple-format
|
||||||
|
#f "http://localhost:~A/index.html" forwarded-port)
|
||||||
|
#:decode-body? #t)))
|
||||||
(list (response-code response) text)))
|
(list (response-code response) text)))
|
||||||
|
|
||||||
;; There should be a log file in here.
|
#$@(if log-file
|
||||||
(test-assert "log file"
|
`((test-assert ,(string-append "log file exists " log-file)
|
||||||
(marionette-eval
|
(marionette-eval
|
||||||
'(file-exists? "/var/log/nginx/access.log")
|
'(file-exists? ,log-file)
|
||||||
marionette))
|
marionette)))
|
||||||
|
'())
|
||||||
|
|
||||||
(test-end)
|
(test-end)
|
||||||
(exit (= (test-runner-fail-count (test-runner-current)) 0)))))
|
(exit (= (test-runner-fail-count (test-runner-current)) 0)))))
|
||||||
|
|
||||||
(gexp->derivation "nginx-test" test))
|
(gexp->derivation (string-append name "-test") test))
|
||||||
|
|
||||||
|
|
||||||
|
;;;
|
||||||
|
;;; NGINX
|
||||||
|
;;;
|
||||||
|
|
||||||
|
(define %nginx-servers
|
||||||
|
;; Server blocks.
|
||||||
|
(list (nginx-server-configuration
|
||||||
|
(listen '("8080")))))
|
||||||
|
|
||||||
|
(define %nginx-os
|
||||||
|
;; Operating system under test.
|
||||||
|
(simple-operating-system
|
||||||
|
(dhcp-client-service)
|
||||||
|
(service nginx-service-type
|
||||||
|
(nginx-configuration
|
||||||
|
(log-directory "/var/log/nginx")
|
||||||
|
(server-blocks %nginx-servers)))
|
||||||
|
(simple-service 'make-http-root activation-service-type
|
||||||
|
%make-http-root)))
|
||||||
|
|
||||||
(define %test-nginx
|
(define %test-nginx
|
||||||
(system-test
|
(system-test
|
||||||
(name "nginx")
|
(name "nginx")
|
||||||
(description "Connect to a running NGINX server.")
|
(description "Connect to a running NGINX server.")
|
||||||
(value (run-nginx-test))))
|
(value (run-webserver-test name %nginx-os
|
||||||
|
#:log-file "/var/log/nginx/access.log"))))
|
||||||
|
|
||||||
|
|
||||||
;;;
|
;;;
|
||||||
|
|
Loading…
Reference in New Issue