tests: Add GNU dicod test.
[jackhill/guix/guix.git] / gnu / tests / dict.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
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 dict)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system nss)
23 #:use-module (gnu system vm)
24 #:use-module (gnu services)
25 #:use-module (gnu services dict)
26 #:use-module (gnu services networking)
27 #:use-module (gnu packages wordnet)
28 #:use-module (guix gexp)
29 #:use-module (guix store)
30 #:use-module (guix monads)
31 #:use-module (guix packages)
32 #:use-module (guix modules)
33 #:export (%test-dicod))
34
35 (define %dicod-os
36 (simple-operating-system
37 (dhcp-client-service)
38 (service dicod-service-type
39 (dicod-configuration
40 (interfaces '("0.0.0.0"))
41 (handlers (list (dicod-handler
42 (name "wordnet")
43 (module "dictorg")
44 (options
45 ;; XXX: Not useful since WordNet does not
46 ;; provide DICT-formatted data?
47 (list #~(string-append "dbdir=" #$wordnet))))))
48 (databases (list (dicod-database
49 (name "wordnet")
50 (complex? #t)
51 (handler "wordnet")
52 (options '("database=wn")))
53 %dicod-database:gcide))))))
54
55 (define* (run-dicod-test)
56 "Run tests of 'dicod-service-type'."
57 (mlet* %store-monad ((os -> (marionette-operating-system
58 %dicod-os
59 #:imported-modules
60 (source-module-closure '((gnu services herd)))))
61 (command (system-qemu-image/shared-store-script
62 os #:graphic? #f)))
63 (define test
64 (with-imported-modules '((gnu build marionette))
65 #~(begin
66 (use-modules (ice-9 rdelim)
67 (ice-9 regex)
68 (srfi srfi-64)
69 (gnu build marionette))
70 (define marionette
71 ;; Forward the guest's DICT port to local port 8000.
72 (make-marionette (list #$command "-net"
73 "user,hostfwd=tcp::8000-:2628")))
74
75 (define %dico-socket
76 (socket PF_INET SOCK_STREAM 0))
77
78 (mkdir #$output)
79 (chdir #$output)
80
81 (test-begin "dicod")
82
83 ;; Wait for the service to be started.
84 (test-eq "service is running"
85 'running!
86 (marionette-eval
87 '(begin
88 (use-modules (gnu services herd))
89 (start-service 'dicod)
90 'running!)
91 marionette))
92
93 ;; Wait until dicod is actually listening.
94 ;; TODO: Use a PID file instead.
95 (test-assert "connect inside"
96 (marionette-eval
97 '(begin
98 (use-modules (ice-9 rdelim))
99 (let ((sock (socket PF_INET SOCK_STREAM 0)))
100 (let loop ()
101 (pk 'try)
102 (catch 'system-error
103 (lambda ()
104 (connect sock AF_INET INADDR_LOOPBACK 2628))
105 (lambda args
106 (pk 'connection-error args)
107 (sleep 1)
108 (loop))))
109 (read-line sock 'concat)))
110 marionette))
111
112 (test-assert "connect"
113 (let ((addr (make-socket-address AF_INET INADDR_LOOPBACK 8000)))
114 (connect %dico-socket addr)
115 (read-line %dico-socket 'concat)))
116
117 (test-equal "CLIENT"
118 "250 ok\r\n"
119 (begin
120 (display "CLIENT \"GNU Guile\"\r\n" %dico-socket)
121 (read-line %dico-socket 'concat)))
122
123 (test-assert "DEFINE"
124 (begin
125 (display "DEFINE ! hello\r\n" %dico-socket)
126 (display "QUIT\r\n" %dico-socket)
127 (let ((result (read-string %dico-socket)))
128 (and (string-contains result "gcide")
129 (string-contains result "hello")
130 result))))
131
132 (test-end)
133 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
134
135 (gexp->derivation "dicod" test)))
136
137 (define %test-dicod
138 (system-test
139 (name "dicod")
140 (description "Connect to the dicod DICT server.")
141 (value (run-dicod-test))))