Merge branch 'master' into staging
[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 ((i 0))
101 (pk 'try i)
102 (catch 'system-error
103 (lambda ()
104 (connect sock AF_INET INADDR_LOOPBACK 2628))
105 (lambda args
106 (pk 'connection-error args)
107 (when (< i 20)
108 (sleep 1)
109 (loop (+ 1 i))))))
110 (read-line sock 'concat)))
111 marionette))
112
113 (test-assert "connect"
114 (let ((addr (make-socket-address AF_INET INADDR_LOOPBACK 8000)))
115 (connect %dico-socket addr)
116 (read-line %dico-socket 'concat)))
117
118 (test-equal "CLIENT"
119 "250 ok\r\n"
120 (begin
121 (display "CLIENT \"GNU Guile\"\r\n" %dico-socket)
122 (read-line %dico-socket 'concat)))
123
124 (test-assert "DEFINE"
125 (begin
126 (display "DEFINE ! hello\r\n" %dico-socket)
127 (display "QUIT\r\n" %dico-socket)
128 (let ((result (read-string %dico-socket)))
129 (and (string-contains result "gcide")
130 (string-contains result "hello")
131 result))))
132
133 (test-end)
134 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
135
136 (gexp->derivation "dicod" test)))
137
138 (define %test-dicod
139 (system-test
140 (name "dicod")
141 (description "Connect to the dicod DICT server.")
142 (value (run-dicod-test))))