databases.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. (current-output-port
  172. (open-file "/dev/console" "w0"))
  173. (let ((server-log-file
  174. (string-append #$%postgresql-log-directory
  175. "/pg_ctl.log")))
  176. (and (file-exists? server-log-file)
  177. (display
  178. (call-with-input-file server-log-file
  179. get-string-all)))
  180. #t))
  181. marionette))
  182. (test-assert "database ready"
  183. (begin
  184. (marionette-eval
  185. '(begin
  186. (let loop ((i 10))
  187. (unless (or (zero? i)
  188. (and (file-exists? #$%role-log-file)
  189. (string-contains
  190. (call-with-input-file #$%role-log-file
  191. get-string-all)
  192. ";\nCREATE DATABASE")))
  193. (sleep 1)
  194. (loop (- i 1)))))
  195. marionette)))
  196. (test-assert "database creation"
  197. (marionette-eval
  198. '(begin
  199. (use-modules (gnu services herd)
  200. (ice-9 popen))
  201. (current-output-port
  202. (open-file "/dev/console" "w0"))
  203. (let* ((port (open-pipe*
  204. OPEN_READ
  205. #$(file-append postgresql "/bin/psql")
  206. "-tA" "-c" "SELECT 1 FROM pg_database WHERE
  207. datname='root'"))
  208. (output (get-string-all port)))
  209. (close-pipe port)
  210. (string-contains output "1")))
  211. marionette))
  212. (test-end))))
  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. ;; Test TimescaleDB, a PostgreSQL extension.
  220. (define %timescaledb-os
  221. (let* ((postgresql-services (operating-system-services %postgresql-os))
  222. (postgresql-service-configuration
  223. (service-value (find (lambda (svc)
  224. (eq? (service-kind svc) postgresql-service-type))
  225. postgresql-services)))
  226. (postgresql-role-service-configuration
  227. (service-value (find (lambda (svc)
  228. (eq? (service-kind svc)
  229. postgresql-role-service-type))
  230. postgresql-services))))
  231. (simple-operating-system
  232. (service postgresql-service-type
  233. (postgresql-configuration
  234. (inherit postgresql-service-configuration)
  235. (extension-packages (list timescaledb))
  236. (config-file
  237. (postgresql-config-file
  238. (inherit (postgresql-configuration-file
  239. postgresql-service-configuration))
  240. (extra-config
  241. (append '(("shared_preload_libraries" "timescaledb"))
  242. (postgresql-config-file-extra-config
  243. (postgresql-configuration-file
  244. postgresql-service-configuration))))))))
  245. (service postgresql-role-service-type
  246. (postgresql-role-configuration
  247. (inherit postgresql-role-service-configuration))))))
  248. (define (run-timescaledb-test)
  249. "Run tests in %TIMESCALEDB-OS."
  250. (define os
  251. (marionette-operating-system
  252. %timescaledb-os
  253. #:imported-modules '((gnu services herd)
  254. (guix combinators))))
  255. (define vm
  256. (virtual-machine
  257. (operating-system os)
  258. (memory-size 512)))
  259. (define test
  260. (with-imported-modules '((gnu build marionette))
  261. #~(begin
  262. (use-modules (srfi srfi-64)
  263. (gnu build marionette))
  264. (define marionette
  265. (make-marionette (list #$vm)))
  266. (test-runner-current (system-test-runner #$output))
  267. (test-begin "timescaledb")
  268. (test-assert "PostgreSQL running"
  269. (marionette-eval
  270. '(begin
  271. (use-modules (gnu services herd))
  272. (start-service 'postgres))
  273. marionette))
  274. (test-assert "database ready"
  275. (begin
  276. (marionette-eval
  277. '(begin
  278. (let loop ((i 10))
  279. (unless (or (zero? i)
  280. (and (file-exists? #$%role-log-file)
  281. (string-contains
  282. (call-with-input-file #$%role-log-file
  283. get-string-all)
  284. ";\nCREATE DATABASE")))
  285. (sleep 1)
  286. (loop (- i 1)))))
  287. marionette)))
  288. (test-assert "database creation"
  289. (marionette-eval
  290. '(begin
  291. (current-output-port
  292. (open-file "/dev/console" "w0"))
  293. (invoke #$(file-append postgresql "/bin/psql")
  294. "-tA" "-c" "CREATE DATABASE test"))
  295. marionette))
  296. (test-assert "load extension"
  297. (marionette-eval
  298. '(begin
  299. (current-output-port (open-file "/dev/console" "w0"))
  300. ;; Capture stderr for the next test.
  301. (current-error-port (open-file "timescaledb.stderr" "w0"))
  302. (invoke #$(file-append postgresql "/bin/psql")
  303. "-tA" "-c" "CREATE EXTENSION timescaledb"
  304. "test"))
  305. marionette))
  306. (test-assert "telemetry is disabled"
  307. (marionette-eval
  308. '(begin
  309. (string-contains (call-with-input-file "timescaledb.stderr"
  310. (lambda (port)
  311. (get-string-all port)))
  312. "Please enable telemetry"))
  313. marionette))
  314. (test-assert "create hypertable"
  315. (marionette-eval
  316. '(begin
  317. (current-output-port (open-file "/dev/console" "w0"))
  318. (invoke #$(file-append postgresql "/bin/psql")
  319. "-tA" "-c" "CREATE TABLE ht (
  320. time TIMESTAMP NOT NULL,
  321. data double PRECISION NULL
  322. )"
  323. "test")
  324. (invoke #$(file-append postgresql "/bin/psql")
  325. "-tA" "-c" "SELECT create_hypertable('ht','time')"
  326. "test"))
  327. marionette))
  328. (test-end))))
  329. (gexp->derivation "timescaledb-test" test))
  330. (define %test-timescaledb
  331. (system-test
  332. (name "timescaledb")
  333. (description "Test the TimescaleDB PostgreSQL extension.")
  334. (value (run-timescaledb-test))))
  335. ;;;
  336. ;;; The MySQL service.
  337. ;;;
  338. (define %mysql-os
  339. (simple-operating-system
  340. (service mysql-service-type)))
  341. (define* (run-mysql-test)
  342. "Run tests in %MYSQL-OS."
  343. (define os
  344. (marionette-operating-system
  345. %mysql-os
  346. #:imported-modules '((gnu services herd)
  347. (guix combinators))))
  348. (define vm
  349. (virtual-machine
  350. (operating-system os)
  351. (memory-size 512)))
  352. (define test
  353. (with-imported-modules '((gnu build marionette))
  354. #~(begin
  355. (use-modules (srfi srfi-11) (srfi srfi-64)
  356. (gnu build marionette))
  357. (define marionette
  358. (make-marionette (list #$vm)))
  359. (test-runner-current (system-test-runner #$output))
  360. (test-begin "mysql")
  361. (test-assert "service running"
  362. (marionette-eval
  363. '(begin
  364. (use-modules (gnu services herd))
  365. (match (start-service 'mysql)
  366. (#f #f)
  367. (('service response-parts ...)
  368. (match (assq-ref response-parts 'running)
  369. ((pid) (number? pid))))))
  370. marionette))
  371. (test-assert "mysql_upgrade completed"
  372. (wait-for-file "/var/lib/mysql/mysql_upgrade_info" marionette))
  373. (test-eq "create database"
  374. 0
  375. (marionette-eval
  376. '(begin
  377. (system* #$(file-append mariadb "/bin/mysql")
  378. "-e" "CREATE DATABASE guix;"))
  379. marionette))
  380. (test-eq "create table"
  381. 0
  382. (marionette-eval
  383. '(begin
  384. (system*
  385. #$(file-append mariadb "/bin/mysql") "guix"
  386. "-e" "CREATE TABLE facts (id INT, data VARCHAR(12));"))
  387. marionette))
  388. (test-eq "insert data"
  389. 0
  390. (marionette-eval
  391. '(begin
  392. (system* #$(file-append mariadb "/bin/mysql") "guix"
  393. "-e" "INSERT INTO facts VALUES (1, 'awesome')"))
  394. marionette))
  395. (test-equal "retrieve data"
  396. "awesome\n"
  397. (marionette-eval
  398. '(begin
  399. (use-modules (ice-9 popen))
  400. (let* ((port (open-pipe*
  401. OPEN_READ
  402. #$(file-append mariadb "/bin/mysql") "guix"
  403. "-NB" "-e" "SELECT data FROM facts WHERE id=1;"))
  404. (output (get-string-all port)))
  405. (close-pipe port)
  406. output))
  407. marionette))
  408. (test-end))))
  409. (gexp->derivation "mysql-test" test))
  410. (define %test-mysql
  411. (system-test
  412. (name "mysql")
  413. (description "Start the MySQL service.")
  414. (value (run-mysql-test))))