| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net> |
| 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 guix) |
| 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 guix) |
| 27 | #:use-module (gnu services databases) |
| 28 | #:use-module (gnu services shepherd) |
| 29 | #:use-module (gnu services networking) |
| 30 | #:use-module (gnu packages databases) |
| 31 | #:use-module (guix packages) |
| 32 | #:use-module (guix modules) |
| 33 | #:use-module (guix records) |
| 34 | #:use-module (guix gexp) |
| 35 | #:use-module (guix store) |
| 36 | #:use-module (guix utils) |
| 37 | #:use-module (ice-9 match) |
| 38 | #:export (%test-guix-build-coordinator |
| 39 | %test-guix-data-service)) |
| 40 | |
| 41 | ;;; |
| 42 | ;;; Guix Build Coordinator |
| 43 | ;;; |
| 44 | |
| 45 | (define %guix-build-coordinator-os |
| 46 | (simple-operating-system |
| 47 | (service dhcp-client-service-type) |
| 48 | (service guix-build-coordinator-service-type))) |
| 49 | |
| 50 | (define (run-guix-build-coordinator-test) |
| 51 | (define os |
| 52 | (marionette-operating-system |
| 53 | %guix-build-coordinator-os |
| 54 | #:imported-modules '((gnu services herd) |
| 55 | (guix combinators)))) |
| 56 | |
| 57 | (define forwarded-port 8745) |
| 58 | |
| 59 | (define vm |
| 60 | (virtual-machine |
| 61 | (operating-system os) |
| 62 | (memory-size 1024) |
| 63 | (port-forwardings `((,forwarded-port . 8745))))) |
| 64 | |
| 65 | (define test |
| 66 | (with-imported-modules '((gnu build marionette)) |
| 67 | #~(begin |
| 68 | (use-modules (srfi srfi-11) (srfi srfi-64) |
| 69 | (gnu build marionette) |
| 70 | (web uri) |
| 71 | (web client) |
| 72 | (web response)) |
| 73 | |
| 74 | (define marionette |
| 75 | (make-marionette (list #$vm))) |
| 76 | |
| 77 | (mkdir #$output) |
| 78 | (chdir #$output) |
| 79 | |
| 80 | (test-begin "guix-build-coordinator") |
| 81 | |
| 82 | (test-assert "service running" |
| 83 | (marionette-eval |
| 84 | '(begin |
| 85 | (use-modules (gnu services herd)) |
| 86 | (match (start-service 'guix-build-coordinator) |
| 87 | (#f #f) |
| 88 | (('service response-parts ...) |
| 89 | (match (assq-ref response-parts 'running) |
| 90 | ((pid) (number? pid)))))) |
| 91 | marionette)) |
| 92 | |
| 93 | (test-equal "http-get" |
| 94 | 200 |
| 95 | (let-values |
| 96 | (((response text) |
| 97 | (http-get #$(simple-format |
| 98 | #f "http://localhost:~A/metrics" forwarded-port) |
| 99 | #:decode-body? #t))) |
| 100 | (response-code response))) |
| 101 | |
| 102 | (test-end) |
| 103 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) |
| 104 | |
| 105 | (gexp->derivation "guix-build-coordinator-test" test)) |
| 106 | |
| 107 | (define %test-guix-build-coordinator |
| 108 | (system-test |
| 109 | (name "guix-build-coordinator") |
| 110 | (description "Connect to a running Guix Build Coordinator.") |
| 111 | (value (run-guix-build-coordinator-test)))) |
| 112 | |
| 113 | \f |
| 114 | ;;; |
| 115 | ;;; Guix Data Service |
| 116 | ;;; |
| 117 | |
| 118 | (define guix-data-service-initial-database-setup-service |
| 119 | (let ((user "guix_data_service") |
| 120 | (name "guix_data_service")) |
| 121 | (define start-gexp |
| 122 | #~(lambda () |
| 123 | (let ((pid (primitive-fork)) |
| 124 | (postgres (getpwnam "postgres"))) |
| 125 | (if (eq? pid 0) |
| 126 | (dynamic-wind |
| 127 | (const #t) |
| 128 | (lambda () |
| 129 | (setgid (passwd:gid postgres)) |
| 130 | (setuid (passwd:uid postgres)) |
| 131 | (primitive-exit |
| 132 | (if (and |
| 133 | (zero? |
| 134 | (system* #$(file-append postgresql "/bin/createuser") |
| 135 | #$user)) |
| 136 | (zero? |
| 137 | (system* #$(file-append postgresql "/bin/createdb") |
| 138 | "-O" #$user #$name))) |
| 139 | 0 |
| 140 | 1))) |
| 141 | (lambda () |
| 142 | (primitive-exit 1))) |
| 143 | (zero? (cdr (waitpid pid))))))) |
| 144 | |
| 145 | (shepherd-service |
| 146 | (requirement '(postgres)) |
| 147 | (provision '(guix-data-service-initial-database-setup)) |
| 148 | (start start-gexp) |
| 149 | (stop #~(const #f)) |
| 150 | (respawn? #f) |
| 151 | (one-shot? #t) |
| 152 | (documentation "Setup Guix Data Service database.")))) |
| 153 | |
| 154 | (define %guix-data-service-os |
| 155 | (simple-operating-system |
| 156 | (service dhcp-client-service-type) |
| 157 | (service postgresql-service-type |
| 158 | (postgresql-configuration |
| 159 | (config-file |
| 160 | (postgresql-config-file |
| 161 | (hba-file |
| 162 | (plain-file "pg_hba.conf" |
| 163 | " |
| 164 | local all all trust |
| 165 | host all all 127.0.0.1/32 trust |
| 166 | host all all ::1/128 trust")))))) |
| 167 | (service guix-data-service-type |
| 168 | (guix-data-service-configuration |
| 169 | (host "0.0.0.0"))) |
| 170 | (simple-service 'guix-data-service-database-setup |
| 171 | shepherd-root-service-type |
| 172 | (list guix-data-service-initial-database-setup-service)))) |
| 173 | |
| 174 | (define (run-guix-data-service-test) |
| 175 | (define os |
| 176 | (marionette-operating-system |
| 177 | %guix-data-service-os |
| 178 | #:imported-modules '((gnu services herd) |
| 179 | (guix combinators)))) |
| 180 | |
| 181 | (define forwarded-port 8080) |
| 182 | |
| 183 | (define vm |
| 184 | (virtual-machine |
| 185 | (operating-system os) |
| 186 | (memory-size 1024) |
| 187 | (port-forwardings `((,forwarded-port . 8765))))) |
| 188 | |
| 189 | (define test |
| 190 | (with-imported-modules '((gnu build marionette)) |
| 191 | #~(begin |
| 192 | (use-modules (srfi srfi-11) (srfi srfi-64) |
| 193 | (gnu build marionette) |
| 194 | (web uri) |
| 195 | (web client) |
| 196 | (web response)) |
| 197 | |
| 198 | (define marionette |
| 199 | (make-marionette (list #$vm))) |
| 200 | |
| 201 | (mkdir #$output) |
| 202 | (chdir #$output) |
| 203 | |
| 204 | (test-begin "guix-data-service") |
| 205 | |
| 206 | (test-assert "service running" |
| 207 | (marionette-eval |
| 208 | '(begin |
| 209 | (use-modules (gnu services herd)) |
| 210 | (match (start-service 'guix-data-service) |
| 211 | (#f #f) |
| 212 | (('service response-parts ...) |
| 213 | (match (assq-ref response-parts 'running) |
| 214 | ((pid) (number? pid)))))) |
| 215 | marionette)) |
| 216 | |
| 217 | (test-assert "process jobs service running" |
| 218 | (marionette-eval |
| 219 | '(begin |
| 220 | (use-modules (gnu services herd)) |
| 221 | (match (start-service 'guix-data-service-process-jobs) |
| 222 | (#f #f) |
| 223 | (('service response-parts ...) |
| 224 | (match (assq-ref response-parts 'running) |
| 225 | ((pid) (number? pid)))))) |
| 226 | marionette)) |
| 227 | |
| 228 | (test-equal "http-get" |
| 229 | 200 |
| 230 | (let-values |
| 231 | (((response text) |
| 232 | (http-get #$(simple-format |
| 233 | #f "http://localhost:~A/healthcheck" forwarded-port) |
| 234 | #:decode-body? #t))) |
| 235 | (response-code response))) |
| 236 | |
| 237 | (test-end) |
| 238 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) |
| 239 | |
| 240 | (gexp->derivation "guix-data-service-test" test)) |
| 241 | |
| 242 | (define %test-guix-data-service |
| 243 | (system-test |
| 244 | (name "guix-data-service") |
| 245 | (description "Connect to a running Guix Data Service.") |
| 246 | (value (run-guix-data-service-test)))) |