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