web.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. (marionette-eval
  416. '(begin
  417. (use-modules (gnu services herd))
  418. (start-service 'tailon))
  419. marionette))
  420. (test-equal "http-get"
  421. 200
  422. (#$retry-on-error
  423. (lambda ()
  424. (let-values (((response text)
  425. (http-get #$(format
  426. #f
  427. "http://localhost:~A/"
  428. http-port)
  429. #:decode-body? #t)))
  430. (response-code response)))
  431. #:times 10
  432. #:delay 5))
  433. (test-end))))
  434. (gexp->derivation "tailon-test" test))
  435. (define %test-tailon
  436. (system-test
  437. (name "tailon")
  438. (description "Connect to a running Tailon server.")
  439. (value (run-tailon-test))))
  440. ;;;
  441. ;;; Patchwork
  442. ;;;
  443. (define (patchwork-initial-database-setup-service configuration)
  444. (define start-gexp
  445. #~(lambda ()
  446. (let ((pid (primitive-fork))
  447. (postgres (getpwnam "postgres")))
  448. (if (eq? pid 0)
  449. (dynamic-wind
  450. (const #t)
  451. (lambda ()
  452. (setgid (passwd:gid postgres))
  453. (setuid (passwd:uid postgres))
  454. (primitive-exit
  455. (if (and
  456. (zero?
  457. (system* #$(file-append postgresql "/bin/createuser")
  458. #$(patchwork-database-configuration-user
  459. configuration)))
  460. (zero?
  461. (system* #$(file-append postgresql "/bin/createdb")
  462. "-O"
  463. #$(patchwork-database-configuration-user
  464. configuration)
  465. #$(patchwork-database-configuration-name
  466. configuration))))
  467. 0
  468. 1)))
  469. (lambda ()
  470. (primitive-exit 1)))
  471. (zero? (cdr (waitpid pid)))))))
  472. (shepherd-service
  473. (requirement '(postgres))
  474. (provision '(patchwork-postgresql-user-and-database))
  475. (start start-gexp)
  476. (stop #~(const #f))
  477. (respawn? #f)
  478. (documentation "Setup patchwork database.")))
  479. (define (patchwork-os patchwork)
  480. (simple-operating-system
  481. (service dhcp-client-service-type)
  482. (service httpd-service-type
  483. (httpd-configuration
  484. (config
  485. (httpd-config-file
  486. (listen '("8080"))))))
  487. (service postgresql-service-type
  488. (postgresql-configuration
  489. (postgresql postgresql)))
  490. (service patchwork-service-type
  491. (patchwork-configuration
  492. (patchwork patchwork)
  493. (domain "localhost")
  494. (settings-module
  495. (patchwork-settings-module
  496. (allowed-hosts (list domain))
  497. (default-from-email "")))
  498. (getmail-retriever-config
  499. (getmail-retriever-configuration
  500. (type "SimpleIMAPSSLRetriever")
  501. (server "imap.example.com")
  502. (port 993)
  503. (username "username")
  504. (password "password")
  505. (extra-parameters
  506. '((mailboxes . ("INBOX"))))))))
  507. (simple-service 'patchwork-database-setup
  508. shepherd-root-service-type
  509. (list
  510. (patchwork-initial-database-setup-service
  511. (patchwork-database-configuration))))))
  512. (define (run-patchwork-test patchwork)
  513. "Run tests in %NGINX-OS, which has nginx running and listening on
  514. HTTP-PORT."
  515. (define os
  516. (marionette-operating-system
  517. (patchwork-os patchwork)
  518. #:imported-modules '((gnu services herd)
  519. (guix combinators))))
  520. (define forwarded-port 8080)
  521. (define vm
  522. (virtual-machine
  523. (operating-system os)
  524. (port-forwardings `((8080 . ,forwarded-port)))
  525. (memory-size 1024)))
  526. (define test
  527. (with-imported-modules '((gnu build marionette))
  528. #~(begin
  529. (use-modules (srfi srfi-11) (srfi srfi-64)
  530. (ice-9 match)
  531. (gnu build marionette)
  532. (web uri)
  533. (web client)
  534. (web response))
  535. (define marionette
  536. (make-marionette (list #$vm)))
  537. (test-runner-current (system-test-runner #$output))
  538. (test-begin "patchwork")
  539. (test-assert "patchwork-postgresql-user-and-service started"
  540. (marionette-eval
  541. '(begin
  542. (use-modules (gnu services herd))
  543. (match (start-service 'patchwork-postgresql-user-and-database)
  544. (#f #f)
  545. (('service response-parts ...)
  546. (match (assq-ref response-parts 'running)
  547. ((#t) #t)
  548. ((pid) (number? pid))))))
  549. marionette))
  550. (test-assert "httpd running"
  551. (marionette-eval
  552. '(begin
  553. (use-modules (gnu services herd))
  554. (start-service 'httpd))
  555. marionette))
  556. (test-equal "http-get"
  557. 200
  558. (#$retry-on-error
  559. (lambda ()
  560. (let-values
  561. (((response text)
  562. (http-get #$(simple-format
  563. #f "http://localhost:~A/" forwarded-port)
  564. #:decode-body? #t)))
  565. (response-code response)))
  566. #:times 10
  567. #:delay 5))
  568. (test-end))))
  569. (gexp->derivation "patchwork-test" test))
  570. (define %test-patchwork
  571. (system-test
  572. (name "patchwork")
  573. (description "Connect to a running Patchwork service.")
  574. (value (run-patchwork-test patchwork))))