databases.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  3. ;;; Copyright © 2020 Marius Bakke <marius@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu tests databases)
  20. #:use-module (gnu tests)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system file-systems)
  23. #:use-module (gnu system shadow)
  24. #:use-module (gnu system vm)
  25. #:use-module (gnu services)
  26. #:use-module (gnu services databases)
  27. #:use-module (gnu services networking)
  28. #:use-module (gnu packages databases)
  29. #:use-module (guix gexp)
  30. #:use-module (guix store)
  31. #:export (%test-memcached
  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. (test-runner-current (system-test-runner #$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. (gexp->derivation "memcached-test" test))
  104. (define %test-memcached
  105. (system-test
  106. (name "memcached")
  107. (description "Connect to a running MEMCACHED server.")
  108. (value (run-memcached-test))))
  109. ;;;
  110. ;;; The PostgreSQL service.
  111. ;;;
  112. (define %postgresql-log-directory
  113. "/var/log/postgresql")
  114. (define %role-log-file
  115. "/var/log/postgresql_roles.log")
  116. (define %postgresql-os
  117. (simple-operating-system
  118. (service postgresql-service-type
  119. (postgresql-configuration
  120. (postgresql postgresql-10)
  121. (config-file
  122. (postgresql-config-file
  123. (extra-config
  124. '(("session_preload_libraries" "auto_explain")
  125. ("random_page_cost" 2)
  126. ("auto_explain.log_min_duration" "100 ms")
  127. ("work_mem" "500 MB")
  128. ("debug_print_plan" #t)))))))
  129. (service postgresql-role-service-type
  130. (postgresql-role-configuration
  131. (roles
  132. (list (postgresql-role
  133. (name "root")
  134. (create-database? #t))))))))
  135. (define (run-postgresql-test)
  136. "Run tests in %POSTGRESQL-OS."
  137. (define os
  138. (marionette-operating-system
  139. %postgresql-os
  140. #:imported-modules '((gnu services herd)
  141. (guix combinators))))
  142. (define vm
  143. (virtual-machine
  144. (operating-system os)
  145. (memory-size 512)))
  146. (define test
  147. (with-imported-modules '((gnu build marionette))
  148. #~(begin
  149. (use-modules (srfi srfi-64)
  150. (gnu build marionette))
  151. (define marionette
  152. (make-marionette (list #$vm)))
  153. (test-runner-current (system-test-runner #$output))
  154. (test-begin "postgresql")
  155. (test-assert "service running"
  156. (marionette-eval
  157. '(begin
  158. (use-modules (gnu services herd))
  159. (start-service 'postgres))
  160. marionette))
  161. (test-assert "log-file"
  162. (marionette-eval
  163. '(begin
  164. (use-modules (ice-9 ftw)
  165. (ice-9 match))
  166. (current-output-port
  167. (open-file "/dev/console" "w0"))
  168. (let ((server-log-file
  169. (string-append #$%postgresql-log-directory
  170. "/pg_ctl.log")))
  171. (and (file-exists? server-log-file)
  172. (display
  173. (call-with-input-file server-log-file
  174. get-string-all)))
  175. #t))
  176. marionette))
  177. (test-assert "database ready"
  178. (begin
  179. (marionette-eval
  180. '(begin
  181. (let loop ((i 10))
  182. (unless (or (zero? i)
  183. (and (file-exists? #$%role-log-file)
  184. (string-contains
  185. (call-with-input-file #$%role-log-file
  186. get-string-all)
  187. ";\nCREATE DATABASE")))
  188. (sleep 1)
  189. (loop (- i 1)))))
  190. marionette)))
  191. (test-assert "database creation"
  192. (marionette-eval
  193. '(begin
  194. (use-modules (gnu services herd)
  195. (ice-9 popen))
  196. (current-output-port
  197. (open-file "/dev/console" "w0"))
  198. (let* ((port (open-pipe*
  199. OPEN_READ
  200. #$(file-append postgresql "/bin/psql")
  201. "-tA" "-c" "SELECT 1 FROM pg_database WHERE
  202. datname='root'"))
  203. (output (get-string-all port)))
  204. (close-pipe port)
  205. (string-contains output "1")))
  206. marionette))
  207. (test-end))))
  208. (gexp->derivation "postgresql-test" test))
  209. (define %test-postgresql
  210. (system-test
  211. (name "postgresql")
  212. (description "Start the PostgreSQL service.")
  213. (value (run-postgresql-test))))
  214. ;;;
  215. ;;; The MySQL service.
  216. ;;;
  217. (define %mysql-os
  218. (simple-operating-system
  219. (service mysql-service-type)))
  220. (define* (run-mysql-test)
  221. "Run tests in %MYSQL-OS."
  222. (define os
  223. (marionette-operating-system
  224. %mysql-os
  225. #:imported-modules '((gnu services herd)
  226. (guix combinators))))
  227. (define vm
  228. (virtual-machine
  229. (operating-system os)
  230. (memory-size 512)))
  231. (define test
  232. (with-imported-modules '((gnu build marionette))
  233. #~(begin
  234. (use-modules (srfi srfi-11) (srfi srfi-64)
  235. (gnu build marionette))
  236. (define marionette
  237. (make-marionette (list #$vm)))
  238. (test-runner-current (system-test-runner #$output))
  239. (test-begin "mysql")
  240. (test-assert "service running"
  241. (marionette-eval
  242. '(begin
  243. (use-modules (gnu services herd))
  244. (match (start-service 'mysql)
  245. (#f #f)
  246. (('service response-parts ...)
  247. (match (assq-ref response-parts 'running)
  248. ((pid) (number? pid))))))
  249. marionette))
  250. (test-assert "mysql_upgrade completed"
  251. (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette))
  252. (test-eq "create database"
  253. 0
  254. (marionette-eval
  255. '(begin
  256. (system* #$(file-append mariadb "/bin/mysql")
  257. "-e" "CREATE DATABASE guix;"))
  258. marionette))
  259. (test-eq "create table"
  260. 0
  261. (marionette-eval
  262. '(begin
  263. (system*
  264. #$(file-append mariadb "/bin/mysql") "guix"
  265. "-e" "CREATE TABLE facts (id INT, data VARCHAR(12));"))
  266. marionette))
  267. (test-eq "insert data"
  268. 0
  269. (marionette-eval
  270. '(begin
  271. (system* #$(file-append mariadb "/bin/mysql") "guix"
  272. "-e" "INSERT INTO facts VALUES (1, 'awesome')"))
  273. marionette))
  274. (test-equal "retrieve data"
  275. "awesome\n"
  276. (marionette-eval
  277. '(begin
  278. (use-modules (ice-9 popen))
  279. (let* ((port (open-pipe*
  280. OPEN_READ
  281. #$(file-append mariadb "/bin/mysql") "guix"
  282. "-NB" "-e" "SELECT data FROM facts WHERE id=1;"))
  283. (output (get-string-all port)))
  284. (close-pipe port)
  285. output))
  286. marionette))
  287. (test-end))))
  288. (gexp->derivation "mysql-test" test))
  289. (define %test-mysql
  290. (system-test
  291. (name "mysql")
  292. (description "Start the MySQL service.")
  293. (value (run-mysql-test))))