aboutsummaryrefslogtreecommitdiff
path: root/etc/system-tests.scm
blob: 1085deed24b91a4587e848f5cb9b7403745c9b74 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (gnu tests)
             (gnu packages package-management)
             ((gnu ci) #:select (channel-source->package))
             ((guix git-download) #:select (git-predicate))
             ((guix utils) #:select (current-source-directory))
             (git)
             (ice-9 match))

(define (source-commit directory)
  "Return the commit of the head of DIRECTORY or #f if it could not be
determined."
  (let ((repository #f))
    (catch 'git-error
      (lambda ()
        (set! repository (repository-open directory))
        (let* ((head   (repository-head repository))
               (target (reference-target head))
               (commit (oid->string target)))
          (repository-close! repository)
          commit))
      (lambda _
        (when repository
          (repository-close! repository))
        #f))))

(define (tests-for-current-guix source commit)
  "Return a list of tests for perform, using Guix built from SOURCE, a channel
instance."
  ;; Honor the 'TESTS' environment variable so that one can select a subset
  ;; of tests to run in the usual way:
  ;;
  ;;   make check-system TESTS=installed-os
  (parameterize ((current-guix-package
                  (channel-source->package source #:commit commit)))
    (match (getenv "TESTS")
      (#f
       (all-system-tests))
      ((= string-tokenize (tests ...))
       (filter (lambda (test)
                 (member (system-test-name test) tests))
               (all-system-tests))))))

(define (system-test->manifest-entry test)
  "Return a manifest entry for TEST, a system test."
  (manifest-entry
    (name (string-append "test." (system-test-name test)))
    (version "0")
    (item test)))

(define (system-test-manifest)
  "Return a manifest containing all the system tests, or all those selected by
the 'TESTS' environment variable."
  (define source
    (string-append (current-source-directory) "/.."))

  (define commit
    ;; Fetch the current commit ID so we can potentially build the same
    ;; derivation as ci.guix.gnu.org.
    (source-commit source))

  ;; Intern SOURCE so that 'build-from-source' in (guix channels) sees
  ;; "fresh" file names and thus doesn't find itself loading .go files
  ;; from ~/.cache/guile when it loads 'build-aux/build-self.scm'.
  (let* ((source (local-file source
                             (if commit
                                 (string-append "guix-"
                                                (string-take commit 7))
                                 "guix-source")
                             #:recursive? #t
                             #:select?
                             (or (git-predicate source)
                                 (const #t))))
         (tests  (tests-for-current-guix source commit)))
    (format (current-error-port) "Selected ~a system tests...~%"
            (length tests))

    (manifest (map system-test->manifest-entry tests))))

;; Return the manifest.
(system-test-manifest)
(backend "localhost:80") (listen '(":8080")))))) (define %test-varnish (system-test (name "varnish") (description "Test the Varnish Cache server.") (value (run-webserver-test "varnish-default" %varnish-os)))) ;;; ;;; PHP-FPM ;;; (define %make-php-fpm-http-root ;; Create our server root in /srv. #~(begin (mkdir "/srv") (call-with-output-file "/srv/index.php" (lambda (port) (display "<?php phpinfo(); echo(\"Computed by php:\".((string)(2+3))); ?>\n" port))))) (define %php-fpm-nginx-server-blocks (list (nginx-server-configuration (root "/srv") (locations (list (nginx-php-location))) (listen '("8042")) (ssl-certificate #f) (ssl-certificate-key #f)))) (define %php-fpm-os ;; Operating system under test. (simple-operating-system (service dhcp-client-service-type) (service php-fpm-service-type) (service nginx-service-type (nginx-configuration (server-blocks %php-fpm-nginx-server-blocks))) (simple-service 'make-http-root activation-service-type %make-php-fpm-http-root))) (define* (run-php-fpm-test #:optional (http-port 8042)) "Run tests in %PHP-FPM-OS, which has nginx running and listening on HTTP-PORT, along with php-fpm." (define os (marionette-operating-system %php-fpm-os #:imported-modules '((gnu services herd) (guix combinators)))) (define vm (virtual-machine (operating-system os) (port-forwardings `((8080 . ,http-port))))) (define test (with-imported-modules '((gnu build marionette) (guix build utils)) #~(begin (use-modules (srfi srfi-11) (srfi srfi-64) (gnu build marionette) (web uri) (web client) (web response)) (define marionette (make-marionette (list #$vm))) (test-runner-current (system-test-runner #$output)) (test-begin "php-fpm") (test-assert "php-fpm running" (marionette-eval '(begin (use-modules (gnu services herd)) (match (start-service 'php-fpm) (#f #f) (('service response-parts ...) (match (assq-ref response-parts 'running) ((pid) pid))))) marionette)) (test-assert "nginx running" (marionette-eval '(begin (use-modules (gnu services herd)) (start-service 'nginx)) marionette)) (test-equal "http-get" 200 (let-values (((response text) (http-get "http://localhost:8080/index.php" #:decode-body? #t))) (response-code response))) (test-equal "php computed result is sent" "Computed by php:5" (let-values (((response text) (http-get "http://localhost:8080/index.php" #:decode-body? #t))) (begin (use-modules (ice-9 regex)) (let ((matches (string-match "Computed by php:5" text))) (and matches (match:substring matches 0)))))) (test-end)))) (gexp->derivation "php-fpm-test" test)) (define %test-php-fpm (system-test (name "php-fpm") (description "Test PHP-FPM through nginx.") (value (run-php-fpm-test)))) ;;; ;;; hpcguix-web ;;; (define* (run-hpcguix-web-server-test name test-os) "Run tests in %HPCGUIX-WEB-OS, which has hpcguix-web running." (define os (marionette-operating-system test-os #:imported-modules '((gnu services herd) (guix combinators)))) (define vm (virtual-machine (operating-system os) (port-forwardings '((8080 . 5000))) (memory-size 1024))) (define test (with-imported-modules '((gnu build marionette)) #~(begin (use-modules (srfi srfi-11) (srfi srfi-64) (ice-9 match) (gnu build marionette) (web uri) (web client) (web response)) (define marionette (make-marionette (list #$vm))) (test-runner-current (system-test-runner #$output)) (test-begin #$name) (test-assert "hpcguix-web running" (marionette-eval '(begin (use-modules (gnu services herd)) (match (start-service 'hpcguix-web) (#f #f) (('service response-parts ...) (match (assq-ref response-parts 'running) ((pid) pid))))) marionette)) (test-equal "http-get" 200 (begin (wait-for-tcp-port 5000 marionette) (#$retry-on-error (lambda () (let-values (((response text) (http-get "http://localhost:8080"))) (response-code response))) #:times 10 #:delay 5))) (test-end)))) (gexp->derivation (string-append name "-test") test)) (define %hpcguix-web-specs ;; Server config gexp. #~(hpcweb-configuration (title-prefix "[TEST] HPCGUIX-WEB"))) (define %hpcguix-web-os (simple-operating-system (service dhcp-client-service-type) (service hpcguix-web-service-type (hpcguix-web-configuration (specs %hpcguix-web-specs) (address "0.0.0.0"))))) (define %test-hpcguix-web (system-test (name "hpcguix-web") (description "Connect to a running hpcguix-web server.") (value (run-hpcguix-web-server-test name %hpcguix-web-os)))) (define %tailon-os ;; Operating system under test. (simple-operating-system (service dhcp-client-service-type) (service tailon-service-type (tailon-configuration (config-file (tailon-configuration-file (bind "0.0.0.0:8080"))))))) (define* (run-tailon-test #:optional (http-port 8081)) "Run tests in %TAILON-OS, which has tailon running and listening on HTTP-PORT." (define os (marionette-operating-system %tailon-os #:imported-modules '((gnu services herd) (guix combinators)))) (define vm (virtual-machine (operating-system os) (port-forwardings `((,http-port . 8080))))) (define test (with-imported-modules '((gnu build marionette)) #~(begin (use-modules (srfi srfi-11) (srfi srfi-64) (ice-9 match) (gnu build marionette) (web uri) (web client) (web response)) (define marionette ;; Forward the guest's HTTP-PORT, where tailon is listening, to ;; port 8080 in the host. (make-marionette (list #$vm))) (test-runner-current (system-test-runner #$output)) (test-begin "tailon") (test-assert "service running" (wait-for-tcp-port 8080 marionette)) (test-equal "http-get" 200 (#$retry-on-error (lambda () (let-values (((response text) (http-get #$(format #f "http://localhost:~A/" http-port) #:decode-body? #t))) (response-code response))) #:times 10 #:delay 5)) (test-end)))) (gexp->derivation "tailon-test" test)) (define %test-tailon (system-test (name "tailon") (description "Connect to a running Tailon server.") (value (run-tailon-test)))) ;;; ;;; Patchwork ;;; (define (patchwork-initial-database-setup-service configuration) (define start-gexp #~(lambda () (let ((pid (primitive-fork)) (postgres (getpwnam "postgres"))) (if (eq? pid 0) (dynamic-wind (const #t) (lambda () (setgid (passwd:gid postgres)) (setuid (passwd:uid postgres)) (primitive-exit (if (and (zero? (system* #$(file-append postgresql "/bin/createuser") #$(patchwork-database-configuration-user configuration))) (zero? (system* #$(file-append postgresql "/bin/createdb") "-O" #$(patchwork-database-configuration-user configuration) #$(patchwork-database-configuration-name configuration)))) 0 1))) (lambda () (primitive-exit 1))) (zero? (cdr (waitpid pid))))))) (shepherd-service (requirement '(postgres)) (provision '(patchwork-postgresql-user-and-database)) (start start-gexp) (stop #~(const #f)) (respawn? #f) (documentation "Setup patchwork database."))) (define (patchwork-os patchwork) (simple-operating-system (service dhcp-client-service-type) (service httpd-service-type (httpd-configuration (config (httpd-config-file (listen '("8080")))))) (service postgresql-service-type (postgresql-configuration (postgresql postgresql))) (service patchwork-service-type (patchwork-configuration (patchwork patchwork) (domain "localhost") (settings-module (patchwork-settings-module (allowed-hosts (list domain)) (default-from-email ""))) (getmail-retriever-config (getmail-retriever-configuration (type "SimpleIMAPSSLRetriever") (server "imap.example.com") (port 993) (username "username") (password "password") (extra-parameters '((mailboxes . ("INBOX")))))))) (simple-service 'patchwork-database-setup shepherd-root-service-type (list (patchwork-initial-database-setup-service (patchwork-database-configuration)))))) (define (run-patchwork-test patchwork) "Run tests in %NGINX-OS, which has nginx running and listening on HTTP-PORT." (define os (marionette-operating-system (patchwork-os patchwork) #:imported-modules '((gnu services herd) (guix combinators)))) (define forwarded-port 8080) (define vm (virtual-machine (operating-system os) (port-forwardings `((8080 . ,forwarded-port))) (memory-size 1024))) (define test (with-imported-modules '((gnu build marionette)) #~(begin (use-modules (srfi srfi-11) (srfi srfi-64) (ice-9 match) (gnu build marionette) (web uri) (web client) (web response)) (define marionette (make-marionette (list #$vm))) (test-runner-current (system-test-runner #$output)) (test-begin "patchwork") (test-assert "patchwork-postgresql-user-and-service started" (marionette-eval '(begin (use-modules (gnu services herd)) (match (start-service 'patchwork-postgresql-user-and-database) (#f #f) (('service response-parts ...) (match (assq-ref response-parts 'running) ((#t) #t) ((pid) pid))))) marionette)) (test-assert "httpd running" (marionette-eval '(begin (use-modules (gnu services herd)) (start-service 'httpd)) marionette)) (test-equal "http-get" 200 (#$retry-on-error (lambda () (let-values (((response text) (http-get #$(simple-format #f "http://localhost:~A/" forwarded-port) #:decode-body? #t))) (response-code response))) #:times 10 #:delay 5)) (test-end)))) (gexp->derivation "patchwork-test" test)) (define %test-patchwork (system-test (name "patchwork") (description "Connect to a running Patchwork service.") (value (run-patchwork-test patchwork)))) ;;; ;;; Agate ;;; (define %index.gmi-contents ;; Contents of the /index.gmi file. "Hello, guix!") (define %make-agate-root ;; Create our server root in /srv. #~(begin (mkdir "/srv") (mkdir "/srv/gemini") (mkdir "/srv/gemini-certs") ;; Directory should be writable for Agate user to generate certificates (let ((user (getpw "agate"))) (chown "/srv/gemini-certs" (passwd:uid user) (passwd:gid user))) (call-with-output-file (string-append "/srv/gemini/index.gmi") (lambda (port) (display #$%index.gmi-contents port))))) (define %agate-os (simple-operating-system (service dhcp-client-service-type) (simple-service 'make-agate-root activation-service-type %make-agate-root) (service agate-service-type (agate-configuration (hostnames '("localhost")))))) (define* (run-agate-test name test-os expected-content) (define os (marionette-operating-system test-os #:imported-modules '((gnu services herd) (guix combinators)) #:extensions (list guile-gemini guile-gnutls))) (define forwarded-port 1965) (define vm (virtual-machine (operating-system os) (port-forwardings `((1965 . ,forwarded-port))))) (define test (with-imported-modules '((gnu build marionette)) #~(begin (use-modules (srfi srfi-64) (gnu build marionette)) (define marionette (make-marionette (list #$vm))) (test-runner-current (system-test-runner #$output)) (test-begin #$name) (test-assert #$(string-append name " service running") (marionette-eval '(begin (use-modules (gnu services herd)) (match (start-service '#$(string->symbol name)) (#f #f) (('service response-parts ...) (match (assq-ref response-parts 'running) ((#t) #t) ((pid) pid))))) marionette)) (test-assert "Agate TCP port ready, IPv4" (wait-for-tcp-port #$forwarded-port marionette)) (test-assert "Agate TCP port ready, IPv6" (wait-for-tcp-port #$forwarded-port marionette #:address '(make-socket-address AF_INET6 (inet-pton AF_INET6 "::1") #$forwarded-port))) (test-equal "Agate responses with the specified index.gmi" #$expected-content (marionette-eval '(begin (use-modules (ice-9 iconv) (gemini client) (gemini request) (gemini response)) (bytevector->string (gemini-response-body-bytes (send-gemini-request (build-gemini-request #:host "localhost" #:port #$forwarded-port))) "utf8")) marionette)) (test-end)))) (gexp->derivation "agate-test" test)) (define %test-agate (system-test (name "agate") (description "Connect to a running Agate service.") (value (run-agate-test name %agate-os %index.gmi-contents))))