guix.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020, 2021, 2022 Christopher Baines <mail@cbaines.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests guix)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system file-systems)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services guix)
  26. #:use-module (gnu services databases)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu services networking)
  29. #:use-module (gnu packages databases)
  30. #:use-module (guix packages)
  31. #:use-module (guix modules)
  32. #:use-module (guix records)
  33. #:use-module (guix gexp)
  34. #:use-module (guix store)
  35. #:use-module (guix utils)
  36. #:use-module (ice-9 match)
  37. #:export (%test-guix-build-coordinator
  38. %test-guix-data-service
  39. %test-nar-herder
  40. %test-bffe))
  41. ;;;
  42. ;;; Guix Build Coordinator
  43. ;;;
  44. (define %guix-build-coordinator-os
  45. (simple-operating-system
  46. (service dhcp-client-service-type)
  47. (service guix-build-coordinator-service-type)))
  48. (define (run-guix-build-coordinator-test)
  49. (define os
  50. (marionette-operating-system
  51. %guix-build-coordinator-os
  52. #:imported-modules '((gnu services herd)
  53. (guix combinators))))
  54. (define forwarded-port 8745)
  55. (define vm
  56. (virtual-machine
  57. (operating-system os)
  58. (memory-size 1024)
  59. (port-forwardings `((,forwarded-port . 8745)))))
  60. (define test
  61. (with-imported-modules '((gnu build marionette))
  62. #~(begin
  63. (use-modules (srfi srfi-11) (srfi srfi-64)
  64. (gnu build marionette)
  65. (web uri)
  66. (web client)
  67. (web response))
  68. (define marionette
  69. (make-marionette (list #$vm)))
  70. (test-runner-current (system-test-runner #$output))
  71. (test-begin "guix-build-coordinator")
  72. (test-assert "service running"
  73. (marionette-eval
  74. '(begin
  75. (use-modules (gnu services herd))
  76. (match (start-service 'guix-build-coordinator)
  77. (#f #f)
  78. (('service response-parts ...)
  79. (match (assq-ref response-parts 'running)
  80. ((pid) (number? pid))))))
  81. marionette))
  82. (test-equal "http-get"
  83. 200
  84. (let-values
  85. (((response text)
  86. (http-get #$(simple-format
  87. #f "http://localhost:~A/metrics" forwarded-port)
  88. #:decode-body? #t)))
  89. (response-code response)))
  90. (test-end))))
  91. (gexp->derivation "guix-build-coordinator-test" test))
  92. (define %test-guix-build-coordinator
  93. (system-test
  94. (name "guix-build-coordinator")
  95. (description "Connect to a running Guix Build Coordinator.")
  96. (value (run-guix-build-coordinator-test))))
  97. ;;;
  98. ;;; Guix Data Service
  99. ;;;
  100. (define guix-data-service-initial-database-setup-service
  101. (let ((user "guix_data_service")
  102. (name "guix_data_service"))
  103. (define start-gexp
  104. #~(lambda ()
  105. (let ((pid (primitive-fork))
  106. (postgres (getpwnam "postgres")))
  107. (if (eq? pid 0)
  108. (dynamic-wind
  109. (const #t)
  110. (lambda ()
  111. (setgid (passwd:gid postgres))
  112. (setuid (passwd:uid postgres))
  113. (primitive-exit
  114. (if (and
  115. (zero?
  116. (system* #$(file-append postgresql "/bin/createuser")
  117. #$user))
  118. (zero?
  119. (system* #$(file-append postgresql "/bin/createdb")
  120. "-O" #$user #$name)))
  121. 0
  122. 1)))
  123. (lambda ()
  124. (primitive-exit 1)))
  125. (zero? (cdr (waitpid pid)))))))
  126. (shepherd-service
  127. (requirement '(postgres))
  128. (provision '(guix-data-service-initial-database-setup))
  129. (start start-gexp)
  130. (stop #~(const #f))
  131. (respawn? #f)
  132. (one-shot? #t)
  133. (documentation "Setup Guix Data Service database."))))
  134. (define %guix-data-service-os
  135. (simple-operating-system
  136. (service dhcp-client-service-type)
  137. (service postgresql-service-type
  138. (postgresql-configuration
  139. (postgresql postgresql-10)
  140. (config-file
  141. (postgresql-config-file
  142. (hba-file
  143. (plain-file "pg_hba.conf"
  144. "
  145. local all all trust
  146. host all all 127.0.0.1/32 trust
  147. host all all ::1/128 trust"))))))
  148. (service guix-data-service-type
  149. (guix-data-service-configuration
  150. (host "0.0.0.0")))
  151. (simple-service 'guix-data-service-database-setup
  152. shepherd-root-service-type
  153. (list guix-data-service-initial-database-setup-service))))
  154. (define (run-guix-data-service-test)
  155. (define os
  156. (marionette-operating-system
  157. %guix-data-service-os
  158. #:imported-modules '((gnu services herd)
  159. (guix combinators))))
  160. (define forwarded-port 8080)
  161. (define vm
  162. (virtual-machine
  163. (operating-system os)
  164. (memory-size 1024)
  165. (port-forwardings `((,forwarded-port . 8765)))))
  166. (define test
  167. (with-imported-modules '((gnu build marionette))
  168. #~(begin
  169. (use-modules (srfi srfi-11) (srfi srfi-64)
  170. (gnu build marionette)
  171. (web uri)
  172. (web client)
  173. (web response))
  174. (define marionette
  175. (make-marionette (list #$vm)))
  176. (test-runner-current (system-test-runner #$output))
  177. (test-begin "guix-data-service")
  178. (test-assert "service running"
  179. (marionette-eval
  180. '(begin
  181. (use-modules (gnu services herd))
  182. (match (start-service 'guix-data-service)
  183. (#f #f)
  184. (('service response-parts ...)
  185. (match (assq-ref response-parts 'running)
  186. ((pid) (number? pid))))))
  187. marionette))
  188. (test-assert "process jobs service running"
  189. (marionette-eval
  190. '(begin
  191. (use-modules (gnu services herd))
  192. (match (start-service 'guix-data-service-process-jobs)
  193. (#f #f)
  194. (('service response-parts ...)
  195. (match (assq-ref response-parts 'running)
  196. ((pid) (number? pid))))))
  197. marionette))
  198. ;; The service starts immediately but replies with status 500 until
  199. ;; initialization is complete, so keep trying for a while.
  200. (define (try-http-get attempts)
  201. (let ((status
  202. (let-values (((response text)
  203. (http-get #$(simple-format
  204. #f "http://localhost:~A/healthcheck"
  205. forwarded-port))))
  206. (response-code response))))
  207. (if (or (= status 200) (<= attempts 1))
  208. status
  209. (begin (sleep 5)
  210. (try-http-get (- attempts 1))))))
  211. (test-equal "http-get"
  212. 200
  213. (try-http-get 12))
  214. (test-end))))
  215. (gexp->derivation "guix-data-service-test" test))
  216. (define %test-guix-data-service
  217. (system-test
  218. (name "guix-data-service")
  219. (description "Connect to a running Guix Data Service.")
  220. (value (run-guix-data-service-test))))
  221. ;;;
  222. ;;; Nar Herder
  223. ;;;
  224. (define %nar-herder-os
  225. (simple-operating-system
  226. (service dhcp-client-service-type)
  227. (service nar-herder-service-type
  228. (nar-herder-configuration
  229. (host "0.0.0.0")
  230. ;; Not a realistic value, but works for the test
  231. (storage "/tmp")))))
  232. (define (run-nar-herder-test)
  233. (define os
  234. (marionette-operating-system
  235. %nar-herder-os
  236. #:imported-modules '((gnu services herd)
  237. (guix combinators))))
  238. (define forwarded-port
  239. (nar-herder-configuration-port
  240. (nar-herder-configuration)))
  241. (define vm
  242. (virtual-machine
  243. (operating-system os)
  244. (memory-size 1024)
  245. (port-forwardings `((,forwarded-port . ,forwarded-port)))))
  246. (define test
  247. (with-imported-modules '((gnu build marionette))
  248. #~(begin
  249. (use-modules (srfi srfi-11) (srfi srfi-64)
  250. (gnu build marionette)
  251. (web uri)
  252. (web client)
  253. (web response))
  254. (define marionette
  255. (make-marionette (list #$vm)))
  256. (test-runner-current (system-test-runner #$output))
  257. (test-begin "nar-herder")
  258. (test-assert "service running"
  259. (marionette-eval
  260. '(begin
  261. (use-modules (gnu services herd))
  262. (match (start-service 'nar-herder)
  263. (#f #f)
  264. (('service response-parts ...)
  265. (match (assq-ref response-parts 'running)
  266. ((pid) (number? pid))))))
  267. marionette))
  268. (test-equal "http-get"
  269. 404
  270. (let-values
  271. (((response text)
  272. (http-get #$(simple-format
  273. #f "http://localhost:~A/" forwarded-port)
  274. #:decode-body? #t)))
  275. (response-code response)))
  276. (test-end))))
  277. (gexp->derivation "nar-herder-test" test))
  278. (define %test-nar-herder
  279. (system-test
  280. (name "nar-herder")
  281. (description "Connect to a running Nar Herder server.")
  282. (value (run-nar-herder-test))))
  283. ;;;
  284. ;;; Build Farm Front-end
  285. ;;;
  286. (define %bffe-os
  287. (simple-operating-system
  288. (service dhcp-client-service-type)
  289. (service guix-build-coordinator-service-type)
  290. (service bffe-service-type
  291. (bffe-configuration
  292. (arguments
  293. #~(list
  294. #:web-server-args
  295. '(#:port 8767
  296. #:controller-args
  297. (#:title "Test title"))))))))
  298. (define (run-bffe-test)
  299. (define os
  300. (marionette-operating-system
  301. %bffe-os
  302. #:imported-modules '((gnu services herd)
  303. (guix combinators))))
  304. (define forwarded-port 8767)
  305. (define vm
  306. (virtual-machine
  307. (operating-system os)
  308. (memory-size 1024)
  309. (port-forwardings `((,forwarded-port . 8767)))))
  310. (define test
  311. (with-imported-modules '((gnu build marionette))
  312. #~(begin
  313. (use-modules (srfi srfi-11) (srfi srfi-64)
  314. (gnu build marionette)
  315. (web uri)
  316. (web client)
  317. (web response))
  318. (define marionette
  319. (make-marionette (list #$vm)))
  320. (test-runner-current (system-test-runner #$output))
  321. (test-begin "bffe")
  322. (test-assert "service running"
  323. (marionette-eval
  324. '(begin
  325. (use-modules (gnu services herd))
  326. (match (start-service 'bffe)
  327. (#f #f)
  328. (('service response-parts ...)
  329. (match (assq-ref response-parts 'running)
  330. ((pid) (number? pid))))))
  331. marionette))
  332. (test-equal "http-get"
  333. 200
  334. (let-values
  335. (((response text)
  336. (http-get #$(simple-format
  337. #f "http://localhost:~A/" forwarded-port)
  338. #:decode-body? #t)))
  339. (response-code response)))
  340. (test-end))))
  341. (gexp->derivation "bffe-test" test))
  342. (define %test-bffe
  343. (system-test
  344. (name "bffe")
  345. (description "Connect to a running Build Farm Front-end.")
  346. (value (run-bffe-test))))