1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
3 ;;; Copyright © 2020 Marius Bakke <marius@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu tests databases)
21 #:use-module (gnu tests)
22 #:use-module (gnu system)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services databases)
28 #:use-module (gnu services networking)
29 #:use-module (gnu packages databases)
30 #:use-module (guix gexp)
31 #:use-module (guix store)
32 #: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))))
131 ;;; The PostgreSQL service.
134 (define %postgresql-log-directory
135 "/var/log/postgresql")
137 (define %role-log-file
138 "/var/log/postgresql_roles.log")
140 (define %postgresql-os
141 (simple-operating-system
142 (service postgresql-service-type
143 (postgresql-configuration
144 (postgresql postgresql-10)
146 (postgresql-config-file
148 '(("session_preload_libraries" "auto_explain")
149 ("random_page_cost" 2)
150 ("auto_explain.log_min_duration" "100 ms")
151 ("work_mem" "500 MB")
152 ("debug_print_plan" #t)))))))
153 (service postgresql-role-service-type
154 (postgresql-role-configuration
156 (list (postgresql-role
158 (create-database? #t))))))))
160 (define (run-postgresql-test)
161 "Run tests in %POSTGRESQL-OS."
163 (marionette-operating-system
165 #:imported-modules '((gnu services herd)
166 (guix combinators))))
170 (operating-system os)
174 (with-imported-modules '((gnu build marionette))
176 (use-modules (srfi srfi-64)
177 (gnu build marionette))
180 (make-marionette (list #$vm)))
185 (test-begin "postgresql")
187 (test-assert "service running"
190 (use-modules (gnu services herd))
191 (start-service 'postgres))
194 (test-assert "log-file"
197 (use-modules (ice-9 ftw)
200 (open-file "/dev/console" "w0"))
201 (let ((server-log-file
202 (string-append #$%postgresql-log-directory
204 (and (file-exists? server-log-file)
206 (call-with-input-file server-log-file
211 (test-assert "database ready"
216 (unless (or (zero? i)
217 (and (file-exists? #$%role-log-file)
219 (call-with-input-file #$%role-log-file
221 ";\nCREATE DATABASE")))
226 (test-assert "database creation"
229 (use-modules (gnu services herd)
232 (open-file "/dev/console" "w0"))
233 (let* ((port (open-pipe*
235 #$(file-append postgresql "/bin/psql")
236 "-tAh" "/var/run/postgresql"
237 "-c" "SELECT 1 FROM pg_database WHERE
239 (output (get-string-all port)))
241 (string-contains output "1")))
245 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
247 (gexp->derivation "postgresql-test" test))
249 (define %test-postgresql
252 (description "Start the PostgreSQL service.")
253 (value (run-postgresql-test))))
257 ;;; The MySQL service.
261 (simple-operating-system
262 (service mysql-service-type)))
264 (define* (run-mysql-test)
265 "Run tests in %MYSQL-OS."
267 (marionette-operating-system
269 #:imported-modules '((gnu services herd)
270 (guix combinators))))
274 (operating-system os)
278 (with-imported-modules '((gnu build marionette))
280 (use-modules (srfi srfi-11) (srfi srfi-64)
281 (gnu build marionette))
284 (make-marionette (list #$vm)))
291 (test-assert "service running"
294 (use-modules (gnu services herd))
295 (match (start-service 'mysql)
297 (('service response-parts ...)
298 (match (assq-ref response-parts 'running)
299 ((pid) (number? pid))))))
302 (test-assert "mysql_upgrade completed"
303 (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette))
305 (test-eq "create database"
309 (system* #$(file-append mariadb "/bin/mysql")
310 "-e" "CREATE DATABASE guix;"))
313 (test-eq "create table"
318 #$(file-append mariadb "/bin/mysql") "guix"
319 "-e" "CREATE TABLE facts (id INT, data VARCHAR(12));"))
322 (test-eq "insert data"
326 (system* #$(file-append mariadb "/bin/mysql") "guix"
327 "-e" "INSERT INTO facts VALUES (1, 'awesome')"))
330 (test-equal "retrieve data"
334 (use-modules (ice-9 popen))
335 (let* ((port (open-pipe*
337 #$(file-append mariadb "/bin/mysql") "guix"
338 "-NB" "-e" "SELECT data FROM facts WHERE id=1;"))
339 (output (get-string-all port)))
345 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
347 (gexp->derivation "mysql-test" test))
352 (description "Start the MySQL service.")
353 (value (run-mysql-test))))