guix.scm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ;;;
  41. ;;; Guix Build Coordinator
  42. ;;;
  43. (define %guix-build-coordinator-os
  44. (simple-operating-system
  45. (service dhcp-client-service-type)
  46. (service guix-build-coordinator-service-type)))
  47. (define (run-guix-build-coordinator-test)
  48. (define os
  49. (marionette-operating-system
  50. %guix-build-coordinator-os
  51. #:imported-modules '((gnu services herd)
  52. (guix combinators))))
  53. (define forwarded-port 8745)
  54. (define vm
  55. (virtual-machine
  56. (operating-system os)
  57. (memory-size 1024)
  58. (port-forwardings `((,forwarded-port . 8745)))))
  59. (define test
  60. (with-imported-modules '((gnu build marionette))
  61. #~(begin
  62. (use-modules (srfi srfi-11) (srfi srfi-64)
  63. (gnu build marionette)
  64. (web uri)
  65. (web client)
  66. (web response))
  67. (define marionette
  68. (make-marionette (list #$vm)))
  69. (test-runner-current (system-test-runner #$output))
  70. (test-begin "guix-build-coordinator")
  71. (test-assert "service running"
  72. (marionette-eval
  73. '(begin
  74. (use-modules (gnu services herd))
  75. (match (start-service 'guix-build-coordinator)
  76. (#f #f)
  77. (('service response-parts ...)
  78. (match (assq-ref response-parts 'running)
  79. ((pid) (number? pid))))))
  80. marionette))
  81. (test-equal "http-get"
  82. 200
  83. (let-values
  84. (((response text)
  85. (http-get #$(simple-format
  86. #f "http://localhost:~A/metrics" forwarded-port)
  87. #:decode-body? #t)))
  88. (response-code response)))
  89. (test-end))))
  90. (gexp->derivation "guix-build-coordinator-test" test))
  91. (define %test-guix-build-coordinator
  92. (system-test
  93. (name "guix-build-coordinator")
  94. (description "Connect to a running Guix Build Coordinator.")
  95. (value (run-guix-build-coordinator-test))))
  96. ;;;
  97. ;;; Guix Data Service
  98. ;;;
  99. (define guix-data-service-initial-database-setup-service
  100. (let ((user "guix_data_service")
  101. (name "guix_data_service"))
  102. (define start-gexp
  103. #~(lambda ()
  104. (let ((pid (primitive-fork))
  105. (postgres (getpwnam "postgres")))
  106. (if (eq? pid 0)
  107. (dynamic-wind
  108. (const #t)
  109. (lambda ()
  110. (setgid (passwd:gid postgres))
  111. (setuid (passwd:uid postgres))
  112. (primitive-exit
  113. (if (and
  114. (zero?
  115. (system* #$(file-append postgresql "/bin/createuser")
  116. #$user))
  117. (zero?
  118. (system* #$(file-append postgresql "/bin/createdb")
  119. "-O" #$user #$name)))
  120. 0
  121. 1)))
  122. (lambda ()
  123. (primitive-exit 1)))
  124. (zero? (cdr (waitpid pid)))))))
  125. (shepherd-service
  126. (requirement '(postgres))
  127. (provision '(guix-data-service-initial-database-setup))
  128. (start start-gexp)
  129. (stop #~(const #f))
  130. (respawn? #f)
  131. (one-shot? #t)
  132. (documentation "Setup Guix Data Service database."))))
  133. (define %guix-data-service-os
  134. (simple-operating-system
  135. (service dhcp-client-service-type)
  136. (service postgresql-service-type
  137. (postgresql-configuration
  138. (postgresql postgresql-10)
  139. (config-file
  140. (postgresql-config-file
  141. (hba-file
  142. (plain-file "pg_hba.conf"
  143. "
  144. local all all trust
  145. host all all 127.0.0.1/32 trust
  146. host all all ::1/128 trust"))))))
  147. (service guix-data-service-type
  148. (guix-data-service-configuration
  149. (host "0.0.0.0")))
  150. (simple-service 'guix-data-service-database-setup
  151. shepherd-root-service-type
  152. (list guix-data-service-initial-database-setup-service))))
  153. (define (run-guix-data-service-test)
  154. (define os
  155. (marionette-operating-system
  156. %guix-data-service-os
  157. #:imported-modules '((gnu services herd)
  158. (guix combinators))))
  159. (define forwarded-port 8080)
  160. (define vm
  161. (virtual-machine
  162. (operating-system os)
  163. (memory-size 1024)
  164. (port-forwardings `((,forwarded-port . 8765)))))
  165. (define test
  166. (with-imported-modules '((gnu build marionette))
  167. #~(begin
  168. (use-modules (srfi srfi-11) (srfi srfi-64)
  169. (gnu build marionette)
  170. (web uri)
  171. (web client)
  172. (web response))
  173. (define marionette
  174. (make-marionette (list #$vm)))
  175. (test-runner-current (system-test-runner #$output))
  176. (test-begin "guix-data-service")
  177. (test-assert "service running"
  178. (marionette-eval
  179. '(begin
  180. (use-modules (gnu services herd))
  181. (match (start-service 'guix-data-service)
  182. (#f #f)
  183. (('service response-parts ...)
  184. (match (assq-ref response-parts 'running)
  185. ((pid) (number? pid))))))
  186. marionette))
  187. (test-assert "process jobs service running"
  188. (marionette-eval
  189. '(begin
  190. (use-modules (gnu services herd))
  191. (match (start-service 'guix-data-service-process-jobs)
  192. (#f #f)
  193. (('service response-parts ...)
  194. (match (assq-ref response-parts 'running)
  195. ((pid) (number? pid))))))
  196. marionette))
  197. (test-equal "http-get"
  198. 200
  199. (let-values
  200. (((response text)
  201. (http-get #$(simple-format
  202. #f "http://localhost:~A/healthcheck" forwarded-port)
  203. #:decode-body? #t)))
  204. (response-code response)))
  205. (test-end))))
  206. (gexp->derivation "guix-data-service-test" test))
  207. (define %test-guix-data-service
  208. (system-test
  209. (name "guix-data-service")
  210. (description "Connect to a running Guix Data Service.")
  211. (value (run-guix-data-service-test))))
  212. ;;;
  213. ;;; Nar Herder
  214. ;;;
  215. (define %nar-herder-os
  216. (simple-operating-system
  217. (service dhcp-client-service-type)
  218. (service nar-herder-service-type
  219. (nar-herder-configuration
  220. (host "0.0.0.0")
  221. ;; Not a realistic value, but works for the test
  222. (storage "/tmp")))))
  223. (define (run-nar-herder-test)
  224. (define os
  225. (marionette-operating-system
  226. %nar-herder-os
  227. #:imported-modules '((gnu services herd)
  228. (guix combinators))))
  229. (define forwarded-port
  230. (nar-herder-configuration-port
  231. (nar-herder-configuration)))
  232. (define vm
  233. (virtual-machine
  234. (operating-system os)
  235. (memory-size 1024)
  236. (port-forwardings `((,forwarded-port . ,forwarded-port)))))
  237. (define test
  238. (with-imported-modules '((gnu build marionette))
  239. #~(begin
  240. (use-modules (srfi srfi-11) (srfi srfi-64)
  241. (gnu build marionette)
  242. (web uri)
  243. (web client)
  244. (web response))
  245. (define marionette
  246. (make-marionette (list #$vm)))
  247. (test-runner-current (system-test-runner #$output))
  248. (test-begin "nar-herder")
  249. (test-assert "service running"
  250. (marionette-eval
  251. '(begin
  252. (use-modules (gnu services herd))
  253. (match (start-service 'nar-herder)
  254. (#f #f)
  255. (('service response-parts ...)
  256. (match (assq-ref response-parts 'running)
  257. ((pid) (number? pid))))))
  258. marionette))
  259. (test-equal "http-get"
  260. 404
  261. (let-values
  262. (((response text)
  263. (http-get #$(simple-format
  264. #f "http://localhost:~A/" forwarded-port)
  265. #:decode-body? #t)))
  266. (response-code response)))
  267. (test-end))))
  268. (gexp->derivation "nar-herder-test" test))
  269. (define %test-nar-herder
  270. (system-test
  271. (name "nar-herder")
  272. (description "Connect to a running Nar Herder server.")
  273. (value (run-nar-herder-test))))