1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu tests databases)
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 databases)
27 #:use-module (gnu services networking)
28 #:use-module (gnu packages databases)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:export (%test-memcached
37 (simple-operating-system
38 (service dhcp-client-service-type)
39 (service memcached-service-type)))
41 (define* (run-memcached-test #:optional (port 11211))
42 "Run tests in %MEMCACHED-OS, forwarding PORT."
44 (marionette-operating-system
46 #:imported-modules '((gnu services herd)
52 (port-forwardings `((11211 . ,port)))))
55 (with-imported-modules '((gnu build marionette))
57 (use-modules (srfi srfi-11) (srfi srfi-64)
58 (gnu build marionette)
62 (make-marionette (list #$vm)))
67 (test-begin "memcached")
69 ;; Wait for memcached to be up and running.
70 (test-assert "service running"
73 (use-modules (gnu services herd))
74 (match (start-service 'memcached)
76 (('service response-parts ...)
77 (match (assq-ref response-parts 'running)
78 ((pid) (number? pid))))))
81 (let* ((ai (car (getaddrinfo "localhost"
82 #$(number->string port))))
83 (s (socket (addrinfo:fam ai)
84 (addrinfo:socktype ai)
85 (addrinfo:protocol ai)))
88 (connect s (addrinfo:addr ai))
93 (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
100 (simple-format #f "VALUE ~A 0 ~A\r~A\r"
102 (string-length value)
105 (simple-format s "get ~A\r\n" key)
112 ;; There should be a log file in here.
113 (test-assert "log file"
115 '(file-exists? "/var/log/memcached")
119 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
121 (gexp->derivation "memcached-test" test))
123 (define %test-memcached
126 (description "Connect to a running MEMCACHED server.")
127 (value (run-memcached-test))))
132 (simple-operating-system
133 (service dhcp-client-service-type)
134 (service mongodb-service-type)))
135 (packages (cons* mongodb
138 (define* (run-mongodb-test #:optional (port 27017))
139 "Run tests in %MONGODB-OS, forwarding PORT."
141 (marionette-operating-system
143 #:imported-modules '((gnu services herd)
144 (guix combinators))))
148 (operating-system os)
150 (disk-image-size (* 1024 (expt 2 20)))
151 (port-forwardings `((27017 . ,port)))))
154 (with-imported-modules '((gnu build marionette))
156 (use-modules (srfi srfi-11) (srfi srfi-64)
157 (gnu build marionette)
162 (make-marionette (list #$vm)))
167 (test-begin "mongodb")
169 (test-assert "service running"
172 (use-modules (gnu services herd))
173 (match (start-service 'mongodb)
175 (('service response-parts ...)
176 (match (assq-ref response-parts 'running)
177 ((pid) (number? pid))))))
180 (test-eq "test insert"
182 (system* (string-append #$mongodb "/bin/mongo")
185 "db.testCollection.insert({data: 'test-data'})"))
187 (test-equal "test find"
189 (let* ((port (open-pipe*
191 (string-append #$mongodb "/bin/mongo")
195 "db.testCollection.findOne().data"))
196 (output (read-line port))
197 (status (close-pipe port)))
201 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
203 (gexp->derivation "mongodb-test" test))
205 (define %test-mongodb
208 (description "Connect to a running MONGODB server.")
209 (value (run-mongodb-test))))
213 ;;; The PostgreSQL service.
216 (define %postgresql-os
217 (simple-operating-system
218 (service postgresql-service-type)))
220 (define (run-postgresql-test)
221 "Run tests in %POSTGRESQL-OS."
223 (marionette-operating-system
225 #:imported-modules '((gnu services herd)
226 (guix combinators))))
230 (operating-system os)
234 (with-imported-modules '((gnu build marionette))
236 (use-modules (srfi srfi-64)
237 (gnu build marionette))
240 (make-marionette (list #$vm)))
245 (test-begin "postgresql")
247 (test-assert "service running"
250 (use-modules (gnu services herd))
251 (start-service 'postgres))
255 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
257 (gexp->derivation "postgresql-test" test))
259 (define %test-postgresql
262 (description "Start the PostgreSQL service.")
263 (value (run-postgresql-test))))
267 ;;; The MySQL service.
271 (simple-operating-system
274 (define* (run-mysql-test)
275 "Run tests in %MYSQL-OS."
277 (marionette-operating-system
279 #:imported-modules '((gnu services herd)
280 (guix combinators))))
284 (operating-system os)
288 (with-imported-modules '((gnu build marionette))
290 (use-modules (srfi srfi-11) (srfi srfi-64)
291 (gnu build marionette))
294 (make-marionette (list #$vm)))
301 (test-assert "service running"
304 (use-modules (gnu services herd))
305 (match (start-service 'mysql)
307 (('service response-parts ...)
308 (match (assq-ref response-parts 'running)
309 ((pid) (number? pid))))))
313 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
315 (gexp->derivation "mysql-test" test))
320 (description "Start the MySQL service.")
321 (value (run-mysql-test))))