guix.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 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. ;;;
  40. ;;; Guix Build Coordinator
  41. ;;;
  42. (define %guix-build-coordinator-os
  43. (simple-operating-system
  44. (service dhcp-client-service-type)
  45. (service guix-build-coordinator-service-type)))
  46. (define (run-guix-build-coordinator-test)
  47. (define os
  48. (marionette-operating-system
  49. %guix-build-coordinator-os
  50. #:imported-modules '((gnu services herd)
  51. (guix combinators))))
  52. (define forwarded-port 8745)
  53. (define vm
  54. (virtual-machine
  55. (operating-system os)
  56. (memory-size 1024)
  57. (port-forwardings `((,forwarded-port . 8745)))))
  58. (define test
  59. (with-imported-modules '((gnu build marionette))
  60. #~(begin
  61. (use-modules (srfi srfi-11) (srfi srfi-64)
  62. (gnu build marionette)
  63. (web uri)
  64. (web client)
  65. (web response))
  66. (define marionette
  67. (make-marionette (list #$vm)))
  68. (mkdir #$output)
  69. (chdir #$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. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  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. ;; XXX: Remove when postgresql default socket directory is
  149. ;; changed to /var/run/postgresql.
  150. (socket-directory #f)))))
  151. (service guix-data-service-type
  152. (guix-data-service-configuration
  153. (host "0.0.0.0")))
  154. (simple-service 'guix-data-service-database-setup
  155. shepherd-root-service-type
  156. (list guix-data-service-initial-database-setup-service))))
  157. (define (run-guix-data-service-test)
  158. (define os
  159. (marionette-operating-system
  160. %guix-data-service-os
  161. #:imported-modules '((gnu services herd)
  162. (guix combinators))))
  163. (define forwarded-port 8080)
  164. (define vm
  165. (virtual-machine
  166. (operating-system os)
  167. (memory-size 1024)
  168. (port-forwardings `((,forwarded-port . 8765)))))
  169. (define test
  170. (with-imported-modules '((gnu build marionette))
  171. #~(begin
  172. (use-modules (srfi srfi-11) (srfi srfi-64)
  173. (gnu build marionette)
  174. (web uri)
  175. (web client)
  176. (web response))
  177. (define marionette
  178. (make-marionette (list #$vm)))
  179. (mkdir #$output)
  180. (chdir #$output)
  181. (test-begin "guix-data-service")
  182. (test-assert "service running"
  183. (marionette-eval
  184. '(begin
  185. (use-modules (gnu services herd))
  186. (match (start-service 'guix-data-service)
  187. (#f #f)
  188. (('service response-parts ...)
  189. (match (assq-ref response-parts 'running)
  190. ((pid) (number? pid))))))
  191. marionette))
  192. (test-assert "process jobs service running"
  193. (marionette-eval
  194. '(begin
  195. (use-modules (gnu services herd))
  196. (match (start-service 'guix-data-service-process-jobs)
  197. (#f #f)
  198. (('service response-parts ...)
  199. (match (assq-ref response-parts 'running)
  200. ((pid) (number? pid))))))
  201. marionette))
  202. (test-equal "http-get"
  203. 200
  204. (let-values
  205. (((response text)
  206. (http-get #$(simple-format
  207. #f "http://localhost:~A/healthcheck" forwarded-port)
  208. #:decode-body? #t)))
  209. (response-code response)))
  210. (test-end)
  211. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  212. (gexp->derivation "guix-data-service-test" test))
  213. (define %test-guix-data-service
  214. (system-test
  215. (name "guix-data-service")
  216. (description "Connect to a running Guix Data Service.")
  217. (value (run-guix-data-service-test))))