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
36 (simple-operating-system
38 (service memcached-service-type)))
40 (define* (run-memcached-test #:optional (port 11211))
41 "Run tests in %MEMCACHED-OS, forwarding PORT."
43 (marionette-operating-system
45 #:imported-modules '((gnu services herd)
51 (port-forwardings `((11211 . ,port)))))
54 (with-imported-modules '((gnu build marionette))
56 (use-modules (srfi srfi-11) (srfi srfi-64)
57 (gnu build marionette)
61 (make-marionette (list #$vm)))
66 (test-begin "memcached")
68 ;; Wait for memcached to be up and running.
69 (test-assert "service running"
72 (use-modules (gnu services herd))
73 (match (start-service 'memcached)
75 (('service response-parts ...)
76 (match (assq-ref response-parts 'running)
77 ((pid) (number? pid))))))
80 (let* ((ai (car (getaddrinfo "localhost"
81 #$(number->string port))))
82 (s (socket (addrinfo:fam ai)
83 (addrinfo:socktype ai)
84 (addrinfo:protocol ai)))
87 (connect s (addrinfo:addr ai))
92 (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
99 (simple-format #f "VALUE ~A 0 ~A\r~A\r"
101 (string-length value)
104 (simple-format s "get ~A\r\n" key)
111 ;; There should be a log file in here.
112 (test-assert "log file"
114 '(file-exists? "/var/log/memcached")
118 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
120 (gexp->derivation "memcached-test" test))
122 (define %test-memcached
125 (description "Connect to a running MEMCACHED server.")
126 (value (run-memcached-test))))
131 (simple-operating-system
132 (dhcp-client-service)
133 (service mongodb-service-type)))
134 (packages (cons* mongodb
137 (define* (run-mongodb-test #:optional (port 27017))
138 "Run tests in %MONGODB-OS, forwarding PORT."
140 (marionette-operating-system
142 #:imported-modules '((gnu services herd)
143 (guix combinators))))
147 (operating-system os)
149 (disk-image-size (* 1024 (expt 2 20)))
150 (port-forwardings `((27017 . ,port)))))
153 (with-imported-modules '((gnu build marionette))
155 (use-modules (srfi srfi-11) (srfi srfi-64)
156 (gnu build marionette)
161 (make-marionette (list #$vm)))
166 (test-begin "mongodb")
168 (test-assert "service running"
171 (use-modules (gnu services herd))
172 (match (start-service 'mongodb)
174 (('service response-parts ...)
175 (match (assq-ref response-parts 'running)
176 ((pid) (number? pid))))))
179 (test-eq "test insert"
181 (system* (string-append #$mongodb "/bin/mongo")
184 "db.testCollection.insert({data: 'test-data'})"))
186 (test-equal "test find"
188 (let* ((port (open-pipe*
190 (string-append #$mongodb "/bin/mongo")
194 "db.testCollection.findOne().data"))
195 (output (read-line port))
196 (status (close-pipe port)))
200 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
202 (gexp->derivation "mongodb-test" test))
204 (define %test-mongodb
207 (description "Connect to a running MONGODB server.")
208 (value (run-mongodb-test))))
212 ;;; The MySQL service.
216 (simple-operating-system
219 (define* (run-mysql-test)
220 "Run tests in %MYSQL-OS."
222 (marionette-operating-system
224 #:imported-modules '((gnu services herd)
225 (guix combinators))))
229 (operating-system os)
233 (with-imported-modules '((gnu build marionette))
235 (use-modules (srfi srfi-11) (srfi srfi-64)
236 (gnu build marionette))
239 (make-marionette (list #$vm)))
246 (test-assert "service running"
249 (use-modules (gnu services herd))
250 (match (start-service 'mysql)
252 (('service response-parts ...)
253 (match (assq-ref response-parts 'running)
254 ((pid) (number? pid))))))
258 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
260 (gexp->derivation "mysql-test" test))
265 (description "Start the MySQL service.")
266 (value (run-mysql-test))))