dict.scm 4.9 KB

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