services: MySQL: Deprecate 'mysql-service'.
[jackhill/guix/guix.git] / gnu / tests / databases.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 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 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
32 %test-mongodb
33 %test-postgresql
34 %test-mysql))
35
36 (define %memcached-os
37 (simple-operating-system
38 (service dhcp-client-service-type)
39 (service memcached-service-type)))
40
41 (define* (run-memcached-test #:optional (port 11211))
42 "Run tests in %MEMCACHED-OS, forwarding PORT."
43 (define os
44 (marionette-operating-system
45 %memcached-os
46 #:imported-modules '((gnu services herd)
47 (guix combinators))))
48
49 (define vm
50 (virtual-machine
51 (operating-system os)
52 (port-forwardings `((11211 . ,port)))))
53
54 (define test
55 (with-imported-modules '((gnu build marionette))
56 #~(begin
57 (use-modules (srfi srfi-11) (srfi srfi-64)
58 (gnu build marionette)
59 (ice-9 rdelim))
60
61 (define marionette
62 (make-marionette (list #$vm)))
63
64 (mkdir #$output)
65 (chdir #$output)
66
67 (test-begin "memcached")
68
69 ;; Wait for memcached to be up and running.
70 (test-assert "service running"
71 (marionette-eval
72 '(begin
73 (use-modules (gnu services herd))
74 (match (start-service 'memcached)
75 (#f #f)
76 (('service response-parts ...)
77 (match (assq-ref response-parts 'running)
78 ((pid) (number? pid))))))
79 marionette))
80
81 (let* ((ai (car (getaddrinfo "localhost"
82 #$(number->string port))))
83 (s (socket (addrinfo:fam ai)
84 (addrinfo:socktype ai)
85 (addrinfo:protocol ai)))
86 (key "testkey")
87 (value "guix"))
88 (connect s (addrinfo:addr ai))
89
90 (test-equal "set"
91 "STORED\r"
92 (begin
93 (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
94 key
95 (string-length value)
96 value)
97 (read-line s)))
98
99 (test-equal "get"
100 (simple-format #f "VALUE ~A 0 ~A\r~A\r"
101 key
102 (string-length value)
103 value)
104 (begin
105 (simple-format s "get ~A\r\n" key)
106 (string-append
107 (read-line s)
108 (read-line s))))
109
110 (close-port s))
111
112 ;; There should be a log file in here.
113 (test-assert "log file"
114 (marionette-eval
115 '(file-exists? "/var/log/memcached")
116 marionette))
117
118 (test-end)
119 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
120
121 (gexp->derivation "memcached-test" test))
122
123 (define %test-memcached
124 (system-test
125 (name "memcached")
126 (description "Connect to a running MEMCACHED server.")
127 (value (run-memcached-test))))
128
129 (define %mongodb-os
130 (operating-system
131 (inherit
132 (simple-operating-system
133 (service dhcp-client-service-type)
134 (service mongodb-service-type)))
135 (packages (cons* mongodb
136 %base-packages))))
137
138 (define* (run-mongodb-test #:optional (port 27017))
139 "Run tests in %MONGODB-OS, forwarding PORT."
140 (define os
141 (marionette-operating-system
142 %mongodb-os
143 #:imported-modules '((gnu services herd)
144 (guix combinators))))
145
146 (define vm
147 (virtual-machine
148 (operating-system os)
149 (memory-size 1024)
150 (disk-image-size (* 1024 (expt 2 20)))
151 (port-forwardings `((27017 . ,port)))))
152
153 (define test
154 (with-imported-modules '((gnu build marionette))
155 #~(begin
156 (use-modules (srfi srfi-11) (srfi srfi-64)
157 (gnu build marionette)
158 (ice-9 popen)
159 (ice-9 rdelim))
160
161 (define marionette
162 (make-marionette (list #$vm)))
163
164 (mkdir #$output)
165 (chdir #$output)
166
167 (test-begin "mongodb")
168
169 (test-assert "service running"
170 (marionette-eval
171 '(begin
172 (use-modules (gnu services herd))
173 (match (start-service 'mongodb)
174 (#f #f)
175 (('service response-parts ...)
176 (match (assq-ref response-parts 'running)
177 ((pid) (number? pid))))))
178 marionette))
179
180 (test-eq "test insert"
181 0
182 (system* (string-append #$mongodb "/bin/mongo")
183 "test"
184 "--eval"
185 "db.testCollection.insert({data: 'test-data'})"))
186
187 (test-equal "test find"
188 "test-data"
189 (let* ((port (open-pipe*
190 OPEN_READ
191 (string-append #$mongodb "/bin/mongo")
192 "test"
193 "--quiet"
194 "--eval"
195 "db.testCollection.findOne().data"))
196 (output (read-line port))
197 (status (close-pipe port)))
198 output))
199
200 (test-end)
201 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
202
203 (gexp->derivation "mongodb-test" test))
204
205 (define %test-mongodb
206 (system-test
207 (name "mongodb")
208 (description "Connect to a running MONGODB server.")
209 (value (run-mongodb-test))))
210
211 \f
212 ;;;
213 ;;; The PostgreSQL service.
214 ;;;
215
216 (define %postgresql-os
217 (simple-operating-system
218 (service postgresql-service-type
219 (postgresql-configuration
220 (postgresql postgresql-10)))))
221
222 (define (run-postgresql-test)
223 "Run tests in %POSTGRESQL-OS."
224 (define os
225 (marionette-operating-system
226 %postgresql-os
227 #:imported-modules '((gnu services herd)
228 (guix combinators))))
229
230 (define vm
231 (virtual-machine
232 (operating-system os)
233 (memory-size 512)))
234
235 (define test
236 (with-imported-modules '((gnu build marionette))
237 #~(begin
238 (use-modules (srfi srfi-64)
239 (gnu build marionette))
240
241 (define marionette
242 (make-marionette (list #$vm)))
243
244 (mkdir #$output)
245 (chdir #$output)
246
247 (test-begin "postgresql")
248
249 (test-assert "service running"
250 (marionette-eval
251 '(begin
252 (use-modules (gnu services herd))
253 (start-service 'postgres))
254 marionette))
255
256 (test-end)
257 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
258
259 (gexp->derivation "postgresql-test" test))
260
261 (define %test-postgresql
262 (system-test
263 (name "postgresql")
264 (description "Start the PostgreSQL service.")
265 (value (run-postgresql-test))))
266
267 \f
268 ;;;
269 ;;; The MySQL service.
270 ;;;
271
272 (define %mysql-os
273 (simple-operating-system
274 (service mysql-service-type)))
275
276 (define* (run-mysql-test)
277 "Run tests in %MYSQL-OS."
278 (define os
279 (marionette-operating-system
280 %mysql-os
281 #:imported-modules '((gnu services herd)
282 (guix combinators))))
283
284 (define vm
285 (virtual-machine
286 (operating-system os)
287 (memory-size 512)))
288
289 (define test
290 (with-imported-modules '((gnu build marionette))
291 #~(begin
292 (use-modules (srfi srfi-11) (srfi srfi-64)
293 (gnu build marionette))
294
295 (define marionette
296 (make-marionette (list #$vm)))
297
298 (mkdir #$output)
299 (chdir #$output)
300
301 (test-begin "mysql")
302
303 (test-assert "service running"
304 (marionette-eval
305 '(begin
306 (use-modules (gnu services herd))
307 (match (start-service 'mysql)
308 (#f #f)
309 (('service response-parts ...)
310 (match (assq-ref response-parts 'running)
311 ((pid) (number? pid))))))
312 marionette))
313
314 (test-end)
315 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
316
317 (gexp->derivation "mysql-test" test))
318
319 (define %test-mysql
320 (system-test
321 (name "mysql")
322 (description "Start the MySQL service.")
323 (value (run-mysql-test))))