services: Add memcached.
[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 (guix gexp)
29 #:use-module (guix store)
30 #:export (%test-memcached))
31
32 (define %memcached-os
33 (simple-operating-system
34 (dhcp-client-service)
35 (service memcached-service-type)))
36
37 (define* (run-memcached-test #:optional (port 11211))
38 "Run tests in %MEMCACHED-OS, forwarding PORT."
39 (define os
40 (marionette-operating-system
41 %memcached-os
42 #:imported-modules '((gnu services herd)
43 (guix combinators))))
44
45 (define vm
46 (virtual-machine
47 (operating-system os)
48 (port-forwardings `((11211 . ,port)))))
49
50 (define test
51 (with-imported-modules '((gnu build marionette))
52 #~(begin
53 (use-modules (srfi srfi-11) (srfi srfi-64)
54 (gnu build marionette)
55 (ice-9 rdelim))
56
57 (define marionette
58 (make-marionette (list #$vm)))
59
60 (mkdir #$output)
61 (chdir #$output)
62
63 (test-begin "memcached")
64
65 ;; Wait for memcached to be up and running.
66 (test-eq "service running"
67 'running!
68 (marionette-eval
69 '(begin
70 (use-modules (gnu services herd))
71 (start-service 'memcached)
72 'running!)
73 marionette))
74
75 (let* ((ai (car (getaddrinfo "localhost"
76 #$(number->string port))))
77 (s (socket (addrinfo:fam ai)
78 (addrinfo:socktype ai)
79 (addrinfo:protocol ai)))
80 (key "testkey")
81 (value "guix"))
82 (connect s (addrinfo:addr ai))
83
84 (test-equal "set"
85 "STORED\r"
86 (begin
87 (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
88 key
89 (string-length value)
90 value)
91 (read-line s)))
92
93 (test-equal "get"
94 (simple-format #f "VALUE ~A 0 ~A\r~A\r"
95 key
96 (string-length value)
97 value)
98 (begin
99 (simple-format s "get ~A\r\n" key)
100 (string-append
101 (read-line s)
102 (read-line s))))
103
104 (close-port s))
105
106 ;; There should be a log file in here.
107 (test-assert "log file"
108 (marionette-eval
109 '(file-exists? "/var/log/memcached")
110 marionette))
111
112 (test-end)
113 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
114
115 (gexp->derivation "memcached-test" test))
116
117 (define %test-memcached
118 (system-test
119 (name "memcached")
120 (description "Connect to a running MEMCACHED server.")
121 (value (run-memcached-test))))