web.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020-2021, 2023 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. #~(hpcweb-configuration
  364. (title-prefix "[TEST] HPCGUIX-WEB")))
  365. (define %hpcguix-web-os
  366. (simple-operating-system
  367. (service dhcp-client-service-type)
  368. (service hpcguix-web-service-type
  369. (hpcguix-web-configuration
  370. (specs %hpcguix-web-specs)
  371. (address "0.0.0.0")))))
  372. (define %test-hpcguix-web
  373. (system-test
  374. (name "hpcguix-web")
  375. (description "Connect to a running hpcguix-web server.")
  376. (value (run-hpcguix-web-server-test name %hpcguix-web-os))))
  377. (define %tailon-os
  378. ;; Operating system under test.
  379. (simple-operating-system
  380. (service dhcp-client-service-type)
  381. (service tailon-service-type
  382. (tailon-configuration
  383. (config-file
  384. (tailon-configuration-file
  385. (bind "0.0.0.0:8080")))))))
  386. (define* (run-tailon-test #:optional (http-port 8081))
  387. "Run tests in %TAILON-OS, which has tailon running and listening on
  388. HTTP-PORT."
  389. (define os
  390. (marionette-operating-system
  391. %tailon-os
  392. #:imported-modules '((gnu services herd)
  393. (guix combinators))))
  394. (define vm
  395. (virtual-machine
  396. (operating-system os)
  397. (port-forwardings `((,http-port . 8080)))))
  398. (define test
  399. (with-imported-modules '((gnu build marionette))
  400. #~(begin
  401. (use-modules (srfi srfi-11) (srfi srfi-64)
  402. (ice-9 match)
  403. (gnu build marionette)
  404. (web uri)
  405. (web client)
  406. (web response))
  407. (define marionette
  408. ;; Forward the guest's HTTP-PORT, where tailon is listening, to
  409. ;; port 8080 in the host.
  410. (make-marionette (list #$vm)))
  411. (test-runner-current (system-test-runner #$output))
  412. (test-begin "tailon")
  413. (test-assert "service running"
  414. (wait-for-tcp-port 8080 marionette))
  415. (test-equal "http-get"
  416. 200
  417. (#$retry-on-error
  418. (lambda ()
  419. (let-values (((response text)
  420. (http-get #$(format
  421. #f
  422. "http://localhost:~A/"
  423. http-port)
  424. #:decode-body? #t)))
  425. (response-code response)))
  426. #:times 10
  427. #:delay 5))
  428. (test-end))))
  429. (gexp->derivation "tailon-test" test))
  430. (define %test-tailon
  431. (system-test
  432. (name "tailon")
  433. (description "Connect to a running Tailon server.")
  434. (value (run-tailon-test))))
  435. ;;;
  436. ;;; Patchwork
  437. ;;;
  438. (define (patchwork-initial-database-setup-service configuration)
  439. (define start-gexp
  440. #~(lambda ()
  441. (let ((pid (primitive-fork))
  442. (postgres (getpwnam "postgres")))
  443. (if (eq? pid 0)
  444. (dynamic-wind
  445. (const #t)
  446. (lambda ()
  447. (setgid (passwd:gid postgres))
  448. (setuid (passwd:uid postgres))
  449. (primitive-exit
  450. (if (and
  451. (zero?
  452. (system* #$(file-append postgresql "/bin/createuser")
  453. #$(patchwork-database-configuration-user
  454. configuration)))
  455. (zero?
  456. (system* #$(file-append postgresql "/bin/createdb")
  457. "-O"
  458. #$(patchwork-database-configuration-user
  459. configuration)
  460. #$(patchwork-database-configuration-name
  461. configuration))))
  462. 0
  463. 1)))
  464. (lambda ()
  465. (primitive-exit 1)))
  466. (zero? (cdr (waitpid pid)))))))
  467. (shepherd-service
  468. (requirement '(postgres))
  469. (provision '(patchwork-postgresql-user-and-database))
  470. (start start-gexp)
  471. (stop #~(const #f))
  472. (respawn? #f)
  473. (documentation "Setup patchwork database.")))
  474. (define (patchwork-os patchwork)
  475. (simple-operating-system
  476. (service dhcp-client-service-type)
  477. (service httpd-service-type
  478. (httpd-configuration
  479. (config
  480. (httpd-config-file
  481. (listen '("8080"))))))
  482. (service postgresql-service-type
  483. (postgresql-configuration
  484. (postgresql postgresql)))
  485. (service patchwork-service-type
  486. (patchwork-configuration
  487. (patchwork patchwork)
  488. (domain "localhost")
  489. (settings-module
  490. (patchwork-settings-module
  491. (allowed-hosts (list domain))
  492. (default-from-email "")))
  493. (getmail-retriever-config
  494. (getmail-retriever-configuration
  495. (type "SimpleIMAPSSLRetriever")
  496. (server "imap.example.com")
  497. (port 993)
  498. (username "username")
  499. (password "password")
  500. (extra-parameters
  501. '((mailboxes . ("INBOX"))))))))
  502. (simple-service 'patchwork-database-setup
  503. shepherd-root-service-type
  504. (list
  505. (patchwork-initial-database-setup-service
  506. (patchwork-database-configuration))))))
  507. (define (run-patchwork-test patchwork)
  508. "Run tests in %NGINX-OS, which has nginx running and listening on
  509. HTTP-PORT."
  510. (define os
  511. (marionette-operating-system
  512. (patchwork-os patchwork)
  513. #:imported-modules '((gnu services herd)
  514. (guix combinators))))
  515. (define forwarded-port 8080)
  516. (define vm
  517. (virtual-machine
  518. (operating-system os)
  519. (port-forwardings `((8080 . ,forwarded-port)))
  520. (memory-size 1024)))
  521. (define test
  522. (with-imported-modules '((gnu build marionette))
  523. #~(begin
  524. (use-modules (srfi srfi-11) (srfi srfi-64)
  525. (ice-9 match)
  526. (gnu build marionette)
  527. (web uri)
  528. (web client)
  529. (web response))
  530. (define marionette
  531. (make-marionette (list #$vm)))
  532. (test-runner-current (system-test-runner #$output))
  533. (test-begin "patchwork")
  534. (test-assert "patchwork-postgresql-user-and-service started"
  535. (marionette-eval
  536. '(begin
  537. (use-modules (gnu services herd))
  538. (match (start-service 'patchwork-postgresql-user-and-database)
  539. (#f #f)
  540. (('service response-parts ...)
  541. (match (assq-ref response-parts 'running)
  542. ((#t) #t)
  543. ((pid) (number? pid))))))
  544. marionette))
  545. (test-assert "httpd running"
  546. (marionette-eval
  547. '(begin
  548. (use-modules (gnu services herd))
  549. (start-service 'httpd))
  550. marionette))
  551. (test-equal "http-get"
  552. 200
  553. (#$retry-on-error
  554. (lambda ()
  555. (let-values
  556. (((response text)
  557. (http-get #$(simple-format
  558. #f "http://localhost:~A/" forwarded-port)
  559. #:decode-body? #t)))
  560. (response-code response)))
  561. #:times 10
  562. #:delay 5))
  563. (test-end))))
  564. (gexp->derivation "patchwork-test" test))
  565. (define %test-patchwork
  566. (system-test
  567. (name "patchwork")
  568. (description "Connect to a running Patchwork service.")
  569. (value (run-patchwork-test patchwork))))