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
35 (simple-operating-system
37 (service memcached-service-type)))
39 (define* (run-memcached-test #:optional (port 11211))
40 "Run tests in %MEMCACHED-OS, forwarding PORT."
42 (marionette-operating-system
44 #:imported-modules '((gnu services herd)
50 (port-forwardings `((11211 . ,port)))))
53 (with-imported-modules '((gnu build marionette))
55 (use-modules (srfi srfi-11) (srfi srfi-64)
56 (gnu build marionette)
60 (make-marionette (list #$vm)))
65 (test-begin "memcached")
67 ;; Wait for memcached to be up and running.
68 (test-assert "service running"
71 (use-modules (gnu services herd))
72 (match (start-service 'memcached)
74 (('service response-parts ...)
75 (match (assq-ref response-parts 'running)
76 ((pid) (number? pid))))))
79 (let* ((ai (car (getaddrinfo "localhost"
80 #$(number->string port))))
81 (s (socket (addrinfo:fam ai)
82 (addrinfo:socktype ai)
83 (addrinfo:protocol ai)))
86 (connect s (addrinfo:addr ai))
91 (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
98 (simple-format #f "VALUE ~A 0 ~A\r~A\r"
100 (string-length value)
103 (simple-format s "get ~A\r\n" key)
110 ;; There should be a log file in here.
111 (test-assert "log file"
113 '(file-exists? "/var/log/memcached")
117 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
119 (gexp->derivation "memcached-test" test))
121 (define %test-memcached
124 (description "Connect to a running MEMCACHED server.")
125 (value (run-memcached-test))))
130 (simple-operating-system
131 (dhcp-client-service)
132 (service mongodb-service-type)))
133 (packages (cons* mongodb
136 (define* (run-mongodb-test #:optional (port 27017))
137 "Run tests in %MONGODB-OS, forwarding PORT."
139 (marionette-operating-system
141 #:imported-modules '((gnu services herd)
142 (guix combinators))))
146 (operating-system os)
148 (disk-image-size (* 1024 (expt 2 20)))
149 (port-forwardings `((27017 . ,port)))))
152 (with-imported-modules '((gnu build marionette))
154 (use-modules (srfi srfi-11) (srfi srfi-64)
155 (gnu build marionette)
160 (make-marionette (list #$vm)))
165 (test-begin "mongodb")
167 (test-assert "service running"
170 (use-modules (gnu services herd))
171 (match (start-service 'mongodb)
173 (('service response-parts ...)
174 (match (assq-ref response-parts 'running)
175 ((pid) (number? pid))))))
178 (test-eq "test insert"
180 (system* (string-append #$mongodb "/bin/mongo")
183 "db.testCollection.insert({data: 'test-data'})"))
185 (test-equal "test find"
187 (let* ((port (open-pipe*
189 (string-append #$mongodb "/bin/mongo")
193 "db.testCollection.findOne().data"))
194 (output (read-line port))
195 (status (close-pipe port)))
199 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
201 (gexp->derivation "mongodb-test" test))
203 (define %test-mongodb
206 (description "Connect to a running MONGODB server.")
207 (value (run-mongodb-test))))