databases.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. (define-module (gnu tests databases)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system file-systems)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services databases)
  26. #:use-module (gnu services networking)
  27. #:use-module (gnu packages databases)
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:export (%test-memcached
  31. %test-mongodb
  32. %test-mysql))
  33. (define %memcached-os
  34. (simple-operating-system
  35. (dhcp-client-service)
  36. (service memcached-service-type)))
  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. (define vm
  45. (virtual-machine
  46. (operating-system os)
  47. (port-forwardings `((11211 . ,port)))))
  48. (define test
  49. (with-imported-modules '((gnu build marionette))
  50. #~(begin
  51. (use-modules (srfi srfi-11) (srfi srfi-64)
  52. (gnu build marionette)
  53. (ice-9 rdelim))
  54. (define marionette
  55. (make-marionette (list #$vm)))
  56. (mkdir #$output)
  57. (chdir #$output)
  58. (test-begin "memcached")
  59. ;; Wait for memcached to be up and running.
  60. (test-assert "service running"
  61. (marionette-eval
  62. '(begin
  63. (use-modules (gnu services herd))
  64. (match (start-service 'memcached)
  65. (#f #f)
  66. (('service response-parts ...)
  67. (match (assq-ref response-parts 'running)
  68. ((pid) (number? pid))))))
  69. marionette))
  70. (let* ((ai (car (getaddrinfo "localhost"
  71. #$(number->string port))))
  72. (s (socket (addrinfo:fam ai)
  73. (addrinfo:socktype ai)
  74. (addrinfo:protocol ai)))
  75. (key "testkey")
  76. (value "guix"))
  77. (connect s (addrinfo:addr ai))
  78. (test-equal "set"
  79. "STORED\r"
  80. (begin
  81. (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
  82. key
  83. (string-length value)
  84. value)
  85. (read-line s)))
  86. (test-equal "get"
  87. (simple-format #f "VALUE ~A 0 ~A\r~A\r"
  88. key
  89. (string-length value)
  90. value)
  91. (begin
  92. (simple-format s "get ~A\r\n" key)
  93. (string-append
  94. (read-line s)
  95. (read-line s))))
  96. (close-port s))
  97. ;; There should be a log file in here.
  98. (test-assert "log file"
  99. (marionette-eval
  100. '(file-exists? "/var/log/memcached")
  101. marionette))
  102. (test-end)
  103. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  104. (gexp->derivation "memcached-test" test))
  105. (define %test-memcached
  106. (system-test
  107. (name "memcached")
  108. (description "Connect to a running MEMCACHED server.")
  109. (value (run-memcached-test))))
  110. (define %mongodb-os
  111. (operating-system
  112. (inherit
  113. (simple-operating-system
  114. (dhcp-client-service)
  115. (service mongodb-service-type)))
  116. (packages (cons* mongodb
  117. %base-packages))))
  118. (define* (run-mongodb-test #:optional (port 27017))
  119. "Run tests in %MONGODB-OS, forwarding PORT."
  120. (define os
  121. (marionette-operating-system
  122. %mongodb-os
  123. #:imported-modules '((gnu services herd)
  124. (guix combinators))))
  125. (define vm
  126. (virtual-machine
  127. (operating-system os)
  128. (memory-size 1024)
  129. (disk-image-size (* 1024 (expt 2 20)))
  130. (port-forwardings `((27017 . ,port)))))
  131. (define test
  132. (with-imported-modules '((gnu build marionette))
  133. #~(begin
  134. (use-modules (srfi srfi-11) (srfi srfi-64)
  135. (gnu build marionette)
  136. (ice-9 popen)
  137. (ice-9 rdelim))
  138. (define marionette
  139. (make-marionette (list #$vm)))
  140. (mkdir #$output)
  141. (chdir #$output)
  142. (test-begin "mongodb")
  143. (test-assert "service running"
  144. (marionette-eval
  145. '(begin
  146. (use-modules (gnu services herd))
  147. (match (start-service 'mongodb)
  148. (#f #f)
  149. (('service response-parts ...)
  150. (match (assq-ref response-parts 'running)
  151. ((pid) (number? pid))))))
  152. marionette))
  153. (test-eq "test insert"
  154. 0
  155. (system* (string-append #$mongodb "/bin/mongo")
  156. "test"
  157. "--eval"
  158. "db.testCollection.insert({data: 'test-data'})"))
  159. (test-equal "test find"
  160. "test-data"
  161. (let* ((port (open-pipe*
  162. OPEN_READ
  163. (string-append #$mongodb "/bin/mongo")
  164. "test"
  165. "--quiet"
  166. "--eval"
  167. "db.testCollection.findOne().data"))
  168. (output (read-line port))
  169. (status (close-pipe port)))
  170. output))
  171. (test-end)
  172. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  173. (gexp->derivation "mongodb-test" test))
  174. (define %test-mongodb
  175. (system-test
  176. (name "mongodb")
  177. (description "Connect to a running MONGODB server.")
  178. (value (run-mongodb-test))))
  179. ;;;
  180. ;;; The MySQL service.
  181. ;;;
  182. (define %mysql-os
  183. (simple-operating-system
  184. (mysql-service)))
  185. (define* (run-mysql-test)
  186. "Run tests in %MYSQL-OS."
  187. (define os
  188. (marionette-operating-system
  189. %mysql-os
  190. #:imported-modules '((gnu services herd)
  191. (guix combinators))))
  192. (define vm
  193. (virtual-machine
  194. (operating-system os)
  195. (memory-size 512)))
  196. (define test
  197. (with-imported-modules '((gnu build marionette))
  198. #~(begin
  199. (use-modules (srfi srfi-11) (srfi srfi-64)
  200. (gnu build marionette))
  201. (define marionette
  202. (make-marionette (list #$vm)))
  203. (mkdir #$output)
  204. (chdir #$output)
  205. (test-begin "mysql")
  206. (test-assert "service running"
  207. (marionette-eval
  208. '(begin
  209. (use-modules (gnu services herd))
  210. (match (start-service 'mysql)
  211. (#f #f)
  212. (('service response-parts ...)
  213. (match (assq-ref response-parts 'running)
  214. ((pid) (number? pid))))))
  215. marionette))
  216. (test-end)
  217. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  218. (gexp->derivation "mysql-test" test))
  219. (define %test-mysql
  220. (system-test
  221. (name "mysql")
  222. (description "Start the MySQL service.")
  223. (value (run-mysql-test))))