databases.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. (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. ;;;
  112. ;;; The PostgreSQL service.
  113. ;;;
  114. (define %postgresql-log-directory
  115. "/var/log/postgresql")
  116. (define %role-log-file
  117. "/var/log/postgresql_roles.log")
  118. (define %postgresql-os
  119. (simple-operating-system
  120. (service postgresql-service-type
  121. (postgresql-configuration
  122. (postgresql postgresql-10)
  123. (config-file
  124. (postgresql-config-file
  125. (extra-config
  126. '(("session_preload_libraries" "auto_explain")
  127. ("random_page_cost" 2)
  128. ("auto_explain.log_min_duration" "100 ms")
  129. ("work_mem" "500 MB")
  130. ("debug_print_plan" #t)))))))
  131. (service postgresql-role-service-type
  132. (postgresql-role-configuration
  133. (roles
  134. (list (postgresql-role
  135. (name "root")
  136. (create-database? #t))))))))
  137. (define (run-postgresql-test)
  138. "Run tests in %POSTGRESQL-OS."
  139. (define os
  140. (marionette-operating-system
  141. %postgresql-os
  142. #:imported-modules '((gnu services herd)
  143. (guix combinators))))
  144. (define vm
  145. (virtual-machine
  146. (operating-system os)
  147. (memory-size 512)))
  148. (define test
  149. (with-imported-modules '((gnu build marionette))
  150. #~(begin
  151. (use-modules (srfi srfi-64)
  152. (gnu build marionette))
  153. (define marionette
  154. (make-marionette (list #$vm)))
  155. (mkdir #$output)
  156. (chdir #$output)
  157. (test-begin "postgresql")
  158. (test-assert "service running"
  159. (marionette-eval
  160. '(begin
  161. (use-modules (gnu services herd))
  162. (start-service 'postgres))
  163. marionette))
  164. (test-assert "log-file"
  165. (marionette-eval
  166. '(begin
  167. (use-modules (ice-9 ftw)
  168. (ice-9 match))
  169. (current-output-port
  170. (open-file "/dev/console" "w0"))
  171. (let ((server-log-file
  172. (string-append #$%postgresql-log-directory
  173. "/pg_ctl.log")))
  174. (and (file-exists? server-log-file)
  175. (display
  176. (call-with-input-file server-log-file
  177. get-string-all)))
  178. #t))
  179. marionette))
  180. (test-assert "database ready"
  181. (begin
  182. (marionette-eval
  183. '(begin
  184. (let loop ((i 10))
  185. (unless (or (zero? i)
  186. (and (file-exists? #$%role-log-file)
  187. (string-contains
  188. (call-with-input-file #$%role-log-file
  189. get-string-all)
  190. ";\nCREATE DATABASE")))
  191. (sleep 1)
  192. (loop (- i 1)))))
  193. marionette)))
  194. (test-assert "database creation"
  195. (marionette-eval
  196. '(begin
  197. (use-modules (gnu services herd)
  198. (ice-9 popen))
  199. (current-output-port
  200. (open-file "/dev/console" "w0"))
  201. (let* ((port (open-pipe*
  202. OPEN_READ
  203. #$(file-append postgresql "/bin/psql")
  204. "-tAh" "/tmp"
  205. "-c" "SELECT 1 FROM pg_database WHERE
  206. datname='root'"))
  207. (output (get-string-all port)))
  208. (close-pipe port)
  209. (string-contains output "1")))
  210. marionette))
  211. (test-end)
  212. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  213. (gexp->derivation "postgresql-test" test))
  214. (define %test-postgresql
  215. (system-test
  216. (name "postgresql")
  217. (description "Start the PostgreSQL service.")
  218. (value (run-postgresql-test))))
  219. ;;;
  220. ;;; The MySQL service.
  221. ;;;
  222. (define %mysql-os
  223. (simple-operating-system
  224. (service mysql-service-type)))
  225. (define* (run-mysql-test)
  226. "Run tests in %MYSQL-OS."
  227. (define os
  228. (marionette-operating-system
  229. %mysql-os
  230. #:imported-modules '((gnu services herd)
  231. (guix combinators))))
  232. (define vm
  233. (virtual-machine
  234. (operating-system os)
  235. (memory-size 512)))
  236. (define test
  237. (with-imported-modules '((gnu build marionette))
  238. #~(begin
  239. (use-modules (srfi srfi-11) (srfi srfi-64)
  240. (gnu build marionette))
  241. (define marionette
  242. (make-marionette (list #$vm)))
  243. (mkdir #$output)
  244. (chdir #$output)
  245. (test-begin "mysql")
  246. (test-assert "service running"
  247. (marionette-eval
  248. '(begin
  249. (use-modules (gnu services herd))
  250. (match (start-service 'mysql)
  251. (#f #f)
  252. (('service response-parts ...)
  253. (match (assq-ref response-parts 'running)
  254. ((pid) (number? pid))))))
  255. marionette))
  256. (test-assert "mysql_upgrade completed"
  257. (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette))
  258. (test-eq "create database"
  259. 0
  260. (marionette-eval
  261. '(begin
  262. (system* #$(file-append mariadb "/bin/mysql")
  263. "-e" "CREATE DATABASE guix;"))
  264. marionette))
  265. (test-eq "create table"
  266. 0
  267. (marionette-eval
  268. '(begin
  269. (system*
  270. #$(file-append mariadb "/bin/mysql") "guix"
  271. "-e" "CREATE TABLE facts (id INT, data VARCHAR(12));"))
  272. marionette))
  273. (test-eq "insert data"
  274. 0
  275. (marionette-eval
  276. '(begin
  277. (system* #$(file-append mariadb "/bin/mysql") "guix"
  278. "-e" "INSERT INTO facts VALUES (1, 'awesome')"))
  279. marionette))
  280. (test-equal "retrieve data"
  281. "awesome\n"
  282. (marionette-eval
  283. '(begin
  284. (use-modules (ice-9 popen))
  285. (let* ((port (open-pipe*
  286. OPEN_READ
  287. #$(file-append mariadb "/bin/mysql") "guix"
  288. "-NB" "-e" "SELECT data FROM facts WHERE id=1;"))
  289. (output (get-string-all port)))
  290. (close-pipe port)
  291. output))
  292. marionette))
  293. (test-end)
  294. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  295. (gexp->derivation "mysql-test" test))
  296. (define %test-mysql
  297. (system-test
  298. (name "mysql")
  299. (description "Start the MySQL service.")
  300. (value (run-mysql-test))))