web.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 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* (run-webserver-test name test-os #:key (log-file #f) (http-port 8080))
  65. "Run tests in %NGINX-OS, which has nginx running and listening on
  66. HTTP-PORT."
  67. (define os
  68. (marionette-operating-system
  69. test-os
  70. #:imported-modules '((gnu services herd)
  71. (guix combinators))))
  72. (define forwarded-port 8080)
  73. (define vm
  74. (virtual-machine
  75. (operating-system os)
  76. (port-forwardings `((,http-port . ,forwarded-port)))))
  77. (define test
  78. (with-imported-modules '((gnu build marionette))
  79. #~(begin
  80. (use-modules (srfi srfi-11) (srfi srfi-64)
  81. (gnu build marionette)
  82. (web uri)
  83. (web client)
  84. (web response))
  85. (define marionette
  86. (make-marionette (list #$vm)))
  87. (mkdir #$output)
  88. (chdir #$output)
  89. (test-begin #$name)
  90. (test-assert #$(string-append name " service running")
  91. (marionette-eval
  92. '(begin
  93. (use-modules (gnu services herd))
  94. (match (start-service '#$(string->symbol name))
  95. (#f #f)
  96. (('service response-parts ...)
  97. (match (assq-ref response-parts 'running)
  98. ((#t) #t)
  99. ((pid) (number? pid))))))
  100. marionette))
  101. ;; Retrieve the index.html file we put in /srv.
  102. (test-equal "http-get"
  103. '(200 #$%index.html-contents)
  104. (let-values
  105. (((response text)
  106. (http-get #$(simple-format
  107. #f "http://localhost:~A/index.html" forwarded-port)
  108. #:decode-body? #t)))
  109. (list (response-code response) text)))
  110. #$@(if log-file
  111. `((test-assert ,(string-append "log file exists " log-file)
  112. (marionette-eval
  113. '(file-exists? ,log-file)
  114. marionette)))
  115. '())
  116. (test-end)
  117. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  118. (gexp->derivation (string-append name "-test") test))
  119. ;;;
  120. ;;; HTTPD
  121. ;;;
  122. (define %httpd-os
  123. (simple-operating-system
  124. (service dhcp-client-service-type)
  125. (service httpd-service-type
  126. (httpd-configuration
  127. (config
  128. (httpd-config-file
  129. (listen '("8080"))))))
  130. (simple-service 'make-http-root activation-service-type
  131. %make-http-root)))
  132. (define %test-httpd
  133. (system-test
  134. (name "httpd")
  135. (description "Connect to a running HTTPD server.")
  136. (value (run-webserver-test name %httpd-os
  137. #:log-file "/var/log/httpd/error_log"))))
  138. ;;;
  139. ;;; NGINX
  140. ;;;
  141. (define %nginx-servers
  142. ;; Server blocks.
  143. (list (nginx-server-configuration
  144. (listen '("8080")))))
  145. (define %nginx-os
  146. ;; Operating system under test.
  147. (simple-operating-system
  148. (service dhcp-client-service-type)
  149. (service nginx-service-type
  150. (nginx-configuration
  151. (log-directory "/var/log/nginx")
  152. (server-blocks %nginx-servers)))
  153. (simple-service 'make-http-root activation-service-type
  154. %make-http-root)))
  155. (define %test-nginx
  156. (system-test
  157. (name "nginx")
  158. (description "Connect to a running NGINX server.")
  159. (value (run-webserver-test name %nginx-os
  160. #:log-file "/var/log/nginx/access.log"))))
  161. ;;;
  162. ;;; Varnish
  163. ;;;
  164. (define %varnish-vcl
  165. (mixed-text-file
  166. "varnish-test.vcl"
  167. "vcl 4.0;
  168. backend dummy { .host = \"127.1.1.1\"; }
  169. sub vcl_recv { return(synth(200, \"OK\")); }
  170. sub vcl_synth {
  171. synthetic(\"" %index.html-contents "\");
  172. set resp.http.Content-Type = \"text/plain\";
  173. return(deliver);
  174. }"))
  175. (define %varnish-os
  176. (simple-operating-system
  177. (service dhcp-client-service-type)
  178. ;; Pretend to be a web server that serves %index.html-contents.
  179. (service varnish-service-type
  180. (varnish-configuration
  181. (name "/tmp/server")
  182. ;; Use a small VSL buffer to fit in the test VM.
  183. (parameters '(("vsl_space" . "4M")))
  184. (vcl %varnish-vcl)))
  185. ;; Proxy the "server" using the builtin configuration.
  186. (service varnish-service-type
  187. (varnish-configuration
  188. (parameters '(("vsl_space" . "4M")))
  189. (backend "localhost:80")
  190. (listen '(":8080"))))))
  191. (define %test-varnish
  192. (system-test
  193. (name "varnish")
  194. (description "Test the Varnish Cache server.")
  195. (value (run-webserver-test "varnish-default" %varnish-os))))
  196. ;;;
  197. ;;; PHP-FPM
  198. ;;;
  199. (define %make-php-fpm-http-root
  200. ;; Create our server root in /srv.
  201. #~(begin
  202. (mkdir "/srv")
  203. (call-with-output-file "/srv/index.php"
  204. (lambda (port)
  205. (display "<?php
  206. phpinfo();
  207. echo(\"Computed by php:\".((string)(2+3)));
  208. ?>\n" port)))))
  209. (define %php-fpm-nginx-server-blocks
  210. (list (nginx-server-configuration
  211. (root "/srv")
  212. (locations
  213. (list (nginx-php-location)))
  214. (listen '("8042"))
  215. (ssl-certificate #f)
  216. (ssl-certificate-key #f))))
  217. (define %php-fpm-os
  218. ;; Operating system under test.
  219. (simple-operating-system
  220. (service dhcp-client-service-type)
  221. (service php-fpm-service-type)
  222. (service nginx-service-type
  223. (nginx-configuration
  224. (server-blocks %php-fpm-nginx-server-blocks)))
  225. (simple-service 'make-http-root activation-service-type
  226. %make-php-fpm-http-root)))
  227. (define* (run-php-fpm-test #:optional (http-port 8042))
  228. "Run tests in %PHP-FPM-OS, which has nginx running and listening on
  229. HTTP-PORT, along with php-fpm."
  230. (define os
  231. (marionette-operating-system
  232. %php-fpm-os
  233. #:imported-modules '((gnu services herd)
  234. (guix combinators))))
  235. (define vm
  236. (virtual-machine
  237. (operating-system os)
  238. (port-forwardings `((8080 . ,http-port)))))
  239. (define test
  240. (with-imported-modules '((gnu build marionette)
  241. (guix build utils))
  242. #~(begin
  243. (use-modules (srfi srfi-11) (srfi srfi-64)
  244. (gnu build marionette)
  245. (web uri)
  246. (web client)
  247. (web response))
  248. (define marionette
  249. (make-marionette (list #$vm)))
  250. (mkdir #$output)
  251. (chdir #$output)
  252. (test-begin "php-fpm")
  253. (test-assert "php-fpm running"
  254. (marionette-eval
  255. '(begin
  256. (use-modules (gnu services herd))
  257. (match (start-service 'php-fpm)
  258. (#f #f)
  259. (('service response-parts ...)
  260. (match (assq-ref response-parts 'running)
  261. ((pid) (number? pid))))))
  262. marionette))
  263. (test-assert "nginx running"
  264. (marionette-eval
  265. '(begin
  266. (use-modules (gnu services herd))
  267. (start-service 'nginx))
  268. marionette))
  269. (test-equal "http-get"
  270. 200
  271. (let-values (((response text)
  272. (http-get "http://localhost:8080/index.php"
  273. #:decode-body? #t)))
  274. (response-code response)))
  275. (test-equal "php computed result is sent"
  276. "Computed by php:5"
  277. (let-values (((response text)
  278. (http-get "http://localhost:8080/index.php"
  279. #:decode-body? #t)))
  280. (begin
  281. (use-modules (ice-9 regex))
  282. (let ((matches (string-match "Computed by php:5" text)))
  283. (and matches
  284. (match:substring matches 0))))))
  285. (test-end)
  286. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  287. (gexp->derivation "php-fpm-test" test))
  288. (define %test-php-fpm
  289. (system-test
  290. (name "php-fpm")
  291. (description "Test PHP-FPM through nginx.")
  292. (value (run-php-fpm-test))))
  293. ;;;
  294. ;;; hpcguix-web
  295. ;;;
  296. (define* (run-hpcguix-web-server-test name test-os)
  297. "Run tests in %HPCGUIX-WEB-OS, which has hpcguix-web running."
  298. (define os
  299. (marionette-operating-system
  300. test-os
  301. #:imported-modules '((gnu services herd)
  302. (guix combinators))))
  303. (define vm
  304. (virtual-machine
  305. (operating-system os)
  306. (port-forwardings '((8080 . 5000)))))
  307. (define test
  308. (with-imported-modules '((gnu build marionette))
  309. #~(begin
  310. (use-modules (srfi srfi-11) (srfi srfi-64)
  311. (gnu build marionette)
  312. (web uri)
  313. (web client)
  314. (web response))
  315. (define marionette
  316. (make-marionette (list #$vm)))
  317. (mkdir #$output)
  318. (chdir #$output)
  319. (test-begin #$name)
  320. (test-assert "hpcguix-web running"
  321. (marionette-eval
  322. '(begin
  323. (use-modules (gnu services herd))
  324. (match (start-service 'hpcguix-web)
  325. (#f #f)
  326. (('service response-parts ...)
  327. (match (assq-ref response-parts 'running)
  328. ((pid) (number? pid))))))
  329. marionette))
  330. (test-equal "http-get"
  331. 200
  332. (begin
  333. (wait-for-tcp-port 5000 marionette)
  334. (let-values (((response text)
  335. (http-get "http://localhost:8080")))
  336. (response-code response))))
  337. (test-end)
  338. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  339. (gexp->derivation (string-append name "-test") test))
  340. (define %hpcguix-web-specs
  341. ;; Server config gexp.
  342. #~(define site-config
  343. (hpcweb-configuration
  344. (title-prefix "[TEST] HPCGUIX-WEB"))))
  345. (define %hpcguix-web-os
  346. (simple-operating-system
  347. (service dhcp-client-service-type)
  348. (service hpcguix-web-service-type
  349. (hpcguix-web-configuration
  350. (specs %hpcguix-web-specs)))))
  351. (define %test-hpcguix-web
  352. (system-test
  353. (name "hpcguix-web")
  354. (description "Connect to a running hpcguix-web server.")
  355. (value (run-hpcguix-web-server-test name %hpcguix-web-os))))
  356. (define %tailon-os
  357. ;; Operating system under test.
  358. (simple-operating-system
  359. (service dhcp-client-service-type)
  360. (service tailon-service-type
  361. (tailon-configuration
  362. (config-file
  363. (tailon-configuration-file
  364. (bind "0.0.0.0:8080")))))))
  365. (define* (run-tailon-test #:optional (http-port 8081))
  366. "Run tests in %TAILON-OS, which has tailon running and listening on
  367. HTTP-PORT."
  368. (define os
  369. (marionette-operating-system
  370. %tailon-os
  371. #:imported-modules '((gnu services herd)
  372. (guix combinators))))
  373. (define vm
  374. (virtual-machine
  375. (operating-system os)
  376. (port-forwardings `((,http-port . 8080)))))
  377. (define test
  378. (with-imported-modules '((gnu build marionette))
  379. #~(begin
  380. (use-modules (srfi srfi-11) (srfi srfi-64)
  381. (ice-9 match)
  382. (gnu build marionette)
  383. (web uri)
  384. (web client)
  385. (web response))
  386. (define marionette
  387. ;; Forward the guest's HTTP-PORT, where tailon is listening, to
  388. ;; port 8080 in the host.
  389. (make-marionette (list #$vm)))
  390. (mkdir #$output)
  391. (chdir #$output)
  392. (test-begin "tailon")
  393. (test-assert "service running"
  394. (marionette-eval
  395. '(begin
  396. (use-modules (gnu services herd))
  397. (start-service 'tailon))
  398. marionette))
  399. (define* (retry-on-error f #:key times delay)
  400. (let loop ((attempt 1))
  401. (match (catch
  402. #t
  403. (lambda ()
  404. (cons #t
  405. (f)))
  406. (lambda args
  407. (cons #f
  408. args)))
  409. ((#t . return-value)
  410. return-value)
  411. ((#f . error-args)
  412. (if (>= attempt times)
  413. error-args
  414. (begin
  415. (sleep delay)
  416. (loop (+ 1 attempt))))))))
  417. (test-equal "http-get"
  418. 200
  419. (retry-on-error
  420. (lambda ()
  421. (let-values (((response text)
  422. (http-get #$(format
  423. #f
  424. "http://localhost:~A/"
  425. http-port)
  426. #:decode-body? #t)))
  427. (response-code response)))
  428. #:times 10
  429. #:delay 5))
  430. (test-end)
  431. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  432. (gexp->derivation "tailon-test" test))
  433. (define %test-tailon
  434. (system-test
  435. (name "tailon")
  436. (description "Connect to a running Tailon server.")
  437. (value (run-tailon-test))))
  438. ;;;
  439. ;;; Patchwork
  440. ;;;
  441. (define patchwork-initial-database-setup-service
  442. (match-lambda
  443. (($ <patchwork-database-configuration>
  444. engine name user password host port)
  445. (define start-gexp
  446. #~(lambda ()
  447. (let ((pid (primitive-fork))
  448. (postgres (getpwnam "postgres")))
  449. (if (eq? pid 0)
  450. (dynamic-wind
  451. (const #t)
  452. (lambda ()
  453. (setgid (passwd:gid postgres))
  454. (setuid (passwd:uid postgres))
  455. (primitive-exit
  456. (if (and
  457. (zero?
  458. (system* #$(file-append postgresql "/bin/createuser")
  459. #$user))
  460. (zero?
  461. (system* #$(file-append postgresql "/bin/createdb")
  462. "-O" #$user #$name)))
  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. (service patchwork-service-type
  485. (patchwork-configuration
  486. (patchwork patchwork)
  487. (domain "localhost")
  488. (settings-module
  489. (patchwork-settings-module
  490. (allowed-hosts (list domain))
  491. (default-from-email "")))
  492. (getmail-retriever-config
  493. (getmail-retriever-configuration
  494. (type "SimpleIMAPSSLRetriever")
  495. (server "imap.example.com")
  496. (port 993)
  497. (username "username")
  498. (password "password")
  499. (extra-parameters
  500. '((mailboxes . ("INBOX"))))))))
  501. (simple-service 'patchwork-database-setup
  502. shepherd-root-service-type
  503. (list
  504. (patchwork-initial-database-setup-service
  505. (patchwork-database-configuration))))))
  506. (define (run-patchwork-test patchwork)
  507. "Run tests in %NGINX-OS, which has nginx running and listening on
  508. HTTP-PORT."
  509. (define os
  510. (marionette-operating-system
  511. (patchwork-os patchwork)
  512. #:imported-modules '((gnu services herd)
  513. (guix combinators))))
  514. (define forwarded-port 8080)
  515. (define vm
  516. (virtual-machine
  517. (operating-system os)
  518. (port-forwardings `((8080 . ,forwarded-port)))))
  519. (define test
  520. (with-imported-modules '((gnu build marionette))
  521. #~(begin
  522. (use-modules (srfi srfi-11) (srfi srfi-64)
  523. (gnu build marionette)
  524. (web uri)
  525. (web client)
  526. (web response))
  527. (define marionette
  528. (make-marionette (list #$vm)))
  529. (mkdir #$output)
  530. (chdir #$output)
  531. (test-begin "patchwork")
  532. (test-assert "patchwork-postgresql-user-and-service started"
  533. (marionette-eval
  534. '(begin
  535. (use-modules (gnu services herd))
  536. (match (start-service 'patchwork-postgresql-user-and-database)
  537. (#f #f)
  538. (('service response-parts ...)
  539. (match (assq-ref response-parts 'running)
  540. ((#t) #t)
  541. ((pid) (number? pid))))))
  542. marionette))
  543. (test-assert "httpd running"
  544. (marionette-eval
  545. '(begin
  546. (use-modules (gnu services herd))
  547. (start-service 'httpd))
  548. marionette))
  549. (test-equal "http-get"
  550. 200
  551. (let-values
  552. (((response text)
  553. (http-get #$(simple-format
  554. #f "http://localhost:~A/" forwarded-port)
  555. #:decode-body? #t)))
  556. (response-code response)))
  557. (test-end)
  558. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  559. (gexp->derivation "patchwork-test" test))
  560. (define %test-patchwork
  561. (system-test
  562. (name "patchwork")
  563. (description "Connect to a running Patchwork service.")
  564. (value (run-patchwork-test patchwork))))