databases.scm 9.2 KB

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