web.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020-2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017, 2019 Christopher Baines <mail@cbaines.net>
  4. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  5. ;;; Copyright © 2018 Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
  6. ;;; Copyright © 2018 Marius Bakke <mbakke@fastmail.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu tests web)
  23. #:use-module (gnu tests)
  24. #:use-module (gnu system)
  25. #:use-module (gnu system file-systems)
  26. #:use-module (gnu system shadow)
  27. #:use-module (gnu system vm)
  28. #:use-module (gnu services)
  29. #:use-module (gnu services web)
  30. #:use-module (gnu services databases)
  31. #:use-module (gnu services getmail)
  32. #:use-module (gnu services networking)
  33. #:use-module (gnu services shepherd)
  34. #:use-module (gnu services mail)
  35. #:use-module (gnu packages databases)
  36. #:use-module (gnu packages patchutils)
  37. #:use-module (gnu packages python)
  38. #:use-module (gnu packages web)
  39. #:use-module (guix packages)
  40. #:use-module (guix modules)
  41. #:use-module (guix records)
  42. #:use-module (guix gexp)
  43. #:use-module (guix store)
  44. #:use-module (guix utils)
  45. #:use-module (ice-9 match)
  46. #:export (%test-httpd
  47. %test-nginx
  48. %test-varnish
  49. %test-php-fpm
  50. %test-hpcguix-web
  51. %test-tailon
  52. %test-patchwork))
  53. (define %index.html-contents
  54. ;; Contents of the /index.html file.
  55. "Hello, guix!")
  56. (define %make-http-root
  57. ;; Create our server root in /srv.
  58. #~(begin
  59. (mkdir "/srv")
  60. (mkdir "/srv/http")
  61. (call-with-output-file "/srv/http/index.html"
  62. (lambda (port)
  63. (display #$%index.html-contents port)))))
  64. (define retry-on-error
  65. #~(lambda* (f #:key times delay)
  66. (let loop ((attempt 1))
  67. (match (catch
  68. #t
  69. (lambda ()
  70. (cons #t
  71. (f)))
  72. (lambda args
  73. (cons #f
  74. args)))
  75. ((#t . return-value)
  76. return-value)
  77. ((#f . error-args)
  78. (if (>= attempt times)
  79. error-args
  80. (begin
  81. (sleep delay)
  82. (loop (+ 1 attempt)))))))))
  83. (define* (run-webserver-test name test-os #:key (log-file #f) (http-port 8080))
  84. "Run tests in %NGINX-OS, which has nginx running and listening on
  85. HTTP-PORT."
  86. (define os
  87. (marionette-operating-system
  88. test-os
  89. #:imported-modules '((gnu services herd)
  90. (guix combinators))))
  91. (define forwarded-port 8080)
  92. (define vm
  93. (virtual-machine
  94. (operating-system os)
  95. (port-forwardings `((,http-port . ,forwarded-port)))))
  96. (define test
  97. (with-imported-modules '((gnu build marionette))
  98. #~(begin
  99. (use-modules (srfi srfi-11) (srfi srfi-64)
  100. (gnu build marionette)
  101. (web uri)
  102. (web client)
  103. (web response))
  104. (define marionette
  105. (make-marionette (list #$vm)))
  106. (test-runner-current (system-test-runner #$output))
  107. (test-begin #$name)
  108. (test-assert #$(string-append name " service running")
  109. (marionette-eval
  110. '(begin
  111. (use-modules (gnu services herd))
  112. (match (start-service '#$(string->symbol name))
  113. (#f #f)
  114. (('service response-parts ...)
  115. (match (assq-ref response-parts 'running)
  116. ((#t) #t)
  117. ((pid) (number? pid))))))
  118. marionette))
  119. (test-assert "HTTP port ready"
  120. (wait-for-tcp-port #$forwarded-port marionette))
  121. ;; Retrieve the index.html file we put in /srv.
  122. (test-equal "http-get"
  123. '(200 #$%index.html-contents)
  124. (let-values
  125. (((response text)
  126. (http-get #$(simple-format
  127. #f "http://localhost:~A/index.html" forwarded-port)
  128. #:decode-body? #t)))
  129. (list (response-code response) text)))
  130. #$@(if log-file
  131. `((test-assert ,(string-append "log file exists " log-file)
  132. (marionette-eval
  133. '(file-exists? ,log-file)
  134. marionette)))
  135. '())
  136. (test-end))))
  137. (gexp->derivation (string-append name "-test") test))
  138. ;;;
  139. ;;; HTTPD
  140. ;;;
  141. (define %httpd-os
  142. (simple-operating-system
  143. (service dhcp-client-service-type)
  144. (service httpd-service-type
  145. (httpd-configuration
  146. (config
  147. (httpd-config-file
  148. (listen '("8080"))))))
  149. (simple-service 'make-http-root activation-service-type
  150. %make-http-root)))
  151. (define %test-httpd
  152. (system-test
  153. (name "httpd")
  154. (description "Connect to a running HTTPD server.")
  155. (value (run-webserver-test name %httpd-os
  156. #:log-file "/var/log/httpd/error_log"))))
  157. ;;;
  158. ;;; NGINX
  159. ;;;
  160. (define %nginx-servers
  161. ;; Server blocks.
  162. (list (nginx-server-configuration
  163. (listen '("8080")))))
  164. (define %nginx-os
  165. ;; Operating system under test.
  166. (simple-operating-system
  167. (service dhcp-client-service-type)
  168. (service nginx-service-type
  169. (nginx-configuration
  170. (log-directory "/var/log/nginx")
  171. (server-blocks %nginx-servers)))
  172. (simple-service 'make-http-root activation-service-type
  173. %make-http-root)))
  174. (define %test-nginx
  175. (system-test
  176. (name "nginx")
  177. (description "Connect to a running NGINX server.")
  178. (value (run-webserver-test name %nginx-os
  179. #:log-file "/var/log/nginx/access.log"))))
  180. ;;;
  181. ;;; Varnish
  182. ;;;
  183. (define %varnish-vcl
  184. (mixed-text-file
  185. "varnish-test.vcl"
  186. "vcl 4.0;
  187. backend dummy { .host = \"127.1.1.1\"; }
  188. sub vcl_recv { return(synth(200, \"OK\")); }
  189. sub vcl_synth {
  190. synthetic(\"" %index.html-contents "\");
  191. set resp.http.Content-Type = \"text/plain\";
  192. return(deliver);
  193. }"))
  194. (define %varnish-os
  195. (simple-operating-system
  196. (service dhcp-client-service-type)
  197. ;; Pretend to be a web server that serves %index.html-contents.
  198. (service varnish-service-type
  199. (varnish-configuration
  200. (name "/tmp/server")
  201. ;; Use a small VSL buffer to fit in the test VM.
  202. (parameters '(("vsl_space" . "4M")))
  203. (vcl %varnish-vcl)))
  204. ;; Proxy the "server" using the builtin configuration.
  205. (service varnish-service-type
  206. (varnish-configuration
  207. (parameters '(("vsl_space" . "4M")))
  208. (backend "localhost:80")
  209. (listen '(":8080"))))))
  210. (define %test-varnish
  211. (system-test
  212. (name "varnish")
  213. (description "Test the Varnish Cache server.")
  214. (value (run-webserver-test "varnish-default" %varnish-os))))
  215. ;;;
  216. ;;; PHP-FPM
  217. ;;;
  218. (define %make-php-fpm-http-root
  219. ;; Create our server root in /srv.
  220. #~(begin
  221. (mkdir "/srv")
  222. (call-with-output-file "/srv/index.php"
  223. (lambda (port)
  224. (display "<?php
  225. phpinfo();
  226. echo(\"Computed by php:\".((string)(2+3)));
  227. ?>\n" port)))))
  228. (define %php-fpm-nginx-server-blocks
  229. (list (nginx-server-configuration
  230. (root "/srv")
  231. (locations
  232. (list (nginx-php-location)))
  233. (listen '("8042"))
  234. (ssl-certificate #f)
  235. (ssl-certificate-key #f))))
  236. (define %php-fpm-os
  237. ;; Operating system under test.
  238. (simple-operating-system
  239. (service dhcp-client-service-type)
  240. (service php-fpm-service-type)
  241. (service nginx-service-type
  242. (nginx-configuration
  243. (server-blocks %php-fpm-nginx-server-blocks)))
  244. (simple-service 'make-http-root activation-service-type
  245. %make-php-fpm-http-root)))
  246. (define* (run-php-fpm-test #:optional (http-port 8042))
  247. "Run tests in %PHP-FPM-OS, which has nginx running and listening on
  248. HTTP-PORT, along with php-fpm."
  249. (define os
  250. (marionette-operating-system
  251. %php-fpm-os
  252. #:imported-modules '((gnu services herd)
  253. (guix combinators))))
  254. (define vm
  255. (virtual-machine
  256. (operating-system os)
  257. (port-forwardings `((8080 . ,http-port)))))
  258. (define test
  259. (with-imported-modules '((gnu build marionette)
  260. (guix build utils))
  261. #~(begin
  262. (use-modules (srfi srfi-11) (srfi srfi-64)
  263. (gnu build marionette)
  264. (web uri)
  265. (web client)
  266. (web response))
  267. (define marionette
  268. (make-marionette (list #$vm)))
  269. (test-runner-current (system-test-runner #$output))
  270. (test-begin "php-fpm")
  271. (test-assert "php-fpm running"
  272. (marionette-eval
  273. '(begin
  274. (use-modules (gnu services herd))
  275. (match (start-service 'php-fpm)
  276. (#f #f)
  277. (('service response-parts ...)
  278. (match (assq-ref response-parts 'running)
  279. ((pid) (number? pid))))))
  280. marionette))
  281. (test-assert "nginx running"
  282. (marionette-eval
  283. '(begin
  284. (use-modules (gnu services herd))
  285. (start-service 'nginx))
  286. marionette))
  287. (test-equal "http-get"
  288. 200
  289. (let-values (((response text)
  290. (http-get "http://localhost:8080/index.php"
  291. #:decode-body? #t)))
  292. (response-code response)))
  293. (test-equal "php computed result is sent"
  294. "Computed by php:5"
  295. (let-values (((response text)
  296. (http-get "http://localhost:8080/index.php"
  297. #:decode-body? #t)))
  298. (begin
  299. (use-modules (ice-9 regex))
  300. (let ((matches (string-match "Computed by php:5" text)))
  301. (and matches
  302. (match:substring matches 0))))))
  303. (test-end))))
  304. (gexp->derivation "php-fpm-test" test))
  305. (define %test-php-fpm
  306. (system-test
  307. (name "php-fpm")
  308. (description "Test PHP-FPM through nginx.")
  309. (value (run-php-fpm-test))))
  310. ;;;
  311. ;;; hpcguix-web
  312. ;;;
  313. (define* (run-hpcguix-web-server-test name test-os)
  314. "Run tests in %HPCGUIX-WEB-OS, which has hpcguix-web running."
  315. (define os
  316. (marionette-operating-system
  317. test-os
  318. #:imported-modules '((gnu services herd)
  319. (guix combinators))))
  320. (define vm
  321. (virtual-machine
  322. (operating-system os)
  323. (port-forwardings '((8080 . 5000)))
  324. (memory-size 1024)))
  325. (define test
  326. (with-imported-modules '((gnu build marionette))
  327. #~(begin
  328. (use-modules (srfi srfi-11) (srfi srfi-64)
  329. (ice-9 match)
  330. (gnu build marionette)
  331. (web uri)
  332. (web client)
  333. (web response))
  334. (define marionette
  335. (make-marionette (list #$vm)))
  336. (test-runner-current (system-test-runner #$output))
  337. (test-begin #$name)
  338. (test-assert "hpcguix-web running"
  339. (marionette-eval
  340. '(begin
  341. (use-modules (gnu services herd))
  342. (match (start-service 'hpcguix-web)
  343. (#f #f)
  344. (('service response-parts ...)
  345. (match (assq-ref response-parts 'running)
  346. ((pid) (number? pid))))))
  347. marionette))
  348. (test-equal "http-get"
  349. 200
  350. (begin
  351. (wait-for-tcp-port 5000 marionette)
  352. (#$retry-on-error
  353. (lambda ()
  354. (let-values (((response text)
  355. (http-get "http://localhost:8080")))
  356. (response-code response)))
  357. #:times 10
  358. #:delay 5)))
  359. (test-end))))
  360. (gexp->derivation (string-append name "-test") test))
  361. (define %hpcguix-web-specs
  362. ;; Server config gexp.
  363. #~(define site-config
  364. (hpcweb-configuration
  365. (title-prefix "[TEST] HPCGUIX-WEB"))))
  366. (define %hpcguix-web-os
  367. (simple-operating-system
  368. (service dhcp-client-service-type)
  369. (service hpcguix-web-service-type
  370. (hpcguix-web-configuration
  371. (specs %hpcguix-web-specs)
  372. (address "0.0.0.0")))))
  373. (define %test-hpcguix-web
  374. (system-test
  375. (name "hpcguix-web")
  376. (description "Connect to a running hpcguix-web server.")
  377. (value (run-hpcguix-web-server-test name %hpcguix-web-os))))
  378. (define %tailon-os
  379. ;; Operating system under test.
  380. (simple-operating-system
  381. (service dhcp-client-service-type)
  382. (service tailon-service-type
  383. (tailon-configuration
  384. (config-file
  385. (tailon-configuration-file
  386. (bind "0.0.0.0:8080")))))))
  387. (define* (run-tailon-test #:optional (http-port 8081))
  388. "Run tests in %TAILON-OS, which has tailon running and listening on
  389. HTTP-PORT."
  390. (define os
  391. (marionette-operating-system
  392. %tailon-os
  393. #:imported-modules '((gnu services herd)
  394. (guix combinators))))
  395. (define vm
  396. (virtual-machine
  397. (operating-system os)
  398. (port-forwardings `((,http-port . 8080)))))
  399. (define test
  400. (with-imported-modules '((gnu build marionette))
  401. #~(begin
  402. (use-modules (srfi srfi-11) (srfi srfi-64)
  403. (ice-9 match)
  404. (gnu build marionette)
  405. (web uri)
  406. (web client)
  407. (web response))
  408. (define marionette
  409. ;; Forward the guest's HTTP-PORT, where tailon is listening, to
  410. ;; port 8080 in the host.
  411. (make-marionette (list #$vm)))
  412. (test-runner-current (system-test-runner #$output))
  413. (test-begin "tailon")
  414. (test-assert "service running"
  415. (wait-for-tcp-port 8080 marionette))
  416. (test-equal "http-get"
  417. 200
  418. (#$retry-on-error
  419. (lambda ()
  420. (let-values (((response text)
  421. (http-get #$(format
  422. #f
  423. "http://localhost:~A/"
  424. http-port)
  425. #:decode-body? #t)))
  426. (response-code response)))
  427. #:times 10
  428. #:delay 5))
  429. (test-end))))
  430. (gexp->derivation "tailon-test" test))
  431. (define %test-tailon
  432. (system-test
  433. (name "tailon")
  434. (description "Connect to a running Tailon server.")
  435. (value (run-tailon-test))))
  436. ;;;
  437. ;;; Patchwork
  438. ;;;
  439. (define (patchwork-initial-database-setup-service configuration)
  440. (define start-gexp
  441. #~(lambda ()
  442. (let ((pid (primitive-fork))
  443. (postgres (getpwnam "postgres")))
  444. (if (eq? pid 0)
  445. (dynamic-wind
  446. (const #t)
  447. (lambda ()
  448. (setgid (passwd:gid postgres))
  449. (setuid (passwd:uid postgres))
  450. (primitive-exit
  451. (if (and
  452. (zero?
  453. (system* #$(file-append postgresql "/bin/createuser")
  454. #$(patchwork-database-configuration-user
  455. configuration)))
  456. (zero?
  457. (system* #$(file-append postgresql "/bin/createdb")
  458. "-O"
  459. #$(patchwork-database-configuration-user
  460. configuration)
  461. #$(patchwork-database-configuration-name
  462. configuration))))
  463. 0
  464. 1)))
  465. (lambda ()
  466. (primitive-exit 1)))
  467. (zero? (cdr (waitpid pid)))))))
  468. (shepherd-service
  469. (requirement '(postgres))
  470. (provision '(patchwork-postgresql-user-and-database))
  471. (start start-gexp)
  472. (stop #~(const #f))
  473. (respawn? #f)
  474. (documentation "Setup patchwork database.")))
  475. (define (patchwork-os patchwork)
  476. (simple-operating-system
  477. (service dhcp-client-service-type)
  478. (service httpd-service-type
  479. (httpd-configuration
  480. (config
  481. (httpd-config-file
  482. (listen '("8080"))))))
  483. (service postgresql-service-type
  484. (postgresql-configuration
  485. (postgresql postgresql)))
  486. (service patchwork-service-type
  487. (patchwork-configuration
  488. (patchwork patchwork)
  489. (domain "localhost")
  490. (settings-module
  491. (patchwork-settings-module
  492. (allowed-hosts (list domain))
  493. (default-from-email "")))
  494. (getmail-retriever-config
  495. (getmail-retriever-configuration
  496. (type "SimpleIMAPSSLRetriever")
  497. (server "imap.example.com")
  498. (port 993)
  499. (username "username")
  500. (password "password")
  501. (extra-parameters
  502. '((mailboxes . ("INBOX"))))))))
  503. (simple-service 'patchwork-database-setup
  504. shepherd-root-service-type
  505. (list
  506. (patchwork-initial-database-setup-service
  507. (patchwork-database-configuration))))))
  508. (define (run-patchwork-test patchwork)
  509. "Run tests in %NGINX-OS, which has nginx running and listening on
  510. HTTP-PORT."
  511. (define os
  512. (marionette-operating-system
  513. (patchwork-os patchwork)
  514. #:imported-modules '((gnu services herd)
  515. (guix combinators))))
  516. (define forwarded-port 8080)
  517. (define vm
  518. (virtual-machine
  519. (operating-system os)
  520. (port-forwardings `((8080 . ,forwarded-port)))
  521. (memory-size 1024)))
  522. (define test
  523. (with-imported-modules '((gnu build marionette))
  524. #~(begin
  525. (use-modules (srfi srfi-11) (srfi srfi-64)
  526. (ice-9 match)
  527. (gnu build marionette)
  528. (web uri)
  529. (web client)
  530. (web response))
  531. (define marionette
  532. (make-marionette (list #$vm)))
  533. (test-runner-current (system-test-runner #$output))
  534. (test-begin "patchwork")
  535. (test-assert "patchwork-postgresql-user-and-service started"
  536. (marionette-eval
  537. '(begin
  538. (use-modules (gnu services herd))
  539. (match (start-service 'patchwork-postgresql-user-and-database)
  540. (#f #f)
  541. (('service response-parts ...)
  542. (match (assq-ref response-parts 'running)
  543. ((#t) #t)
  544. ((pid) (number? pid))))))
  545. marionette))
  546. (test-assert "httpd running"
  547. (marionette-eval
  548. '(begin
  549. (use-modules (gnu services herd))
  550. (start-service 'httpd))
  551. marionette))
  552. (test-equal "http-get"
  553. 200
  554. (#$retry-on-error
  555. (lambda ()
  556. (let-values
  557. (((response text)
  558. (http-get #$(simple-format
  559. #f "http://localhost:~A/" forwarded-port)
  560. #:decode-body? #t)))
  561. (response-code response)))
  562. #:times 10
  563. #:delay 5))
  564. (test-end))))
  565. (gexp->derivation "patchwork-test" test))
  566. (define %test-patchwork
  567. (system-test
  568. (name "patchwork")
  569. (description "Connect to a running Patchwork service.")
  570. (value (run-patchwork-test patchwork))))