databases.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  3. ;;; Copyright © 2020, 2022 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. #:use-module (srfi srfi-1)
  32. #:export (%test-memcached
  33. %test-postgresql
  34. %test-timescaledb
  35. %test-mysql))
  36. ;;;
  37. ;;; The Memcached service.
  38. ;;;
  39. (define %memcached-os
  40. (simple-operating-system
  41. (service dhcp-client-service-type)
  42. (service memcached-service-type)))
  43. (define* (run-memcached-test #:optional (port 11211))
  44. "Run tests in %MEMCACHED-OS, forwarding PORT."
  45. (define os
  46. (marionette-operating-system
  47. %memcached-os
  48. #:imported-modules '((gnu services herd)
  49. (guix combinators))))
  50. (define vm
  51. (virtual-machine
  52. (operating-system os)
  53. (port-forwardings `((11211 . ,port)))))
  54. (define test
  55. (with-imported-modules '((gnu build marionette))
  56. #~(begin
  57. (use-modules (srfi srfi-11) (srfi srfi-64)
  58. (gnu build marionette)
  59. (ice-9 rdelim))
  60. (define marionette
  61. (make-marionette (list #$vm)))
  62. (test-runner-current (system-test-runner #$output))
  63. (test-begin "memcached")
  64. ;; Wait for memcached to be up and running.
  65. (test-assert "service running"
  66. (marionette-eval
  67. '(begin
  68. (use-modules (gnu services herd))
  69. (match (start-service 'memcached)
  70. (#f #f)
  71. (('service response-parts ...)
  72. (match (assq-ref response-parts 'running)
  73. ((pid) (number? pid))))))
  74. marionette))
  75. (let* ((ai (car (getaddrinfo "localhost"
  76. #$(number->string port))))
  77. (s (socket (addrinfo:fam ai)
  78. (addrinfo:socktype ai)
  79. (addrinfo:protocol ai)))
  80. (key "testkey")
  81. (value "guix"))
  82. (connect s (addrinfo:addr ai))
  83. (test-equal "set"
  84. "STORED\r"
  85. (begin
  86. (simple-format s "set ~A 0 60 ~A\r\n~A\r\n"
  87. key
  88. (string-length value)
  89. value)
  90. (read-line s)))
  91. (test-equal "get"
  92. (simple-format #f "VALUE ~A 0 ~A\r~A\r"
  93. key
  94. (string-length value)
  95. value)
  96. (begin
  97. (simple-format s "get ~A\r\n" key)
  98. (string-append
  99. (read-line s)
  100. (read-line s))))
  101. (close-port s))
  102. ;; There should be a log file in here.
  103. (test-assert "log file"
  104. (marionette-eval
  105. '(file-exists? "/var/log/memcached")
  106. marionette))
  107. (test-end))))
  108. (gexp->derivation "memcached-test" test))
  109. (define %test-memcached
  110. (system-test
  111. (name "memcached")
  112. (description "Connect to a running MEMCACHED server.")
  113. (value (run-memcached-test))))
  114. ;;;
  115. ;;; The PostgreSQL service.
  116. ;;;
  117. (define %postgresql-log-directory
  118. "/var/log/postgresql")
  119. (define %role-log-file
  120. "/var/log/postgresql_roles.log")
  121. (define %postgresql-os
  122. (simple-operating-system
  123. (service postgresql-service-type
  124. (postgresql-configuration
  125. (postgresql postgresql)
  126. (config-file
  127. (postgresql-config-file
  128. (extra-config
  129. '(("session_preload_libraries" "auto_explain")
  130. ("random_page_cost" 2)
  131. ("auto_explain.log_min_duration" "100 ms")
  132. ("work_mem" "500 MB")
  133. ("debug_print_plan" #t)))))))
  134. (service postgresql-role-service-type
  135. (postgresql-role-configuration
  136. (roles
  137. (list (postgresql-role
  138. (name "root")
  139. (create-database? #t))))))))
  140. (define (run-postgresql-test)
  141. "Run tests in %POSTGRESQL-OS."
  142. (define os
  143. (marionette-operating-system
  144. %postgresql-os
  145. #:imported-modules '((gnu services herd)
  146. (guix combinators))))
  147. (define vm
  148. (virtual-machine
  149. (operating-system os)
  150. (memory-size 512)))
  151. (define test
  152. (with-imported-modules '((gnu build marionette))
  153. #~(begin
  154. (use-modules (srfi srfi-64)
  155. (gnu build marionette))
  156. (define marionette
  157. (make-marionette (list #$vm)))
  158. (test-runner-current (system-test-runner #$output))
  159. (test-begin "postgresql")
  160. (test-assert "service running"
  161. (marionette-eval
  162. '(begin
  163. (use-modules (gnu services herd))
  164. (start-service 'postgres))
  165. marionette))
  166. (test-assert "log-file"
  167. (marionette-eval
  168. '(begin
  169. (use-modules (ice-9 ftw)
  170. (ice-9 match)
  171. (rnrs io ports))
  172. (current-output-port
  173. (open-file "/dev/console" "w0"))
  174. (let ((server-log-file
  175. (string-append #$%postgresql-log-directory
  176. "/pg_ctl.log")))
  177. (and (file-exists? server-log-file)
  178. (display
  179. (call-with-input-file server-log-file
  180. get-string-all)))
  181. #t))
  182. marionette))
  183. (test-assert "database ready"
  184. (begin
  185. (marionette-eval
  186. '(begin
  187. (let loop ((i 10))
  188. (unless (or (zero? i)
  189. (and (file-exists? #$%role-log-file)
  190. (string-contains
  191. (call-with-input-file #$%role-log-file
  192. get-string-all)
  193. ";\nCREATE DATABASE")))
  194. (sleep 1)
  195. (loop (- i 1)))))
  196. marionette)))
  197. (test-assert "database creation"
  198. (marionette-eval
  199. '(begin
  200. (use-modules (gnu services herd)
  201. (ice-9 popen))
  202. (current-output-port
  203. (open-file "/dev/console" "w0"))
  204. (let* ((port (open-pipe*
  205. OPEN_READ
  206. #$(file-append postgresql "/bin/psql")
  207. "-tA" "-c" "SELECT 1 FROM pg_database WHERE
  208. datname='root'"))
  209. (output (get-string-all port)))
  210. (close-pipe port)
  211. (string-contains output "1")))
  212. marionette))
  213. (test-end))))
  214. (gexp->derivation "postgresql-test" test))
  215. (define %test-postgresql
  216. (system-test
  217. (name "postgresql")
  218. (description "Start the PostgreSQL service.")
  219. (value (run-postgresql-test))))
  220. ;; Test TimescaleDB, a PostgreSQL extension.
  221. (define %timescaledb-os
  222. (let* ((postgresql-services (operating-system-services %postgresql-os))
  223. (postgresql-service-configuration
  224. (service-value (find (lambda (svc)
  225. (eq? (service-kind svc) postgresql-service-type))
  226. postgresql-services)))
  227. (postgresql-role-service-configuration
  228. (service-value (find (lambda (svc)
  229. (eq? (service-kind svc)
  230. postgresql-role-service-type))
  231. postgresql-services))))
  232. (simple-operating-system
  233. (service postgresql-service-type
  234. (postgresql-configuration
  235. (inherit postgresql-service-configuration)
  236. (extension-packages (list timescaledb))
  237. (config-file
  238. (postgresql-config-file
  239. (inherit (postgresql-configuration-file
  240. postgresql-service-configuration))
  241. (extra-config
  242. (append '(("shared_preload_libraries" "timescaledb"))
  243. (postgresql-config-file-extra-config
  244. (postgresql-configuration-file
  245. postgresql-service-configuration))))))))
  246. (service postgresql-role-service-type
  247. (postgresql-role-configuration
  248. (inherit postgresql-role-service-configuration))))))
  249. (define (run-timescaledb-test)
  250. "Run tests in %TIMESCALEDB-OS."
  251. (define os
  252. (marionette-operating-system
  253. %timescaledb-os
  254. #:imported-modules '((gnu services herd)
  255. (guix combinators))))
  256. (define vm
  257. (virtual-machine
  258. (operating-system os)
  259. (memory-size 512)))
  260. (define test
  261. (with-imported-modules '((gnu build marionette))
  262. #~(begin
  263. (use-modules (srfi srfi-64)
  264. (gnu build marionette))
  265. (define marionette
  266. (make-marionette (list #$vm)))
  267. (test-runner-current (system-test-runner #$output))
  268. (test-begin "timescaledb")
  269. (test-assert "PostgreSQL running"
  270. (marionette-eval
  271. '(begin
  272. (use-modules (gnu services herd))
  273. (start-service 'postgres))
  274. marionette))
  275. (test-assert "database ready"
  276. (begin
  277. (marionette-eval
  278. '(begin
  279. (use-modules (rnrs io ports))
  280. (let loop ((i 10))
  281. (unless (or (zero? i)
  282. (and (file-exists? #$%role-log-file)
  283. (string-contains
  284. (call-with-input-file #$%role-log-file
  285. get-string-all)
  286. ";\nCREATE DATABASE")))
  287. (sleep 1)
  288. (loop (- i 1)))))
  289. marionette)))
  290. (test-assert "database creation"
  291. (marionette-eval
  292. '(begin
  293. (use-modules (guix build utils))
  294. (current-output-port (open-file "/dev/console" "w0"))
  295. (invoke #$(file-append postgresql "/bin/psql")
  296. "-tA" "-c" "CREATE DATABASE test"))
  297. marionette))
  298. (test-assert "load extension"
  299. (marionette-eval
  300. '(begin
  301. (current-output-port (open-file "/dev/console" "w0"))
  302. ;; Capture stderr for the next test.
  303. (current-error-port (open-file "timescaledb.stderr" "w0"))
  304. (invoke #$(file-append postgresql "/bin/psql")
  305. "-tA" "-c" "CREATE EXTENSION timescaledb"
  306. "test"))
  307. marionette))
  308. (test-assert "telemetry is disabled"
  309. (marionette-eval
  310. '(begin
  311. (string-contains (call-with-input-file "timescaledb.stderr"
  312. (lambda (port)
  313. (get-string-all port)))
  314. "Please enable telemetry"))
  315. marionette))
  316. (test-assert "create hypertable"
  317. (marionette-eval
  318. '(begin
  319. (current-output-port (open-file "/dev/console" "w0"))
  320. (invoke #$(file-append postgresql "/bin/psql")
  321. "-tA" "-c" "CREATE TABLE ht (
  322. time TIMESTAMP NOT NULL,
  323. data double PRECISION NULL
  324. )"
  325. "test")
  326. (invoke #$(file-append postgresql "/bin/psql")
  327. "-tA" "-c" "SELECT create_hypertable('ht','time')"
  328. "test"))
  329. marionette))
  330. (test-end))))
  331. (gexp->derivation "timescaledb-test" test))
  332. (define %test-timescaledb
  333. (system-test
  334. (name "timescaledb")
  335. (description "Test the TimescaleDB PostgreSQL extension.")
  336. (value (run-timescaledb-test))))
  337. ;;;
  338. ;;; The MySQL service.
  339. ;;;
  340. (define %mysql-os
  341. (simple-operating-system
  342. (service mysql-service-type
  343. (mysql-configuration
  344. ;; Disable O_DIRECT since it's not supported on overlayfs.
  345. ;; See <https://jira.mariadb.org/browse/MDEV-28751>.
  346. (extra-content "innodb-flush-method = fsync")))))
  347. (define* (run-mysql-test)
  348. "Run tests in %MYSQL-OS."
  349. (define os
  350. (marionette-operating-system
  351. %mysql-os
  352. #:imported-modules '((gnu services herd)
  353. (guix combinators))))
  354. (define vm
  355. (virtual-machine
  356. (operating-system os)
  357. (memory-size 512)))
  358. (define test
  359. (with-imported-modules '((gnu build marionette))
  360. #~(begin
  361. (use-modules (srfi srfi-11) (srfi srfi-64)
  362. (gnu build marionette))
  363. (define marionette
  364. (make-marionette (list #$vm)))
  365. (test-runner-current (system-test-runner #$output))
  366. (test-begin "mysql")
  367. (test-assert "service running"
  368. (marionette-eval
  369. '(begin
  370. (use-modules (gnu services herd))
  371. (match (start-service 'mysql)
  372. (#f #f)
  373. (('service response-parts ...)
  374. (match (assq-ref response-parts 'running)
  375. ((pid) (number? pid))))))
  376. marionette))
  377. (test-assert "mysql_upgrade completed"
  378. (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette))
  379. (test-assert "socket is ready"
  380. (wait-for-unix-socket "/run/mysqld/mysqld.sock" marionette))
  381. (test-eq "create database"
  382. 0
  383. (marionette-eval
  384. '(begin
  385. (system* #$(file-append mariadb "/bin/mysql")
  386. "-e" "CREATE DATABASE guix;"))
  387. marionette))
  388. (test-eq "create table"
  389. 0
  390. (marionette-eval
  391. '(begin
  392. (system*
  393. #$(file-append mariadb "/bin/mysql") "guix"
  394. "-e" "CREATE TABLE facts (id INT, data VARCHAR(12));"))
  395. marionette))
  396. (test-eq "insert data"
  397. 0
  398. (marionette-eval
  399. '(begin
  400. (system* #$(file-append mariadb "/bin/mysql") "guix"
  401. "-e" "INSERT INTO facts VALUES (1, 'awesome')"))
  402. marionette))
  403. (test-equal "retrieve data"
  404. "awesome\n"
  405. (marionette-eval
  406. '(begin
  407. (use-modules (ice-9 popen)
  408. (rnrs io ports))
  409. (let* ((port (open-pipe*
  410. OPEN_READ
  411. #$(file-append mariadb "/bin/mysql") "guix"
  412. "-NB" "-e" "SELECT data FROM facts WHERE id=1;"))
  413. (output (get-string-all port)))
  414. (close-pipe port)
  415. output))
  416. marionette))
  417. (test-end))))
  418. (gexp->derivation "mysql-test" test))
  419. (define %test-mysql
  420. (system-test
  421. (name "mysql")
  422. (description "Start the MySQL service.")
  423. (value (run-mysql-test))))